Skip to content
This repository
  • 11 commits
  • 8 files changed
  • 0 comments
  • 2 contributors
Aug 07, 2011
Tim Sharpe Remove quotes from resource refs to match puppetlabs style 3e2b4ce
Aug 10, 2011
Tim Sharpe Update README to suggest contain_ over create_ d13bee6
Dan Bode bodepd Remove Faces API call.
Previously, used the catalog face for
compilation with the :use_node option.

Starting in 2.7.2rc2, all faces options
must be explicit, so the catalog face
no longer supports the use_node option.

This commit changes the call to use
the indirection directly.
f50fc9c
Dan Bode bodepd Merge branch 'puppetlabs-master' 1cbaf48
Tim Sharpe Merge pull request #11 from puppetlabs/master
fixes 2.7 support
41c07fa
Tim Sharpe Bump to 0.0.6 77307cb
Aug 11, 2011
Tim Sharpe Prettify README with some syntax highlighting 20cfb2d
Aug 29, 2011
Tim Sharpe Add support for testing undefined resource parameters
Closes #13
ac695d6
Tim Sharpe Bump to 0.0.7 c2fd88f
Tim Sharpe munge symbol fact names to strings 26920e9
Tim Sharpe Bump to 0.0.8 6026434
86 README.md
Source Rendered
@@ -33,13 +33,15 @@ If you use the above directory structure, your examples will automatically be
33 33 placed in the correct groups and have access to the custom matchers. If you
34 34 choose not to, you can force the examples into the required groups as follows.
35 35
36   - describe 'myclass', :type => :class do
37   - ...
38   - end
  36 +```ruby
  37 +describe 'myclass', :type => :class do
  38 + ...
  39 +end
39 40
40   - describe 'mydefine', :type => :define do
41   - ...
42   - end
  41 +describe 'mydefine', :type => :define do
  42 + ...
  43 +end
  44 +```
43 45
44 46 ## Matchers
45 47
@@ -49,21 +51,39 @@ You can test if a class has been included in the catalogue with the
49 51 `include_class` matcher. It takes the class name as a string as its only
50 52 argument
51 53
52   - it { should include_class('foo') }
  54 +```ruby
  55 +it { should include_class('foo') }
  56 +```
53 57
54 58 ### Checking if a resources exists
55 59
56 60 You can test if a resource exists in the catalogue with the generic
57   -`creates_<resource type>` matcher. If your resource type includes :: (e.g.
58   -`foo::bar` simply replace the :: with -
  61 +`contain_<resource type>` matcher.
59 62
60   - it { should create_augeas('bleh') }
61   - it { should create_foo-bar('baz') }
  63 +```ruby
  64 +it { should contain_augeas('bleh') }
  65 +```
  66 +
  67 +If your resource type includes :: (e.g.
  68 +`foo::bar` simply replace the :: with __ (two underscores).
  69 +
  70 +```ruby
  71 +it { should contain_foo__bar('baz') }
  72 +```
62 73
63 74 You can further test the parameters that have been passed to the resources with
64 75 the generic `with_<parameter>` chains.
65 76
66   - it { should create_package('mysql-server').with_ensure('present') }
  77 +```ruby
  78 +it { should contain_package('mysql-server').with_ensure('present') }
  79 +```
  80 +
  81 +You can also test that specific parameters have been left undefined with the
  82 +generic `without_<parameter>` chains.
  83 +
  84 +```ruby
  85 +it { should contain_file('/foo/bar').without_mode }
  86 +```
67 87
68 88 ## Writing tests
69 89
@@ -81,46 +101,60 @@ Will cause the following resource to be in included in catalogue for a host
81 101 command => '/sbin/sysctl -p /etc/sysctl.conf',
82 102 }
83 103
84   -We can write the following testcase
  104 +We can write the following testcase (in `spec/defines/sysctl_spec.rb`)
85 105
86   - describe 'sysctl' do
87   - let(:title) { 'baz' }
88   - let(:params) { { :value => 'foo' } }
  106 +```ruby
  107 +describe 'sysctl' do
  108 + let(:title) { 'baz' }
  109 + let(:params) { { :value => 'foo' } }
89 110
90   - it { should create_exec('sysctl/reload').with_command("/sbin/sysctl -p /etc/sysctl.conf") }
91   - end
  111 + it { should contain_exec('sysctl/reload').with_command("/sbin/sysctl -p /etc/sysctl.conf") }
  112 +end
  113 +```
92 114
93 115 ### Specifying the title of a resource
94 116
95   - let(:title) { 'foo' }
  117 +```ruby
  118 +let(:title) { 'foo' }
  119 +```
96 120
97 121 ### Specifying the parameters to pass to a resources or parametised class
98 122
99   - let(:params) { {:ensure => 'present', ...} }
  123 +```ruby
  124 +let(:params) { {:ensure => 'present', ...} }
  125 +```
100 126
101 127 ### Specifying the FQDN of the test node
102 128
103 129 If the manifest you're testing expects to run on host with a particular name,
104 130 you can specify this as follows
105 131
106   - let(:node) { 'testhost.example.com' }
  132 +```ruby
  133 +let(:node) { 'testhost.example.com' }
  134 +```
107 135
108 136 ### Specifying the facts that should be available to your manifest
109 137
110 138 By default, the test environment contains no facts for your manifest to use.
111 139 You can set them with a hash
112 140
113   - let(:facts) { {:operatingsystem => 'Debian', :kernel => 'Linux', ...} }
  141 +```ruby
  142 +let(:facts) { {:operatingsystem => 'Debian', :kernel => 'Linux', ...} }
  143 +```
114 144
115 145 ### Specifying the path to find your modules
116 146
117 147 I recommend setting a default module path by adding the following code to your
118 148 `spec_helper.rb`
119 149
120   - RSpec.configure do |c|
121   - c.module_path = '/path/to/your/module/dir'
122   - end
  150 +```ruby
  151 +RSpec.configure do |c|
  152 + c.module_path = '/path/to/your/module/dir'
  153 +end
  154 +```
123 155
124 156 However, if you want to specify it in each example, you can do so
125 157
126   - let(:module_path) { '/path/to/your/module/dir' }
  158 +```ruby
  159 +let(:module_path) { '/path/to/your/module/dir' }
  160 +```
6 lib/rspec-puppet/example/class_example_group.rb
@@ -36,7 +36,11 @@ def catalogue
36 36 Puppet[:code] = pre_cond + "\n" + Puppet[:code]
37 37
38 38 nodename = self.respond_to?(:node) ? node : Puppet[:certname]
39   - facts_val = self.respond_to?(:facts) ? facts : {}
  39 + facts_val = {
  40 + 'hostname' => nodename.split('.').first,
  41 + 'fqdn' => nodename,
  42 + }
  43 + facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
40 44
41 45 build_catalog(nodename, facts_val)
42 46 end
2  lib/rspec-puppet/example/define_example_group.rb
@@ -42,7 +42,7 @@ def catalogue
42 42 'hostname' => nodename.split('.').first,
43 43 'fqdn' => nodename,
44 44 }
45   - facts_val.merge!(facts) if self.respond_to?(:facts)
  45 + facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
