Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 11 commits
  • 12 files changed
  • 0 commit comments
  • 4 contributors
Commits on Jul 31, 2012
@fungusakafungus fungusakafungus Cache the result of build_catalog 4ae3e45
Commits on Aug 02, 2012
@bodepd bodepd Add support for pre_condition with functions
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
41a2fc8
Commits on Aug 03, 2012
@fungusakafungus fungusakafungus fix regression, Puppet[:code] should be part of cache key 0bc21c7
Commits on Aug 08, 2012
@rodjek Merge pull request #36 from bodepd/pre_condition_catalogs_function_te…
…sting

Add support for pre_condition with functions
e3495af
@fungusakafungus fungusakafungus fix tests for rspec-2.11.0 e474433
Commits on Aug 09, 2012
@rodjek Merge branch 'memoize-build-catalog' of https://github.com/Jimdo/rspe…
…c-puppet into Jimdo-memoize-build-catalog
c49240e
@rodjek Fix bug with optional arrays in the matcher
When a type parameter takes an array with a single value, the compiled version
only has the single value not as an array.
2db185b
@rodjek Bump to 0.1.4 8d4c964
Commits on Aug 14, 2012
@jeffmccune jeffmccune Use puppetlabs_spec_helper to test parser functions
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.
e6b88cf
Commits on Aug 15, 2012
@jeffmccune jeffmccune Merge pull request #39 from jeffmccune/fix/master/clean_function_testing
Use puppetlabs_spec_helper to test parser functions
4afd64e
Commits on Oct 02, 2012
@rodjek Preliminary Puppet 3.0 support 60ec4ae
View
2 .travis.yml
@@ -11,6 +11,8 @@ notifications:
env:
- PUPPET_VERSION=2.6.14
- PUPPET_VERSION=2.7.12
+ - RSPEC_VERSION=2.9.0
+ - RSPEC_VERSION=2.11.0
matrix:
exclude:
- rvm: 1.9.2
View
4 Gemfile
@@ -1,7 +1,9 @@
source :rubygems
puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 2.7']
+rspecversion = ENV.key?('RSPEC_VERSION') ? "= #{ENV['RSPEC_VERSION']}" : ['>= 2.9']
gem 'rake'
-gem 'rspec'
+gem 'rspec', rspecversion
gem 'puppet', puppetversion
+gem 'puppetlabs_spec_helper'
View
6 lib/rspec-puppet.rb
@@ -1,9 +1,15 @@
require 'puppet'
require 'rspec'
+require 'fileutils'
+require 'tmpdir'
require 'rspec-puppet/matchers'
require 'rspec-puppet/example'
require 'rspec-puppet/setup'
+if Integer(Puppet.version.split('.').first) >= 3
+ Puppet.initialize_settings
+end
+
RSpec.configure do |c|
c.add_setting :module_path, :default => '/etc/puppet/modules'
c.add_setting :manifest_dir, :default => nil
View
13 lib/rspec-puppet/example/class_example_group.rb
@@ -8,6 +8,9 @@ def subject
end
def catalogue
+ vardir = Dir.mktmpdir
+ Puppet[:vardir] = vardir
+ Puppet[:hiera_config] = File.join(vardir, "hiera.yaml") if Puppet[:hiera_config] == "/dev/null"
Puppet[:modulepath] = self.respond_to?(:module_path) ? module_path : RSpec.configuration.module_path
Puppet[:manifestdir] = self.respond_to?(:manifest_dir) ? manifest_dir : RSpec.configuration.manifest_dir
Puppet[:manifest] = self.respond_to?(:manifest) ? manifest : RSpec.configuration.manifest
@@ -34,12 +37,12 @@ def catalogue
end
if !self.respond_to?(:params) || params == {}
- Puppet[:code] = import_str + "include #{klass_name}"
+ code = import_str + "include #{klass_name}"
else
- Puppet[:code] = import_str + 'class' + " { \"" + klass_name + "\": " + params.keys.map { |r| "#{r.to_s} => #{params[r].inspect}"
+ code = import_str + 'class' + " { \"" + klass_name + "\": " + params.keys.map { |r| "#{r.to_s} => #{params[r].inspect}"
}.join(',' ) + " }"
end
- Puppet[:code] = pre_cond + "\n" + Puppet[:code]
+ code = pre_cond + "\n" + code
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
facts_val = {
@@ -49,7 +52,9 @@ def catalogue
}
facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
- build_catalog(nodename, facts_val)
+ catalogue = build_catalog(nodename, facts_val, code)
+ FileUtils.rm_rf(vardir) if File.directory?(vardir)
+ catalogue
end
end
end
View
8 lib/rspec-puppet/example/define_example_group.rb
@@ -10,6 +10,8 @@ def subject
def catalogue
define_name = self.class.top_level_description.downcase
+ vardir = Dir.mktmpdir
+ Puppet[:vardir] = vardir
Puppet[:modulepath] = self.respond_to?(:module_path) ? module_path : RSpec.configuration.module_path
Puppet[:manifestdir] = self.respond_to?(:manifest_dir) ? manifest_dir : RSpec.configuration.manifest_dir
Puppet[:manifest] = self.respond_to?(:manifest) ? manifest : RSpec.configuration.manifest
@@ -41,7 +43,7 @@ def catalogue
pre_cond = ""
end
- Puppet[:code] = pre_cond + "\n" + import_str + define_name + " { \"" + title + "\": " + param_str + " }"
+ code = pre_cond + "\n" + import_str + define_name + " { \"" + title + "\": " + param_str + " }"
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
facts_val = {
@@ -51,7 +53,9 @@ def catalogue
}
facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
- build_catalog(nodename, facts_val)
+ catalogue = build_catalog(nodename, facts_val, code)
+ FileUtils.rm_rf(vardir) if File.directory?(vardir)
+ catalogue
end
end
end
View
43 lib/rspec-puppet/example/function_example_group.rb
@@ -1,17 +1,54 @@
+require 'puppetlabs_spec_helper/puppetlabs_spec/puppet_internals'
+
module RSpec::Puppet
module FunctionExampleGroup
include RSpec::Puppet::FunctionMatchers
+ PuppetInternals = PuppetlabsSpec::PuppetInternals
def subject
function_name = self.class.top_level_description.downcase
Puppet[:modulepath] = self.respond_to?(:module_path) ? module_path : RSpec.configuration.module_path
Puppet[:libdir] = Dir["#{Puppet[:modulepath]}/*/lib"].entries.join(File::PATH_SEPARATOR)
- Puppet::Parser::Functions.autoloader.loadall
- scope = Puppet::Parser::Scope.new
+ # if we specify a pre_condition, we should ensure that we compile that code
+ # into a catalog that is accessible from the scope where the function is called
+ if self.respond_to? :pre_condition
+ Puppet[:code] = pre_condition
+ nodename = self.respond_to?(:node) ? node : Puppet[:certname]
+ facts_val = {
+ 'hostname' => nodename.split('.').first,
+ 'fqdn' => nodename,
+ 'domain' => nodename.split('.').last,
+ }
+ facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
+ # we need to get a compiler, b/c we can attach that to a scope
+ @compiler = build_compiler(nodename, facts_val)
+ else
+ @compiler = PuppetInternals.compiler
+ end
+
+ scope = PuppetInternals.scope(:compiler => @compiler)
+
+ # Return the method instance for the function. This can be used with
+ # method.call
+ method = PuppetInternals.function_method(function_name, :scope => scope)
+ end
+
+ def compiler
+ @compiler
+ end
- scope.method "function_#{function_name}".to_sym
+ # get a compiler with an attached compiled catalog
+ def build_compiler(node_name, fact_values)
+ node_options = {
+ :name => node_name,
+ :options => { :parameters => fact_values },
+ }
+ node = PuppetInternals.node(node_options)
+ compiler = PuppetInternals.compiler(:node => node)
+ compiler.compile
+ compiler
end
end
end
View
8 lib/rspec-puppet/example/host_example_group.rb
@@ -8,12 +8,14 @@ def subject
end
def catalogue
+ vardir = Dir.mktmpdir
+ Puppet[:vardir] = vardir
Puppet[:modulepath] = self.respond_to?(:module_path) ? module_path : RSpec.configuration.module_path
Puppet[:manifestdir] = self.respond_to?(:manifest_dir) ? manifest_dir : RSpec.configuration.manifest_dir
Puppet[:manifest] = self.respond_to?(:manifest) ? manifest : RSpec.configuration.manifest
Puppet[:templatedir] = self.respond_to?(:template_dir) ? template_dir : RSpec.configuration.template_dir
Puppet[:config] = self.respond_to?(:config) ? config : RSpec.configuration.config
- Puppet[:code] = ""
+ code = ""
nodename = self.class.top_level_description.downcase
@@ -24,7 +26,9 @@ def catalogue
}
facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
- build_catalog(nodename, facts_val)
+ catalogue = build_catalog(nodename, facts_val, code)
+ FileUtils.rm_rf(vardir) if File.directory?(vardir)
+ catalogue
end
end
end
View
2 lib/rspec-puppet/matchers/create_generic.rb
@@ -51,7 +51,7 @@ def matches?(catalogue)
(@errors ||= []) << "#{name.to_s} matching `#{value.inspect}` but its value of `#{rsrc_hsh[name.to_sym].inspect}` does not"
end
elsif value.kind_of?(Array) then
- unless rsrc_hsh[name.to_sym].join == value.join
+ unless Array(rsrc_hsh[name.to_sym]).flatten.join == value.flatten.join
ret = false
(@errors ||= []) << "#{name.to_s} set to `#{value.inspect}` but it is set to `#{rsrc_hsh[name.to_sym].inspect}` in the catalogue"
end
View
13 lib/rspec-puppet/support.rb
@@ -1,6 +1,12 @@
module RSpec::Puppet
module Support
- def build_catalog nodename, facts_val
+
+ @@cache = {}
+
+ protected
+ def build_catalog_without_cache(nodename, facts_val, code)
+ Puppet[:code] = code
+
node_obj = Puppet::Node.new(nodename)
node_obj.merge(facts_val)
@@ -13,6 +19,11 @@ def build_catalog nodename, facts_val
end
end
+ public
+ def build_catalog *args
+ @@cache[args] ||= self.build_catalog_without_cache(*args)
+ end
+
def munge_facts(facts)
output = {}
facts.keys.each { |key| output[key.to_s] = facts[key] }
View
2 rspec-puppet.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'rspec-puppet'
- s.version = '0.1.3'
+ s.version = '0.1.4'
s.homepage = 'https://github.com/rodjek/rspec-puppet/'
s.summary = 'RSpec tests for your Puppet manifests'
s.description = 'RSpec tests for your Puppet manifests'
View
4 spec/classes/sysctl_common_spec.rb
@@ -8,7 +8,7 @@
it 'should fail if the parameter is not contained in the resource' do
expect do
subject.should contain_exec('sysctl/reload').with('foo' => 'bar')
- end.should raise_error(RSpec::Expectations::ExpectationNotMetError)
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
it 'should pass if the parameters are contained in the resource' do
subject.should contain_exec('sysctl/reload').with(
@@ -27,7 +27,7 @@
it 'should fail if any of the parameter names are contained in the resource' do
expect do
subject.should contain_exec('sysctl/reload').without(['foo', 'returns'])
- end.should raise_error(RSpec::Expectations::ExpectationNotMetError)
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end
end
end
View
2 spec/functions/split_spec.rb
@@ -6,6 +6,6 @@
it { should_not run.with_params('foo').and_raise_error(Puppet::DevError) }
it 'something' do
- expect { subject.call('foo') }.should raise_error(Puppet::ParseError)
+ expect { subject.call('foo') }.to raise_error(ArgumentError)
end
end

No commit comments for this range

Something went wrong with that request. Please try again.