puppet_litmus 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,189 +1,203 @@
1
- # frozen_string_literal: true
2
-
3
- # helper functions for running puppet commands. They execute a target system specified by ENV['TARGET_HOST']
4
- # heavily uses functions from here https://github.com/puppetlabs/bolt/blob/master/developer-docs/bolt_spec-run.md
5
- module PuppetLitmus::Serverspec
6
- # Applies a manifest twice. First checking for errors. Secondly to make sure no changes occur.
7
- #
8
- # @param manifest [String] puppet manifest code to be applied.
9
- # @return [Boolean] The result of the 2 apply manifests.
10
- def idempotent_apply(manifest)
11
- manifest_file_location = create_manifest_file(manifest)
12
- apply_manifest(nil, catch_failures: true, manifest_file_location: manifest_file_location)
13
- apply_manifest(nil, catch_changes: true, manifest_file_location: manifest_file_location)
14
- end
15
-
16
- # rubocop:disable Layout/TrailingWhitespace
17
-
18
- # Applies a manifest. returning the result of that apply. Mimics the apply_manifest from beaker
19
- #
20
- # @param manifest [String] puppet manifest code to be applied.
21
- # @param opts [Hash] Alters the behaviour of the command. Valid options are:
22
- # :catch_changes [Boolean] exit status of 1 if there were changes.
23
- # :expect_failures [Boolean] doesnt return an exit code of non-zero if the apply failed.
24
- # :manifest_file_location [Path] The place on the target system.
25
- # :prefix_command [String] prefixes the puppet apply command; eg "export LANGUAGE='ja'".
26
- # :debug [Boolean] run puppet apply with the debug flag.
27
- # :noop [Boolean] run puppet apply with the noop flag.
28
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
29
- # @return [Object] A result object from the apply.
30
- def apply_manifest(manifest, opts = {})
31
- # rubocop:enable Layout/TrailingWhitespace
32
- target_node_name = ENV['TARGET_HOST']
33
- raise 'manifest and manifest_file_location in the opts hash are mutually exclusive arguments, pick one' if !manifest.nil? && !opts[:manifest_file_location].nil?
34
- raise 'please pass a manifest or the manifest_file_location in the opts hash' if (manifest.nil? || manifest == '') && opts[:manifest_file_location].nil?
35
-
36
- manifest_file_location = opts[:manifest_file_location] || create_manifest_file(manifest)
37
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
38
- nil
39
- else
40
- inventory_hash_from_inventory_file
41
- end
42
- command_to_run = "#{opts[:prefix_command]} puppet apply #{manifest_file_location}"
43
- command_to_run += " --modulepath #{Dir.pwd}/spec/fixtures/modules" if target_node_name.nil? || target_node_name == 'localhost'
44
- command_to_run += ' --detailed-exitcodes' if !opts[:catch_changes].nil? && (opts[:catch_changes] == true)
45
- command_to_run += ' --debug' if !opts[:debug].nil? && (opts[:debug] == true)
46
- command_to_run += ' --noop' if !opts[:noop].nil? && (opts[:noop] == true)
47
- result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
48
-
49
- raise "apply manifest failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
50
-
51
- result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
52
- stdout: result.first['result']['stdout'],
53
- stderr: result.first['result']['stderr'])
54
- yield result if block_given?
55
- result
56
- end
57
-
58
- # Creates a manifest file locally in a temp location, if its a remote target copy it to there.
59
- #
60
- # @param manifest [String] puppet manifest code.
61
- # @return [String] The path to the location of the manifest.
62
- def create_manifest_file(manifest)
63
- require 'tmpdir'
64
- target_node_name = ENV['TARGET_HOST']
65
- tmp_filename = File.join(Dir.tmpdir, "manifest_#{Time.now.strftime('%Y%m%d')}_#{Process.pid}_#{rand(0x100000000).to_s(36)}.pp")
66
- manifest_file = File.open(tmp_filename, 'w')
67
- manifest_file.write(manifest)
68
- manifest_file.close
69
- if target_node_name.nil? || target_node_name == 'localhost'
70
- # no need to transfer
71
- manifest_file_location = manifest_file.path
72
- else
73
- # transfer to TARGET_HOST
74
- inventory_hash = inventory_hash_from_inventory_file
75
- manifest_file_location = "/tmp/#{File.basename(manifest_file)}"
76
- result = upload_file(manifest_file.path, manifest_file_location, target_node_name, options: {}, config: nil, inventory: inventory_hash)
77
- raise result.first['result'].to_s unless result.first['status'] == 'success'
78
- end
79
- manifest_file_location
80
- end
81
-
82
- # Runs a command against the target system
83
- #
84
- # @param command_to_run [String] The command to execute.
85
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
86
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
87
- # @return [Object] A result object from the command.
88
- def run_shell(command_to_run, opts = {})
89
- target_node_name = ENV['TARGET_HOST']
90
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
91
- nil
92
- else
93
- inventory_hash_from_inventory_file
94
- end
95
- result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
96
- raise "shell failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
97
-
98
- result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
99
- stdout: result.first['result']['stdout'],
100
- stderr: result.first['result']['stderr'])
101
- yield result if block_given?
102
- result
103
- end
104
-
105
- # Copies file to the target, using its respective transport
106
- #
107
- # @param source [String] place locally, to copy from.
108
- # @param destination [String] place on the target, to copy to.
109
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
110
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
111
- # @return [Object] A result object from the command.
112
- def bolt_upload_file(source, destination, options = {})
113
- target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
114
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
115
- nil
116
- else
117
- inventory_hash_from_inventory_file
118
- end
119
-
120
- result = upload_file(source, destination, target_node_name, options: options, config: nil, inventory: inventory_hash)
121
-
122
- raise "upload file failed\n======\n#{result}" if result.first['status'] != 'success'
123
-
124
- exit_code = if result.first['status'] == 'success'
125
- 0
126
- else
127
- 255
128
- end
129
- result = OpenStruct.new(exit_code: exit_code,
130
- stdout: result.first['result']['status'],
131
- stderr: result.first['result']['status'])
132
- yield result if block_given?
133
- result
134
- end
135
-
136
- # Runs a task against the target system.
137
- #
138
- # @param task_name [String] The name of the task to run.
139
- # @param params [Hash] key : value pairs to be passed to the task.
140
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
141
- # @return [Object] A result object from the task.The values available are stdout, stderr and result.
142
- def run_bolt_task(task_name, params = {}, opts = {})
143
- config_data = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') }
144
- inventory_hash = inventory_hash_from_inventory_file
145
- target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
146
-
147
- result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
148
-
149
- raise "task failed\n`#{task_name}`\n======\n#{result}" if result.first['status'] != 'success' && opts[:expect_failures] != true
150
-
151
- exit_code = if result.first['status'] == 'success'
152
- 0
153
- else
154
- 255
155
- end
156
- result = OpenStruct.new(exit_code: exit_code,
157
- stdout: result.first['result']['status'],
158
- stderr: result.first['result']['stderr'],
159
- result: result.first['result'])
160
- yield result if block_given?
161
- result
162
- end
163
-
164
- # Runs a script against the target system.
165
- #
166
- # @param script [String] The path to the script on the source machine
167
- # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
168
- # @param arguments [Array] Array of arguments to pass to script on runtime
169
- # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
170
- # @return [Object] A result object from the script run.
171
- def bolt_run_script(script, opts = {}, arguments: [])
172
- target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
173
- inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
174
- nil
175
- else
176
- inventory_hash_from_inventory_file
177
- end
178
-
179
- result = run_script(script, target_node_name, arguments, options: opts, config: nil, inventory: inventory_hash)
180
-
181
- raise "script run failed\n`#{script}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
182
-
183
- result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
184
- stdout: result.first['result']['stdout'],
185
- stderr: result.first['result']['stderr'])
186
- yield result if block_given?
187
- result
188
- end
189
- end
1
+ # frozen_string_literal: true
2
+
3
+ # helper functions for running puppet commands. They execute a target system specified by ENV['TARGET_HOST']
4
+ # heavily uses functions from here https://github.com/puppetlabs/bolt/blob/master/developer-docs/bolt_spec-run.md
5
+ module PuppetLitmus::Serverspec
6
+ # Applies a manifest twice. First checking for errors. Secondly to make sure no changes occur.
7
+ #
8
+ # @param manifest [String] puppet manifest code to be applied.
9
+ # @return [Boolean] The result of the 2 apply manifests.
10
+ def idempotent_apply(manifest)
11
+ manifest_file_location = create_manifest_file(manifest)
12
+ apply_manifest(nil, catch_failures: true, manifest_file_location: manifest_file_location)
13
+ apply_manifest(nil, catch_changes: true, manifest_file_location: manifest_file_location)
14
+ end
15
+
16
+ # rubocop:disable Layout/TrailingWhitespace
17
+
18
+ # Applies a manifest. returning the result of that apply. Mimics the apply_manifest from beaker
19
+ #
20
+ # @param manifest [String] puppet manifest code to be applied.
21
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are:
22
+ # :catch_changes [Boolean] exit status of 1 if there were changes.
23
+ # :expect_failures [Boolean] doesnt return an exit code of non-zero if the apply failed.
24
+ # :manifest_file_location [Path] The place on the target system.
25
+ # :prefix_command [String] prefixes the puppet apply command; eg "export LANGUAGE='ja'".
26
+ # :debug [Boolean] run puppet apply with the debug flag.
27
+ # :noop [Boolean] run puppet apply with the noop flag.
28
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
29
+ # @return [Object] A result object from the apply.
30
+ def apply_manifest(manifest, opts = {})
31
+ # rubocop:enable Layout/TrailingWhitespace
32
+ target_node_name = ENV['TARGET_HOST']
33
+ raise 'manifest and manifest_file_location in the opts hash are mutually exclusive arguments, pick one' if !manifest.nil? && !opts[:manifest_file_location].nil?
34
+ raise 'please pass a manifest or the manifest_file_location in the opts hash' if (manifest.nil? || manifest == '') && opts[:manifest_file_location].nil?
35
+
36
+ manifest_file_location = opts[:manifest_file_location] || create_manifest_file(manifest)
37
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
38
+ nil
39
+ else
40
+ inventory_hash_from_inventory_file
41
+ end
42
+ command_to_run = "#{opts[:prefix_command]} puppet apply #{manifest_file_location}"
43
+ command_to_run += " --modulepath #{Dir.pwd}/spec/fixtures/modules" if target_node_name.nil? || target_node_name == 'localhost'
44
+ command_to_run += ' --detailed-exitcodes' if !opts[:catch_changes].nil? && (opts[:catch_changes] == true)
45
+ command_to_run += ' --debug' if !opts[:debug].nil? && (opts[:debug] == true)
46
+ command_to_run += ' --noop' if !opts[:noop].nil? && (opts[:noop] == true)
47
+ result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
48
+
49
+ raise "apply manifest failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
50
+
51
+ result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
52
+ stdout: result.first['result']['stdout'],
53
+ stderr: result.first['result']['stderr'])
54
+ yield result if block_given?
55
+ result
56
+ end
57
+
58
+ # Creates a manifest file locally in a temp location, if its a remote target copy it to there.
59
+ #
60
+ # @param manifest [String] puppet manifest code.
61
+ # @return [String] The path to the location of the manifest.
62
+ def create_manifest_file(manifest)
63
+ require 'tmpdir'
64
+ target_node_name = ENV['TARGET_HOST']
65
+ tmp_filename = File.join(Dir.tmpdir, "manifest_#{Time.now.strftime('%Y%m%d')}_#{Process.pid}_#{rand(0x100000000).to_s(36)}.pp")
66
+ manifest_file = File.open(tmp_filename, 'w')
67
+ manifest_file.write(manifest)
68
+ manifest_file.close
69
+ if target_node_name.nil? || target_node_name == 'localhost'
70
+ # no need to transfer
71
+ manifest_file_location = manifest_file.path
72
+ else
73
+ # transfer to TARGET_HOST
74
+ inventory_hash = inventory_hash_from_inventory_file
75
+ manifest_file_location = "/tmp/#{File.basename(manifest_file)}"
76
+ result = upload_file(manifest_file.path, manifest_file_location, target_node_name, options: {}, config: nil, inventory: inventory_hash)
77
+ raise result.first['result'].to_s unless result.first['status'] == 'success'
78
+ end
79
+ manifest_file_location
80
+ end
81
+
82
+ # Runs a command against the target system
83
+ #
84
+ # @param command_to_run [String] The command to execute.
85
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
86
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
87
+ # @return [Object] A result object from the command.
88
+ def run_shell(command_to_run, opts = {})
89
+ target_node_name = ENV['TARGET_HOST']
90
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
91
+ nil
92
+ else
93
+ inventory_hash_from_inventory_file
94
+ end
95
+ result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
96
+ raise "shell failed\n`#{command_to_run}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
97
+
98
+ result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
99
+ stdout: result.first['result']['stdout'],
100
+ stderr: result.first['result']['stderr'])
101
+ yield result if block_given?
102
+ result
103
+ end
104
+
105
+ # Copies file to the target, using its respective transport
106
+ #
107
+ # @param source [String] place locally, to copy from.
108
+ # @param destination [String] place on the target, to copy to.
109
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
110
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
111
+ # @return [Object] A result object from the command.
112
+ def bolt_upload_file(source, destination, opts = {}, options = {})
113
+ target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
114
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
115
+ nil
116
+ else
117
+ inventory_hash_from_inventory_file
118
+ end
119
+
120
+ result = upload_file(source, destination, target_node_name, options: options, config: nil, inventory: inventory_hash)
121
+
122
+ result_obj = {
123
+ exit_code: 0,
124
+ stdout: result.first['result']['_output'],
125
+ stderr: nil,
126
+ result: result.first['result'],
127
+ }
128
+
129
+ if result.first['status'] != 'success'
130
+ raise "upload file failed\n======\n#{result}" if opts[:expect_failures] != true
131
+
132
+ result_obj[:exit_code] = 255
133
+ result_obj[:stderr] = result.first['result']['_error']['msg']
134
+ end
135
+
136
+ result = OpenStruct.new(exit_code: result_obj[:exit_code],
137
+ stdout: result_obj[:stdout],
138
+ stderr: result_obj[:stderr])
139
+ yield result if block_given?
140
+ result
141
+ end
142
+
143
+ # Runs a task against the target system.
144
+ #
145
+ # @param task_name [String] The name of the task to run.
146
+ # @param params [Hash] key : value pairs to be passed to the task.
147
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
148
+ # @return [Object] A result object from the task.The values available are stdout, stderr and result.
149
+ def run_bolt_task(task_name, params = {}, opts = {})
150
+ config_data = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') }
151
+ inventory_hash = inventory_hash_from_inventory_file
152
+ target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
153
+
154
+ result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
155
+
156
+ result_obj = {
157
+ exit_code: 0,
158
+ stdout: result.first['result']['_output'],
159
+ stderr: nil,
160
+ result: result.first['result'],
161
+ }
162
+
163
+ if result.first['status'] != 'success'
164
+ raise "task failed\n`#{task_name}`\n======\n#{result}" if opts[:expect_failures] != true
165
+
166
+ result_obj[:exit_code] = result.first['result']['_error']['details'].fetch('exitcode', 255)
167
+ result_obj[:stderr] = result.first['result']['_error']['msg']
168
+ end
169
+
170
+ result = OpenStruct.new(exit_code: result_obj[:exit_code],
171
+ stdout: result_obj[:stdout],
172
+ stderr: result_obj[:stderr],
173
+ result: result_obj[:result])
174
+ yield result if block_given?
175
+ result
176
+ end
177
+
178
+ # Runs a script against the target system.
179
+ #
180
+ # @param script [String] The path to the script on the source machine
181
+ # @param opts [Hash] Alters the behaviour of the command. Valid options are :expect_failures [Boolean] doesnt return an exit code of non-zero if the command failed.
182
+ # @param arguments [Array] Array of arguments to pass to script on runtime
183
+ # @yieldreturn [Block] this method will yield to a block of code passed by the caller; this can be used for additional validation, etc.
184
+ # @return [Object] A result object from the script run.
185
+ def bolt_run_script(script, opts = {}, arguments: [])
186
+ target_node_name = ENV['TARGET_HOST'] if target_node_name.nil?
187
+ inventory_hash = if target_node_name.nil? || target_node_name == 'localhost'
188
+ nil
189
+ else
190
+ inventory_hash_from_inventory_file
191
+ end
192
+
193
+ result = run_script(script, target_node_name, arguments, options: opts, config: nil, inventory: inventory_hash)
194
+
195
+ raise "script run failed\n`#{script}`\n======\n#{result}" if result.first['result']['exit_code'] != 0 && opts[:expect_failures] != true
196
+
197
+ result = OpenStruct.new(exit_code: result.first['result']['exit_code'],
198
+ stdout: result.first['result']['stdout'],
199
+ stderr: result.first['result']['stderr'])
200
+ yield result if block_given?
201
+ result
202
+ end
203
+ end
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
- # version of this gem
4
- module PuppetLitmus
5
- VERSION ||= '0.7.0'
6
- end
1
+ # frozen_string_literal: true
2
+
3
+ # version of this gem
4
+ module PuppetLitmus
5
+ VERSION ||= '0.7.1'
6
+ end
@@ -1,16 +1,16 @@
1
- ---
2
- groups:
3
- - name: ssh_nodes
4
- nodes:
5
- - name: test.delivery.puppetlabs.net
6
- config:
7
- transport: ssh
8
- ssh:
9
- user: root
10
- password: Qu@lity!
11
- host-key-check: false
12
- facts:
13
- provisioner: vmpooler
14
- platform: centos-5-x86_64
15
- - name: winrm_nodes
16
- nodes: []
1
+ ---
2
+ groups:
3
+ - name: ssh_nodes
4
+ nodes:
5
+ - name: test.delivery.puppetlabs.net
6
+ config:
7
+ transport: ssh
8
+ ssh:
9
+ user: root
10
+ password: Qu@lity!
11
+ host-key-check: false
12
+ facts:
13
+ provisioner: vmpooler
14
+ platform: centos-5-x86_64
15
+ - name: winrm_nodes
16
+ nodes: []
@@ -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