rspec-puppet 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -88,6 +88,17 @@ the generic `with_<parameter>` chains.
88
88
  it { should contain_package('mysql-server').with_ensure('present') }
89
89
  ```
90
90
 
91
+ You can use the `with` method to verify the value of multiple parameters.
92
+
93
+ ```ruby
94
+ it do should contain_service('keystone').with(
95
+ 'ensure' => 'running',
96
+ 'enable' => 'true',
97
+ 'hasstatus' => 'true',
98
+ 'hasrestart' => 'true'
99
+ ) end
100
+ ```
101
+
91
102
  You can also test that specific parameters have been left undefined with the
92
103
  generic `without_<parameter>` chains.
93
104
 
@@ -95,6 +106,15 @@ generic `without_<parameter>` chains.
95
106
  it { should contain_file('/foo/bar').without_mode }
96
107
  ```
97
108
 
109
+ You can use the without method to verify that a list of parameters have not been
110
+ defined
111
+
112
+ ```ruby
113
+ it { should contain_service('keystone').without(
114
+ ['restart', 'status']
115
+ )}
116
+ ```
117
+
98
118
  ### Writing tests
99
119
 
100
120
  #### Basic test structure
data/Rakefile CHANGED
@@ -2,5 +2,6 @@ require 'rake'
2
2
  require 'rspec/core/rake_task'
3
3
 
4
4
  task :default => :test
5
+ task :spec => :test
5
6
 
6
7
  RSpec::Core::RakeTask.new(:test)
@@ -1,4 +1,5 @@
1
1
  require 'puppet'
2
+ require 'rspec'
2
3
  require 'rspec-puppet/matchers'
3
4
  require 'rspec-puppet/example'
4
5
 
@@ -9,6 +9,18 @@ module RSpec::Puppet
9
9
  @title = args[0]
10
10
  end
11
11
 
12
+ def with(*args, &block)
13
+ params = args.shift
14
+ @expected_params = (@expected_params || []) | params.to_a
15
+ self
16
+ end
17
+
18
+ def without(*args, &block)
19
+ params = args.shift
20
+ @expected_undef_params = (@expected_undef_params || []) | params.to_a
21
+ self
22
+ end
23
+
12
24
  def method_missing(method, *args, &block)
13
25
  if method.to_s =~ /^with_/
14
26
  param = method.to_s.gsub(/^with_/, '')
@@ -30,11 +42,19 @@ module RSpec::Puppet
30
42
  if resource.nil?
31
43
  ret = false
32
44
  else
45
+ rsrc_hsh = resource.to_hash
33
46
  if @expected_params
34
47
  @expected_params.each do |name, value|
35
- unless resource.send(:parameters)[name.to_sym].to_s == value.to_s
36
- ret = false
37
- (@errors ||= []) << "#{name.to_s} set to `#{value.inspect}`"
48
+ if value.kind_of?(Regexp) then
49
+ unless rsrc_hsh[name.to_sym].to_s =~ value
50
+ ret = false
51
+ (@errors ||= []) << "#{name.to_s} matching `#{value.inspect}` but its value of `#{rsrc_hsh[name.to_sym].inspect}` does not"
52
+ end
53
+ else
54
+ unless rsrc_hsh[name.to_sym].to_s == value.to_s
55
+ ret = false
56
+ (@errors ||= []) << "#{name.to_s} set to `#{value.inspect}` but it is set to `#{rsrc_hsh[name.to_sym].inspect}` in the catalogue"
57
+ end
38
58
  end
39
59
  end
40
60
  end
@@ -71,7 +91,7 @@ module RSpec::Puppet
71
91
  end
72
92
 
73
93
  def errors
74
- @errors.nil? ? "" : " with #{@errors.join(', ')}"
94
+ @errors.nil? ? "" : " with #{@errors.join(', and parameter ')}"
75
95
  end
76
96
  end
77
97
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rspec-puppet'
3
- s.version = '0.1.0'
3
+ s.version = '0.1.1'
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'
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  'Rakefile',
22
22
  'README.md',
23
23
  'rspec-puppet.gemspec',
24
+ 'spec/classes/boolean_regexp_spec.rb',
24
25
  'spec/classes/boolean_spec.rb',
25
26
  'spec/classes/sysctl_common_spec.rb',
26
27
  'spec/defines/sysctl_before_spec.rb',
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'boolean' do
4
+ let(:title) { 'bool.testing' }
5
+ let(:params) { { :bool => false } }
6
+ let(:message_re) { /bool is false/ }
7
+
8
+ it { should create_notify("bool testing").with_message(message_re) }
9
+ it { should_not create_notify("bool testing").with_message(/true/) }
10
+ end
@@ -4,6 +4,32 @@ describe 'sysctl::common' do
4
4
  it { should contain_exec('sysctl/reload') \
5
5
  .with_command('/sbin/sysctl -p /etc/sysctl.conf').with_returns([0, 2]) }
6
6
  it { should_not create_augeas('foo') }
7
+ describe 'when using with to specify a hash of parameters' do
8
+ it 'should fail if the parameter is not contained in the resource' do
9
+ expect do
10
+ subject.should contain_exec('sysctl/reload').with('foo' => 'bar')
11
+ end.should raise_error(RSpec::Expectations::ExpectationNotMetError)
12
+ end
13
+ it 'should pass if the parameters are contained in the resource' do
14
+ subject.should contain_exec('sysctl/reload').with(
15
+ 'refreshonly' => 'true',
16
+ 'returns' => [0, 2]
17
+ )
18
+ end
19
+ end
20
+ describe 'when using without to specify parameter name(s)' do
21
+ it 'should pass if the parameter name is not contained in the resource' do
22
+ subject.should contain_exec('sysctl/reload').without('foo')
23
+ end
24
+ it 'should pass if the parameter names are not contained in the resource' do
25
+ subject.should contain_exec('sysctl/reload').without(['foo', 'bar'])
26
+ end
27
+ it 'should fail if any of the parameter names are contained in the resource' do
28
+ expect do
29
+ subject.should contain_exec('sysctl/reload').without(['foo', 'returns'])
30
+ end.should raise_error(RSpec::Expectations::ExpectationNotMetError)
31
+ end
32
+ end
7
33
  end
8
34
 
9
35
  describe 'sysctl::common' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-puppet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Sharpe
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-03 00:00:00 Z
18
+ date: 2012-01-20 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
@@ -55,6 +55,7 @@ files:
55
55
  - Rakefile
56
56
  - README.md
57
57
  - rspec-puppet.gemspec
58
+ - spec/classes/boolean_regexp_spec.rb
58
59
  - spec/classes/boolean_spec.rb
59
60
  - spec/classes/sysctl_common_spec.rb
60
61
  - spec/defines/sysctl_before_spec.rb