hiera-puppet 0.3.0 → 1.0.0rc2

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.
@@ -1,36 +1,8 @@
1
1
  module Puppet::Parser::Functions
2
- newfunction(:hiera_array, :type => :rvalue) do |*args|
3
- if args[0].is_a?(Array)
4
- args = args[0]
5
- end
6
-
7
- key = args[0]
8
- default = args[1]
9
- override = args[2]
10
-
11
- configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
12
-
13
- raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
14
- raise(Puppet::ParseError, "You need rubygems to use Hiera") unless Puppet.features.rubygems?
15
-
16
- require 'hiera'
17
- require 'hiera/scope'
18
-
19
- config = YAML.load_file(configfile)
20
- config[:logger] = "puppet"
21
-
22
- hiera = Hiera.new(:config => config)
23
-
24
- if self.respond_to?("[]")
25
- hiera_scope = self
26
- else
27
- hiera_scope = Hiera::Scope.new(self)
28
- end
29
-
30
- answer = hiera.lookup(key, default, hiera_scope, override, :array)
31
-
32
- raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
33
-
34
- answer
35
- end
2
+ newfunction(:hiera_array, :type => :rvalue) do |*args|
3
+ require 'hiera_puppet'
4
+ key, default, override = HieraPuppet.parse_args(args)
5
+ HieraPuppet.lookup(key, default, self, override, :array)
6
+ end
36
7
  end
8
+
@@ -1,36 +1,8 @@
1
1
  module Puppet::Parser::Functions
2
- newfunction(:hiera_hash, :type => :rvalue) do |*args|
3
- if args[0].is_a?(Array)
4
- args = args[0]
5
- end
6
-
7
- key = args[0]
8
- default = args[1]
9
- override = args[2]
10
-
11
- configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
12
-
13
- raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
14
- raise(Puppet::ParseError, "You need rubygems to use Hiera") unless Puppet.features.rubygems?
15
-
16
- require 'hiera'
17
- require 'hiera/scope'
18
-
19
- config = YAML.load_file(configfile)
20
- config[:logger] = "puppet"
21
-
22
- hiera = Hiera.new(:config => config)
23
-
24
- if self.respond_to?("{}")
25
- hiera_scope = self
26
- else
27
- hiera_scope = Hiera::Scope.new(self)
28
- end
29
-
30
- answer = hiera.lookup(key, default, hiera_scope, override, :hash)
31
-
32
- raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
33
-
34
- answer
35
- end
2
+ newfunction(:hiera_hash, :type => :rvalue) do |*args|
3
+ require 'hiera_puppet'
4
+ key, default, override = HieraPuppet.parse_args(args)
5
+ HieraPuppet.lookup(key, default, self, override, :hash)
6
+ end
36
7
  end
8
+
@@ -1,37 +1,11 @@
1
1
  module Puppet::Parser::Functions
2
- newfunction(:hiera_include) do |*args|
3
- if args[0].is_a?(Array)
4
- args = args[0]
5
- end
6
-
7
- key = args[0]
8
- default = args[1]
9
- override = args[2]
10
-
11
- configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
12
-
13
- raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
14
- raise(Puppet::ParseError, "You need rubygems to use Hiera") unless Puppet.features.rubygems?
15
-
16
- require 'hiera'
17
- require 'hiera/scope'
18
-
19
- config = YAML.load_file(configfile)
20
- config[:logger] = "puppet"
21
-
22
- hiera = Hiera.new(:config => config)
23
-
24
- if self.respond_to?("[]")
25
- hiera_scope = self
26
- else
27
- hiera_scope = Hiera::Scope.new(self)
28
- end
29
-
30
- answer = hiera.lookup(key, default, hiera_scope, override, :array)
31
-
32
- raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
33
-
34
- method = Puppet::Parser::Functions.function(:include)
35
- send(method, answer)
36
- end
2
+ newfunction(:hiera_include) do |*args|
3
+ require 'hiera_puppet'
4
+ key, default, override = HieraPuppet.parse_args(args)
5
+ answer = HieraPuppet.lookup(key, default, self, override, :array)
6
+
7
+ method = Puppet::Parser::Functions.function(:include)
8
+ send(method, answer)
9
+ end
37
10
  end
11
+
File without changes
@@ -1,20 +1,14 @@
1
- $:.insert(0, File.join([File.dirname(__FILE__), "..", "..", "lib"]))
1
+ $LOAD_PATH.insert(0, File.join([File.dirname(__FILE__), "..", "..", "lib"]))
2
2
 
3
3
  require 'rubygems'
4
4
  require 'rspec'
