Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add relationship matchers for new syntax
  • Loading branch information
rodjek committed Sep 19, 2017
commit 3b0c8c2f2c65c562cccc76d4f161d594e14f62af
5 changes: 5 additions & 0 deletions lib/rspec-puppet/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
require 'rspec-puppet/matchers/dynamic_matchers'
require 'rspec-puppet/matchers/type_matchers'
require 'rspec-puppet/matchers/allow_value'

require 'rspec-puppet/matchers/notify'
require 'rspec-puppet/matchers/subscribe_to'
require 'rspec-puppet/matchers/require'
require 'rspec-puppet/matchers/come_before'
25 changes: 25 additions & 0 deletions lib/rspec-puppet/matchers/come_before.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module RSpec::Puppet
module ManifestMatchers
extend RSpec::Matchers::DSL

matcher :come_before do |*preceding_resources|
match do |resource|
preceding_resources.flatten.all? do |expected_precede|
resource.comes_before_resource?(expected_precede)
end
end

description do
"come before #{preceding_resources.flatten.join(', ')}"
end

failure_message do |resource|
"expected to come before #{preceding_resources.flatten.join(', ')}"
end

failure_message_when_negated do |resource|
"expected not to come before #{preceding_resources.flatten.join(', ')}"
end
end
end
end
25 changes: 25 additions & 0 deletions lib/rspec-puppet/matchers/notify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module RSpec::Puppet
module ManifestMatchers
extend RSpec::Matchers::DSL

matcher :notify do |*notified_resources|
match do |resource|
notified_resources.flatten.all? do |expected_notify|
resource.notifies_resource?(expected_notify)
end
end

description do
"notify #{notified_resources.flatten.join(', ')}"
end

failure_message do |resource|
"expected to notify #{notified_resources.flatten.join(', ')}"
end

failure_message_when_negated do |resource|
"expected not to notify #{notified_resources.flatten.join(', ')}"
end
end
end
end
25 changes: 25 additions & 0 deletions lib/rspec-puppet/matchers/require.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module RSpec::Puppet
module ManifestMatchers
extend RSpec::Matchers::DSL

matcher :require do |*required_resources|
match do |resource|
required_resources.flatten.all? do |expected_require|
resource.requires_resource?(expected_require)
end
end

description do
"require #{required_resources.flatten.join(', ')}"
end

failure_message do |resource|
"expected to require #{required_resources.flatten.join(', ')}"
end

failure_message_when_negated do |resource|
"expected not to require #{required_resources.flatten.join(', ')}"
end
end
end
end
25 changes: 25 additions & 0 deletions lib/rspec-puppet/matchers/subscribe_to.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module RSpec::Puppet
module ManifestMatchers
extend RSpec::Matchers::DSL

matcher :subscribe_to do |*subscribed_resources|
match do |resource|
subscribed_resources.flatten.all? do |expected_subscribe|
resource.subscribes_to_resource?(expected_subscribe)
end
end

description do
"subscribe to #{subscribed_resources.flatten.join(', ')}"
end

failure_message do |resource|
"expected to subscribe to #{subscribed_resources.flatten.join(', ')}"
end

failure_message_when_negated do |resource|
"expected not to subscribe to #{subscribed_resources.flatten.join(', ')}"
end
end
end
end
43 changes: 43 additions & 0 deletions lib/rspec-puppet/v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def puppet_resource(type, title)
end

class Resource
include RSpec::Puppet::Helpers::Relationships

def initialize(type, title)
@type = type
@title = title
Expand Down Expand Up @@ -34,11 +36,52 @@ def exist?
end
alias_method :in_catalogue?, :exist?
alias_method :in_catalog?, :exist?

def sanitise_resource(resource)
if resource.is_a?(self.class)
resource.catalogue_resource
else
canonicalize_resource(resource)
end
end

def notifies_resource?(other_resource)
return false unless exist?
other_resource = sanitise_resource(other_resource)
return false if other_resource.nil?

