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.
  • 9 commits
  • 8 files changed
  • 0 commit comments
  • 2 contributors
View
84 README.md
@@ -33,13 +33,15 @@ If you use the above directory structure, your examples will automatically be
placed in the correct groups and have access to the custom matchers. If you
choose not to, you can force the examples into the required groups as follows.
- describe 'myclass', :type => :class do
- ...
- end
+```ruby
+describe 'myclass', :type => :class do
+ ...
+end
- describe 'mydefine', :type => :define do
- ...
- end
+describe 'mydefine', :type => :define do
+ ...
+end
+```
## Matchers
@@ -49,21 +51,39 @@ You can test if a class has been included in the catalogue with the
`include_class` matcher. It takes the class name as a string as its only
argument
- it { should include_class('foo') }
+```ruby
+it { should include_class('foo') }
+```
### Checking if a resources exists
You can test if a resource exists in the catalogue with the generic
-`creates_<resource type>` matcher. If your resource type includes :: (e.g.
+`contain_<resource type>` matcher.
+
+```ruby
+it { should contain_augeas('bleh') }
+```
+
+If your resource type includes :: (e.g.
`foo::bar` simply replace the :: with __ (two underscores).
- it { should contain_augeas('bleh') }
- it { should contain_foo__bar('baz') }
+```ruby
+it { should contain_foo__bar('baz') }
+```
You can further test the parameters that have been passed to the resources with
the generic `with_<parameter>` chains.
- it { should contain_package('mysql-server').with_ensure('present') }
+```ruby
+it { should contain_package('mysql-server').with_ensure('present') }
+```
+
+You can also test that specific parameters have been left undefined with the
+generic `without_<parameter>` chains.
+
+```ruby
+it { should contain_file('/foo/bar').without_mode }
+```
## Writing tests
@@ -81,46 +101,60 @@ Will cause the following resource to be in included in catalogue for a host
command => '/sbin/sysctl -p /etc/sysctl.conf',
}
-We can write the following testcase
+We can write the following testcase (in `spec/defines/sysctl_spec.rb`)
- describe 'sysctl' do
- let(:title) { 'baz' }
- let(:params) { { :value => 'foo' } }
+```ruby
+describe 'sysctl' do
+ let(:title) { 'baz' }
+ let(:params) { { :value => 'foo' } }
- it { should contain_exec('sysctl/reload').with_command("/sbin/sysctl -p /etc/sysctl.conf") }
- end
+ it { should contain_exec('sysctl/reload').with_command("/sbin/sysctl -p /etc/sysctl.conf") }
+end
+```
### Specifying the title of a resource
- let(:title) { 'foo' }
+```ruby
+let(:title) { 'foo' }
+```
### Specifying the parameters to pass to a resources or parametised class
- let(:params) { {:ensure => 'present', ...} }
+```ruby
+let(:params) { {:ensure => 'present', ...} }
+```
### Specifying the FQDN of the test node
If the manifest you're testing expects to run on host with a particular name,
you can specify this as follows
- let(:node) { 'testhost.example.com' }
+```ruby
+let(:node) { 'testhost.example.com' }
+```
### Specifying the facts that should be available to your manifest
By default, the test environment contains no facts for your manifest to use.
You can set them with a hash
- let(:facts) { {:operatingsystem => 'Debian', :kernel => 'Linux', ...} }
+```ruby
+let(:facts) { {:operatingsystem => 'Debian', :kernel => 'Linux', ...} }
+```
### Specifying the path to find your modules
I recommend setting a default module path by adding the following code to your
`spec_helper.rb`
- RSpec.configure do |c|
- c.module_path = '/path/to/your/module/dir'
- end
+```ruby
+RSpec.configure do |c|
+ c.module_path = '/path/to/your/module/dir'
+end
+```
However, if you want to specify it in each example, you can do so
- let(:module_path) { '/path/to/your/module/dir' }
+```ruby
+let(:module_path) { '/path/to/your/module/dir' }
+```
View
3  lib/rspec-puppet.rb
@@ -4,4 +4,7 @@
RSpec.configure do |c|
c.add_setting :module_path, :default => '/etc/puppet/modules'
+ c.add_setting :manifest_dir, :default => nil
+ c.add_setting :manifest, :default => nil
+ c.add_setting :template_dir, :default => nil
end
View
12 lib/rspec-puppet/example/class_example_group.rb
@@ -9,6 +9,9 @@ def subject
def catalogue
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
klass_name = self.class.top_level_description.downcase
@@ -17,6 +20,8 @@ def catalogue
if File.exists?(File.join(Puppet[:modulepath], 'manifests', 'init.pp'))
path_to_manifest = File.join([Puppet[:modulepath], 'manifests', klass_name.split('::')[1..-1]].flatten)
import_str = "import '#{Puppet[:modulepath]}/manifests/init.pp'\nimport '#{path_to_manifest}.pp'\n"
+ elsif File.exists?(Puppet[:modulepath])
+ import_str = "import '#{Puppet[:manifest]}'\n"
else
import_str = ""
end
@@ -36,7 +41,12 @@ def catalogue
Puppet[:code] = pre_cond + "\n" + Puppet[:code]
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
- facts_val = self.respond_to?(:facts) ? facts : {}
+ facts_val = {
+ 'hostname' => nodename.split('.').first,
+ 'fqdn' => nodename,
+ 'domain' => nodename.split('.').last,
+ }
+ facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
build_catalog(nodename, facts_val)
end
View
8 lib/rspec-puppet/example/define_example_group.rb
@@ -11,12 +11,17 @@ def catalogue
define_name = self.class.top_level_description.downcase
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
# If we're testing a standalone module (i.e. one that's outside of a
# puppet tree), the autoloader won't work, so we need to fudge it a bit.
if File.exists?(File.join(Puppet[:modulepath], 'manifests', 'init.pp'))
path_to_manifest = File.join([Puppet[:modulepath], 'manifests', define_name.split('::')[1..-1]].flatten)
import_str = "import '#{Puppet[:modulepath]}/manifests/init.pp'\nimport '#{path_to_manifest}.pp'\n"
+ elsif File.exists?(Puppet[:modulepath])
+ import_str = "import '#{Puppet[:manifest]}'\n"
else
import_str = ""
end
@@ -41,8 +46,9 @@ def catalogue
facts_val = {
'hostname' => nodename.split('.').first,
'fqdn' => nodename,
+ 'domain' => nodename.split('.', 2).last,
}
- facts_val.merge!(facts) if self.respond_to?(:facts)
+ facts_val.merge!(munge_facts(facts)) if self.respond_to?(:facts)
build_catalog(nodename, facts_val)
end
View
13 lib/rspec-puppet/matchers/create_generic.rb
@@ -14,6 +14,10 @@ def method_missing(method, *args, &block)
param = method.to_s.gsub(/^with_/, '')
(@expected_params ||= []) << [param, args[0]]
self
+ elsif method.to_s =~ /^without_/
+ param = method.to_s.gsub(/^without_/, '')
+ (@expected_undef_params ||= []) << param
+ self
else
super
end
@@ -34,6 +38,15 @@ def matches?(catalogue)
end
end
end
+
+ if @expected_undef_params
+ @expected_undef_params.each do |name|
+ unless resource.send(:parameters)[name.to_sym].nil?
+ ret = false
+ (@errors ||= []) << "#{name.to_s} undefined"
+ end
+ end
+ end
end
ret
View
6 lib/rspec-puppet/support.rb
@@ -12,5 +12,11 @@ def build_catalog nodename, facts_val
Puppet::Resource::Catalog.indirection.find(node_obj.name, :use_node => node_obj)
end
end
+
+ def munge_facts(facts)
+ output = {}
+ facts.keys.each { |key| output[key.to_s] = facts[key] }
+ output
+ 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.6'
+ s.version = '0.0.9'
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
3  spec/defines/sysctl_spec.rb
@@ -9,5 +9,6 @@
.with_context('/files/etc/sysctl.conf') \
.with_changes("set vm.swappiness '60'") \
.with_onlyif("match vm.swappiness[.='60'] size == 0") \
- .with_notify('Exec[sysctl/reload]') }
+ .with_notify('Exec[sysctl/reload]')\
+ .without_foo }
end

No commit comments for this range

Something went wrong with that request. Please try again.