Without this patch applied rspec-puppet reaches deep inside of Puppet
and uses an unsupported and private interface to obtain functions and
scope instances. This is a problem because Puppet Labs considers
internal classes such as Puppet::Parser::Scope as private and internal.
We change these classes without warning and this is a maintenance issue
for third party testing tools.
This patch fixes this maintenance problem by using the compatibility
layer implemented in the puppetlabs_spec_helper. When we change the
internal behavior of private classes such as Puppet::Parser::Scope,
we'll update the spec helper module methods to provide a version
agnostic way to get instances of these internal objects.
As an example, a supported way to get a scope instance is:
PuppetlabsSpec::PuppetInternals.scope
Instead of the private and unsupported method of:
Puppet::Parser::Scope.new
This patch introduces a Gem dependency onto the puppetlabs_spec_helper
gem.
This commit adds support for using the pre_condition
hook to specify the source code that should be used to
compile a catalog that can be introspected by functions for
testing purposes.
This allows users to test functions that need access to
catalog contents.
This allows the following snippet to test a function that
needs access to the catalog generated by pre_condition:
describe 'defined_with_params' do
#describe 'when resource is passed as a string' do
let :pre_condition do
'user { "dan": }'
end
it { should run.with_params('User[dan]', {}).and_return('true') }
end
Adds a method 'without' which accepts a list of
parameters that should not be defined in the resource.
Example:
it { should contain_service('keystone').without(
['restart', 'status']
)}
This commit adds a method 'with' that can be used
to test mulitple parameters/values with a single
method call
Example:
it do should contain_service('keystone').with(
'ensure' => 'running',
'enable' => 'true',
'hasstatus' => 'true',
'hasrestart' => 'true'
) end
It was created as a more condensed alternative to
chaining methods per parameter to validate.
Without this patch it is difficult to match portions of long strings
which are attribute values of resource parameters. For example, the
content parameter of a file resource with a multi-line template.
This patch makes it easier to match portions of the parameter's value by
passing a Regexp instance to the with_ catch all matcher.
The use case looks like:
describe 'with lsbmajdistrelease available' do
let(:facts) { @facter_facts.merge({'lsbmajdistrelease' => '6'}) }
it { should_not contain_file('puppetenterprise.repo').with_content(missing_content) }
it { should contain_package 'pe-ruby-devel' }
it do
should contain_file('puppetenterprise.repo').with_content(/baseurl=http/)
should contain_file('puppetenterprise.repo').with_content(/pe_base/)
should contain_file('puppetenterprise.repo').with_content(/pe_updates/)
should contain_file('puppetenterprise.repo').with_content(/pe_extras/)
end
end
This feature allows the same parameter to be matched multiple times using
multiple different regular expressions.
This patch also improves the error message by presenting the actual value in
the catalog when it does not match the expectation. This change applies to
both String and Regexp expectation matches.
The error output now looks like:
1) pe_devel on redhat el6 os families with lsbmajdistrelease available
Failure/Error: should contain_file('puppetenterprise.repo').with_content(/JEFF MCCUNE/)
expected that the catalogue would contain File[puppetenterprise.repo] with content \
matching `/JEFF MCCUNE/` but its value of `"# KERMIT FROG\n"` does not
Spec tests have been added to exercise the handling of Regexp instances.
Reviewed-by: Dan Bode
Without this patch, working with rspec-puppet in IRB fails when directly
requiring the library:
ruby-1.8.7-p334 :001 > require 'rspec-puppet'
NameError: uninitialized constant RSpec
from /Users/jeff/.rvm/gems/ruby-1.8.7-p334@puppet/gems/rspec-puppet-0.1.0/lib/rspec-puppet/matchers/create_generic.rb:1
from /Users/jeff/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /Users/jeff/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from /Users/jeff/.rvm/gems/ruby-1.8.7-p334@puppet/gems/rspec-puppet-0.1.0/lib/rspec-puppet/matchers.rb:1
from /Users/jeff/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /Users/jeff/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from /Users/jeff/.rvm/gems/ruby-1.8.7-p334@puppet/gems/rspec-puppet-0.1.0/lib/rspec-puppet.rb:2
from /Users/jeff/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:58:in `gem_original_require'
from /Users/jeff/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:58:in `require'
from (irb):1
This patch fixes the problem by requiring rspec directly from rspec-puppet.rb