File: basic_deployment.rb

package info (click to toggle)
r10k 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,228 kB
  • sloc: ruby: 18,180; makefile: 10; sh: 1
file content (176 lines) | stat: -rw-r--r-- 7,569 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require 'git_utils'
require 'r10k_utils'
require 'master_manipulator'


test_name 'Basic Environment Deployment Workflows'

# This isn't a block because we want to use the local variables throughout the file
step 'init'
  @env_path             = on(master, puppet('config print environmentpath')).stdout.rstrip
  r10k_fqp              = get_r10k_fqp(master)

  control_repo_gitdir   = '/git_repos/environments.git'
  control_repo_worktree = '/root/environments'
  last_commit           = git_last_commit(master, control_repo_worktree)
  git_provider          = ENV['GIT_PROVIDER']

  config_path           = get_r10k_config_file_path(master)
  config_backup_path    = "#{config_path}.bak"

  puppetfile1 =<<-EOS
    mod 'puppetlabs/apache', '0.10.0'
    mod 'puppetlabs/stdlib', '8.0.0'
    EOS

  r10k_conf = <<-CONF
    cachedir: '/var/cache/r10k'
    git:
      provider: '#{git_provider}'
    sources:
      control:
        basedir: "#{@env_path}"
        remote: "#{control_repo_gitdir}"
    deploy:
      purge_levels: ['deployment','environment','puppetfile']

    CONF


def and_stdlib_is_correct
  metadata_path = "#{@env_path}/production/modules/stdlib/metadata.json"
  on(master, "test -f #{metadata_path}", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 0, 'stdlib content has been inappropriately purged')
  end
  metadata_info = JSON.parse(on(master, "cat #{metadata_path}").stdout)
  assert(metadata_info['version'] == '8.0.0', 'stdlib deployed to wrong version')
end

teardown do
  on(master, "mv #{config_backup_path} #{config_path}")
  clean_up_r10k(master, last_commit, control_repo_worktree)
end

step 'Set up r10k and control repo' do

  # Backup and replace r10k config
  on(master, "mv #{config_path} #{config_backup_path}")
  create_remote_file(master, config_path, r10k_conf)

  # Place our Puppetfile in the control repo's production branch
  git_on(master, 'checkout production', control_repo_worktree)
  create_remote_file(master, "#{control_repo_worktree}/Puppetfile", puppetfile1)
  git_add_commit_push(master, 'production', 'add Puppetfile for Basic Deployment test', control_repo_worktree)

  # Ensure the production environment will be deployed anew
  on(master, "rm -rf #{@env_path}/production")
end

test_path = "#{@env_path}/production/modules/apache/metadata.json"
step 'Test initial environment deploy works' do
  on(master, "#{r10k_fqp} deploy environment production --verbose=info") do |result|
    assert(result.output =~ /.*Deploying module to .*apache.*/, 'Did not log apache deployment')
    assert(result.output =~ /.*Deploying module to .*stdlib.*/, 'Did not log stdlib deployment')
  end
  on(master, "test -f #{test_path}", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 0, 'Expected module in Puppetfile was not installed')
  end

  and_stdlib_is_correct
end

original_apache_info = JSON.parse(on(master, "cat #{test_path}").stdout)

