puppet_litmus 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,55 +1,55 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'rake'
5
-
6
- describe 'litmus rake tasks' do
7
- before(:all) do # rubocop:disable RSpec/BeforeAfterAll
8
- load File.expand_path('../../../lib/puppet_litmus/rake_tasks.rb', __dir__)
9
- # the spec_prep task is stubbed, rather than load from another gem.
10
- Rake::Task.define_task(:spec_prep)
11
- end
12
-
13
- context 'with litmus:metadata task' do
14
- it 'happy path' do
15
- metadata = { 'name' => 'puppetlabs-postgresql',
16
- 'version' => '6.0.0',
17
- 'operatingsystem_support' =>
18
- [{ 'operatingsystem' => 'RedHat', 'operatingsystemrelease' => ['5'] },
19
- { 'operatingsystem' => 'Ubuntu', 'operatingsystemrelease' => ['14.04', '18.04'] }],
20
- 'template-ref' => 'heads/master-0-g7827fc2' }
21
- expect(File).to receive(:read).with(any_args).once
22
- expect(JSON).to receive(:parse).with(any_args).and_return(metadata)
23
- expect(STDOUT).to receive(:puts).with('redhat-5-x86_64')
24
- expect(STDOUT).to receive(:puts).with('ubuntu-1404-x86_64')
25
- expect(STDOUT).to receive(:puts).with('ubuntu-1804-x86_64')
26
- Rake::Task['litmus:metadata'].invoke
27
- end
28
- end
29
-
30
- context 'with litmus:provision_install task' do
31
- it 'happy path' do
32
- expect(Rake::Task['spec_prep']).to receive(:invoke).and_return('').once
33
- expect(Rake::Task['litmus:provision_list']).to receive(:invoke).with('default').once
34
- expect(Rake::Task['litmus:install_agent']).to receive(:invoke).with('puppet6').once
35
- expect(Rake::Task['litmus:install_module']).to receive(:invoke).once
36
- Rake::Task['litmus:provision_install'].invoke('default', 'puppet6')
37
- end
38
- end
39
-
40
- context 'with litmus:provision task' do
41
- it 'provisions' do
42
- results = [{ 'node' => 'localhost',
43
- 'target' => 'localhost',
44
- 'action' => 'task',
45
- 'object' => 'provision::docker',
46
- 'status' => 'success',
47
- 'result' => { 'status' => 'ok', 'node_name' => 'localhost:2222' } }]
48
-
49
- allow(File).to receive(:directory?).with(any_args).and_return(true)
50
- allow_any_instance_of(BoltSpec::Run).to receive(:run_task).with(any_args).and_return(results) # rubocop:disable RSpec/AnyInstance
51
- expect(STDOUT).to receive(:puts).with('localhost:2222, centos:7')
52
- Rake::Task['litmus:provision'].invoke('docker', 'centos:7')
53
- end
54
- end
55
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'rake'
5
+
6
+ describe 'litmus rake tasks' do
7
+ before(:all) do # rubocop:disable RSpec/BeforeAfterAll
8
+ load File.expand_path('../../../lib/puppet_litmus/rake_tasks.rb', __dir__)
9
+ # the spec_prep task is stubbed, rather than load from another gem.
10
+ Rake::Task.define_task(:spec_prep)
11
+ end
12
+
13
+ context 'with litmus:metadata task' do
14
+ it 'happy path' do
15
+ metadata = { 'name' => 'puppetlabs-postgresql',
16
+ 'version' => '6.0.0',
17
+ 'operatingsystem_support' =>
18
+ [{ 'operatingsystem' => 'RedHat', 'operatingsystemrelease' => ['5'] },
19
+ { 'operatingsystem' => 'Ubuntu', 'operatingsystemrelease' => ['14.04', '18.04'] }],
20
+ 'template-ref' => 'heads/master-0-g7827fc2' }
21
+ expect(File).to receive(:read).with(any_args).once
22
+ expect(JSON).to receive(:parse).with(any_args).and_return(metadata)
23
+ expect(STDOUT).to receive(:puts).with('redhat-5-x86_64')
24
+ expect(STDOUT).to receive(:puts).with('ubuntu-1404-x86_64')
25
+ expect(STDOUT).to receive(:puts).with('ubuntu-1804-x86_64')
26
+ Rake::Task['litmus:metadata'].invoke
27
+ end
28
+ end
29
+
30
+ context 'with litmus:provision_install task' do
31
+ it 'happy path' do
32
+ expect(Rake::Task['spec_prep']).to receive(:invoke).and_return('').once
33
+ expect(Rake::Task['litmus:provision_list']).to receive(:invoke).with('default').once
34
+ expect(Rake::Task['litmus:install_agent']).to receive(:invoke).with('puppet6').once
35
+ expect(Rake::Task['litmus:install_module']).to receive(:invoke).once
36
+ Rake::Task['litmus:provision_install'].invoke('default', 'puppet6')
37
+ end
38
+ end
39
+
40
+ context 'with litmus:provision task' do
41
+ it 'provisions' do
42
+ results = [{ 'node' => 'localhost',
43
+ 'target' => 'localhost',
44
+ 'action' => 'task',
45
+ 'object' => 'provision::docker',
46
+ 'status' => 'success',
47
+ 'result' => { 'status' => 'ok', 'node_name' => 'localhost:2222' } }]
48
+
49
+ allow(File).to receive(:directory?).with(any_args).and_return(true)
50
+ allow_any_instance_of(BoltSpec::Run).to receive(:run_task).with(any_args).and_return(results) # rubocop:disable RSpec/AnyInstance
51
+ expect(STDOUT).to receive(:puts).with('localhost:2222, centos:7')
52
+ Rake::Task['litmus:provision'].invoke('docker', 'centos:7')
53
+ end
54
+ end
55
+ end
@@ -1,150 +1,196 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe PuppetLitmus::Serverspec do
6
- class DummyClass
7
- end
8
- let(:dummy_class) do
9
- dummy_class = DummyClass.new
10
- dummy_class.extend(described_class)
11
- dummy_class
12
- end
13
-
14
- context 'with idempotent_apply' do
15
- let(:manifest) do
16
- "include '::doot'"
17
- end
18
-
19
- it 'calls all functions' do
20
- expect(dummy_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
21
- expect(dummy_class).to receive(:apply_manifest).with(nil, catch_failures: true, manifest_file_location: '/bla.pp')
22
- expect(dummy_class).to receive(:apply_manifest).with(nil, catch_changes: true, manifest_file_location: '/bla.pp')
23
- dummy_class.idempotent_apply(manifest)
24
- end
25
- end
26
-
27
- describe '.run_shell' do
28
- let(:command_to_run) { "puts 'doot'" }
29
- let(:result) { ['result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil }] }
30
- let(:inventory_hash) { Hash.new(0) }
31
-
32
- it 'responds to run_shell' do
33
- expect(dummy_class).to respond_to(:run_shell).with(1..2).arguments
34
- end
35
-
36
- context 'when running against localhost and no inventory.yaml file' do
37
- it 'does run_shell against localhost without error' do
38
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('localhost')
39
- expect(dummy_class).to receive(:run_command).with(command_to_run, 'localhost', config: nil, inventory: nil).and_return(result)
40
- expect { dummy_class.run_shell(command_to_run) }.not_to raise_error
41
- end
42
- end
43
-
44
- context 'when running against remote host' do
45
- it 'does run_shell against remote host without error' do
46
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
47
- expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
48
- expect(dummy_class).to receive(:run_command).with(command_to_run, 'some.host', config: nil, inventory: inventory_hash).and_return(result)
49
- expect { dummy_class.run_shell(command_to_run) }.not_to raise_error
50
- end
51
- end
52
- end
53
-
54
- describe '.bolt_upload_file' do
55
- let(:local) { '/tmp' }
56
- let(:remote) { '/remote_tmp' }
57
- let(:result) { ['status' => 'success', 'result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil }] }
58
- let(:inventory_hash) { Hash.new(0) }
59
-
60
- it 'responds to run_shell' do
61
- expect(dummy_class).to respond_to(:bolt_upload_file).with(2..3).arguments
62
- end
63
-
64
- context 'when running against remote host' do
65
- it 'does upload_file against remote host without error' do
66
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
67
- expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
68
- expect(dummy_class).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result)
69
- expect { dummy_class.bolt_upload_file(local, remote) }.not_to raise_error
70
- end
71
- end
72
-
73
- context 'when running against remote host' do
74
- it 'does upload_file against localhost without error' do
75
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('localhost')
76
- expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
77
- expect(dummy_class).to receive(:upload_file).with(local, remote, 'localhost', options: {}, config: nil, inventory: nil).and_return(result)
78
- expect { dummy_class.bolt_upload_file(local, remote) }.not_to raise_error
79
- end
80
- end
81
- end
82
-
83
- describe '.bolt_run_script' do
84
- let(:script) { '/tmp/script.sh' }
85
- let(:result) { ['result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil }] }
86
- let(:inventory_hash) { Hash.new(0) }
87
-
88
- it 'responds to bolt_run_script' do
89
- expect(dummy_class).to respond_to(:bolt_run_script).with(1..2).arguments
90
- end
91
-
92
- context 'when running against localhost and no inventory.yaml file' do
93
- it 'does bolt_run_script against localhost without error' do
94
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('localhost')
95
- expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
96
- expect(dummy_class).to receive(:run_script).with(script, 'localhost', [], options: {}, config: nil, inventory: nil).and_return(result)
97
- expect { dummy_class.bolt_run_script(script) }.not_to raise_error
98
- end
99
- end
100
-
101
- context 'when running against remote host' do
102
- it 'does bolt_run_script against remote host without error' do
103
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
104
- expect(dummy_class).to receive(:inventory_hash_from_inventory_file)
105
- expect(dummy_class).to receive(:run_script).with(script, 'some.host', [], options: {}, config: nil, inventory: nil).and_return(result)
106
- expect { dummy_class.bolt_run_script(script) }.not_to raise_error
107
- end
108
- end
109
-
110
- context 'when running with arguments' do
111
- it 'does bolt_run_script with arguments without error' do
112
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('localhost')
113
- expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
114
- expect(dummy_class).to receive(:run_script).with(script, 'localhost', ['doot'], options: {}, config: nil, inventory: nil).and_return(result)
115
- expect { dummy_class.bolt_run_script(script, arguments: ['doot']) }.not_to raise_error
116
- end
117
- end
118
- end
119
-
120
- describe '.run_bolt_task' do
121
- let(:task_name) { 'testtask' }
122
- let(:params) { { 'action' => 'install', 'name' => 'foo' } }
123
- let(:config_data) { { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') } }
124
- let(:result_success) { ['status' => 'success', 'result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil, 'result' => nil }] }
125
- let(:result_failure) { ['status' => 'failure', 'result' => { 'exit_code' => 255, 'stdout' => 'failure', 'stderr' => 'failure', 'result' => nil }] }
126
- let(:inventory_hash) { Hash.new(0) }
127
-
128
- it 'responds to bolt_run_task' do
129
- expect(dummy_class).to respond_to(:run_bolt_task).with(2..3).arguments
130
- end
131
-
132
- context 'when bolt returns success' do
133
- it 'does bolt_task_run gives no runtime error for success' do
134
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
135
- expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
136
- expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_success)
137
- expect { dummy_class.run_bolt_task(task_name, params, opts: {}) }.not_to raise_error
138
- end
139
- end
140
-
141
- context 'when bolt returns failure' do
142
- it 'does bolt_task_run gives runtime error for failure' do
143
- allow(ENV).to receive(:[]).with('TARGET_HOST').and_return('some.host')
144
- expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
145
- expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
146
- expect { dummy_class.run_bolt_task(task_name, params, opts: {}) }.to raise_error(RuntimeError, %r{task failed})
147
- end
148
- end
149
- end
150
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe PuppetLitmus::Serverspec do
6
+ class DummyClass
7
+ end
8
+ let(:dummy_class) do
9
+ dummy_class = DummyClass.new
10
+ dummy_class.extend(described_class)
11
+ dummy_class
12
+ end
13
+
14
+ context 'with idempotent_apply' do
15
+ let(:manifest) do
16
+ "include '::doot'"
17
+ end
18
+
19
+ it 'calls all functions' do
20
+ expect(dummy_class).to receive(:create_manifest_file).with(manifest).and_return('/bla.pp')
21
+ expect(dummy_class).to receive(:apply_manifest).with(nil, catch_failures: true, manifest_file_location: '/bla.pp')
22
+ expect(dummy_class).to receive(:apply_manifest).with(nil, catch_changes: true, manifest_file_location: '/bla.pp')
23
+ dummy_class.idempotent_apply(manifest)
24
+ end
25
+ end
26
+
27
+ describe '.run_shell' do
28
+ let(:command_to_run) { "puts 'doot'" }
29
+ let(:result) { ['result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil }] }
30
+ let(:inventory_hash) { Hash.new(0) }
31
+
32
+ it 'responds to run_shell' do
33
+ expect(dummy_class).to respond_to(:run_shell).with(1..2).arguments
34
+ end
35
+
36
+ context 'when running against localhost and no inventory.yaml file' do
37
+ it 'does run_shell against localhost without error' do
38
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
39
+ expect(dummy_class).to receive(:run_command).with(command_to_run, 'localhost', config: nil, inventory: nil).and_return(result)
40
+ expect { dummy_class.run_shell(command_to_run) }.not_to raise_error
41
+ end
42
+ end
43
+
44
+ context 'when running against remote host' do
45
+ it 'does run_shell against remote host without error' do
46
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
47
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
48
+ expect(dummy_class).to receive(:run_command).with(command_to_run, 'some.host', config: nil, inventory: inventory_hash).and_return(result)
49
+ expect { dummy_class.run_shell(command_to_run) }.not_to raise_error
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '.bolt_upload_file' do
55
+ let(:local) { '/tmp' }
56
+ let(:remote) { '/remote_tmp' }
57
+ # Ignore rubocop because these hashes are representative of output from an external method and editing them leads to test failures.
58
+ # rubocop:disable SpaceInsideHashLiteralBraces, SpaceInsideBlockBraces, SpaceAroundOperators, LineLength, SpaceAfterComma
59
+ let(:result_success) {[{'node'=>'some.host','target'=>'some.host','action'=>'upload','object'=>'C:\foo\bar.ps1','status'=>'success','result'=>{'_output'=>'Uploaded \'C:\foo\bar.ps1\' to \'some.host:C:\bar\''}}]}
60
+ let(:result_failure) {[{'node'=>'some.host','target'=>'some.host','action'=>nil,'object'=>nil,'status'=>'failure','result'=>{'_error'=>{'kind'=>'puppetlabs.tasks/task_file_error','msg'=>'No such file or directory @ rb_sysopen - /nonexistant/file/path','details'=>{},'issue_code'=>'WRITE_ERROR'}}}]}
61
+ # rubocop:enable SpaceInsideHashLiteralBraces, SpaceInsideBlockBraces, SpaceAroundOperators, LineLength, SpaceAfterComma
62
+ let(:inventory_hash) { Hash.new(0) }
63
+
64
+ it 'responds to run_shell' do
65
+ expect(dummy_class).to respond_to(:bolt_upload_file).with(2..3).arguments
66
+ end
67
+
68
+ context 'when upload returns success' do
69
+ it 'does upload_file against remote host without error' do
70
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
71
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
72
+ expect(dummy_class).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_success)
73
+ expect { dummy_class.bolt_upload_file(local, remote) }.not_to raise_error
74
+ end
75
+ it 'does upload_file against localhost without error' do
76
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
77
+ expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
78
+ expect(dummy_class).to receive(:upload_file).with(local, remote, 'localhost', options: {}, config: nil, inventory: nil).and_return(result_success)
79
+ expect { dummy_class.bolt_upload_file(local, remote) }.not_to raise_error
80
+ end
81
+ end
82
+
83
+ context 'when upload returns failure' do
84
+ it 'does upload_file gives runtime error for failure' do
85
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
86
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
87
+ expect(dummy_class).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_failure)
88
+ expect { dummy_class.bolt_upload_file(local, remote) }.to raise_error(RuntimeError, %r{upload file failed})
89
+ end
90
+ it 'returns the exit code and error message when expecting failure' do
91
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
92
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
93
+ expect(dummy_class).to receive(:upload_file).with(local, remote, 'some.host', options: {}, config: nil, inventory: inventory_hash).and_return(result_failure)
94
+ method_result = dummy_class.bolt_upload_file(local, remote, expect_failures: true)
95
+ expect(method_result.exit_code).to be(255)
96
+ expect(method_result.stderr).to be('No such file or directory @ rb_sysopen - /nonexistant/file/path')
97
+ end
98
+ end
99
+ end
100
+
101
+ describe '.bolt_run_script' do
102
+ let(:script) { '/tmp/script.sh' }
103
+ let(:result) { ['result' => { 'exit_code' => 0, 'stdout' => nil, 'stderr' => nil }] }
104
+ let(:inventory_hash) { Hash.new(0) }
105
+
106
+ it 'responds to bolt_run_script' do
107
+ expect(dummy_class).to respond_to(:bolt_run_script).with(1..2).arguments
108
+ end
109
+
110
+ context 'when running against localhost and no inventory.yaml file' do
111
+ it 'does bolt_run_script against localhost without error' do
112
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
113
+ expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
114
+ expect(dummy_class).to receive(:run_script).with(script, 'localhost', [], options: {}, config: nil, inventory: nil).and_return(result)
115
+ expect { dummy_class.bolt_run_script(script) }.not_to raise_error
116
+ end
117
+ end
118
+
119
+ context 'when running against remote host' do
120
+ it 'does bolt_run_script against remote host without error' do
121
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
122
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file)
123
+ expect(dummy_class).to receive(:run_script).with(script, 'some.host', [], options: {}, config: nil, inventory: nil).and_return(result)
124
+ expect { dummy_class.bolt_run_script(script) }.not_to raise_error
125
+ end
126
+ end
127
+
128
+ context 'when running with arguments' do
129
+ it 'does bolt_run_script with arguments without error' do
130
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'localhost'))
131
+ expect(dummy_class).not_to receive(:inventory_hash_from_inventory_file)
132
+ expect(dummy_class).to receive(:run_script).with(script, 'localhost', ['doot'], options: {}, config: nil, inventory: nil).and_return(result)
133
+ expect { dummy_class.bolt_run_script(script, arguments: ['doot']) }.not_to raise_error
134
+ end
135
+ end
136
+ end
137
+
138
+ describe '.run_bolt_task' do
139
+ let(:task_name) { 'testtask' }
140
+ let(:params) { { 'action' => 'install', 'name' => 'foo' } }
141
+ let(:config_data) { { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') } }
142
+ # Ignore rubocop because these hashes are representative of output from an external method and editing them leads to test failures.
143
+ # rubocop:disable SpaceInsideHashLiteralBraces, SpaceBeforeBlockBraces, SpaceInsideBlockBraces, SpaceAroundOperators, LineLength, SpaceAfterComma
144
+ let(:result_unstructured_task_success){ [{'node'=>'some.host','target'=>'some.host','action'=>'task','object'=>'testtask::unstructured','status'=>'success','result'=>{'_output'=>'SUCCESS!'}}]}
145
+ let(:result_structured_task_success){ [{'node'=>'some.host','target'=>'some.host','action'=>'task','object'=>'testtask::structured','status'=>'success','result'=>{'key1'=>'foo','key2'=>'bar'}}]}
146
+ let(:result_failure) {[{'node'=>'some.host','target'=>'some.host','action'=>'task','object'=>'testtask::unstructured','status'=>'failure','result'=>{'_error'=>{'msg'=>'FAILURE!','kind'=>'puppetlabs.tasks/task-error','details'=>{'exitcode'=>123}}}}]}
147
+ # rubocop:enable SpaceInsideHashLiteralBraces, SpaceBeforeBlockBraces, SpaceInsideBlockBraces, SpaceAroundOperators, LineLength, SpaceAfterComma
148
+ let(:inventory_hash) { Hash.new(0) }
149
+
150
+ it 'responds to bolt_run_task' do
151
+ expect(dummy_class).to respond_to(:run_bolt_task).with(2..3).arguments
152
+ end
153
+
154
+ context 'when bolt returns success' do
155
+ it 'does bolt_task_run gives no runtime error for success' do
156
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
157
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
158
+ expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
159
+ expect { dummy_class.run_bolt_task(task_name, params, opts: {}) }.not_to raise_error
160
+ end
161
+ it 'returns stdout for unstructured-data tasks' do
162
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
163
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
164
+ expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_unstructured_task_success)
165
+ method_result = dummy_class.run_bolt_task(task_name, params, opts: {})
166
+ expect(method_result.stdout).to eq('SUCCESS!')
167
+ end
168
+ it 'returns structured output for structured-data tasks' do
169
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
170
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
171
+ expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_structured_task_success)
172
+ method_result = dummy_class.run_bolt_task(task_name, params, opts: {})
173
+ expect(method_result.stdout).to eq(nil)
174
+ expect(method_result.result['key1']).to eq('foo')
175
+ expect(method_result.result['key2']).to eq('bar')
176
+ end
177
+ end
178
+
179
+ context 'when bolt returns failure' do
180
+ it 'does bolt_task_run gives runtime error for failure' do
181
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
182
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
183
+ expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
184
+ expect { dummy_class.run_bolt_task(task_name, params, opts: {}) }.to raise_error(RuntimeError, %r{task failed})
185
+ end
186
+ it 'returns the exit code and error message when expecting failure' do
187
+ stub_const('ENV', ENV.to_hash.merge('TARGET_HOST' => 'some.host'))
188
+ expect(dummy_class).to receive(:inventory_hash_from_inventory_file).and_return(inventory_hash)
189
+ expect(dummy_class).to receive(:run_task).with(task_name, 'some.host', params, config: config_data, inventory: inventory_hash).and_return(result_failure)
190
+ method_result = dummy_class.run_bolt_task(task_name, params, expect_failures: true)
191
+ expect(method_result.exit_code).to be(123)
192
+ expect(method_result.stderr).to be('FAILURE!')
193
+ end
194
+ end
195
+ end
196
+ end