puppet_litmus 0.30.0 → 0.31.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 325cdb47d0f7db0541938b78bf14879da2db05ba40d9a946ce99fd506d0f9c1d
4
- data.tar.gz: d157bb8ff3192444f9eb974a12428e2b24dc4a3340a40a0bfaed2e5a33c3dd99
3
+ metadata.gz: 80914970290db5501971ee324794c82101f204d1c78a74cbd618547f8fb345c0
4
+ data.tar.gz: 596c114a8c75fb36f6ab8350850eca9005036fbdf12ed7d3465e2375c937579e
5
5
  SHA512:
6
- metadata.gz: e8384b785f1b183bf5554f51113171af51d3c2ebe5ec5c5695222be7cac2cfd30716c0b28d47aa277c2b7bacbd9a2715af780d93479817cfb2ac0f2d1b964c1a
7
- data.tar.gz: d387235b43cae7895acc007cd9bb314136488d29b9196fc5b51a2b7ab45357befb0c2e4a1c63f11a6b3cb24232ce6e5230b300fb7c5658bca1bdb5a6abe5ac64
6
+ metadata.gz: eef1f207558172836438bc1bb5686610af52560bd7eb99ae2ff9ae332fa3705f249587dd4eb4f12c4ffd83c0011d03764dd6fa974b21be4012144829d984a429
7
+ data.tar.gz: 57d962b1ea4e49fd483319514197f5b1d119ab9b69e981c2e3a17f151b71358477b6a7276bbee3101f3d0a5f1d00a0cb081de953dfbf750e1ba6d53016836f58
@@ -13,6 +13,7 @@ IMAGE_TABLE = {
13
13
  'Windows-2012 R2' => 'windows-2012-r2-core',
14
14
  'Windows-2016' => 'windows-2016',
15
15
  'Windows-2019' => 'windows-2019-core',
16
+ 'Windows-2022' => 'windows-2022',
16
17
  }.freeze
17
18
 
