Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

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

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rodjek/rspec-puppet
...
head fork: rodjek/rspec-puppet
Checking mergeability… Don't worry, you can still create the pull request.
  • 8 commits
  • 11 files changed
  • 0 commit comments
  • 3 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
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
3  Gemfile
@@ -1,7 +1,8 @@
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
View
8 lib/rspec-puppet/example/class_example_group.rb
@@ -34,12 +34,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 +49,7 @@ def catalogue
}
facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
- build_catalog(nodename, facts_val)
+ build_catalog(nodename, facts_val, code)
end
end
end
View
4 lib/rspec-puppet/example/define_example_group.rb
@@ -41,7 +41,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 +51,7 @@ def catalogue
}
facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
- build_catalog(nodename, facts_val)
+ build_catalog(nodename, facts_val, code)
end
end
end
View
29 lib/rspec-puppet/example/function_example_group.rb
@@ -9,9 +9,36 @@ def subject
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 = nil
+ end
+
+ scope = Puppet::Parser::Scope.new(:compiler => @compiler)
scope.method "function_#{function_name}".to_sym
end
+
+ def compiler
+ @compiler
+ end
+ # get a compiler with an attached compiled catalog
+ def build_compiler(node_name, fact_values)
+ compiler = Puppet::Parser::Compiler.new(Puppet::Node.new(node_name, :parameters => fact_values))
+ compiler.compile
+ compiler
+ end
end
end
View
4 lib/rspec-puppet/example/host_example_group.rb
@@ -13,7 +13,7 @@ def catalogue
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 +24,7 @@ def catalogue
}
facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
- build_catalog(nodename, facts_val)
+ build_catalog(nodename, facts_val, code)
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(Puppet::ParseError)
end
end

No commit comments for this range

Something went wrong with that request. Please try again.