Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .tagged and .not_tagged methods for resource matcher #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ it { is_expected.to contain_service('keystone').without(
)}
```

You can test if the resource has the expected tags with the `tagged` and
`not_tagged` methods.

```ruby
it { is_expected.to contain_service('keystone').tagged('site:cph') }
```

#### Checking the number of resources

You can test the number of resources in the catalogue with the
Expand Down
78 changes: 63 additions & 15 deletions lib/rspec-puppet/matchers/create_generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def initialize(*args, &block)
@subscribes = []
@requires = []
@befores = []
@tagged = []
@not_tagged = []
end

def with(*args, &block)
Expand Down Expand Up @@ -60,6 +62,16 @@ def that_comes_before(resource)
self
end

def tagged(resource)
@tagged.concat(Array(resource))
self
end

def not_tagged(resource)
@not_tagged.concat(Array(resource))
self
end

def method_missing(method, *args, &block)
if method.to_s =~ /^with_/
param = method.to_s.gsub(/^with_/, '')
Expand Down Expand Up @@ -114,6 +126,8 @@ def matches?(catalogue)

check_params(rsrc_hsh, @expected_params, :should) if @expected_params.any?
check_params(rsrc_hsh, @expected_undef_params, :not) if @expected_undef_params.any?
check_tags(resource, @tagged, :should) if @tagged.any?
check_tags(resource, @not_tagged, :not) if @not_tagged.any?
check_befores(@catalogue, resource) if @befores.any?
check_requires(@catalogue, resource) if @requires.any?
check_notifies(@catalogue, resource) if @notifies.any?
Expand All @@ -132,8 +146,8 @@ def failure_message_when_negated
end

def description
tests = []
values = []
value_str_prefix = "with"

if @expected_params_count
values << "exactly #{@expected_params_count} parameters"
Expand All @@ -148,34 +162,39 @@ def description
end

if @notifies.any?
value_str_prefix = "that notifies"
values = @notifies
tests << english_list("that notifies", @notifies)
end

if @subscribes.any?
value_str_prefix = "that subscribes to"
values = @subscribes
tests << english_list("that subscribes to", @subscribes)
end

if @requires.any?
value_str_prefix = "that requires"
values = @requires
tests << english_list("that requires", @requires)
end

if @befores.any?
value_str_prefix = "that comes before"
values = @befores
tests << english_list("that comes before", @befores)
end

if @tagged.any?
tests << english_list("that is tagged", @tagged)
end

if @not_tagged.any?
tests << english_list("that is not tagged", @not_tagged, "nor")
end

unless values.empty?
if values.length == 1
value_str = " #{value_str_prefix} #{values.first}"
else
value_str = " #{value_str_prefix} #{values[0..-2].join(", ")} and #{values[-1]}"
end
tests << english_list("with", values)
end

"contain #{@referenced_type}[#{@title}]#{value_str}"
tests_str = ""
unless tests.empty?
tests_str = english_list("", tests, "and", true)
end

"contain #{@referenced_type}[#{@title}]#{tests_str}"
end

def diffable?
Expand Down Expand Up @@ -225,6 +244,16 @@ def generate_param_list(list, type)
output
end

def english_list(value_str_prefix, values, joiner='and', oxford_comma=false)
if values.length == 1
"#{value_str_prefix} #{values.first}"
elsif oxford_comma
"#{value_str_prefix} #{values[0..-2].join(", ")}, #{joiner} #{values[-1]}"
else
"#{value_str_prefix} #{values[0..-2].join(", ")} #{joiner} #{values[-1]}"
end
end

def check_befores(catalogue, resource)
@befores.each do |ref|
unless precedes?(resource, canonicalize_resource(ref))
Expand Down Expand Up @@ -385,6 +414,25 @@ def check_params(resource, list, type)
end
end
end
# @param resource [Puppet::Resource] The resource in the catalog
# @param list [Array<String>] The expected tags for the resource
# @param type [:should, :not] Whether the given tags should/not be present
def check_tags(resource, list, type)
case type
when :should
list.each do |tag|
unless resource.tags.include? tag
@errors << "#{tag} is not set"
end
end
when :not
list.each do |tag|
if resource.tags.include? tag
@errors << "#{tag} is set"
end
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions spec/fixtures/manifests/site.pp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@
path => "cert ${clientcert}"
}
}

node 'tags_testing' {
tag 'keyword_tag'
include sysctl::common
file { '/tmp/a':
ensure => present
}
file { '/tmp/b':
ensure => present,
tag => 'metaparam_tag'
}
}
19 changes: 19 additions & 0 deletions spec/hosts/tags_testing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

describe 'tags_testing' do
it { should compile }
it { should contain_class('sysctl::common')
.tagged('sysctl')
.tagged('keyword_tag')
.not_tagged('no_such_tag')
}
it { should contain_file('/tmp/a')
.tagged('keyword_tag')
.not_tagged('not_even_this')
.not_tagged('metaparam_tag')
}
it { should contain_file('/tmp/b')
.with_ensure('present')
.tagged(['keyword_tag', 'metaparam_tag'])
}
end
4 changes: 2 additions & 2 deletions spec/unit/matchers/compile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@
before(:each) { subject.and_raise_error(expected_error) }

if Puppet::Util::Package.versioncmp(Puppet.version, '5.3.4') >= 0
let(:error_detail) { "failure (line: 52, column: 1)" }
let(:error_detail) { "failure (line: 64, column: 1)" }
else
let(:error_detail) { "failure at line 52:1" }
let(:error_detail) { "failure at line 64:1" }
end

if Puppet.version.to_f >= 4.0
Expand Down