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
Next Next commit
Basic resource matching
  • Loading branch information
rodjek committed Sep 19, 2017
commit b4b6c5ced7995ffdbf888dd24ec9a4a2ac8d347c
2 changes: 2 additions & 0 deletions lib/rspec-puppet.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'puppet'
require 'rspec'
require 'rspec/its'
require 'fileutils'
require 'tmpdir'
require 'rspec-puppet/errors'
Expand All @@ -9,6 +10,7 @@
require 'rspec-puppet/coverage'
require 'rspec-puppet/adapters'
require 'rspec-puppet/consts'
require 'rspec-puppet/v3'

begin
require 'puppet/test/test_helper'
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec-puppet/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Support
@@cache = RSpec::Puppet::Cache.new

def subject
lambda { catalogue }
described_class || lambda { catalogue }
end

def environment
Expand Down
44 changes: 44 additions & 0 deletions lib/rspec-puppet/v3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module RSpec::Puppet
module V3
def puppet_resource(type, title)
RSpec::Puppet::V3::Resource.new(type, title)
end

class Resource
def initialize(type, title)
@type = type
@title = title
end

def to_s
"#{@type.capitalize}[#{@title}]"
end
alias_method :inspect, :to_s

def catalogue
@catalogue ||= RSpec.current_example.example_group_instance.catalogue
end

def catalogue_resource
@catalogue_resource ||= catalogue.resource(@type, @title)
end

def parameters
exist? ? catalogue_resource.to_hash : {}
end
alias_method :params, :parameters

def exist?
RSpec::Puppet::Coverage.cover!(catalogue_resource)
!catalogue_resource.nil?
end
alias_method :in_catalogue?, :exist?
alias_method :in_catalog?, :exist?
end
end
end

# TODO: Limit this to only rspec-puppet example groups
class RSpec::Core::ExampleGroup
extend RSpec::Puppet::V3
end
1 change: 1 addition & 0 deletions rspec-puppet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Gem::Specification.new do |s|
s.files = Dir['CHANGELOG.md', 'LICENSE.md', 'README.md', 'lib/**/*', 'bin/**/*']

s.add_dependency 'rspec'
s.add_dependency 'rspec-its'

s.authors = ['Tim Sharpe']
s.email = '[email protected]'
Expand Down
37 changes: 37 additions & 0 deletions spec/classes/test_basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
describe 'test::basic' do
it { should contain_fake('foo').with_three([{'foo' => 'bar'}]) }

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

context 'testing node based facts' do
let(:pre_condition) { 'notify { $::fqdn: }' }
let(:node) { 'test123.test.com' }
Expand All @@ -18,11 +25,31 @@
it { should contain_notify('test123.test.com') }
it { should_not contain_notify('notthis.test.com') }

context 'using the new syntax', :if => RSpec::Core::Version::STRING.start_with?('3') do
describe puppet_resource('notify', 'test123.test.com') do
it { is_expected.to exist }
end

describe puppet_resource('notify', 'notthis.test.com') do
it { is_expected.not_to exist }
end
end

context 'existing networking facts should not be clobbered', :if => Puppet.version.to_f >= 4.0 do
let(:pre_condition) { 'notify { [$facts["networking"]["primary"], $facts["networking"]["hostname"]]: }' }

it { should contain_notify('eth0') }
it { should contain_notify('test123') }

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

describe puppet_resource('notify', 'test123') do
it { is_expected.to exist }
end
end
end

context 'when derive_node_facts_from_nodename => false' do
Expand All @@ -40,6 +67,16 @@

it { should contain_notify('myhostname.test.com') }
it { should_not contain_notify('mycertname.test.com') }

context 'using the new syntax', :if => RSpec::Core::Version::STRING.start_with?('3') do
describe puppet_resource('notify', 'myhostname.test.com') do
it { is_expected.to exist }
end

describe puppet_resource('notify', 'mycertname.test.com') do
it { is_expected.not_to exist }
end
end
end
end
end