18
19
  DOCKER_PLATFORMS = {
@@ -53,6 +53,85 @@ module PuppetLitmus::InventoryManipulation
53
53
  end
54
54
  end
55
55
 
56
+ # Recursively find and iterate over the groups in an inventory. If no block is passed
57
+ # to the function then only the name of the group is returned. If a block is passed
58
+ # then the block is executed against each group and the value of the block is returned.
59
+ #
60
+ # @param inventory_hash [Hash] Inventory hash from inventory.yaml
61
+ # @param block [Block] Block to execute against each node
62
+ def groups_in_inventory(inventory_hash, &block)
63
+ inventory_hash['groups'].flat_map do |group|
64
+ output_collector = []
65
+ output_collector << if block_given?
66
+ yield group
67
+ else
68
+ group['name'].downcase
69
+ end
70
+ output_collector << groups_in_inventory({ 'groups' => group['groups'] }, &block) if group.key? 'groups'
71
+ output_collector.flatten.compact
72
+ end
73
+ end
74
+
75
+ # Iterate over all targets in an inventory. If no block is given to the function
76
+ # it will return the name of every target in the inventory. If a block is passed
77
+ # it will execute the block on each target and return the value of the block.
78
+ #
79
+ # @param inventory_hash [Hash] Inventory hash from inventory.yaml
80
+ # @param block [Block] Block to execute against each node
81
+ def targets_in_inventory(inventory_hash)
82
+ groups_in_inventory(inventory_hash) do |group|
83
+ if group.key? 'targets'
84
+ group['targets'].map do |target|
85
+ if block_given?
86
+ (yield target)
87
+ else
88
+ target['uri'].downcase
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ # Find all targets in an inventory that have a role. The roles for a target are
96
+ # specified in the vars hash for a target. This function is tolerant to the roles
97
+ # hash being called either 'role' or 'roles' and it is tolerant to the roles being
98
+ # either a single key value or an array of roles.
99
+ #
100
+ # @param role [String] The name of a role to search for
101
+ # @param inventory [Hash] Inventory hash from inventory.yaml
102
+ def nodes_with_role(role, inventory)
103
+ output_collector = []
104
+ targets_in_inventory(inventory) do |target|
105
+ vars = target['vars']
106
+ roles = [(vars['role'] || vars['roles'])].flatten
107
+ roles = roles.map { |r| r.downcase }
108
+ output_collector << target['uri'] if roles.include? role.downcase
109
+ end
110
+ output_collector unless output_collector.empty?
111
+ end
112
+
113
+ # Searches through the inventory hash to either validate that a group being targeted exists,
114
+ # validate that a specific target being targeted exists, or resolves role names to a
115
+ # list of nodes to target. Targets and roles can be specified as strings or as symbols, and
116
+ # the functions are tolerant to incorrect capitalization.
117
+ #
118
+ # @param target [String] || [Array[String]] A list of targets
119
+ # @param inventory [Hash] inventory hash from inventory.yaml
120
+ def search_for_target(target, inventory)
121
+ result_collector = []
122
+ groups = groups_in_inventory(inventory)
123
+ Array(target).map do |name|
124
+ result_collector << name if groups.include? name.to_s.downcase
125
+ result_collector << name if targets_in_inventory(inventory).include? name.to_s.downcase
126
+ result_collector << nodes_with_role(name.to_s, inventory)
127
+ end
128
+
129
+ result_collector = result_collector.flatten.compact
130
+ raise 'targets not found in inventory' if result_collector.empty?
131
+
132
+ result_collector
133
+ end
134
+
56
135
  # Determines if a node_name exists in a group in the inventory_hash.
57
136
  #
58
137
  # @param inventory_hash [Hash] hash of the inventory.yaml file
@@ -86,14 +165,13 @@ module PuppetLitmus::InventoryManipulation
86
165
  # @param node_name [String] node to locate in the group
87
166
  # @return [Hash] config for node of name node_name
88
167
  def config_from_node(inventory_hash, node_name)
89
- inventory_hash['groups'].each do |group|
90
- group['targets'].each do |node|
91
- if node['uri'] == node_name
92
- return node['config']
93
- end
94
- end
168
+ config = targets_in_inventory(inventory_hash) do |target|
169
+ next unless target['uri'].downcase == node_name.downcase
170
+
171
+ return target['config'] unless target['config'].nil?
95
172
  end
96
- raise "No config was found for #{node_name}"
173
+
174
+ config.empty? ? nil : config[0]
97
175
  end
98
176
 
99
177
  # Finds a facts hash in the inventory hash by searching for a node name.
@@ -102,14 +180,13 @@ module PuppetLitmus::InventoryManipulation
102
180
  # @param node_name [String] node to locate in the group
103
181
  # @return [Hash] facts for node of name node_name
104
182
  def facts_from_node(inventory_hash, node_name)
105
- inventory_hash['groups'].each do |group|
106
- group['targets'].each do |node|
107
- if node['uri'] == node_name
108
- return node['facts']
109
- end
110
- end
183
+ facts = targets_in_inventory(inventory_hash) do |target|
184
+ next unless target['uri'].downcase == node_name.downcase
185
+
186
+ target['facts'] unless target['facts'].nil?
111
187
  end
112
- raise "No facts were found for #{node_name}"
188
+
189
+ facts.empty? ? nil : facts[0]
113
190
  end
114
191
 
115
192
  # Finds a var hash in the inventory hash by searching for a node name.
@@ -118,14 +195,12 @@ module PuppetLitmus::InventoryManipulation
118
195
  # @param node_name [String] node to locate in the group
119
196
  # @return [Hash] vars for node of name node_name
120
197
  def vars_from_node(inventory_hash, node_name)
121
- inventory_hash['groups'].each do |group|
122
- group['targets'].each do |node|
123
- if node['uri'] == node_name
124
- return node['vars']
125
- end
126
- end
198
+ vars = targets_in_inventory(inventory_hash) do |target|
199
+ next unless target['uri'].downcase == node_name.downcase
200
+
201
+ target['vars'] unless target['vars'].nil?
127
202
  end
128
- {}
203
+ vars.empty? ? {} : vars[0]
129
204
  end
130
205
 
131
206
  # Adds a node to a group specified, if group_name exists in inventory hash.
@@ -266,4 +341,27 @@ module PuppetLitmus::InventoryManipulation
266
341
  end
267
342
  Honeycomb.current_span.add_field('litmus.platform', facts&.dig('platform'))
268
343
  end
344
+
345
+ # Add platform custom information field to the current span for each node being targeted.
346
+ # If more than one node is being targeted, each node will be given a separate custom field.
347
+ #
348
+ # @param span [Honeycomb::Span] The current span
349
+ # @param target_node_names [Array[String]] Nodes being targeted
350
+ # @param inventory_hash [Hash] Hash of the inventory.yaml file
351
+ def add_node_fields_to_span(span, target_node_names, inventory_hash)
352
+ node_counter = 1
353
+ Array(target_node_names).each do |target_name|
354
+ name_field = 'litmus.node_name'
355
+ platform_field = 'litmus.platform'
356
+
357
+ name_field = "#{name_field}_#{node_counter}"
358
+ platform_field = "#{platform_field}_#{node_counter}"
359
+ span.add_field(name_field, target_name)
360
+ if target_in_inventory?(inventory_hash, target_name)
361
+ facts = facts_from_node(inventory_hash, target_name)
362
+ span.add_field(platform_field, facts&.dig('platform')) unless facts.nil?
363
+ end
364
+ node_counter += 1
365
+ end
366
+ end
269
367
  end
@@ -81,10 +81,15 @@ module PuppetLitmus::PuppetHelpers
81
81
 
82
82
  manifest_file_location = opts[:manifest_file_location] || create_manifest_file(manifest)
83
83
  inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
84
- raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
85
84
 
86
- span.add_field('litmus.node_name', target_node_name)
87
- add_platform_field(inventory_hash, target_node_name)
85
+ target_option = opts['targets'] || opts[:targets]
86
+ if target_option.nil?
87
+ raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
88
+ else
89
+ target_node_name = search_for_target(target_option, inventory_hash)
90
+ end
91
+
92
+ add_node_fields_to_span(span, target_node_name, inventory_hash)
88
93
 
89
94
  # Forcibly set the locale of the command
90
95
  locale = if os[:family] != 'windows'
@@ -143,7 +148,7 @@ module PuppetLitmus::PuppetHelpers
143
148
  #
144
149
  # @param manifest [String] puppet manifest code.
145
150
  # @return [String] The path to the location of the manifest.
146
- def create_manifest_file(manifest)
151
+ def create_manifest_file(manifest, opts = {})
147
152
  Honeycomb.start_span(name: 'litmus.create_manifest_file') do |span|
148
153
  ENV['HONEYCOMB_TRACE'] = span.to_trace_header
149
154
  span.add_field('litmus.manifest', manifest)
@@ -160,8 +165,9 @@ module PuppetLitmus::PuppetHelpers
160
165
  else
161
166
  # transfer to TARGET_HOST
162
167
  inventory_hash = inventory_hash_from_inventory_file
163
- span.add_field('litmus.node_name', target_node_name)
164
- add_platform_field(inventory_hash, target_node_name)
168
+ target_option = opts['targets'] || opts[:targets]
169
+ target_node_name = search_for_target(target_option, inventory_hash) unless target_option.nil?
170
+ add_node_fields_to_span(span, target_node_name, inventory_hash)
165
171
 
166
172
  manifest_file_location = File.basename(manifest_file)
167
173
  bolt_result = upload_file(manifest_file.path, manifest_file_location, target_node_name, options: {}, config: nil, inventory: inventory_hash)
@@ -180,13 +186,16 @@ module PuppetLitmus::PuppetHelpers
180
186
  # @param content [String] String data to write to the file.
181
187
  # @param destination [String] The path on the target node to write the file.
182
188
  # @return [Bool] Success. The file was succesfully writtne on the target.
183
- def write_file(content, destination)
189
+ def write_file(content, destination, opts = {})
184
190
  Honeycomb.start_span(name: 'litmus.write_file') do |span|
185
191
  ENV['HONEYCOMB_TRACE'] = span.to_trace_header
186
192
  span.add_field('litmus.destination', destination)
187
193
 
188
194
  require 'tmpdir'
195
+ inventory_hash = inventory_hash_from_inventory_file
189
196
  target_node_name = ENV['TARGET_HOST']
197
+ target_option = opts['targets'] || opts[:targets]
198
+ target_node_name = search_for_target(target_option, inventory_hash) unless target_option.nil?
190
199
 
191
200
  Tempfile.create('litmus') do |tmp_file|
192
201
  tmp_file.write(content)
@@ -197,9 +206,7 @@ module PuppetLitmus::PuppetHelpers
197
206
  FileUtils.cp(tmp_file.path, destination)
198
207
  else
199
208
  # transfer to TARGET_HOST
200
- inventory_hash = inventory_hash_from_inventory_file
201
- span.add_field('litmus.node_name', target_node_name)
202
- add_platform_field(inventory_hash, target_node_name)
209
+ add_node_fields_to_span(span, target_node_name, inventory_hash)
203
210
 
204
211
  bolt_result = upload_file(tmp_file.path, destination, target_node_name, options: {}, config: nil, inventory: inventory_hash)
205
212
  span.add_field('litmus.bolt_result.file_upload', bolt_result)
@@ -223,12 +230,17 @@ module PuppetLitmus::PuppetHelpers
223
230
  span.add_field('litmus.command_to_run', command_to_run)
224
231
  span.add_field('litmus.opts', opts)
225
232
 
226
- target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV['TARGET_HOST']
227
233
  inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
228
- raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
229
234
 
230
- span.add_field('litmus.node_name', target_node_name)
231
- add_platform_field(inventory_hash, target_node_name)
235
+ target_option = opts['targets'] || opts[:targets]
236
+ if target_option.nil?
237
+ target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV['TARGET_HOST']
238
+ raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
239
+ else
240
+ target_node_name = search_for_target(target_option, inventory_hash)
241
+ end
242
+
243
+ add_node_fields_to_span(span, target_node_name, inventory_hash)
232
244
 
233
245
  bolt_result = run_command(command_to_run, target_node_name, config: nil, inventory: inventory_hash)
234
246
  span.add_field('litmus.bolt_result', bolt_result)
@@ -262,12 +274,16 @@ module PuppetLitmus::PuppetHelpers
262
274
  span.add_field('litmus.opts', opts)
263
275
  span.add_field('litmus.options', options)
264
276
 
265
- target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV['TARGET_HOST']
266
277
  inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
267
- raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
278
+ target_option = opts['targets'] || opts[:targets]
279
+ if target_option.nil?
280
+ target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV['TARGET_HOST']
281
+ raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
282
+ else
283
+ target_node_name = search_for_target(target_option, inventory_hash)
284
+ end
268
285
 
269
- span.add_field('litmus.node_name', target_node_name)
270
- add_platform_field(inventory_hash, target_node_name)
286
+ add_node_fields_to_span(span, target_node_name, inventory_hash)
271
287
 
272
288
  bolt_result = upload_file(source, destination, target_node_name, options: options, config: nil, inventory: inventory_hash)
273
289
  span.add_field('litmus.bolt_result', bolt_result)
@@ -322,10 +338,15 @@ module PuppetLitmus::PuppetHelpers
322
338
  else
323
339
  localhost_inventory_hash
324
340
  end
325
- raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
326
341
 
327
- span.add_field('litmus.node_name', target_node_name)
328
- add_platform_field(inventory_hash, target_node_name)
342
+ target_option = opts['targets'] || opts[:targets]
343
+ if target_option.nil?
344
+ raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
345
+ else
346
+ target_node_name = search_for_target(target_option, inventory_hash)
347
+ end
348
+
349
+ add_node_fields_to_span(span, target_node_name, inventory_hash)
329
350
 
330
351
  bolt_result = run_task(task_name, target_node_name, params, config: config_data, inventory: inventory_hash)
331
352
  result_obj = {
@@ -383,10 +404,14 @@ module PuppetLitmus::PuppetHelpers
383
404
 
384
405
  target_node_name = targeting_localhost? ? 'litmus_localhost' : ENV['TARGET_HOST']
385
406
  inventory_hash = File.exist?('spec/fixtures/litmus_inventory.yaml') ? inventory_hash_from_inventory_file : localhost_inventory_hash
386
- raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
407
+ target_option = opts['targets'] || opts[:targets]
408
+ if target_option.nil?
409
+ raise "Target '#{target_node_name}' not found in spec/fixtures/litmus_inventory.yaml" unless target_in_inventory?(inventory_hash, target_node_name)
410
+ else
411
+ target_node_name = search_for_target(target_option, inventory_hash)
412
+ end
387
413
 
388
- span.add_field('litmus.node_name', target_node_name)
389
- add_platform_field(inventory_hash, target_node_name)
414
+ add_node_fields_to_span(span, target_node_name, inventory_hash)
390
415
 
391
416
  bolt_result = run_script(script, target_node_name, arguments, options: opts, config: nil, inventory: inventory_hash)
392
417
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  # version of this gem
4
4
  module PuppetLitmus
5
- VERSION ||= '0.30.0'
5
+ VERSION ||= '0.31.0'
6
6
  end
@@ -1,96 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
+ require 'support/inventory'
4
5
 
5
6
  RSpec.describe PuppetLitmus::InventoryManipulation do
6
- context 'with config_from_node' do
7
- let(:no_config_hash) do
8
- { 'groups' =>
9
- [{ 'name' => 'ssh_nodes',
10
- 'targets' =>
11
- [{ 'uri' => 'test.delivery.puppetlabs.net',
12
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
13
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
14
- end
15
-
16
- let(:no_docker_hash) do
17
- { 'groups' =>
18
- [{ 'name' => 'ssh_nodes', 'targets' => [] },
19
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
20
- end
21
-
22
- let(:config_hash) do
23
- { 'groups' =>
24
- [{ 'name' => 'ssh_nodes',
25
- 'targets' =>
26
- [{ 'uri' => 'test.delivery.puppetlabs.net',
27
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
28
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
29
- 'vars' => { 'role' => 'agent' } }] },
30
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
31
- end
32
-
33
- let(:inventory_full_path) { 'spec/data/inventory.yaml' }
34
-
35
- let(:no_feature_hash) do
36
- { 'groups' =>
37
- [{ 'name' => 'ssh_nodes',
38
- 'targets' =>
39
- [{ 'uri' => 'test.delivery.puppetlabs.net',
40
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
41
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
42
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
43
- end
44
-
45
- let(:feature_hash_group) do
46
- { 'groups' =>
47
- [{ 'name' => 'ssh_nodes',
48
- 'targets' =>
49
- [{ 'uri' => 'test.delivery.puppetlabs.net',
50
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
51
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
52
- 'features' => ['puppet-agent'] },
53
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
54
- end
55
-
56
- let(:empty_feature_hash_group) do
57
- { 'groups' =>
58
- [{ 'name' => 'ssh_nodes',
59
- 'targets' =>
60
- [{ 'uri' => 'test.delivery.puppetlabs.net',
61
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
62
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
63
- 'features' => [] },
64
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
65
- end
66
-
67
- let(:feature_hash_node) do
68
- { 'groups' =>
69
- [{ 'name' => 'ssh_nodes',
70
- 'targets' =>
71
- [{ 'uri' => 'test.delivery.puppetlabs.net',
72
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
73
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
74
- 'features' => ['puppet-agent'] }] },
75
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
76
- end
77
-
78
- let(:empty_feature_hash_node) do
79
- { 'groups' =>
80
- [{ 'name' => 'ssh_nodes',
81
- 'targets' =>
82
- [{ 'uri' => 'test.delivery.puppetlabs.net',
83
- 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
84
- 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
85
- 'features' => [] }] },
86
- { 'name' => 'winrm_nodes', 'targets' => [] }] }
87
- end
88
-
89
- let(:foo_node) do
90
- { 'uri' => 'foo',
91
- 'facts' => { 'provisioner' => 'bar', 'platform' => 'ubuntu' } }
92
- end
7
+ let(:inventory_full_path) { 'spec/data/inventory.yaml' }
93
8
 
9
+ context 'with config_from_node' do
94
10
  it 'no matching node, raises' do
95
11
  expect { config_from_node(config_hash, 'not.here') }.to raise_error('No config was found for not.here')
96
12
  end
@@ -159,4 +75,70 @@ RSpec.describe PuppetLitmus::InventoryManipulation do
159
75
  [{ 'name' => 'ssh_nodes', 'targets' => [foo_node] }, { 'name' => 'winrm_nodes', 'targets' => [] }])
160
76
  end
161
77
  end
78
+
79
+ context 'with target searching' do
80
+ it 'gets correct groups names from an inventory' do
81
+ expect(groups_in_inventory(complex_inventory)).to eql(%w[ssh_nodes frontend winrm_nodes])
82
+ end
83
+
84
+ it 'applies a code block to groups' do
85
+ counts = groups_in_inventory(complex_inventory) do |group|
86
+ if group.key? 'targets'
87
+ group['targets'].count
88
+ end
89
+ end
90
+ expect(counts.sum).to be 4
91
+ end
92
+
93
+ it 'gets names of targets' do
94
+ target_list = ['test.delivery.puppetlabs.net', 'test2.delivery.puppetlabs.net', 'test3.delivery.puppetlabs.net', 'test4.delivery.puppetlabs.net']
95
+ expect(targets_in_inventory(complex_inventory)).to eql target_list
96
+ end
97
+
98
+ it 'applies a code block to targets' do
99
+ target_list = targets_in_inventory(complex_inventory) do |target|
100
+ next unless target['config']['transport'] == 'winrm'
101
+
102
+ target['uri']
103
+ end
104
+
105
+ expect(target_list).to eql ['test4.delivery.puppetlabs.net']
106
+ end
107
+
108
+ it 'returns agent nodes' do
109
+ node_list = nodes_with_role('agent', complex_inventory)
110
+ expected_node_list = ['test.delivery.puppetlabs.net', 'test3.delivery.puppetlabs.net', 'test4.delivery.puppetlabs.net']
111
+ expect(node_list).to eql expected_node_list
112
+ end
113
+
114
+ it 'returns agent nodes with different capitolization' do
115
+ node_list = nodes_with_role('Agent', complex_inventory)
116
+ expected_node_list = ['test.delivery.puppetlabs.net', 'test3.delivery.puppetlabs.net', 'test4.delivery.puppetlabs.net']
117
+ expect(node_list).to eql expected_node_list
118
+ end
119
+
120
+ it 'searches for a group' do
121
+ expect(search_for_target('winrm_nodes', complex_inventory)).to eql ['winrm_nodes']
122
+ end
123
+
124
+ it 'seaches for an array of groups' do
125
+ expect(search_for_target(%w[winrm_nodes ssh_nodes], complex_inventory)).to eql %w[winrm_nodes ssh_nodes]
126
+ end
127
+
128
+ it 'searches for a specific target' do
129
+ expect(search_for_target('test.delivery.puppetlabs.net', complex_inventory)).to eql ['test.delivery.puppetlabs.net']
130
+ end
131
+
132
+ it 'searches for an array of roles' do
133
+ expect(search_for_target(%w[iis nginx], complex_inventory)).to eql ['test4.delivery.puppetlabs.net', 'test3.delivery.puppetlabs.net']
134
+ end
135
+
136
+ it 'searches for roles as symbols' do
137
+ expect(search_for_target([:iis, :nginx], complex_inventory)).to eql ['test4.delivery.puppetlabs.net', 'test3.delivery.puppetlabs.net']
138
+ end
139
+
140
+ it 'raises an error if target not found' do
141
+ expect { search_for_target(:blah, complex_inventory) }.to raise_error 'targets not found in inventory'
142
+ end
143
+ end
162
144
  end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ def no_config_hash
4
+ { 'groups' =>
5
+ [
6
+ { 'name' => 'ssh_nodes',
7
+ 'targets' =>
8
+ [{ 'uri' => 'test.delivery.puppetlabs.net',
9
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
10
+ { 'name' => 'winrm_nodes', 'targets' => [] },
11
+ ] }
12
+ end
13
+
14
+ def no_docker_hash
15
+ { 'groups' =>
16
+ [{ 'name' => 'ssh_nodes', 'targets' => [] },
17
+ { 'name' => 'winrm_nodes', 'targets' => [] }] }
18
+ end
19
+
20
+ def config_hash
21
+ { 'groups' =>
22
+ [{ 'name' => 'ssh_nodes',
23
+ 'targets' =>
24
+ [{ 'uri' => 'test.delivery.puppetlabs.net',
25
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
26
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
27
+ 'vars' => { 'role' => 'agent' } }] },
28
+ { 'name' => 'winrm_nodes', 'targets' => [] }] }
29
+ end
30
+
31
+ def no_feature_hash
32
+ { 'groups' =>
33
+ [{ 'name' => 'ssh_nodes',
34
+ 'targets' =>
35
+ [{ 'uri' => 'test.delivery.puppetlabs.net',
36
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
37
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }] },
38
+ { 'name' => 'winrm_nodes', 'targets' => [] }] }
39
+ end
40
+
41
+ def feature_hash_group
42
+ { 'groups' =>
43
+ [{ 'name' => 'ssh_nodes',
44
+ 'targets' =>
45
+ [{ 'uri' => 'test.delivery.puppetlabs.net',
46
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
47
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
48
+ 'features' => ['puppet-agent'] },
49
+ { 'name' => 'winrm_nodes', 'targets' => [] }] }
50
+ end
51
+
52
+ def empty_feature_hash_group
53
+ { 'groups' =>
54
+ [{ 'name' => 'ssh_nodes',
55
+ 'targets' =>
56
+ [{ 'uri' => 'test.delivery.puppetlabs.net',
57
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
58
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' } }],
59
+ 'features' => [] },
60
+ { 'name' => 'winrm_nodes', 'targets' => [] }] }
61
+ end
62
+
63
+ def feature_hash_node
64
+ { 'groups' =>
65
+ [{ 'name' => 'ssh_nodes',
66
+ 'targets' =>
67
+ [{ 'uri' => 'test.delivery.puppetlabs.net',
68
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
69
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
70
+ 'features' => ['puppet-agent'] }] },
71
+ { 'name' => 'winrm_nodes', 'targets' => [] }] }
72
+ end
73
+
74
+ def empty_feature_hash_node
75
+ { 'groups' =>
76
+ [{ 'name' => 'ssh_nodes',
77
+ 'targets' =>
78
+ [{ 'uri' => 'test.delivery.puppetlabs.net',
79
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
80
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
81
+ 'features' => [] }] },
82
+ { 'name' => 'winrm_nodes', 'targets' => [] }] }
83
+ end
84
+
85
+ def foo_node
86
+ { 'uri' => 'foo',
87
+ 'facts' => { 'provisioner' => 'bar', 'platform' => 'ubuntu' } }
88
+ end
89
+
90
+ def complex_inventory
91
+ { 'groups' =>
92
+ [
93
+ {
94
+ 'name' => 'ssh_nodes',
95
+ 'groups' => [
96
+ { 'name' => 'frontend',
97
+ 'targets' => [
98
+ {
99
+ 'uri' => 'test.delivery.puppetlabs.net',
100
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
101
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
102
+ 'vars' => { 'role' => 'agent' },
103
+ },
104
+ {
105
+ 'uri' => 'test2.delivery.puppetlabs.net',
106
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
107
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
108
+ 'vars' => { 'role' => 'server' },
109
+ },
110
+ {
111
+ 'uri' => 'test3.delivery.puppetlabs.net',
112
+ 'config' => { 'transport' => 'ssh', 'ssh' => { 'user' => 'root', 'password' => 'Qu@lity!', 'host-key-check' => false } },
113
+ 'vars' => { 'roles' => %w[agent nginx webserver] },
114
+ },
115
+ ] },
116
+ ],
117
+ },
118
+ {
119
+ 'name' => 'winrm_nodes',
120
+ 'targets' => [
121
+ {
122
+ 'uri' => 'test4.delivery.puppetlabs.net',
123
+ 'config' => { 'transport' => 'winrm', 'winrm' => { 'user' => 'admin', 'password' => 'Qu@lity!' } },
124
+ 'facts' => { 'provisioner' => 'vmpooler', 'platform' => 'centos-5-x86_64' },
125
+ 'vars' => { 'roles' => %w[agent iis webserver] },
126
+ },
127
+ ],
128
+ },
129
+ ] }
130
+ end
@@ -0,0 +1,41 @@
1
+ groups:
2
+ - name: ssh_nodes
3
+ groups:
4
+ - name: webservers
5
+ targets:
6
+ - 192.168.100.179
7
+ - 192.168.100.180
8
+ - 192.168.100.181
9
+ - name: memcached
10
+ targets:
11
+ - 192.168.101.50
12
+ - 192.168.101.60
13
+ config:
14
+ ssh:
15
+ user: root
16
+ config:
17
+ transport: ssh
18
+ ssh:
19
+ user: centos
20
+ private-key: ~/.ssh/id_rsa
21
+ host-key-check: false
22
+ - name: win_nodes
23
+ groups:
24
+ - name: domaincontrollers
25
+ targets:
26
+ - 192.168.110.10
27
+ - 192.168.110.20
28
+ - name: testservers
29
+ targets:
30
+ - 172.16.219.20
31
+ - 172.16.219.30
32
+ config:
33
+ winrm:
34
+ realm: MYDOMAIN
35
+ ssl: false
36
+ config:
37
+ transport: winrm
38
+ winrm:
39
+ user: DOMAIN\opsaccount
40
+ password: S3cretP@ssword
41
+ ssl: true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppet_litmus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.0
4
+ version: 0.31.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet, Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-28 00:00:00.000000000 Z
11
+ date: 2022-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bolt
@@ -194,6 +194,8 @@ files:
194
194
  - spec/lib/puppet_litmus/rake_tasks_spec.rb
195
195
  - spec/lib/puppet_litmus/util_spec.rb
196
196
  - spec/spec_helper.rb
197
+ - spec/support/inventory.rb
198
+ - spec/support/inventorytesting.yaml
197
199
  homepage: https://github.com/puppetlabs/puppet_litmus
198
200
  licenses:
199
201
  - Apache-2.0
@@ -220,6 +222,8 @@ summary: Providing a simple command line tool for puppet content creators, to en
220
222
  simple and complex test deployments.
221
223
  test_files:
222
224
  - spec/spec_helper.rb
225
+ - spec/support/inventory.rb
226
+ - spec/support/inventorytesting.yaml
223
227
  - spec/lib/puppet_litmus/rake_tasks_spec.rb
224
228
  - spec/lib/puppet_litmus/puppet_litmus_version_spec.rb
225
229
  - spec/lib/puppet_litmus/util_spec.rb