hiera-puppet-helper 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ metadata.gz: 6865edd07321653bcbd6d79820e025cb123ad18990468c1d28057df1886b2aa9e1430ee695c0e9bb6b10ac3049915de58c48f9ce248225d1c25fd3c5e2b32f2e
4
+ data.tar.gz: cd834a8a545bced7409efa40f624acdd959a51b8d42c14ebcc53870b01c24587de1cf8eda807ab633dd38653889ed2c332c839a6e7488a963e986eac39637568
5
+ SHA1:
6
+ metadata.gz: 71e91d3a9d2fdda500f25ac85e0567c1253f13c8
7
+ data.tar.gz: 022b038e0166688d4fa7e655f10f6dfc9ca603c9
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,131 @@
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-puppet'
110
+
111
+ fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
112
+
113
+ shared_context "hieradata" do
114
+ let(:hiera_config) do
115
+ { :backends => ['rspec', 'yaml'],
116
+ :hierarchy => [
117
+ '%{fqdn}/%{calling_module}',
118
+ '%{calling_module}'],
119
+ :yaml => {
120
+ :datadir => File.join(fixture_path, 'hieradata') },
121
+ :rspec => respond_to?(:hiera_data) ? hiera_data : {} }
122
+ end
123
+ end
124
+
125
+ _spec/classes/example\_spec.rb_
126
+
127
+ describe "example" do
128
+ include_context "hieradata"
129
+
130
+ it { should contain_notify("foo").with_message("bar") }
131
+ end
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hiera-puppet-helper/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Maarten Thibaut"]
6
+ gem.email = ["mthibaut@cisco.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/mthibaut/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 = "hiera-puppet-helper"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Rspec::Hiera::Puppet::VERSION
17
+
18
+ gem.add_dependency('puppet', '>= 3.0')
19
+ gem.add_dependency('hiera', '>= 1.0')
20
+ gem.add_dependency('hiera-puppet', '>= 1.0')
21
+ gem.add_dependency('rspec')
22
+ gem.add_dependency('rspec-puppet')
23
+ end
@@ -0,0 +1,3 @@
1
+ require 'hiera-puppet-helper/version'
2
+ require 'hiera-puppet-helper/puppet'
3
+ require 'hiera-puppet-helper/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 HieraPuppetHelper
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(HieraPuppetHelper::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 = "1.0.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,60 @@
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 = nil
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
+ # Extra logging that we found the key. This can be outputted
30
+ # multiple times if the resolution type is array or hash but that
31
+ # should be expected as the logging will then tell the user ALL the
32
+ # places where the key is found.
33
+ Hiera.debug("Found #{key} in #{source}")
34
+
35
+ # for array resolution we just append to the array whatever
36
+ # we find, we then goes onto the next file and keep adding to
37
+ # the array
38
+ #
39
+ # for priority searches we break after the first found data item
40
+ new_answer = Backend.parse_answer(raw_answer, scope)
41
+ case resolution_type
42
+ when :array
43
+ raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
44
+ answer ||= []
45
+ answer << new_answer
46
+ when :hash
47
+ raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
48
+ answer ||= {}
49
+ answer = new_answer.merge answer
50
+ else
51
+ answer = new_answer
52
+ break
53
+ end
54
+ end
55
+
56
+ return answer
57
+ end
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-puppet-helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Maarten Thibaut
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2013-04-03 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: puppet
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: "3.0"
22
+ type: :runtime
23
+ version_requirements: *id001
24
+ - !ruby/object:Gem::Dependency
25
+ name: hiera
26
+ prerelease: false
27
+ requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "1.0"
32
+ type: :runtime
33
+ version_requirements: *id002
34
+ - !ruby/object:Gem::Dependency
35
+ name: hiera-puppet
36
+ prerelease: false
37
+ requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "1.0"
42
+ type: :runtime
43
+ version_requirements: *id003
44
+ - !ruby/object:Gem::Dependency
45
+ name: rspec
46
+ prerelease: false
47
+ requirement: &id004 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - &id005
50
+ - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ type: :runtime
54
+ version_requirements: *id004
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-puppet
57
+ prerelease: false
58
+ requirement: &id006 !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - *id005
61
+ type: :runtime
62
+ version_requirements: *id006
63
+ description: Hiera fixtures for rspec-puppet tests.
64
+ email:
65
+ - mthibaut@cisco.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .gitignore
74
+ - Gemfile
75
+ - LICENSE
76
+ - README.md
77
+ - Rakefile
78
+ - hiera-puppet-helper.gemspec
79
+ - lib/hiera-puppet-helper.rb
80
+ - lib/hiera-puppet-helper/puppet.rb
81
+ - lib/hiera-puppet-helper/rspec.rb
82
+ - lib/hiera-puppet-helper/version.rb
83
+ - lib/hiera/backend/rspec_backend.rb
84
+ homepage: https://github.com/mthibaut/rspec-hiera-puppet
85
+ licenses: []
86
+
87
+ metadata: {}
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - *id005
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - *id005
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 2.0.3
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Hiera fixtures for rspec-puppet tests.
107
+ test_files: []
108
+