rspec-hiera-puppet 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-hiera-puppet.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Michael Franz Aigner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,132 @@
1
+ # Rspec::Hiera::Puppet
2
+
3
+ Hiera fixtures for puppet-rspec tests.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rspec-hiera-puppet'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rspec-hiera-puppet
18
+
19
+ ## Usage
20
+
21
+ ### Basic
22
+
23
+ The following assumes that you are already be familiar with
24
+ [rspec-puppet](https://github.com/rodjek/rspec-puppet/).
25
+
26
+ To use this gem add the following include to spec/spec\_helper.rb:
27
+
28
+ require 'rspec-hiera-puppet'
29
+
30
+ For the purpose of demonstrating a Hiera fixture, lets assume there is the
31
+ following contrived example of a Puppet class:
32
+
33
+ _modules/example/manifests/init.pp_
34
+
35
+ class example {
36
+ notify { 'foo': message => hiera('foo_message') }
37
+ }
38
+
39
+ In your specs for this class you can define a Hiera fixture by setting
40
+ 'hiera\_data' to a hash that contains the data that you want the Puppet
41
+ 'hiera' function to return:
42
+
43
+ _spec/classes/example\_spec.rb_
44
+
45
+ describe "example" do
46
+ let(:hiera_data) { { :foo_message => "bar" } }
47
+
48
+ it { should contain_notify("foo").with_message("bar") }
49
+ end
50
+
51
+ ### Advanced
52
+
53
+ It is possible to load the hiera fixtures with any Hiera backend, just define
54
+ a method 'hiera\_config' that returns the desired Hiera configuration. For a
55
+ list of possible configuration options, see
56
+ [https://github.com/puppetlabs/hiera#configuration](https://github.com/puppetlabs/hiera#configuration).
57
+
58
+ The following example spec loads the Hiera fixture data from Yaml files:
59
+
60
+ _spec/classes/example\_spec.rb_
61
+
62
+ describe "example" do
63
+ let(:hiera_config) do
64
+ { :backends => ['yaml'],
65
+ :hierarchy => [
66
+ '%{fqdn}/%{calling_module}',
67
+ '%{calling_module}'],
68
+ :yaml => {
69
+ :datadir => File.expand_path(File.join(__FILE__, '..', '..', 'hieradata')) }}
70
+ end
71
+
72
+ it { should contain_notify("foo").with_message("bar") }
73
+ end
74
+
75
+ Be aware that setting 'hiera\_config' takes precedence before setting
76
+ 'hiera\_data', meaning with the above Hiera configuration, the value of
77
+ 'hiera\_data' will be ignored. The next example will demonstrate how to address
78
+ that.
79
+
80
+ If you want a combination of fixture data from Yaml files and fixture data from
81
+ 'hiera\_data', you can use the Hiera 'rspec' backend, which is also provided
82
+ by this gem. The 'rspec' backend uses its configuration hash as data store to
83
+ look up data. The following example combine both the 'rspec' backend, and the
84
+ 'yaml' backend, with the effect that the data is first looked up in the
85
+ 'hiera\_data' fixture defined by the spec and then in Yaml files as well:
86
+
87
+ _spec/classes/example\_spec.rb_
88
+
89
+ describe "example" do
90
+ let(:hiera_config) do
91
+ { :backends => ['rspec', 'yaml'],
92
+ :hierarchy => [
93
+ '%{fqdn}/%{calling_module}',
94
+ '%{calling_module}'],
95
+ :yaml => {
96
+ :datadir => File.expand_path(File.join(__FILE__, '..', '..', 'hieradata')) },
97
+ :rspec => respond_to?(:hiera_data) ? hiera_data : {} }
98
+ end
99
+
100
+ it { should contain_notify("foo").with_message("bar") }
101
+ end
102
+
103
+ To avoid having to copy-paste the Hier configuration into each and every spec,
104
+ you can use a shared context. The following is a full-featured example that
105
+ demonstrates the use of a shared context.
106
+
107
+ _spec/spec\_helper.rb_
108
+
109
+ require 'rspec-hiera'
110
+ require 'rspec-hiera-puppet'
111
+
112
+ fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
113
+
114
+ shared_context "hieradata" do
115
+ let(:hiera_config) do
116
+ { :backends => ['rspec', 'yaml'],
117
+ :hierarchy => [
118
+ '%{fqdn}/%{calling_module}',
119
+ '%{calling_module}'],
120
+ :yaml => {
121
+ :datadir => File.join(fixture_path, 'hieradata') },
122
+ :rspec => respond_to?(:hiera_data) ? hiera_data : {} }
123
+ end
124
+ end
125
+
126
+ _spec/classes/example\_spec.rb_
127
+
128
+ describe "example" do
129
+ include_context "hieradata"
130
+
131
+ it { should contain_notify("foo").with_message("bar") }
132
+ end
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,52 @@
1
+ class Hiera
2
+ module Backend
3
+ class Rspec_backend
4
+ def initialize
5
+ Hiera.debug("Hiera RSpec backend starting")
6
+ end
7
+
8
+ def lookup(key, scope, order_override, resolution_type)
9
+ answer = Backend.empty_answer(resolution_type)
10
+
11
+ Hiera.debug("Looking up #{key} in RSpec backend")
12
+
13
+ Backend.datasources(scope, order_override) do |source|
14
+ Hiera.debug("Looking for data source #{source}")
15
+
16
+ data = Config[:rspec]
17
+
18
+ next if ! data
19
+ next if data.empty?
20
+
21
+ if data.include?(key)
22
+ raw_answer = data[key]
23
+ elsif data.include?(key.to_sym)
24
+ raw_answer = data[key.to_sym]
25
+ else
26
+ next
27
+ end
28
+
29
+ # for array resolution we just append to the array whatever
30
+ # we find, we then goes onto the next file and keep adding to
31
+ # the array
32
+ #
33
+ # for priority searches we break after the first found data item
34
+ new_answer = Backend.parse_answer(raw_answer, scope)
35
+ case resolution_type
36
+ when :array
37
+ raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
38
+ answer << new_answer
39
+ when :hash
40
+ raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
41
+ answer = new_answer.merge answer
42
+ else
43
+ answer = new_answer
44
+ break
45
+ end
46
+ end
47
+
48
+ return answer
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec-hiera-puppet/version'
2
+ require 'rspec-hiera-puppet/puppet'
3
+ require 'rspec-hiera-puppet/rspec'
@@ -0,0 +1,146 @@
1
+ require 'puppet'
2
+
3
+ class Puppet::Parser::Compiler
4
+ alias_method :compile_unadorned, :compile
5
+
6
+ def compile
7
+ spec = Thread.current[:spec]
8
+
9
+ if spec
10
+ register_function_hiera(spec)
11
+ register_function_hiera_array(spec)
12
+ register_function_hiera_hash(spec)
13
+ register_function_hiera_include(spec)
14
+ end
15
+
16
+ compile_unadorned
17
+ end
18
+
19
+ def register_function_hiera(spec)
20
+ Puppet::Parser::Functions.newfunction(:hiera, :type => :rvalue) do |*args|
21
+ # Functions called from puppet manifests that look like this:
22
+ # lookup("foo", "bar")
23
+ # internally in puppet are invoked: func(["foo", "bar"])
24
+ #
25
+ # where as calling from templates should work like this:
26
+ # scope.function_lookup("foo", "bar")
27
+ #
28
+ # Therefore, declare this function with args '*args' to accept any number
29
+ # of arguments and deal with puppet's special calling mechanism now:
30
+ if args[0].is_a?(Array)
31
+ args = args[0]
32
+ end
33
+
34
+ key = args[0]
35
+ default = args[1]
36
+ override = args[2]
37
+
38
+ require 'hiera'
39
+ require 'hiera/scope'
40
+
41
+ hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))
42
+
43
+ if self.respond_to?("[]")
44
+ hiera_scope = self
45
+ else
46
+ hiera_scope = Hiera::Scope.new(self)
47
+ end
48
+
49
+ answer = hiera.lookup(key, default, hiera_scope, override, :priority)
50
+
51
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.nil?
52
+
53
+ return answer
54
+ end
55
+ end
56
+
57
+ def register_function_hiera_array(spec)
58
+ Puppet::Parser::Functions.newfunction(:hiera_array, :type => :rvalue) do |*args|
59
+ if args[0].is_a?(Array)
60
+ args = args[0]
61
+ end
62
+
63
+ key = args[0]
64
+ default = args[1]
65
+ override = args[2]
66
+
67
+ require 'hiera'
68
+ require 'hiera/scope'
69
+
70
+ hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))
71
+
72
+ if self.respond_to?("[]")
73
+ hiera_scope = self
74
+ else
75
+ hiera_scope = Hiera::Scope.new(self)
76
+ end
77
+
78
+ answer = hiera.lookup(key, default, hiera_scope, override, :array)
79
+
80
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
81
+
82
+ answer
83
+ end
84
+ end
85
+
86
+ def register_function_hiera_hash(spec)
87
+ Puppet::Parser::Functions.newfunction(:hiera_hash, :type => :rvalue) do |*args|
88
+ if args[0].is_a?(Array)
89
+ args = args[0]
90
+ end
91
+
92
+ raise(Puppet::ParseError, "Please supply a parameter to perform a Hiera lookup") if args.empty?
93
+
94
+ key = args[0]
95
+ default = args[1]
96
+ override = args[2]
97
+
98
+ require 'hiera'
99
+ require 'hiera/scope'
100
+
101
+ hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))
102
+
103
+ if self.respond_to?("{}")
104
+ hiera_scope = self
105
+ else
106
+ hiera_scope = Hiera::Scope.new(self)
107
+ end
108
+
109
+ answer = hiera.lookup(key, default, hiera_scope, override, :hash)
110
+
111
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
112
+
113
+ answer
114
+ end
115
+ end
116
+
117
+ def register_function_hiera_include(spec)
118
+ Puppet::Parser::Functions.newfunction(:hiera_include) do |*args|
119
+ if args[0].is_a?(Array)
120
+ args = args[0]
121
+ end
122
+
123
+ key = args[0]
124
+ default = args[1]
125
+ override = args[2]
126
+
127
+ require 'hiera'
128
+ require 'hiera/scope'
129
+
130
+ hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))
131
+
132
+ if self.respond_to?("[]")
133
+ hiera_scope = self
134
+ else
135
+ hiera_scope = Hiera::Scope.new(self)
136
+ end
137
+
138
+ answer = hiera.lookup(key, default, hiera_scope, override, :array)
139
+
140
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
141
+
142
+ method = Puppet::Parser::Functions.function(:include)
143
+ send(method, answer)
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,24 @@
1
+ require 'rspec'
2
+
3
+ module RSpecHieraPuppet
4
+ module HieraDefaultConfiguration
5
+ extend RSpec::SharedContext
6
+
7
+ let(:hiera_config) do
8
+ { :backends => ['rspec'],
9
+ :rspec => respond_to?(:hiera_data) ? hiera_data : {} }
10
+ end
11
+ end
12
+ end
13
+
14
+ RSpec.configure do |c|
15
+ c.include(RSpecHieraPuppet::HieraDefaultConfiguration)
16
+
17
+ c.before(:each) do
18
+ Thread.current[:spec] = self
19
+ end
20
+
21
+ c.after(:each) do
22
+ Thread.current[:spec] = nil
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module Rspec
2
+ module Hiera
3
+ module Puppet
4
+ VERSION = "0.3.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rspec-hiera-puppet/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Michael Franz Aigner"]
6
+ gem.email = ["amfranz@gmail.com"]
7
+ gem.description = %q{Hiera fixtures for rspec-puppet tests.}
8
+ gem.summary = %q{Hiera fixtures for rspec-puppet tests.}
9
+ gem.homepage = "https://github.com/amfranz/rspec-hiera-puppet"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rspec-hiera-puppet"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rspec::Hiera::Puppet::VERSION
17
+
18
+ gem.add_dependency('puppet', '>= 2.7')
19
+ gem.add_dependency('hiera', '>= 0.3')
20
+ gem.add_dependency('hiera-puppet', '>= 0.3')
21
+ gem.add_dependency('rspec')
22
+ gem.add_dependency('rspec-puppet')
23
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-hiera-puppet
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Franz Aigner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: puppet
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 13
29
+ segments:
30
+ - 2
31
+ - 7
32
+ version: "2.7"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: hiera
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 13
44
+ segments:
45
+ - 0
46
+ - 3
47
+ version: "0.3"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: hiera-puppet
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 13
59
+ segments:
60
+ - 0
61
+ - 3
62
+ version: "0.3"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: rspec
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :runtime
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: rspec-puppet
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ type: :runtime
92
+ version_requirements: *id005
93
+ description: Hiera fixtures for rspec-puppet tests.
94
+ email:
95
+ - amfranz@gmail.com
96
+ executables: []
97
+
98
+ extensions: []
99
+
100
+ extra_rdoc_files: []
101
+
102
+ files:
103
+ - .gitignore
104
+ - Gemfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - lib/hiera/backend/rspec_backend.rb
109
+ - lib/rspec-hiera-puppet.rb
110
+ - lib/rspec-hiera-puppet/puppet.rb
111
+ - lib/rspec-hiera-puppet/rspec.rb
112
+ - lib/rspec-hiera-puppet/version.rb
113
+ - rspec-hiera-puppet.gemspec
114
+ homepage: https://github.com/amfranz/rspec-hiera-puppet
115
+ licenses: []
116
+
117
+ post_install_message:
118
+ rdoc_options: []
119
+
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 3
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ requirements: []
141
+
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.21
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: Hiera fixtures for rspec-puppet tests.
147
+ test_files: []
148
+