Skip to content

Commit 88004c7

Browse files
committed
Add .tagged and .not_tagged methods for resource matcher
1 parent f2f597d commit 88004c7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ it { is_expected.to contain_service('keystone').without(
360360
)}
361361
```
362362

363+
You can test if the resource has the expected tags with the `tagged` and
364+
`not_tagged` methods.
365+
366+
```ruby
367+
it { is_expected.to contain_service('keystone').tagged('site:cph') }
368+
```
369+
363370
#### Checking the number of resources
364371

365372
You can test the number of resources in the catalogue with the

lib/rspec-puppet/matchers/create_generic.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def initialize(*args, &block)
2020
@subscribes = []
2121
@requires = []
2222
@befores = []
23+
@tagged = []
24+
@not_tagged = []
2325
end
2426

2527
def with(*args, &block)
@@ -60,6 +62,16 @@ def that_comes_before(resource)
6062
self
6163
end
6264

65+
def tagged(resource)
66+
@tagged.concat(Array(resource))
67+
self
68+
end
69+
70+
def not_tagged(resource)
71+
@not_tagged.concat(Array(resource))
72+
self
73+
end
74+
6375
def method_missing(method, *args, &block)
6476
if method.to_s =~ /^with_/
6577
param = method.to_s.gsub(/^with_/, '')
@@ -109,6 +121,8 @@ def matches?(catalogue)
109121

110122
check_params(rsrc_hsh, @expected_params, :should) if @expected_params.any?
111123
check_params(rsrc_hsh, @expected_undef_params, :not) if @expected_undef_params.any?
124+
check_tags(resource, @tagged, :should) if @tagged.any?
125+
check_tags(resource, @not_tagged, :not) if @not_tagged.any?
112126
check_befores(@catalogue, resource) if @befores.any?
113127
check_requires(@catalogue, resource) if @requires.any?
114128
check_notifies(@catalogue, resource) if @notifies.any?
@@ -162,6 +176,16 @@ def description
162176
values = @befores
163177
end
164178

179+
if @tagged.any?
180+
value_str_prefix = "that is tagged"
181+
values = @tagged
182+
end
183+
184+
if @not_tagged.any?
185+
value_str_prefix = "that is not tagged"
186+
values = @not_tagged
187+
end
188+
165189
unless values.empty?
166190
if values.length == 1
167191
value_str = " #{value_str_prefix} #{values.first}"
@@ -371,6 +395,25 @@ def check_params(resource, list, type)
371395
end
372396
end
373397
end
398+
# @param resource [Puppet::Resource] The resource in the catalog
399+
# @param list [Array<String>] The expected tags for the resource
400+
# @param type [:should, :not] Whether the given tags should/not be present
401+
def check_tags(resource, list, type)
402+
case type
403+
when :should
404+
list.each do |tag|
405+
unless resource.tags.include? tag
406+
@errors << "#{tag} is not set"
407+
end
408+
end
409+
when :not
410+
list.each do |tag|
411+
if resource.tags.include? tag
412+
@errors << "#{tag} is set"
413+
end
414+
end
415+
end
416+
end
374417
end
375418
end
376419
end

0 commit comments

Comments
 (0)