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
  • 3 contributors
Commits on Jul 21, 2011
@rodjek Use Puppet::Resource::Catalog#resource dc7bb98
Commits on Aug 01, 2011
@haus haus Moved repeated code from both example_groups into a support module an…
…d build_catalog method in support.rb.
cb7722f
Commits on Aug 02, 2011
@rodjek Merge pull request #7 from mlitteken/patch_four
DRY up class_example_group and define_example_group
1c794fc
Commits on Aug 03, 2011
@haus haus Add pre_condition
Allows you to specify external dependencies
for classes/defines.

Reviewed-by: Dan Bode <dan@puppetlabs.com>
Signed-off-by: Matthaus Litteken <matthaus@puppetlabs.com>
7a8c7e9
@haus haus Quote class name
In case it contains non-alpha-numerics.

Previously, you could not specify classes with
namespaces like: foo::bar

Reviewed-by: Dan Bode <dan@puppetlabs.com>
Signed-off-by: Matthaus Litteken <matthaus@puppetlabs.com>
7d46c1e
@bodepd bodepd Support non-string types as param values
Previously boolean, hash, and array param
values were not supported for defined or classes.

Now, we are using inspect instead of to_s so they
will be returned as the correct reprentation of
their type to Puppet.

Reviewed-by: Matthaus Litteken <matthaus@puppetlabs.com>
Signed-off-by: Dan Bode <dan@puppetlabs.com>
0888e0e
Dan Bode Merge pull request #1 from mlitteken/merged_with_matthaus
Commits cleaned up, ready for merge
b4cf689
Commits on Aug 04, 2011
@bodepd bodepd Check resources exist before verifying their params
Previously, I was seeing the error
undefined method `parameters' for nil:NilClass in
cases where with_param was specified for resources
that did not exist in the catalog.

This patch will not check the chained params if
the resource does not exist.
0679654
Dan Bode Merge pull request #2 from bodepd/resource_exists_check
Resource exists check
45323bb
@rodjek Merge pull request #8 from puppetlabs/master
pull request
6d6c990
Commits on Aug 07, 2011
@rodjek Bump minor version 73f1781
View
1 lib/rspec-puppet/example.rb
@@ -1,3 +1,4 @@
+require 'rspec-puppet/support'
require 'rspec-puppet/example/define_example_group'
require 'rspec-puppet/example/class_example_group'
View
24 lib/rspec-puppet/example/class_example_group.rb
@@ -1,6 +1,7 @@
module RSpec::Puppet
module ClassExampleGroup
include RSpec::Puppet::Matchers
+ include RSpec::Puppet::Support
def subject
@catalogue ||= catalogue
@@ -20,27 +21,24 @@ def catalogue
import_str = ""
end
+ if self.respond_to? :pre_condition
+ pre_cond = pre_condition
+ else
+ pre_cond = ''
+ end
+
if !self.respond_to?(:params) || params == {}
Puppet[:code] = import_str + "include #{klass_name}"
else
- Puppet[:code] = import_str + 'class' + " { " + klass_name + ": " + params.keys.map { |r| "#{r.to_s} => '#{params[r].to_s}'"
- }.join(', ') + " }"
+ Puppet[: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]
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
facts_val = self.respond_to?(:facts) ? facts : {}
- node_obj = Puppet::Node.new(nodename)
-
- node_obj.merge(facts_val)
-
- # trying to be compatible with 2.7 as well as 2.6
- if Puppet::Resource::Catalog.respond_to? :find
- Puppet::Resource::Catalog.find(node_obj.name, :use_node => node_obj)
- else
- require 'puppet/face'
- Puppet::Face[:catalog, :current].find(node_obj.name, :use_node => node_obj)
- end
+ build_catalog(nodename, facts_val)
end
end
end
View
23 lib/rspec-puppet/example/define_example_group.rb
@@ -1,6 +1,7 @@
module RSpec::Puppet
module DefineExampleGroup
include RSpec::Puppet::Matchers
+ include RSpec::Puppet::Support
def subject
@catalogue ||= catalogue
@@ -22,13 +23,19 @@ def catalogue
if self.respond_to? :params
param_str = params.keys.map { |r|
- "#{r.to_s} => \"#{params[r].to_s}\""
+ "#{r.to_s} => #{params[r].inspect}"
}.join(', ')
else
param_str = ""
end
- Puppet[:code] = import_str + define_name + " { \"" + title + "\": " + param_str + " }"
+ if self.respond_to? :pre_condition
+ pre_cond = pre_condition
+ else
+ pre_cond = ""
+ end
+
+ Puppet[:code] = pre_cond + "\n" + import_str + define_name + " { \"" + title + "\": " + param_str + " }"
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
facts_val = {
@@ -37,17 +44,7 @@ def catalogue
}
facts_val.merge!(facts) if self.respond_to?(:facts)
- node_obj = Puppet::Node.new(nodename)
-
- node_obj.merge(facts_val)
-
- # trying to be compatible with 2.7 as well as 2.6
- if Puppet::Resource::Catalog.respond_to? :find
- Puppet::Resource::Catalog.find(node_obj.name, :use_node => node_obj)
- else
- require 'puppet/face'
- Puppet::Face[:catalog, :current].find(node_obj.name, :use_node => node)
- end
+ build_catalog(nodename, facts_val)
end
end
end
View
10 lib/rspec-puppet/matchers/create_generic.rb
@@ -21,18 +21,14 @@ def method_missing(method, *args, &block)
def matches?(catalogue)
ret = true
- resources = catalogue.resources.select { |r|
- r.type == @referenced_type
- }.select { |r|
- r.title == @title if r.respond_to? :title
- }
+ resource = catalogue.resource(@referenced_type, @title)
- unless resources.length == 1
+ if resource.nil?
ret = false
else
if @expected_params
@expected_params.each do |name, value|
- unless resources.first.send(:parameters)[name.to_sym].to_s == value.to_s
+ unless resource.send(:parameters)[name.to_sym].to_s == value.to_s
ret = false
(@errors ||= []) << "#{name.to_s} set to `#{value.inspect}`"
end
View
2 lib/rspec-puppet/matchers/create_resource.rb
@@ -15,7 +15,7 @@ module Matchers
ret = false
end
- if @expected_params
+ if @expected_params and resources.length != 0
@expected_params.each do |name, value|
unless resources.first.send(:parameters)[name.to_sym] == value
ret = false
View
17 lib/rspec-puppet/support.rb
@@ -0,0 +1,17 @@
+module RSpec::Puppet
+ module Support
+ def build_catalog nodename, facts_val
+ node_obj = Puppet::Node.new(nodename)
+
+ node_obj.merge(facts_val)
+
+ # trying to be compatible with 2.7 as well as 2.6
+ if Puppet::Resource::Catalog.respond_to? :find
+ Puppet::Resource::Catalog.find(node_obj.name, :use_node => node_obj)
+ else
+ require 'puppet/face'
+ Puppet::Face[:catalog, :current].find(node_obj.name, :use_node => node_obj)
+ end
+ end
+ end
+end
View
2 rspec-puppet.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'rspec-puppet'
- s.version = '0.0.3'
+ s.version = '0.0.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
10 spec/classes/boolean_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+describe 'boolean' do
+ let(:title) { 'bool.testing' }
+ let(:params) { { :bool => false } }
+
+ it { should create_notify("bool testing")\
+ .with_message("This will print when \$bool is false.") }
+
+end
View
7 spec/classes/sysctl_common_spec.rb
@@ -5,3 +5,10 @@
.with_command('/sbin/sysctl -p /etc/sysctl.conf').with_returns([0, 2]) }
it { should_not create_augeas('foo') }
end
+
+describe 'sysctl::common' do
+ let(:params) { { :test_param => "yes" } }
+
+ it { should create_class("sysctl::common")\
+ .with_test_param("yes") }
+end
View
24 spec/defines/sysctl_before_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'sysctl::before' do
+ let(:title) { 'sysctl::before' }
+ let(:params) { { :value => "title" } }
+
+ it "Should raise an error about needing the sysctl::common class" do
+ expect { should create_notify("message-title")\
+ .with_message("This should print if the class is here first.") }\
+ .to raise_error(Puppet::Error, /Could not find resource 'Class\[Sysctl::Common\]/)
+ end
+end
+
+describe 'sysctl::before' do
+ let(:title) { 'test define' }
+ let(:pre_condition) { 'class {"sysctl::common":}' }
+ let(:params) { { :value => "title" } }
+
+ it { should create_resource("sysctl::before", 'test define')\
+ .with_param(:value, "title") }
+
+ it { should include_class("sysctl::common") }
+
+end
View
12 spec/fixtures/boolean/manifests/init.pp
@@ -0,0 +1,12 @@
+class boolean($bool) {
+ $real_bool = $bool ? {
+ true => false,
+ false => true,
+ }
+
+ if ($real_bool) {
+ notify {"bool testing":
+ message => "This will print when \$bool is false."
+ }
+ }
+}
View
23 spec/fixtures/sysctl/manifests/init.pp
@@ -1,4 +1,4 @@
-class sysctl::common {
+class sysctl::common ($test_param = 'yes') {
exec { 'sysctl/reload':
command => '/sbin/sysctl -p /etc/sysctl.conf',
refreshonly => true,
@@ -16,3 +16,24 @@
notify => Exec['sysctl/reload'],
}
}
+
+class boolean($bool) {
+ $real_bool = $bool ? {
+ true => false,
+ false => true,
+ }
+
+ if ($real_bool) {
+ notify {"bool testing":
+ message => "This will print when \$bool is false."
+ }
+ }
+}
+
+define sysctl::before($value) {
+ Class['sysctl::common'] -> Sysctl::Before[$name]
+
+ notify {"message-${name}":
+ message => "This should print if the class is here first."
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.