5
+ require 'rspec/mocks'
6
+ require 'hiera'
7
+ require 'puppetlabs_spec_helper/module_spec_helper'
8
+ require 'hiera_puppet'
5
9
  require 'hiera/backend/puppet_backend'
6
10
  require 'hiera/scope'
7
- require 'rspec/mocks'
8
- require 'mocha'
9
11
 
10
12
  RSpec.configure do |config|
11
- config.mock_with :mocha
13
+ config.mock_with :mocha
12
14
  end
13
-
14
- class Puppet
15
- class Parser
16
- class Functions
17
- end
18
- end
19
- end
20
-
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ class Hiera
4
+ module Backend
5
+ describe Puppet_backend do
6
+ before do
7
+ Hiera.stubs(:warn)
8
+ Hiera.stubs(:debug)
9
+ Backend.stubs(:datasources).yields([])
10
+ Puppet::Parser::Functions.stubs(:function).with(:include)
11
+
12
+ @mockresource = mock
13
+ @mockresource.stubs(:name).returns("ntp::config")
14
+
15
+ @mockscope = mock
16
+ @mockscope.stubs(:resource).returns(@mockresource)
17
+
18
+ @scope = Scope.new(@mockscope)
19
+
20
+ @backend = Puppet_backend.new
21
+ end
22
+
23
+ describe "#hierarchy" do
24
+ it "should use the configured datasource" do
25
+ Config.expects("[]").with(:puppet).returns({:datasource => "rspec"})
26
+ Config.expects("[]").with(:hierarchy)
27
+
28
+ ["ntp", "ntp::config"].each do |klass|
29
+ Backend.expects(:parse_string).with(klass, @scope, {"calling_module" => "ntp", "calling_class" => "ntp::config"}).returns(klass)
30
+ end
31
+
32
+ @backend.hierarchy(@scope, nil).should == ["rspec::ntp::config", "rspec::ntp", "ntp::config::rspec", "ntp::rspec"]
33
+ end
34
+
35
+ it "should not include empty class names" do
36
+ Config.expects("[]").with(:puppet).returns({:datasource => "rspec"})
37
+ Config.expects("[]").with(:hierarchy).returns(["%{foo}", "common"])
38
+
39
+ Backend.expects(:parse_string).with("common", @scope, {"calling_module" => "ntp", "calling_class" => "ntp::config"}).returns("common")
40
+ Backend.expects(:parse_string).with("%{foo}", @scope, {"calling_module" => "ntp", "calling_class" => "ntp::config"}).returns("")
41
+
42
+ @backend.hierarchy(@scope, nil).should == ["rspec::common", "ntp::config::rspec", "ntp::rspec"]
43
+ end
44
+
45
+ it "should allow for an override data source" do
46
+ Config.expects("[]").with(:puppet).returns({:datasource => "rspec"})
47
+ Config.expects("[]").with(:hierarchy)
48
+
49
+ ["ntp", "ntp::config"].each do |klass|
50
+ Backend.expects(:parse_string).with(klass, @scope, {"calling_module" => "ntp", "calling_class" => "ntp::config"}).returns(klass)
51
+ end
52
+
53
+ @backend.hierarchy(@scope, "override").should == ["rspec::override", "rspec::ntp::config", "rspec::ntp", "ntp::config::rspec", "ntp::rspec"]
54
+ end
55
+ end
56
+
57
+ describe "#lookup" do
58
+ it "should attempt to load data from unincluded classes" do
59
+ Backend.expects(:parse_answer).with("rspec", @scope).returns("rspec")
60
+
61
+ catalog = mock
62
+ catalog.expects(:classes).returns([])
63
+
64
+ @scope.expects(:catalog).returns(catalog)
65
+ @scope.expects(:function_include).with("rspec")
66
+ @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec")
67
+
68
+ @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec"])
69
+ @backend.lookup("key", @scope, nil, nil).should == "rspec"
70
+ end
71
+
72
+ it "should not load loaded classes" do
73
+ Backend.expects(:parse_answer).with("rspec", @scope).returns("rspec")
74
+ catalog = mock
75
+ catalog.expects(:classes).returns(["rspec"])
76
+ @mockscope.expects(:catalog).returns(catalog)
77
+ @mockscope.expects(:function_include).never
78
+ @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec")
79
+
80
+ @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec"])
81
+ @backend.lookup("key", @scope, nil, nil).should == "rspec"
82
+ end
83
+
84
+ it "should return the first found data" do
85
+ Backend.expects(:parse_answer).with("rspec", @scope).returns("rspec")
86
+ catalog = mock
87
+ catalog.expects(:classes).returns(["rspec", "override"])
88
+ @mockscope.expects(:catalog).returns(catalog)
89
+ @mockscope.expects(:function_include).never
90
+ @mockscope.expects(:lookupvar).with("override::key").returns("rspec")
91
+ @mockscope.expects(:lookupvar).with("rspec::key").never
92
+
93
+ @backend.expects(:hierarchy).with(@scope, "override").returns(["override", "rspec"])
94
+ @backend.lookup("key", @scope, "override", nil).should == "rspec"
95
+ end
96
+
97
+ it "should return an array of found data for array searches" do
98
+ Backend.expects(:parse_answer).with("rspec::key", @scope).returns("rspec::key")
99
+ Backend.expects(:parse_answer).with("test::key", @scope).returns("test::key")
100
+ catalog = mock
101
+ catalog.expects(:classes).returns(["rspec", "test"])
102
+ @mockscope.expects(:catalog).returns(catalog)
103
+ @mockscope.expects(:function_include).never
104
+ @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec::key")
105
+ @mockscope.expects(:lookupvar).with("test::key").returns("test::key")
106
+
107
+ @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec", "test"])
108
+ @backend.lookup("key", @scope, nil, :array).should == ["rspec::key", "test::key"]
109
+ end
110
+
111
+
112
+ it "should return a hash of found data for hash searches" do
113
+ Backend.expects(:parse_answer).with("rspec::key", @scope).returns({'rspec'=>'key'})
114
+ Backend.expects(:parse_answer).with("test::key", @scope).returns({'test'=>'key'})
115
+ catalog = mock
116
+ catalog.expects(:classes).returns(["rspec", "test"])
117
+ @mockscope.expects(:catalog).returns(catalog)
118
+ @mockscope.expects(:function_include).never
119
+ @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec::key")
120
+ @mockscope.expects(:lookupvar).with("test::key").returns("test::key")
121
+
122
+ @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec", "test"])
123
+ @backend.lookup("key", @scope, nil, :hash).should == {'rspec'=>'key', 'test'=>'key'}
124
+ end
125
+
126
+ it "should return a merged hash of found data for hash searches" do
127
+ Backend.expects(:parse_answer).with("rspec::key", @scope).returns({'rspec'=>'key', 'common'=>'rspec'})
128
+ Backend.expects(:parse_answer).with("test::key", @scope).returns({'test'=>'key', 'common'=>'rspec'})
129
+ catalog = mock
130
+ catalog.expects(:classes).returns(["rspec", "test"])
131
+ @mockscope.expects(:catalog).returns(catalog)
132
+ @mockscope.expects(:function_include).never
133
+ @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec::key")
134
+ @mockscope.expects(:lookupvar).with("test::key").returns("test::key")
135
+
136
+ @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec", "test"])
137
+ @backend.lookup("key", @scope, nil, :hash).should == {'rspec'=>'key', 'common'=>'rspec', 'test'=>'key'}
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ class Hiera
4
+ describe Scope do
5
+ describe "#initialize" do
6
+ it "should store the supplied puppet scope" do
7
+ real = {}
8
+ scope = Scope.new(real)
9
+ scope.real.should == real
10
+ end
11
+ end
12
+
13
+ describe "#[]" do
14
+ it "should treat '' as nil" do
15
+ real = mock
16
+ real.expects(:lookupvar).with("foo").returns("")
17
+
18
+ scope = Scope.new(real)
19
+ scope["foo"].should == nil
20
+ end
21
+
22
+ it "sould return found data" do
23
+ real = mock
24
+ real.expects(:lookupvar).with("foo").returns("bar")
25
+
26
+ scope = Scope.new(real)
27
+ scope["foo"].should == "bar"
28
+ end
29
+
30
+ it "should get calling_class and calling_module from puppet scope" do
31
+ real = mock
32
+ resource = mock
33
+ resource.expects(:name).returns("Foo::Bar").twice
34
+
35
+ real.expects(:resource).returns(resource).twice
36
+
37
+ scope = Scope.new(real)
38
+ scope["calling_class"].should == "foo::bar"
39
+ scope["calling_module"].should == "foo"
40
+ end
41
+ end
42
+
43
+ describe "#include?" do
44
+ it "should correctly report missing data" do
45
+ real = mock
46
+ real.expects(:lookupvar).with("foo").returns("")
47
+
48
+ scope = Scope.new(real)
49
+ scope.include?("foo").should == false
50
+ end
51
+
52
+ it "should always return true for calling_class and calling_module" do
53
+ real = mock
54
+ real.expects(:lookupvar).with("calling_class").never
55
+ real.expects(:lookupvar).with("calling_module").never
56
+
57
+ scope = Scope.new(real)
58
+ scope.include?("calling_class").should == true
59
+ scope.include?("calling_module").should == true
60
+ end
61
+ end
62
+ end
63
+ end
64
+
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'HieraPuppet' do
4
+ describe 'HieraPuppet#hiera_config' do
5
+ let(:hiera_config_data) do
6
+ { :backend => 'yaml' }
7
+ end
8
+
9
+ context "when the hiera_config_file exists" do
10
+ before do
11
+ Hiera::Config.expects(:load).returns(hiera_config_data)
12
+ HieraPuppet.expects(:hiera_config_file).returns(true)
13
+ end
14
+
15
+ it "should return a configuration hash" do
16
+ expected_results = {
17
+ :backend => 'yaml',
18
+ :logger => 'puppet'
19
+ }
20
+ HieraPuppet.send(:hiera_config).should == expected_results
21
+ end
22
+ end
23
+
24
+ context "when the hiera_config_file does not exist" do
25
+ before do
26
+ Hiera::Config.expects(:load).never
27
+ HieraPuppet.expects(:hiera_config_file).returns(nil)
28
+ end
29
+
30
+ it "should return a configuration hash" do
31
+ HieraPuppet.send(:hiera_config).should == { :logger => 'puppet' }
32
+ end
33
+ end
34
+ end
35
+
36
+ describe 'HieraPuppet#hiera_config_file' do
37
+ it "should return nil when we cannot derive the hiera config file form Puppet.settings" do
38
+ begin
39
+ Puppet.settings[:hiera_config] = nil
40
+ rescue ArgumentError => detail
41
+ raise unless detail.message =~ /unknown configuration parameter/
42
+ end
43
+ HieraPuppet.send(:hiera_config_file).should be_nil
44
+ end
45
+
46
+ it "should use Puppet.settings[:hiera_config] as the hiera config file" do
47
+ begin
48
+ Puppet.settings[:hiera_config] = "/dev/null/my_hiera.yaml"
49
+ rescue ArgumentError => detail
50
+ raise unless detail.message =~ /unknown configuration parameter/
51
+ pending("This example does not apply to Puppet #{Puppet.version} because it does not have this setting")
52
+ end
53
+
54
+ File.stubs(:exist?).with("/dev/null/my_hiera.yaml").returns(true)
55
+ HieraPuppet.send(:hiera_config_file).should == '/dev/null/my_hiera.yaml'
56
+ end
57
+
58
+ it "should use Puppet.settings[:confdir] as the base directory when hiera_config is not set" do
59
+ begin
60
+ Puppet.settings[:hiera_config] = nil
61
+ rescue ArgumentError => detail
62
+ raise unless detail.message =~ /unknown configuration parameter/
63
+ end
64
+ Puppet.settings[:confdir] = "/dev/null/puppet"
65
+ File.stubs(:exist?).with('/dev/null/puppet/hiera.yaml').returns(true)
66
+
67
+ HieraPuppet.send(:hiera_config_file).should == '/dev/null/puppet/hiera.yaml'
68
+ end
69
+ end
70
+
71
+ describe 'HieraPuppet#lookup' do
72
+ let(:scope) { PuppetlabsSpec::PuppetSeams.parser_scope }
73
+
74
+ it "should return the value from Hiera" do
75
+ Hiera.any_instance.stubs(:lookup).returns('8080')
76
+ HieraPuppet.lookup('port', nil, scope, nil, :priority).should == '8080'
77
+
78
+ Hiera.any_instance.stubs(:lookup).returns(['foo', 'bar'])
79
+ HieraPuppet.lookup('ntpservers', nil, scope, nil, :array).should == ['foo', 'bar']
80
+
81
+ Hiera.any_instance.stubs(:lookup).returns({'uid' => '1000'})
82
+ HieraPuppet.lookup('user', nil, scope, nil, :hash).should == {'uid' => '1000'}
83
+ end
84
+
85
+ it "should raise a useful error when the answer is nil" do
86
+ Hiera.any_instance.stubs(:lookup).returns(nil)
87
+ expect do
88
+ HieraPuppet.lookup('port', nil, scope, nil, :priority)
89
+ end.to raise_error(Puppet::ParseError,
90
+ /Could not find data item port in any Hiera data file and no default supplied/)
91
+ end
92
+ end
93
+
94
+ describe 'HieraPuppet#parse_args' do
95
+ it 'should return a 3 item array' do
96
+ args = ['foo', '8080', nil, nil]
97
+ HieraPuppet.parse_args(args).should == ['foo', '8080', nil]
98
+ end
99
+
100
+ it 'should raise a useful error when no key is supplied' do
101
+ expect { HieraPuppet.parse_args([]) }.to raise_error(Puppet::ParseError,
102
+ /Please supply a parameter to perform a Hiera lookup/)
103
+ end
104
+ end
105
+ end