puppet_litmus 0.18.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,10 +13,10 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
13
13
  end
14
14
 
15
15
  it 'calls all functions' do
16
- expect(described_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
17
- expect(described_class).to receive(:apply_manifest).with(nil, expect_failures: false, manifest_file_location: '/bla.pp')
18
- expect(described_class).to receive(:apply_manifest).with(nil, catch_changes: true, manifest_file_location: '/bla.pp')
19
- described_class.idempotent_apply(manifest)
16
+ expect(self).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
17
+ expect(self).to receive(:apply_manifest).with(nil, expect_failures: false, manifest_file_location: '/bla.pp')
18
+ expect(self).to receive(:apply_manifest).with(nil, catch_changes: true, manifest_file_location: '/bla.pp')
19
+ idempotent_apply(manifest)
20
20
  end
21
21
  end
22
22
 
@@ -28,11 +28,10 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
28
28
 
29
29
  it 'passes the --hiera_config flag if the :hiera_config opt is specified' do
30
30
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
31
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
32
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
33
- expect(described_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
34
- expect(described_class).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
35
- described_class.apply_manifest(manifest, hiera_config: '/hiera.yaml')
31
+ expect(self).to receive(:target_in_inventory?).and_return(true)
32
+ expect(self).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
33
+ expect(self).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
34
+ apply_manifest(manifest, hiera_config: '/hiera.yaml')
36
35
  end
37
36
  end
38
37
 
@@ -43,42 +42,38 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
43
42
 
44
43
  it 'uses detailed-exitcodes with expect_failures' do
45
44
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
46
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
47
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
48
- expect(described_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
49
- expect(described_class).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
50
- expect { described_class.apply_manifest(manifest, expect_failures: true) }.to raise_error(RuntimeError)
45
+ expect(self).to receive(:target_in_inventory?).and_return(true)
46
+ expect(self).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
47
+ expect(self).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
48
+ expect { apply_manifest(manifest, expect_failures: true) }.to raise_error(RuntimeError)
51
49
  end
52
50
 
53
51
  it 'uses detailed-exitcodes with catch_failures' do
54
52
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
55
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
56
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
57
- expect(described_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
58
- expect(described_class).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
59
- described_class.apply_manifest(manifest, catch_failures: true)
53
+ expect(self).to receive(:target_in_inventory?).and_return(true)
54
+ expect(self).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
55
+ expect(self).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
56
+ apply_manifest(manifest, catch_failures: true)
60
57
  end
61
58
 
62
59
  it 'uses detailed-exitcodes with expect_changes' do
63
60
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
64
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
65
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
66
- expect(described_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
67
- expect(described_class).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
68
- expect { described_class.apply_manifest(manifest, expect_changes: true) }.to raise_error(RuntimeError)
61
+ expect(self).to receive(:target_in_inventory?).and_return(true)
62
+ expect(self).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
63
+ expect(self).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
64
+ expect { apply_manifest(manifest, expect_changes: true) }.to raise_error(RuntimeError)
69
65
  end
70
66
 
71
67
  it 'uses detailed-exitcodes with catch_changes' do
72
68
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
73
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
74
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
75
- expect(described_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
76
- expect(described_class).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
77
- described_class.apply_manifest(manifest, catch_changes: true)
69
+ expect(self).to receive(:target_in_inventory?).and_return(true)
70
+ expect(self).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
71
+ expect(self).to receive(:run_command).with(command, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
72
+ apply_manifest(manifest, catch_changes: true)
78
73
  end
79
74
 
80
75
  it 'uses raises exception for multiple options' do
81
- expect { described_class.apply_manifest(manifest, catch_changes: true, expect_failures: true) }
76
+ expect { apply_manifest(manifest, catch_changes: true, expect_failures: true) }
82
77
  .to raise_error(RuntimeError, 'please specify only one of `catch_changes`, `expect_changes`, `catch_failures` or `expect_failures`')
83
78
  end
84
79
  end
@@ -89,17 +84,16 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
89
84
  let(:result) { ['value' => { 'exit_code' => 0, 'exit_status' => 0, 'stdout' => nil, 'stderr' => nil }] }
90
85
 
91
86
  it 'responds to run_shell' do
92
- expect(described_class).to respond_to(:run_shell).with(1..2).arguments
87
+ expect(self).to respond_to(:run_shell).with(1..2).arguments
93
88
  end
94
89
 
95
90
  context 'when running against localhost and no inventory.yaml file' do
96
91
  it 'does run_shell against localhost without error' do
97
92
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
98
93
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
99
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
100
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
101
- expect(described_class).to receive(:run_command).with(command_to_run, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
102
- expect { described_class.run_shell(command_to_run) }.not_to raise_error
94
+ expect(self).to receive(:target_in_inventory?).and_return(true)
95
+ expect(self).to receive(:run_command).with(command_to_run, 'litmus_localhost', config: nil, inventory: localhost_inventory_hash).and_return(result)
96
+ expect { run_shell(command_to_run) }.not_to raise_error
103
97
  end
104
98
  end
105
99
 
@@ -107,10 +101,10 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
107
101
  it 'does run_shell against remote host without error' do
108
102
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
109
103
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
110
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
111
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
112
- expect(described_class).to receive(:run_command).with(command_to_run, 'some.host', config: nil, inventory: inventory_hash).and_return(result)
113
- expect { described_class.run_shell(command_to_run) }.not_to raise_error
104
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
105
+ expect(self).to receive(:target_in_inventory?).and_return(true)
106
+ expect(self).to receive(:run_command).with(command_to_run, 'some.host', config: nil, inventory: inventory_hash).and_return(result)
107
+ expect { run_shell(command_to_run) }.not_to raise_error
114
108
  end
115
109
  end
116
110
  end
@@ -119,33 +113,32 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
119
113
  let(:local) { '/tmp' }
120
114
  let(:remote) { '/remote_tmp' }
121
115
  # Ignore rubocop because these hashes are representative of output from an external method and editing them leads to test failures.
122
- # rubocop:disable Layout/SpaceInsideHashLiteralBraces, Layout/SpaceInsideBlockBraces, Layout/SpaceAroundOperators, Layout/LineLength, Layout/SpaceAfterComma
123
- let(:result_success) {[{'target'=>'some.host','action'=>'upload','object'=>'C:\foo\bar.ps1','status'=>'success','value'=>{'_output'=>'Uploaded \'C:\foo\bar.ps1\' to \'some.host:C:\bar\''}}]}
124
- let(:result_failure) {[{'target'=>'some.host','action'=>nil,'object'=>nil,'status'=>'failure','value'=>{'_error'=>{'kind'=>'puppetlabs.tasks/task_file_error','msg'=>'No such file or directory @ rb_sysopen - /nonexistant/file/path','details'=>{},'issue_code'=>'WRITE_ERROR'}}}]}
125
- # rubocop:enable Layout/SpaceInsideHashLiteralBraces, Layout/SpaceInsideBlockBraces, Layout/SpaceAroundOperators, Layout/LineLength, Layout/SpaceAfterComma
116
+ # rubocop:disable Layout/SpaceAroundOperators, Layout/LineLength, Layout/SpaceAfterComma
117
+ let(:result_success) { [{ 'target'=>'some.host','action'=>'upload','object'=>'C:\foo\bar.ps1','status'=>'success','value'=>{ '_output'=>'Uploaded \'C:\foo\bar.ps1\' to \'some.host:C:\bar\'' } }] }
118
+ let(:result_failure) { [{ 'target'=>'some.host','action'=>nil,'object'=>nil,'status'=>'failure','value'=>{ '_error'=>{ 'kind'=>'puppetlabs.tasks/task_file_error','msg'=>'No such file or directory @ rb_sysopen - /nonexistant/file/path','details'=>{},'issue_code'=>'WRITE_ERROR' } } }] }
119
+ # rubocop:enable, Layout/SpaceAroundOperators, Layout/LineLength, Layout/SpaceAfterComma
126
120
 
127
121
  it 'responds to run_shell' do
128
- expect(described_class).to respond_to(:bolt_upload_file).with(2..3).arguments
122
+ expect(self).to respond_to(:bolt_upload_file).with(2..3).arguments
129
123
  end
130
124
 
131
125
  context 'when upload returns success' do
132
126
  it 'does upload_file against remote host without error' do
133
127
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
134
128
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
135
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
136
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
137
- expect(described_class).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_success)
138
- expect { described_class.bolt_upload_file(local, remote) }.not_to raise_error
129
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
130
+ expect(self).to receive(:target_in_inventory?).and_return(true)
131
+ expect(self).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_success)
132
+ expect { bolt_upload_file(local, remote) }.not_to raise_error
139
133
  end
140
134
 
141
135
  it 'does upload_file against localhost without error' do
142
136
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
143
137
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
144
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
145
- expect(described_class).not_to receive(:inventory_hash_from_inventory_file)
146
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
147
- expect(described_class).to receive(:upload_file).with(local, remote, 'litmus_localhost', options: {}, config: nil, inventory: localhost_inventory_hash).and_return(result_success)
148
- expect { described_class.bolt_upload_file(local, remote) }.not_to raise_error
138
+ expect(self).not_to receive(:inventory_hash_from_inventory_file)
139
+ expect(self).to receive(:target_in_inventory?).and_return(true)
140
+ expect(self).to receive(:upload_file).with(local, remote, 'litmus_localhost', options: {}, config: nil, inventory: localhost_inventory_hash).and_return(result_success)
141
+ expect { bolt_upload_file(local, remote) }.not_to raise_error
149
142
  end
150
143
  end
151
144
 
@@ -153,19 +146,19 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
153
146
  it 'does upload_file gives runtime error for failure' do
154
147
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
155
148
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
156
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
157
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
158
- expect(described_class).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_failure)
159
- expect { described_class.bolt_upload_file(local, remote) }.to raise_error(RuntimeError, %r{upload file failed})
149
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
150
+ expect(self).to receive(:target_in_inventory?).and_return(true)
151
+ expect(self).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_failure)
152
+ expect { bolt_upload_file(local, remote) }.to raise_error(RuntimeError, %r{upload file failed})
160
153
  end
161
154
 
162
155
  it 'returns the exit code and error message when expecting failure' do
163
156
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
164
157
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
165
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
166
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
167
- expect(described_class).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_failure)
168
- method_result = described_class.bolt_upload_file(local, remote, expect_failures: true)
158
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
159
+ expect(self).to receive(:target_in_inventory?).and_return(true)
160
+ expect(self).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_failure)
161
+ method_result = bolt_upload_file(local, remote, expect_failures: true)
169
162
  expect(method_result.exit_code).to be(255)
170
163
  expect(method_result.stderr).to be('No such file or directory @ rb_sysopen - /nonexistant/file/path')
171
164
  end
@@ -177,18 +170,17 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
177
170
  let(:result) { ['value' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil }] }
178
171
 
179
172
  it 'responds to bolt_run_script' do
180
- expect(described_class).to respond_to(:bolt_run_script).with(1..2).arguments
173
+ expect(self).to respond_to(:bolt_run_script).with(1..2).arguments
181
174
  end
182
175
 
183
176
  context 'when running against localhost and no inventory.yaml file' do
184
177
  it 'does bolt_run_script against localhost without error' do
185
178
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
186
179
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
187
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
188
- expect(described_class).not_to receive(:inventory_hash_from_inventory_file)
189
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
190
- expect(described_class).to receive(:run_script).with(script, 'litmus_localhost', [], options: {}, config: nil, inventory: localhost_inventory_hash).and_return(result)
191
- expect { described_class.bolt_run_script(script) }.not_to raise_error
180
+ expect(self).not_to receive(:inventory_hash_from_inventory_file)
181
+ expect(self).to receive(:target_in_inventory?).and_return(true)
182
+ expect(self).to receive(:run_script).with(script, 'litmus_localhost', [], options: {}, config: nil, inventory: localhost_inventory_hash).and_return(result)
183
+ expect { bolt_run_script(script) }.not_to raise_error
192
184
  end
193
185
  end
194
186
 
@@ -196,10 +188,10 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
196
188
  it 'does bolt_run_script against remote host without error' do
197
189
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
198
190
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
199
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
200
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
201
- expect(described_class).to receive(:run_script).with(script, 'some.host', [], options: {}, config: nil, inventory: inventory_hash).and_return(result)
202
- expect { described_class.bolt_run_script(script) }.not_to raise_error
191
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
192
+ expect(self).to receive(:target_in_inventory?).and_return(true)
193
+ expect(self).to receive(:run_script).with(script, 'some.host', [], options: {}, config: nil, inventory: inventory_hash).and_return(result)
194
+ expect { bolt_run_script(script) }.not_to raise_error
203
195
  end
204
196
  end
205
197
 
@@ -207,11 +199,10 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
207
199
  it 'does bolt_run_script with arguments without error' do
208
200
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
209
201
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(false)
210
- expect(described_class).to receive(:localhost_inventory_hash).and_return(localhost_inventory_hash)
211
- expect(described_class).not_to receive(:inventory_hash_from_inventory_file)
212
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
213
- expect(described_class).to receive(:run_script).with(script, 'litmus_localhost', ['doot'], options: {}, config: nil, inventory: localhost_inventory_hash).and_return(result)
214
- expect { described_class.bolt_run_script(script, arguments: ['doot']) }.not_to raise_error
202
+ expect(self).not_to receive(:inventory_hash_from_inventory_file)
203
+ expect(self).to receive(:target_in_inventory?).and_return(true)
204
+ expect(self).to receive(:run_script).with(script, 'litmus_localhost', ['doot'], options: {}, config: nil, inventory: localhost_inventory_hash).and_return(result)
205
+ expect { bolt_run_script(script, arguments: ['doot']) }.not_to raise_error
215
206
  end
216
207
  end
217
208
  end
@@ -221,52 +212,52 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
221
212
  let(:params) { { 'action' => 'install', 'name' => 'foo' } }
222
213
  let(:config_data) { { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') } }
223
214
  # Ignore rubocop because these hashes are representative of output from an external method and editing them leads to test failures.
224
- # rubocop:disable Layout/SpaceInsideHashLiteralBraces, Layout/SpaceBeforeBlockBraces, Layout/SpaceInsideBlockBraces, Layout/SpaceAroundOperators, Layout/LineLength, Layout/SpaceAfterComma
225
- let(:result_unstructured_task_success){ [{'target'=>'some.host','action'=>'task','object'=>'testtask::unstructured','status'=>'success','value'=>{'_output'=>'SUCCESS!'}}]}
226
- let(:result_structured_task_success){ [{'target'=>'some.host','action'=>'task','object'=>'testtask::structured','status'=>'success','value'=>{'key1'=>'foo','key2'=>'bar'}}]}
227
- let(:result_failure) {[{'target'=>'some.host','action'=>'task','object'=>'testtask::unstructured','status'=>'failure','value'=>{'_error'=>{'msg'=>'FAILURE!','kind'=>'puppetlabs.tasks/task-error','details'=>{'exitcode'=>123}}}}]}
228
- # rubocop:enable Layout/SpaceInsideHashLiteralBraces, Layout/SpaceBeforeBlockBraces, Layout/SpaceInsideBlockBraces, Layout/SpaceAroundOperators, Layout/LineLength, Layout/SpaceAfterComma
215
+ # rubocop:disable Layout/SpaceBeforeBlockBraces
216
+ let(:result_unstructured_task_success){ [{ 'target'=>'some.host','action'=>'task','object'=>'testtask::unstructured','status'=>'success','value'=>{ '_output'=>'SUCCESS!' } }] }
217
+ let(:result_structured_task_success){ [{ 'target'=>'some.host','action'=>'task','object'=>'testtask::structured','status'=>'success','value'=>{ 'key1'=>'foo','key2'=>'bar' } }] }
218
+ let(:result_failure) { [{ 'target'=>'some.host','action'=>'task','object'=>'testtask::unstructured','status'=>'failure','value'=>{ '_error'=>{ 'msg'=>'FAILURE!','kind'=>'puppetlabs.tasks/task-error','details'=>{ 'exitcode'=>123 } } } }] }
219
+ # rubocop:enable Layout/SpaceBeforeBlockBraces, Layout/SpaceAroundOperators, Layout/LineLength, Layout/SpaceAfterComma
229
220
 
230
221
  it 'responds to bolt_run_task' do
231
- expect(described_class).to respond_to(:run_bolt_task).with(2..3).arguments
222
+ expect(self).to respond_to(:run_bolt_task).with(2..3).arguments
232
223
  end
233
224
 
234
225
  context 'when bolt returns success' do
235
226
  it 'does bolt_task_run gives no runtime error for success' do
236
227
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
237
228
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
238
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
239
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
240
- expect(described_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
241
- expect { described_class.run_bolt_task(task_name, params, opts: {}) }.not_to raise_error
229
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
230
+ expect(self).to receive(:target_in_inventory?).and_return(true)
231
+ expect(self).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
232
+ expect { run_bolt_task(task_name, params, opts: {}) }.not_to raise_error
242
233
  end
243
234
 
244
235
  it 'does bolt_task_run gives no runtime error for success, for a named inventory file' do
245
236
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
246
237
  expect(File).to receive(:exist?).with('jim.yaml').and_return(true)
247
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
248
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
249
- expect(described_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
250
- expect { described_class.run_bolt_task(task_name, params, inventory_file: 'jim.yaml') }.not_to raise_error
238
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
239
+ expect(self).to receive(:target_in_inventory?).and_return(true)
240
+ expect(self).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
241
+ expect { run_bolt_task(task_name, params, inventory_file: 'jim.yaml') }.not_to raise_error
251
242
  end
252
243
 
253
244
  it 'returns stdout for unstructured-data tasks' do
254
245
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
255
246
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
256
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
257
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
258
- expect(described_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
259
- method_result = described_class.run_bolt_task(task_name, params, opts: {})
247
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
248
+ expect(self).to receive(:target_in_inventory?).and_return(true)
249
+ expect(self).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
250
+ method_result = run_bolt_task(task_name, params, opts: {})
260
251
  expect(method_result.stdout).to eq('SUCCESS!')
261
252
  end
262
253
 
263
254
  it 'returns structured output for structured-data tasks' do
264
255
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
265
256
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
266
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
267
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
268
- expect(described_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_structured_task_success)
269
- method_result = described_class.run_bolt_task(task_name, params, opts: {})
257
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
258
+ expect(self).to receive(:target_in_inventory?).and_return(true)
259
+ expect(self).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_structured_task_success)
260
+ method_result = run_bolt_task(task_name, params, opts: {})
270
261
  expect(method_result.stdout).to eq('{"key1"=>"foo", "key2"=>"bar"}')
271
262
  expect(method_result.result['key1']).to eq('foo')
272
263
  expect(method_result.result['key2']).to eq('bar')
@@ -277,22 +268,60 @@ RSpec.describe PuppetLitmus::PuppetHelpers do
277
268
  it 'does bolt_task_run gives runtime error for failure' do
278
269
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
279
270
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
280
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
281
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
282
- expect(described_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
283
- expect { described_class.run_bolt_task(task_name, params, opts: {}) }.to raise_error(RuntimeError, %r{task failed})
271
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
272
+ expect(self).to receive(:target_in_inventory?).and_return(true)
273
+ expect(self).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
274
+ expect { run_bolt_task(task_name, params, opts: {}) }.to raise_error(RuntimeError, %r{task failed})
284
275
  end
285
276
 
286
277
  it 'returns the exit code and error message when expecting failure' do
287
278
  stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
288
279
  expect(File).to receive(:exist?).with('inventory.yaml').and_return(true)
289
- expect(described_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
290
- expect(described_class).to receive(:target_in_inventory?).and_return(true)
291
- expect(described_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
292
- method_result = described_class.run_bolt_task(task_name, params, expect_failures: true)
280
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
281
+ expect(self).to receive(:target_in_inventory?).and_return(true)
282
+ expect(self).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
283
+ method_result = run_bolt_task(task_name, params, expect_failures: true)
293
284
  expect(method_result.exit_code).to be(123)
294
285
  expect(method_result.stderr).to be('FAILURE!')
295
286
  end
296
287
  end
297
288
  end
289
+
290
+ describe '.write_file' do
291
+ let(:content) { 'foo' }
292
+ let(:destination) { '/tmp/foo' }
293
+ let(:owner) { 'foo:foo' }
294
+ let(:local_path) { '/tmp/local_foo' }
295
+
296
+ before(:each) do
297
+ allow_any_instance_of(File).to receive(:path).and_return(local_path)
298
+ end
299
+
300
+ it 'responds to write_file' do
301
+ expect(self).to respond_to(:write_file).with(2).arguments
302
+ end
303
+
304
+ context 'without setting owner' do
305
+ it 'call upload file with the correct params' do
306
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
307
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
308
+ result = instance_double('result')
309
+ allow(result).to receive(:first).and_return({ 'status' => 'success' })
310
+ expect(self).to receive(:upload_file).with(local_path, destination, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result)
311
+ result = write_file(content, destination)
312
+ expect(result).to be true
313
+ end
314
+ end
315
+
316
+ context 'when upload encounters an error' do
317
+ it 'call upload file with the correct params' do
318
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
319
+ expect(self).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
320
+ result = instance_double('result')
321
+ allow(result).to receive(:first).and_return({ 'status' => 'failure', 'value' => 'foo error' })
322
+ expect(self).to receive(:upload_file).with(local_path, destination, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result)
323
+ expect { write_file(content, destination) }.to raise_error 'foo error'
324
+ end
325
+ end
326
+ end
298
327
  end
@@ -13,12 +13,12 @@ RSpec.shared_examples 'supported provisioner' do |args|
13
13
  it 'calls function' do
14
14
  allow(File).to receive(:directory?).and_call_original
15
15
  allow(File).to receive(:directory?)
16
- .with(File.join(DEFAULT_CONFIG_DATA['modulepath'], 'provision'))
16
+ .with(File.join(described_class::DEFAULT_CONFIG_DATA['modulepath'], 'provision'))
17
17
  .and_return(true)
18
18
  allow_any_instance_of(BoltSpec::Run).to receive(:run_task)
19
- .with("provision::#{provisioner}", 'localhost', params, config: DEFAULT_CONFIG_DATA, inventory: nil)
19
+ .with("provision::#{provisioner}", 'localhost', params, config: described_class::DEFAULT_CONFIG_DATA, inventory: nil)
20
20
  .and_return(results)
21
- result = described_class.provision(provisioner, platform, inventory_vars)
21
+ result = provision(provisioner, platform, inventory_vars)
22
22
  expect(result).to eq(results)
23
23
  end
24
24
  end
@@ -29,8 +29,8 @@ RSpec.describe PuppetLitmus::RakeHelper do
29
29
  let(:results) { [] }
30
30
 
31
31
  it 'calls function' do
32
- expect(described_class).to receive(:provision).with('docker', 'waffleimage/centos7', nil).and_return(results)
33
- described_class.provision_list(provision_hash, 'default')
32
+ expect(self).to receive(:provision).with('docker', 'waffleimage/centos7', nil).and_return(results)
33
+ provision_list(provision_hash, 'default')
34
34
  end
35
35
  end
36
36
 
@@ -71,9 +71,9 @@ RSpec.describe PuppetLitmus::RakeHelper do
71
71
  let(:params) { { 'action' => 'tear_down', 'node_name' => 'some.host', 'inventory' => Dir.pwd } }
72
72
 
73
73
  it 'calls function' do
74
- allow(File).to receive(:directory?).with(File.join(DEFAULT_CONFIG_DATA['modulepath'], 'provision')).and_return(true)
75
- allow_any_instance_of(BoltSpec::Run).to receive(:run_task).with('provision::docker', 'localhost', params, config: DEFAULT_CONFIG_DATA, inventory: nil).and_return([])
76
- described_class.tear_down_nodes(targets, inventory_hash)
74
+ allow(File).to receive(:directory?).with(File.join(described_class::DEFAULT_CONFIG_DATA['modulepath'], 'provision')).and_return(true)
75
+ allow_any_instance_of(BoltSpec::Run).to receive(:run_task).with('provision::docker', 'localhost', params, config: described_class::DEFAULT_CONFIG_DATA, inventory: nil).and_return([])
76
+ tear_down_nodes(targets, inventory_hash)
77
77
  end
78
78
  end
79
79
 
@@ -87,9 +87,9 @@ RSpec.describe PuppetLitmus::RakeHelper do
87
87
  let(:params) { { 'collection' => 'puppet6' } }
88
88
 
89
89
  it 'calls function' do
90
- allow(File).to receive(:directory?).with(File.join(DEFAULT_CONFIG_DATA['modulepath'], 'puppet_agent')).and_return(true)
91
- allow_any_instance_of(BoltSpec::Run).to receive(:run_task).with('puppet_agent::install', targets, params, config: DEFAULT_CONFIG_DATA, inventory: inventory_hash).and_return([])
92
- described_class.install_agent('puppet6', targets, inventory_hash)
90
+ allow(File).to receive(:directory?).with(File.join(described_class::DEFAULT_CONFIG_DATA['modulepath'], 'puppet_agent')).and_return(true)
91
+ allow_any_instance_of(BoltSpec::Run).to receive(:run_task).with('puppet_agent::install', targets, params, config: described_class::DEFAULT_CONFIG_DATA, inventory: inventory_hash).and_return([])
92
+ install_agent('puppet6', targets, inventory_hash)
93
93
  end
94
94
  end
95
95
 
@@ -100,20 +100,20 @@ RSpec.describe PuppetLitmus::RakeHelper do
100
100
  [{ 'name' => 'ssh_nodes', 'targets' =>
101
101
  [{ 'uri' => 'some.host', 'facts' => { 'provisioner' => 'docker', 'container_name' => 'foo', 'platform' => 'some.host' } }] }] }
102
102
  end
103
- let(:module_tar) { '/tmp/foo.tar.gz' }
103
+ let(:module_tar) { 'foo.tar.gz' }
104
104
  let(:targets) { ['some.host'] }
105
105
  let(:uninstall_module_command) { 'puppet module uninstall foo --force' }
106
- let(:install_module_command) { "puppet module install --module_repository 'https://forgeapi.puppetlabs.com' #{module_tar}" }
106
+ let(:install_module_command) { "puppet module install --module_repository 'https://forgeapi.example.com' #{module_tar}" }
107
107
 
108
108
  it 'calls function' do
109
109
  allow_any_instance_of(BoltSpec::Run).to receive(:upload_file).with(module_tar, module_tar, targets, options: {}, config: nil, inventory: inventory_hash).and_return([])
110
110
  allow(File).to receive(:exist?).with(File.join(Dir.pwd, 'metadata.json')).and_return(true)
111
111
  allow(File).to receive(:read).with(File.join(Dir.pwd, 'metadata.json')).and_return(JSON.dump({ name: 'foo' }))
112
- allow(Open3).to receive(:capture3).with("bundle exec bolt file upload \"#{module_tar}\" /tmp/#{File.basename(module_tar)} --targets all --inventoryfile inventory.yaml")
112
+ allow(Open3).to receive(:capture3).with("bundle exec bolt file upload \"#{module_tar}\" #{File.basename(module_tar)} --targets all --inventoryfile inventory.yaml")
113
113
  .and_return(['success', '', 0])
114
114
  allow_any_instance_of(BoltSpec::Run).to receive(:run_command).with(uninstall_module_command, targets, config: nil, inventory: inventory_hash).and_return([])
115
115
  allow_any_instance_of(BoltSpec::Run).to receive(:run_command).with(install_module_command, targets, config: nil, inventory: inventory_hash).and_return([])
116
- described_class.install_module(inventory_hash, nil, module_tar)
116
+ install_module(inventory_hash, nil, module_tar, 'https://forgeapi.example.com')
117
117
  end
118
118
  end
119
119
 
@@ -129,12 +129,12 @@ RSpec.describe PuppetLitmus::RakeHelper do
129
129
  it 'node available' do
130
130
  allow(Open3).to receive(:capture3).with('cd .').and_return(['success', '', 0])
131
131
  allow_any_instance_of(BoltSpec::Run).to receive(:run_command).with(command, targets, config: nil, inventory: inventory_hash).and_return([{ 'target' => 'some.host', 'status' => 'success' }])
132
- described_class.check_connectivity?(inventory_hash, 'some.host')
132
+ check_connectivity?(inventory_hash, 'some.host')
133
133
  end
134
134
 
135
135
  it 'node unavailable' do
136
136
  allow_any_instance_of(BoltSpec::Run).to receive(:run_command).with(command, targets, config: nil, inventory: inventory_hash).and_return([{ 'target' => 'some.host', 'status' => 'failure' }])
137
- expect { described_class.check_connectivity?(inventory_hash, 'some.host') }.to raise_error(RuntimeError, %r{Connectivity has failed on:})
137
+ expect { check_connectivity?(inventory_hash, 'some.host') }.to raise_error(RuntimeError, %r{Connectivity has failed on:})
138
138
  end
139
139
  end
140
140
 
@@ -149,13 +149,13 @@ RSpec.describe PuppetLitmus::RakeHelper do
149
149
 
150
150
  it 'uninstalls module' do
151
151
  allow_any_instance_of(BoltSpec::Run).to receive(:run_command).with(uninstall_module_command, targets, config: nil, inventory: inventory_hash).and_return([])
152
- expect(described_class).to receive(:metadata_module_name).and_return('foo-bar')
153
- described_class.uninstall_module(inventory_hash, nil)
152
+ expect(self).to receive(:metadata_module_name).and_return('foo-bar')
153
+ uninstall_module(inventory_hash, nil)
154
154
  end
155
155
 
156
156
  it 'and custom name' do
157
157
  allow_any_instance_of(BoltSpec::Run).to receive(:run_command).with(uninstall_module_command, targets, config: nil, inventory: inventory_hash).and_return([])
158
- described_class.uninstall_module(inventory_hash, nil, 'foo-bar')
158
+ uninstall_module(inventory_hash, nil, 'foo-bar')
159
159
  end
160
160
  end
161
161
 
@@ -165,7 +165,7 @@ RSpec.describe PuppetLitmus::RakeHelper do
165
165
  it 'reads module name' do
166
166
  allow(File).to receive(:exist?).with(File.join(Dir.pwd, 'metadata.json')).and_return(true)
167
167
  allow(File).to receive(:read).with(File.join(Dir.pwd, 'metadata.json')).and_return(metadata)
168
- name = described_class.metadata_module_name
168
+ name = metadata_module_name
169
169
  expect(name).to eq('foo-bar')
170
170
  end
171
171
  end
@@ -173,12 +173,12 @@ RSpec.describe PuppetLitmus::RakeHelper do
173
173
  context 'with provisioner_task' do
174
174
  described_class::SUPPORTED_PROVISIONERS.each do |supported_provisioner|
175
175
  it "returns supported provisioner task name for #{supported_provisioner}" do
176
- expect(described_class.provisioner_task(supported_provisioner)).to eq("provision::#{supported_provisioner}")
176
+ expect(provisioner_task(supported_provisioner)).to eq("provision::#{supported_provisioner}")
177
177
  end
178
178
  end
179
179
 
180
180
  it 'returns an unsupported provisioner name' do
181
- expect(described_class.provisioner_task('my_org::custom_provisioner')).to eql('my_org::custom_provisioner')
181
+ expect(provisioner_task('my_org::custom_provisioner')).to eql('my_org::custom_provisioner')
182
182
  end
183
183
  end
184
184
  end