step 'Test second run of deploy updates control repo, but leaves moduledir untouched' do
  puppetfile2 =<<-EOS
    # Current latest of apache is 6.5.1 as of writing this test
    mod 'puppetlabs/apache', :latest
    mod 'puppetlabs/stdlib', '8.0.0'
    mod 'puppetlabs/concat', '7.0.0'
    EOS

  git_on(master, 'checkout production', control_repo_worktree)
  create_remote_file(master, "#{control_repo_worktree}/Puppetfile", puppetfile2)
  git_add_commit_push(master, 'production', 'add Puppetfile for Basic Deployment test', control_repo_worktree)

  on(master, "#{r10k_fqp} deploy environment production --verbose=info") do |result|
    refute(result.output =~ /.*Deploying module to .*apache.*/, 'Inappropriately updated apache')
    refute(result.output =~ /.*Deploying module to .*stdlib.*/, 'Inappropriately updated stdlib')
  end

  on(master, "test -f #{test_path}", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 0, 'Expected module content in Puppetfile was inappropriately purged')
  end

  new_apache_info = JSON.parse(on(master, "cat #{test_path}").stdout)
  on(master, "cat #{@env_path}/production/Puppetfile | grep ':latest'", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 0, 'Puppetfile not updated on subsequent r10k deploys')
  end

  assert(original_apache_info['version'] == new_apache_info['version'] &&
         new_apache_info['version'] == '0.10.0',
         'Module content updated on subsequent r10k invocations w/o providing --modules')

  on(master, "test -f #{@env_path}/production/modules/concat/metadata.json", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 1, 'Module content deployed on subsequent r10k invocation w/o providing --modules')
  end

  and_stdlib_is_correct
end

step 'Test --modules updates modules' do
  on(master, "#{r10k_fqp} deploy environment production --modules --verbose=info") do |result|
    assert(result.output =~ /.*Deploying module to .*apache.*/, 'Did not log apache deployment')
    assert(result.output =~ /.*Deploying module to .*stdlib.*/, 'Did not log stdlib deployment')
    assert(result.output =~ /.*Deploying module to .*concat.*/, 'Did not log concat deployment')
  end

  on(master, "test -f #{test_path}", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 0, 'Expected module content in Puppetfile was inappropriately purged')
  end

  on(master, "test -f #{@env_path}/production/modules/concat/metadata.json", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 0, 'New module content was not deployed when providing --modules')
  end

  new_apache_info = JSON.parse(on(master, "cat #{test_path}").stdout)
  apache_major_version = new_apache_info['version'].split('.').first.to_i
  assert(apache_major_version > 5, 'Module not updated correctly using --modules')

  and_stdlib_is_correct
end

step 'Test --modules --incremental deploys changed & dynamic modules, but not unchanged, static modules' do
  puppetfile3 =<<-EOS
    # Current latest of apache is 6.5.1 as of writing this test
    mod 'puppetlabs/apache', :latest
    mod 'puppetlabs/stdlib', '8.0.0'
    mod 'puppetlabs/concat', '7.1.0'
    EOS

  git_on(master, 'checkout production', control_repo_worktree)
  create_remote_file(master, "#{control_repo_worktree}/Puppetfile", puppetfile3)
  git_add_commit_push(master, 'production', 'add Puppetfile for Basic Deployment test', control_repo_worktree)

  on(master, "#{r10k_fqp} deploy environment production --modules --incremental --verbose=debug1") do |result|
    assert(result.output =~ /.*Deploying module to .*apache.*/, 'Did not log apache deployment')
    assert(result.output =~ /.*Deploying module to .*concat.*/, 'Did not log concat deployment')
    assert(result.output =~ /.*Not updating module stdlib, assuming content unchanged.*/, 'Did not log notice of skipping stdlib')
  end

  on(master, "test -f #{test_path}", accept_all_exit_codes: true) do |result|
    assert(result.exit_code == 0, 'Expected module content in Puppetfile was inappropriately purged')
  end

  new_apache_info = JSON.parse(on(master, "cat #{test_path}").stdout)
  apache_major_version = new_apache_info['version'].split('.').first.to_i
  assert(apache_major_version > 5, 'Module not updated correctly using --modules & --incremental')

  concat_info = JSON.parse(on(master, "cat #{@env_path}/production/modules/concat/metadata.json").stdout)
  concat_minor_version = concat_info['version'].split('.')[1].to_i
  assert(concat_minor_version == 1, 'Module not updated correctly using --modules & --incremental')

  and_stdlib_is_correct
end