46 46
47 47 build_catalog(nodename, facts_val)
48 48 end
19 lib/rspec-puppet/matchers/create_generic.rb
@@ -14,6 +14,10 @@ def method_missing(method, *args, &block)
14 14 param = method.to_s.gsub(/^with_/, '')
15 15 (@expected_params ||= []) << [param, args[0]]
16 16 self
  17 + elsif method.to_s =~ /^without_/
  18 + param = method.to_s.gsub(/^without_/, '')
  19 + (@expected_undef_params ||= []) << param
  20 + self
17 21 else
18 22 super
19 23 end
@@ -34,21 +38,30 @@ def matches?(catalogue)
34 38 end
35 39 end
36 40 end
  41 +
  42 + if @expected_undef_params
  43 + @expected_undef_params.each do |name|
  44 + unless resource.send(:parameters)[name.to_sym].nil?
  45 + ret = false
  46 + (@errors ||= []) << "#{name.to_s} undefined"
  47 + end
  48 + end
  49 + end
37 50 end
38 51
39 52 ret
40 53 end
41 54
42 55 def failure_message_for_should
43   - "expected that the catalogue would contain #{@referenced_type}[\"#{@title}\"]#{errors}"
  56 + "expected that the catalogue would contain #{@referenced_type}[#{@title}]#{errors}"
44 57 end
45 58
46 59 def failure_message_for_should_not
47   - "expected that the catalogue would not contain #{@referenced_type}[\"#{@title}\"]#{errors}"
  60 + "expected that the catalogue would not contain #{@referenced_type}[#{@title}]#{errors}"
48 61 end
49 62
50 63 def description
51   - "create #{@referenced_type}[\"#{@title}\"]"
  64 + "create #{@referenced_type}[#{@title}]"
52 65 end
53 66
54 67 private
4 lib/rspec-puppet/matchers/include_class.rb
@@ -8,11 +8,11 @@ module Matchers
8 8 end
9 9
10 10 description do
11   - "include Class['#{expected_class}']"
  11 + "include Class[#{expected_class}]"
12 12 end
13 13
14 14 failure_message_for_should do |actual|
15   - "expected that the catalogue would include Class['#{expected_class}']"
  15 + "expected that the catalogue would include Class[#{expected_class}]"
16 16 end
17 17 end
18 18 end
11 lib/rspec-puppet/support.rb
... ... @@ -1,5 +1,5 @@
1 1 module RSpec::Puppet
2   - module Support
  2 + module Support
3 3 def build_catalog nodename, facts_val
4 4 node_obj = Puppet::Node.new(nodename)
5 5
@@ -9,9 +9,14 @@ def build_catalog nodename, facts_val
9 9 if Puppet::Resource::Catalog.respond_to? :find
10 10 Puppet::Resource::Catalog.find(node_obj.name, :use_node => node_obj)
11 11 else
12   - require 'puppet/face'
13   - Puppet::Face[:catalog, :current].find(node_obj.name, :use_node => node_obj)
  12 + Puppet::Resource::Catalog.indirection.find(node_obj.name, :use_node => node_obj)
14 13 end
15 14 end
  15 +
  16 + def munge_facts(facts)
  17 + output = {}
  18 + facts.keys.each { |key| output[key.to_s] = facts[key] }
  19 + output
  20 + end
16 21 end
17 22 end
2  rspec-puppet.gemspec
... ... @@ -1,6 +1,6 @@
1 1 Gem::Specification.new do |s|
2 2 s.name = 'rspec-puppet'
3   - s.version = '0.0.5'
  3 + s.version = '0.0.8'
4 4 s.homepage = 'https://github.com/rodjek/rspec-puppet/'
5 5 s.summary = 'RSpec tests for your Puppet manifests'
6 6 s.description = 'RSpec tests for your Puppet manifests'
3  spec/defines/sysctl_spec.rb
@@ -9,5 +9,6 @@
9 9 .with_context('/files/etc/sysctl.conf') \
10 10 .with_changes("set vm.swappiness '60'") \
11 11 .with_onlyif("match vm.swappiness[.='60'] size == 0") \
12   - .with_notify('Exec[sysctl/reload]') }
  12 + .with_notify('Exec[sysctl/reload]')\
  13 + .without_foo }
13 14 end

No commit comments for this range

Something went wrong with that request. Please try again.