notifies?(catalogue_resource, other_resource)
end

def subscribes_to_resource?(other_resource)
return false unless exist?
other_resource = sanitise_resource(other_resource)
return false if other_resource.nil?

notifies?(other_resource, catalogue_resource)
end

def requires_resource?(other_resource)
return false unless exist?
other_resource = sanitise_resource(other_resource)
return false if other_resource.nil?

precedes?(other_resource, catalogue_resource)
end

def comes_before_resource?(other_resource)
return false unless exist?
other_resource = sanitise_resource(other_resource)
return false if other_resource.nil?

precedes?(catalogue_resource, other_resource)
end
end
end
end

# TODO: Limit this to only rspec-puppet example groups
class RSpec::Core::ExampleGroup
extend RSpec::Puppet::V3
include RSpec::Puppet::V3
end
37 changes: 37 additions & 0 deletions spec/classes/relationship__before_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,41 @@

it { should_not contain_class('relationship::before::pre').that_comes_before('Class[relationship::before::unknown]') }
it { should_not contain_class('relationship::before::post').that_requires('Class[relationship::before::unknown]') }

context 'using the new syntax', :if => RSpec::Core::Version::STRING.start_with?('3') do
describe puppet_resource('notify', 'foo') do
it { is_expected.to come_before('Notify[bar]') }
it { is_expected.to come_before('Notify[baz]') }
it { is_expected.to come_before('Notify[bar]').and come_before('Notify[baz]') }
it { is_expected.to come_before('Notify[bar]', 'Notify[baz]') }
it { is_expected.to come_before(['Notify[bar]', 'Notify[baz]']) }
end

describe puppet_resource('notify', 'bar') do
it { is_expected.to come_before('Notify[baz]') }
it { is_expected.to require('Notify[foo]') }
end

describe puppet_resource('notify', 'baz') do
it { is_expected.to require('Notify[foo]') }
it { is_expected.to require('Notify[bar]') }
it { is_expected.to require('Notify[foo]', 'Notify[bar]') }
end

describe puppet_resource('class', 'relationship::before::pre') do
it { is_expected.to come_before('Class[relationship::before::post]') }
end

describe puppet_resource('class', 'relationship::before::post') do
it { is_expected.to require('Class[relationship::before::pre]') }
end

describe puppet_resource('file', '/tmp/foo') do
it { is_expected.to come_before('File[/tmp/foo/bar]') }
end

describe puppet_resource('file', '/tmp/foo/bar') do
it { is_expected.to require('File[/tmp/foo]') }
end
end
end
31 changes: 31 additions & 0 deletions spec/classes/relationship__notify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,35 @@

it { should contain_notify('pre').that_notifies(['Notify[post]']) }
it { should contain_notify('post').that_subscribes_to(['Notify[pre]']) }

context 'using the new syntax', :if => RSpec::Core::Version::STRING.start_with?('3') do
describe puppet_resource('notify', 'foo') do
it { is_expected.to notify('Notify[bar]') }
end

describe puppet_resource('notify', 'baz') do
it { is_expected.to notify('Notify[bar]') }
it { is_expected.to notify('Notify[gronk]') }
it { is_expected.to notify(['Notify[bar]', 'Notify[gronk]']) }
it { is_expected.to notify('Notify[bar]', 'Notify[gronk]') }
end

describe puppet_resource('notify', 'gronk') do
it { is_expected.to subscribe_to('Notify[baz]') }
end

describe puppet_resource('notify', 'bar') do
it { is_expected.to subscribe_to('Notify[baz]', 'Notify[foo]') }
it { is_expected.to subscribe_to(['Notify[baz]', 'Notify[foo]']) }
end

describe puppet_resource('notify', 'pre') do
it { is_expected.to notify('Notify[post]') }
it { is_expected.to notify(puppet_resource('notify', 'post')) }
end

describe puppet_resource('notify', 'post') do
it { is_expected.to subscribe_to('Notify[pre]') }
end
end
end