puppet_litmus 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,102 +1,102 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe PuppetLitmus::InventoryManipulation 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 config_from_node' do
15
- let(:no_config_hash) do
16
- { 'groups' =>
17
- [{ 'name' => 'ssh_nodes',
18
- 'nodes' =>
19
- [{ 'name' => 'test.delivery.puppetlabs.net',
20
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
21
- { 'name' => 'winrm_nodes', 'nodes' => [] }] }
22
- end
23
-
24
- let(:config_hash) do
25
- { 'groups' =>
26
- [{ 'name' => 'ssh_nodes',
27
- 'nodes' =>
28
- [{ 'name' => 'test.delivery.puppetlabs.net',
29
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
30
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
31
- { 'name' => 'winrm_nodes', 'nodes' => [] }] }
32
- end
33
-
34
- let(:inventory_full_path) { 'spec/data/inventory.yaml' }
35
-
36
- let(:no_feature_hash) do
37
- { 'groups' =>
38
- [{ 'name' => 'ssh_nodes',
39
- 'nodes' =>
40
- [{ 'name' => 'test.delivery.puppetlabs.net',
41
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
42
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
43
- { 'name' => 'winrm_nodes', 'nodes' => [] }] }
44
- end
45
-
46
- let(:feature_hash) do
47
- { 'groups' =>
48
- [{ 'name' => 'ssh_nodes',
49
- 'nodes' =>
50
- [{ 'name' => 'test.delivery.puppetlabs.net',
51
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
52
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
53
- 'features' => ['puppet-agent'] },
54
- { 'name' => 'winrm_nodes', 'nodes' => [] }] }
55
- end
56
-
57
- let(:empty_feature_hash) do
58
- { 'groups' =>
59
- [{ 'name' => 'ssh_nodes',
60
- 'nodes' =>
61
- [{ 'name' => 'test.delivery.puppetlabs.net',
62
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
63
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
64
- 'features' => [] },
65
- { 'name' => 'winrm_nodes', 'nodes' => [] }] }
66
- end
67
-
68
- it 'no matching node, raises' do
69
- expect { dummy_class.config_from_node(config_hash, 'not.here') }.to raise_error('No config was found for not.here')
70
- end
71
-
72
- it 'no config section, returns nil' do
73
- expect(dummy_class.config_from_node(no_config_hash, 'test.delivery.puppetlabs.net')).to eq(nil)
74
- end
75
-
76
- it 'config exists, and returns' do
77
- expect(dummy_class.config_from_node(config_hash, 'test.delivery.puppetlabs.net')).to eq('transport' => 'ssh', 'ssh' =>
78
- { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false })
79
- end
80
-
81
- it 'no feature exists, and returns hash with feature added' do
82
- expect(dummy_class.add_feature_to_group(no_feature_hash, 'puppet-agent', 'ssh_nodes')).to eq('groups' => [{ 'features' => ['puppet-agent'], 'name' => 'ssh_nodes', 'nodes' => [{ 'config' => { 'ssh' => { 'host-key-check' => false, 'password' => 'Qu@lity!', 'user' => 'root' }, 'transport' => 'ssh' }, 'facts' => { 'platform' => 'centos-5-x86_64', 'provisioner' => 'vmpooler' }, 'name' => 'test.delivery.puppetlabs.net' }] }, { 'name' => 'winrm_nodes', 'nodes' => [] }]) # rubocop:disable Metrics/LineLength: Line is too long
83
- end
84
-
85
- it 'feature exists, and returns hash with feature removed' do
86
- expect(dummy_class.remove_feature_from_group(feature_hash, 'puppet-agent', 'ssh_nodes')).to eq('groups' => [{ 'features' => [], 'name' => 'ssh_nodes', 'nodes' => [{ 'config' => { 'ssh' => { 'host-key-check' => false, 'password' => 'Qu@lity!', 'user' => 'root' }, 'transport' => 'ssh' }, 'facts' => { 'platform' => 'centos-5-x86_64', 'provisioner' => 'vmpooler' }, 'name' => 'test.delivery.puppetlabs.net' }] }, { 'name' => 'winrm_nodes', 'nodes' => [] }]) # rubocop:disable Metrics/LineLength: Line is too long
87
- end
88
-
89
- it 'write from inventory_hash to inventory_yaml file feature_hash' do
90
- expect { dummy_class.write_to_inventory_file(feature_hash, inventory_full_path) }.not_to raise_error
91
- end
92
-
93
- it 'empty feature exists, and returns hash with feature added' do
94
- expect(dummy_class.add_feature_to_group(empty_feature_hash, 'puppet-agent', 'ssh_nodes')).to eq('groups' => [{ 'features' => ['puppet-agent'], 'name' => 'ssh_nodes', 'nodes' => [{ 'config' => { 'ssh' => { 'host-key-check' => false, 'password' => 'Qu@lity!', 'user' => 'root' }, 'transport' => 'ssh' }, 'facts' => { 'platform' => 'centos-5-x86_64', 'provisioner' => 'vmpooler' }, 'name' => 'test.delivery.puppetlabs.net' }] }, { 'name' => 'winrm_nodes', 'nodes' => [] }]) # rubocop:disable Metrics/LineLength: Line is too long
95
- end
96
-
97
- it 'write from inventory_hash to inventory_yaml file no feature_hash' do
98
- expect(File).to exist(inventory_full_path)
99
- expect { dummy_class.write_to_inventory_file(no_feature_hash, inventory_full_path) }.not_to raise_error
100
- end
101
- end
102
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe PuppetLitmus::InventoryManipulation 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 config_from_node' do
15
+ let(:no_config_hash) do
16
+ { 'groups' =>
17
+ [{ 'name' => 'ssh_nodes',
18
+ 'nodes' =>
19
+ [{ 'name' => 'test.delivery.puppetlabs.net',
20
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
21
+ { 'name' => 'winrm_nodes', 'nodes' => [] }] }
22
+ end
23
+
24
+ let(:config_hash) do
25
+ { 'groups' =>
26
+ [{ 'name' => 'ssh_nodes',
27
+ 'nodes' =>
28
+ [{ 'name' => 'test.delivery.puppetlabs.net',
29
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
30
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
31
+ { 'name' => 'winrm_nodes', 'nodes' => [] }] }
32
+ end
33
+
34
+ let(:inventory_full_path) { 'spec/data/inventory.yaml' }
35
+
36
+ let(:no_feature_hash) do
37
+ { 'groups' =>
38
+ [{ 'name' => 'ssh_nodes',
39
+ 'nodes' =>
40
+ [{ 'name' => 'test.delivery.puppetlabs.net',
41
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
42
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
43
+ { 'name' => 'winrm_nodes', 'nodes' => [] }] }
44
+ end
45
+
46
+ let(:feature_hash) do
47
+ { 'groups' =>
48
+ [{ 'name' => 'ssh_nodes',
49
+ 'nodes' =>
50
+ [{ 'name' => 'test.delivery.puppetlabs.net',
51
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
52
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
53
+ 'features' => ['puppet-agent'] },
54
+ { 'name' => 'winrm_nodes', 'nodes' => [] }] }
55
+ end
56
+
57
+ let(:empty_feature_hash) do
58
+ { 'groups' =>
59
+ [{ 'name' => 'ssh_nodes',
60
+ 'nodes' =>
61
+ [{ 'name' => 'test.delivery.puppetlabs.net',
62
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
63
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
64
+ 'features' => [] },
65
+ { 'name' => 'winrm_nodes', 'nodes' => [] }] }
66
+ end
67
+
68
+ it 'no matching node, raises' do
69
+ expect { dummy_class.config_from_node(config_hash, 'not.here') }.to raise_error('No config was found for not.here')
70
+ end
71
+
72
+ it 'no config section, returns nil' do
73
+ expect(dummy_class.config_from_node(no_config_hash, 'test.delivery.puppetlabs.net')).to eq(nil)
74
+ end
75
+
76
+ it 'config exists, and returns' do
77
+ expect(dummy_class.config_from_node(config_hash, 'test.delivery.puppetlabs.net')).to eq('transport' => 'ssh', 'ssh' =>
78
+ { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false })
79
+ end
80
+
81
+ it 'no feature exists, and returns hash with feature added' do
82
+ expect(dummy_class.add_feature_to_group(no_feature_hash, 'puppet-agent', 'ssh_nodes')).to eq('groups' => [{ 'features' => ['puppet-agent'], 'name' => 'ssh_nodes', 'nodes' => [{ 'config' => { 'ssh' => { 'host-key-check' => false, 'password' => 'Qu@lity!', 'user' => 'root' }, 'transport' => 'ssh' }, 'facts' => { 'platform' => 'centos-5-x86_64', 'provisioner' => 'vmpooler' }, 'name' => 'test.delivery.puppetlabs.net' }] }, { 'name' => 'winrm_nodes', 'nodes' => [] }]) # rubocop:disable Metrics/LineLength: Line is too long
83
+ end
84
+
85
+ it 'feature exists, and returns hash with feature removed' do
86
+ expect(dummy_class.remove_feature_from_group(feature_hash, 'puppet-agent', 'ssh_nodes')).to eq('groups' => [{ 'features' => [], 'name' => 'ssh_nodes', 'nodes' => [{ 'config' => { 'ssh' => { 'host-key-check' => false, 'password' => 'Qu@lity!', 'user' => 'root' }, 'transport' => 'ssh' }, 'facts' => { 'platform' => 'centos-5-x86_64', 'provisioner' => 'vmpooler' }, 'name' => 'test.delivery.puppetlabs.net' }] }, { 'name' => 'winrm_nodes', 'nodes' => [] }]) # rubocop:disable Metrics/LineLength: Line is too long
87
+ end
88
+
89
+ it 'write from inventory_hash to inventory_yaml file feature_hash' do
90
+ expect { dummy_class.write_to_inventory_file(feature_hash, inventory_full_path) }.not_to raise_error
91
+ end
92
+
93
+ it 'empty feature exists, and returns hash with feature added' do
94
+ expect(dummy_class.add_feature_to_group(empty_feature_hash, 'puppet-agent', 'ssh_nodes')).to eq('groups' => [{ 'features' => ['puppet-agent'], 'name' => 'ssh_nodes', 'nodes' => [{ 'config' => { 'ssh' => { 'host-key-check' => false, 'password' => 'Qu@lity!', 'user' => 'root' }, 'transport' => 'ssh' }, 'facts' => { 'platform' => 'centos-5-x86_64', 'provisioner' => 'vmpooler' }, 'name' => 'test.delivery.puppetlabs.net' }] }, { 'name' => 'winrm_nodes', 'nodes' => [] }]) # rubocop:disable Metrics/LineLength: Line is too long
95
+ end
96
+
97
+ it 'write from inventory_hash to inventory_yaml file no feature_hash' do
98
+ expect(File).to exist(inventory_full_path)
99
+ expect { dummy_class.write_to_inventory_file(no_feature_hash, inventory_full_path) }.not_to raise_error
100
+ end
101
+ end
102
+ end
@@ -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,196 +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
- 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
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('{"key1"=>"foo", "key2"=>"bar"}')
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