Skip to content

Commit 79d04d9

Browse files
kjetilhoDavidS
authored andcommitted
Add .tagged and .not_tagged methods for resource matcher
1 parent b8b1412 commit 79d04d9

File tree

5 files changed

+103
-17
lines changed

5 files changed

+103
-17
lines changed

README.md

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

399+
You can test if the resource has the expected tags with the `tagged` and
400+
`not_tagged` methods.
401+
402+
```ruby
403+
it { is_expected.to contain_service('keystone').tagged('site:cph') }
404+
```
405+
399406
#### Checking the number of resources
400407

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

lib/rspec-puppet/matchers/create_generic.rb

Lines changed: 63 additions & 15 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_/, '')
@@ -114,6 +126,8 @@ def matches?(catalogue)
114126

115127
check_params(rsrc_hsh, @expected_params, :should) if @expected_params.any?
116128
check_params(rsrc_hsh, @expected_undef_params, :not) if @expected_undef_params.any?
129+
check_tags(resource, @tagged, :should) if @tagged.any?
130+
check_tags(resource, @not_tagged, :not) if @not_tagged.any?
117131
check_befores(@catalogue, resource) if @befores.any?
118132
check_requires(@catalogue, resource) if @requires.any?
119133
check_notifies(@catalogue, resource) if @notifies.any?
@@ -132,8 +146,8 @@ def failure_message_when_negated
132146
end
133147

134148
def description
149+
tests = []
135150
values = []
136-
value_str_prefix = "with"
137151

138152
if @expected_params_count
139153
values << "exactly #{@expected_params_count} parameters"
@@ -148,34 +162,39 @@ def description
148162
end
149163

150164
if @notifies.any?
151-
value_str_prefix = "that notifies"
152-
values = @notifies
165+
tests << english_list("that notifies", @notifies)
153166
end
154167

155168
if @subscribes.any?
156-
value_str_prefix = "that subscribes to"
157-
values = @subscribes
169+
tests << english_list("that subscribes to", @subscribes)
158170
end
159171

160172
if @requires.any?
161-
value_str_prefix = "that requires"
162-
values = @requires
173+
tests << english_list("that requires", @requires)
163174
end
164175

165176
if @befores.any?
166-
value_str_prefix = "that comes before"
167-
values = @befores
177+
tests << english_list("that comes before", @befores)
178+
end
179+
180+
if @tagged.any?
181+
tests << english_list("that is tagged", @tagged)
182+
end
183+
184+
if @not_tagged.any?
185+
tests << english_list("that is not tagged", @not_tagged, "nor")
168186
end
169187

170188
unless values.empty?
171-
if values.length == 1
172-
value_str = " #{value_str_prefix} #{values.first}"
173-
else
174-
value_str = " #{value_str_prefix} #{values[0..-2].join(", ")} and #{values[-1]}"
175-
end
189+
tests << english_list("with", values)
176190
end
177191

178-
"contain #{@referenced_type}[#{@title}]#{value_str}"
192+
tests_str = ""
193+
unless tests.empty?
194+
tests_str = english_list("", tests, "and", true)
195+
end
196+
197+
"contain #{@referenced_type}[#{@title}]#{tests_str}"
179198
end
180199

181200
def diffable?
@@ -225,6 +244,16 @@ def generate_param_list(list, type)
225244
output
226245
end
227246

247+
def english_list(value_str_prefix, values, joiner='and', oxford_comma=false)
248+
if values.length == 1
249+
"#{value_str_prefix} #{values.first}"
250+
elsif oxford_comma
251+
"#{value_str_prefix} #{values[0..-2].join(", ")}, #{joiner} #{values[-1]}"
252+
else
253+
"#{value_str_prefix} #{values[0..-2].join(", ")} #{joiner} #{values[-1]}"
254+
end
255+
end
256+
228257
def check_befores(catalogue, resource)
229258
@befores.each do |ref|
230259
unless precedes?(resource, canonicalize_resource(ref))
@@ -382,6 +411,25 @@ def check_params(resource, list, type)
382411
end
383412
end
384413
end
414+
# @param resource [Puppet::Resource] The resource in the catalog
415+
# @param list [Array<String>] The expected tags for the resource
416+
# @param type [:should, :not] Whether the given tags should/not be present
417+
def check_tags(resource, list, type)
418+
case type
419+
when :should
420+
list.each do |tag|
421+
unless resource.tags.include? tag
422+
@errors << "#{tag} is not set"
423+
end
424+
end
425+
when :not
426+
list.each do |tag|
427+
if resource.tags.include? tag
428+
@errors << "#{tag} is set"
429+
end
430+
end
431+
end
432+
end
385433
end
386434
end
387435
end

spec/fixtures/manifests/site.pp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,15 @@
4848
path => "cert ${clientcert}"
4949
}
5050
}
51+
52+
node 'tags_testing' {
53+
tag 'keyword_tag'
54+
include sysctl::common
55+
file { '/tmp/a':
56+
ensure => present
57+
}
58+
file { '/tmp/b':
59+
ensure => present,
60+
tag => 'metaparam_tag'
61+
}
62+
}

spec/hosts/tags_testing_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
describe 'tags_testing' do
4+
it { should compile }
5+
it { should contain_class('sysctl::common')
6+
.tagged('sysctl')
7+
.tagged('keyword_tag')
8+
.not_tagged('no_such_tag')
9+
}
10+
it { should contain_file('/tmp/a')
11+
.tagged('keyword_tag')
12+
.not_tagged('not_even_this')
13+
.not_tagged('metaparam_tag')
14+
}
15+
it { should contain_file('/tmp/b')
16+
.with_ensure('present')
17+
.tagged(['keyword_tag', 'metaparam_tag'])
18+
}
19+
end

spec/unit/matchers/compile_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@
152152
before(:each) { subject.and_raise_error(expected_error) }
153153

154154
if Puppet::Util::Package.versioncmp(Puppet.version, '5.3.4') >= 0
155-
let(:error_detail) { "failure (line: 52, column: 1)" }
155+
let(:error_detail) { "failure (line: 64, column: 1)" }
156156
else
157-
let(:error_detail) { "failure at line 52:1" }
157+
let(:error_detail) { "failure at line 64:1" }
158158
end
159159

160160
if Puppet.version.to_f >= 4.0

0 commit comments

Comments
 (0)