rspec-puppet 0.0.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/lib/rspec-puppet.rb +3 -0
- data/lib/rspec-puppet/example.rb +16 -0
- data/lib/rspec-puppet/example/class_example_group.rb +36 -0
- data/lib/rspec-puppet/example/define_example_group.rb +40 -0
- data/lib/rspec-puppet/matchers.rb +3 -0
- data/lib/rspec-puppet/matchers/create_generic.rb +74 -0
- data/lib/rspec-puppet/matchers/create_resource.rb +53 -0
- data/lib/rspec-puppet/matchers/include_class.rb +19 -0
- data/rspec-puppet.gemspec +24 -0
- metadata +88 -0
data/lib/rspec-puppet.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rspec-puppet/example/define_example_group'
|
2
|
+
require 'rspec-puppet/example/class_example_group'
|
3
|
+
|
4
|
+
RSpec::configure do |c|
|
5
|
+
def c.escaped_path(*parts)
|
6
|
+
Regexp.compile(parts.join('[\\\/]'))
|
7
|
+
end
|
8
|
+
|
9
|
+
c.include RSpec::Puppet::DefineExampleGroup, :type => :define, :example_group => {
|
10
|
+
:file_path => c.escaped_path(%w[spec define])
|
11
|
+
}
|
12
|
+
|
13
|
+
c.include RSpec::Puppet::ClassExampleGroup, :type => :class, :example_group => {
|
14
|
+
:file_path => c.escaped_path(%w[spec class])
|
15
|
+
}
|
16
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RSpec::Puppet
|
2
|
+
module ClassExampleGroup
|
3
|
+
include RSpec::Puppet::Matchers
|
4
|
+
|
5
|
+
def subject
|
6
|
+
@catalogue ||= catalogue
|
7
|
+
end
|
8
|
+
|
9
|
+
def catalogue
|
10
|
+
Puppet[:modulepath] = module_path
|
11
|
+
|
12
|
+
klass_name = self.class.top_level_description.downcase
|
13
|
+
if !self.respond_to?(:params) || params == {}
|
14
|
+
Puppet[:code] = "include #{klass_name}"
|
15
|
+
else
|
16
|
+
Puppet[:code] = 'class' + " { " + klass_name + ": " + params.keys.map { |r| "#{r.to_s} => '#{params[r].to_s}'"
|
17
|
+
}.join(', ') + " }"
|
18
|
+
end
|
19
|
+
|
20
|
+
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
|
21
|
+
facts_val = self.respond_to?(:facts) ? facts : {}
|
22
|
+
|
23
|
+
node_obj = Puppet::Node.new(nodename)
|
24
|
+
|
25
|
+
node_obj.merge(facts_val)
|
26
|
+
|
27
|
+
# trying to be compatible with 2.7 as well as 2.6
|
28
|
+
if Puppet::Resource::Catalog.respond_to? :find
|
29
|
+
Puppet::Resource::Catalog.find(node_obj.name, :use_node => node_obj)
|
30
|
+
else
|
31
|
+
require 'puppet/face'
|
32
|
+
Puppet::Face[:catalog, :current].find(node_obj.name, :use_node => node_obj)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module RSpec::Puppet
|
2
|
+
module DefineExampleGroup
|
3
|
+
include RSpec::Puppet::Matchers
|
4
|
+
|
5
|
+
def subject
|
6
|
+
@catalogue ||= catalogue
|
7
|
+
end
|
8
|
+
|
9
|
+
def catalogue
|
10
|
+
define_name = self.class.top_level_description.downcase
|
11
|
+
|
12
|
+
Puppet[:modulepath] = module_path
|
13
|
+
|
14
|
+
if self.respond_to? :params
|
15
|
+
param_str = params.keys.map { |r|
|
16
|
+
"#{r.to_s} => \"#{params[r].to_s}\""
|
17
|
+
}.join(', ')
|
18
|
+
else
|
19
|
+
param_str = ""
|
20
|
+
end
|
21
|
+
|
22
|
+
Puppet[:code] = define_name + " { \"" + title + "\": " + param_str + " }"
|
23
|
+
|
24
|
+
nodename = self.respond_to?(:node) ? node : Puppet[:certname]
|
25
|
+
facts_val = self.respond_to?(:facts) ? facts : {}
|
26
|
+
|
27
|
+
node_obj = Puppet::Node.new(nodename)
|
28
|
+
|
29
|
+
node_obj.merge(facts_val)
|
30
|
+
|
31
|
+
# trying to be compatible with 2.7 as well as 2.6
|
32
|
+
if Puppet::Resource::Catalog.respond_to? :find
|
33
|
+
Puppet::Resource::Catalog.find(node_obj.name, :use_node => node_obj)
|
34
|
+
else
|
35
|
+
require 'puppet/face'
|
36
|
+
Puppet::Face[:catalog, :current].find(node_obj.name, :use_node => node)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module RSpec::Puppet
|
2
|
+
module Matchers
|
3
|
+
class CreateGeneric
|
4
|
+
def initialize(*args, &block)
|
5
|
+
@exp_resource_type = args.shift.to_s.gsub(/^create_/, '')
|
6
|
+
@args = args
|
7
|
+
@block = block
|
8
|
+
@referenced_type = referenced_type(@exp_resource_type)
|
9
|
+
@title = args[0]
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(method, *args, &block)
|
13
|
+
if method.to_s =~ /^with_/
|
14
|
+
param = method.to_s.gsub(/^with_/, '')
|
15
|
+
(@expected_params ||= []) << [param, args[0]]
|
16
|
+
self
|
17
|
+
else
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def matches?(catalogue)
|
23
|
+
ret = true
|
24
|
+
resources = catalogue.resources.select { |r|
|
25
|
+
r.type == @referenced_type
|
26
|
+
}.select { |r|
|
27
|
+
r.title == @title if r.respond_to? :title
|
28
|
+
}
|
29
|
+
|
30
|
+
unless resources.length == 1
|
31
|
+
ret = false
|
32
|
+
else
|
33
|
+
if @expected_params
|
34
|
+
@expected_params.each do |name, value|
|
35
|
+
unless resources.first.send(:parameters)[name.to_sym].to_s == value.to_s
|
36
|
+
ret = false
|
37
|
+
(@errors ||= []) << "#{name.to_s} set to `#{value.inspect}`"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
ret
|
44
|
+
end
|
45
|
+
|
46
|
+
def failure_message_for_should
|
47
|
+
"expected that the catalogue would contain #{@referenced_type}[\"#{@title}\"]#{errors}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def failure_message_for_should_not
|
51
|
+
"expected that the catalogue would not contain #{@referenced_type}[\"#{@title}\"]#{errors}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def description
|
55
|
+
"create #{@referenced_type}[\"#{@title}\"]"
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def referenced_type(type)
|
61
|
+
type.split('-').map { |r| r.capitalize }.join('::')
|
62
|
+
end
|
63
|
+
|
64
|
+
def errors
|
65
|
+
@errors.nil? ? "" : " with #{@errors.join(', ')}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def method_missing(method, *args, &block)
|
70
|
+
return RSpec::Puppet::Matchers::CreateGeneric.new(method, *args, &block) if method.to_s =~ /^create_/
|
71
|
+
super
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module RSpec::Puppet
|
2
|
+
module Matchers
|
3
|
+
extend RSpec::Matchers::DSL
|
4
|
+
|
5
|
+
matcher :create_resource do |expected_type, expected_title|
|
6
|
+
match do |catalogue|
|
7
|
+
ret = true
|
8
|
+
resources = catalogue.resources.select { |r|
|
9
|
+
r.type == referenced_type(expected_type)
|
10
|
+
}.select { |r|
|
11
|
+
r.title == expected_title if r.respond_to? :title
|
12
|
+
}
|
13
|
+
|
14
|
+
unless resources.length == 1
|
15
|
+
ret = false
|
16
|
+
end
|
17
|
+
|
18
|
+
if @expected_params
|
19
|
+
@expected_params.each do |name, value|
|
20
|
+
unless resources.first.send(:parameters)[name.to_sym] == value
|
21
|
+
ret = false
|
22
|
+
(@errors ||= []) << "the parameter #{name.to_s} set to `#{value}`"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ret
|
28
|
+
end
|
29
|
+
|
30
|
+
def errors
|
31
|
+
@errors.nil? ? "" : " with #{@errors.join(', ')}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def referenced_type(type)
|
35
|
+
type.split('::').map { |r| r.capitalize }.join('::')
|
36
|
+
end
|
37
|
+
|
38
|
+
chain :with_param do |param_name,param_value|
|
39
|
+
(@expected_params ||= []) << [param_name, param_value]
|
40
|
+
end
|
41
|
+
|
42
|
+
description do
|
43
|
+
type = referenced_type(expected_type)
|
44
|
+
"create #{type}['#{expected_title}']"
|
45
|
+
end
|
46
|
+
|
47
|
+
failure_message_for_should do |actual|
|
48
|
+
type = referenced_type(expected_type)
|
49
|
+
"expected that the catalogue would contain #{type}['#{expected_title}']#{errors}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module RSpec::Puppet
|
2
|
+
module Matchers
|
3
|
+
extend RSpec::Matchers::DSL
|
4
|
+
|
5
|
+
matcher :include_class do |expected_class|
|
6
|
+
match do |catalogue|
|
7
|
+
catalogue.classes.include? expected_class
|
8
|
+
end
|
9
|
+
|
10
|
+
description do
|
11
|
+
"include Class['#{expected_class}']"
|
12
|
+
end
|
13
|
+
|
14
|
+
failure_message_for_should do |actual|
|
15
|
+
"expected that the catalogue would include Class['#{expected_class}']"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'rspec-puppet'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.homepage = 'https://github.com/rodjek/rspec-puppet/'
|
5
|
+
s.summary = 'RSpec tests for your Puppet manifests'
|
6
|
+
s.description = 'RSpec tests for your Puppet manifests'
|
7
|
+
|
8
|
+
s.files = [
|
9
|
+
'rspec-puppet.gemspec',
|
10
|
+
'lib/rspec-puppet.rb',
|
11
|
+
'lib/rspec-puppet/matchers.rb',
|
12
|
+
'lib/rspec-puppet/matchers/create_generic.rb',
|
13
|
+
'lib/rspec-puppet/matchers/create_resource.rb',
|
14
|
+
'lib/rspec-puppet/matchers/include_class.rb',
|
15
|
+
'lib/rspec-puppet/example.rb',
|
16
|
+
'lib/rspec-puppet/example/define_example_group.rb',
|
17
|
+
'lib/rspec-puppet/example/class_example_group.rb',
|
18
|
+
]
|
19
|
+
|
20
|
+
s.add_dependency 'rspec'
|
21
|
+
|
22
|
+
s.authors = ['Tim Sharpe']
|
23
|
+
s.email = 'tim@sharpe.id.au'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-puppet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tim Sharpe
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-19 00:00:00 +10:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description: RSpec tests for your Puppet manifests
|
36
|
+
email: tim@sharpe.id.au
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- rspec-puppet.gemspec
|
45
|
+
- lib/rspec-puppet.rb
|
46
|
+
- lib/rspec-puppet/matchers.rb
|
47
|
+
- lib/rspec-puppet/matchers/create_generic.rb
|
48
|
+
- lib/rspec-puppet/matchers/create_resource.rb
|
49
|
+
- lib/rspec-puppet/matchers/include_class.rb
|
50
|
+
- lib/rspec-puppet/example.rb
|
51
|
+
- lib/rspec-puppet/example/define_example_group.rb
|
52
|
+
- lib/rspec-puppet/example/class_example_group.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: https://github.com/rodjek/rspec-puppet/
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.10
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: RSpec tests for your Puppet manifests
|
87
|
+
test_files: []
|
88
|
+
|