hiera-puppet 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,13 @@ class Hiera
7
7
  end
8
8
 
9
9
  def [](key)
10
- ans = @real.lookupvar(key)
10
+ if key == "calling_class"
11
+ ans = @real.resource.name.to_s.downcase
12
+ elsif key == "calling_module"
13
+ ans = @real.resource.name.to_s.downcase.split("::").first
14
+ else
15
+ ans = @real.lookupvar(key)
16
+ end
11
17
 
12
18
  # damn you puppet visual basic style variables.
13
19
  return nil if ans == ""
@@ -15,7 +21,9 @@ class Hiera
15
21
  end
16
22
 
17
23
  def include?(key)
18
- @real.lookupvar(key) != ""
24
+ return true if ["calling_class", "calling_module"].include?(key)
25
+
26
+ return @real.lookupvar(key) != ""
19
27
  end
20
28
 
21
29
  def catalog
@@ -30,4 +38,4 @@ class Hiera
30
38
  @real.compiler
31
39
  end
32
40
  end
33
- end
41
+ end
@@ -13,9 +13,9 @@ module Puppet::Parser::Functions
13
13
  args = args[0]
14
14
  end
15
15
 
16
- key = args[0] || nil
17
- default = args[1] || nil
18
- override = args[2] || nil
16
+ key = args[0]
17
+ default = args[1]
18
+ override = args[2]
19
19
 
20
20
  configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
21
21
 
@@ -38,7 +38,7 @@ module Puppet::Parser::Functions
38
38
 
39
39
  answer = hiera.lookup(key, default, hiera_scope, override, :priority)
40
40
 
41
- raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") unless answer
41
+ raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.nil?
42
42
 
43
43
  return answer
44
44
  end
@@ -4,9 +4,9 @@ module Puppet::Parser::Functions
4
4
  args = args[0]
5
5
  end
6
6
 
7
- key = args[0] || nil
8
- default = args[1] || nil
9
- override = args[2] || nil
7
+ key = args[0]
8
+ default = args[1]
9
+ override = args[2]
10
10
 
11
11
  configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
12
12
 
@@ -0,0 +1,36 @@
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
36
+ end
@@ -4,9 +4,9 @@ module Puppet::Parser::Functions
4
4
  args = args[0]
5
5
  end
6
6
 
7
- key = args[0] || nil
8
- default = args[1] || nil
9
- override = args[2] || nil
7
+ key = args[0]
8
+ default = args[1]
9
+ override = args[2]
10
10
 
11
11
  configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
12
12
 
@@ -0,0 +1,20 @@
1
+ $:.insert(0, File.join([File.dirname(__FILE__), "..", "..", "lib"]))
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+ require 'hiera/backend/puppet_backend'
6
+ require 'hiera/scope'
7
+ require 'rspec/mocks'
8
+ require 'mocha'
9
+
10
+ RSpec.configure do |config|
11
+ config.mock_with :mocha
12
+ end
13
+
14
+ class Puppet
15
+ class Parser
16
+ class Functions
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,118 @@
1
+ require File.dirname(__FILE__) + '/../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
+
56
+ describe "#lookup" do
57
+ it "should attempt to load data from unincluded classes" do
58
+ Backend.expects(:empty_answer).returns(nil)
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(:empty_answer).returns(nil)
74
+ Backend.expects(:parse_answer).with("rspec", @scope).returns("rspec")
75
+ catalog = mock
76
+ catalog.expects(:classes).returns(["rspec"])
77
+ @mockscope.expects(:catalog).returns(catalog)
78
+ @mockscope.expects(:function_include).never
79
+ @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec")
80
+
81
+ @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec"])
82
+ @backend.lookup("key", @scope, nil, nil).should == "rspec"
83
+ end
84
+
85
+ it "should return the first found data" do
86
+ Backend.expects(:empty_answer).returns(nil)
87
+ Backend.expects(:parse_answer).with("rspec", @scope).returns("rspec")
88
+ catalog = mock
89
+ catalog.expects(:classes).returns(["rspec", "override"])
90
+ @mockscope.expects(:catalog).returns(catalog)
91
+ @mockscope.expects(:function_include).never
92
+ @mockscope.expects(:lookupvar).with("override::key").returns("rspec")
93
+ @mockscope.expects(:lookupvar).with("rspec::key").never
94
+
95
+ @backend.expects(:hierarchy).with(@scope, "override").returns(["override", "rspec"])
96
+ @backend.lookup("key", @scope, "override", nil).should == "rspec"
97
+ end
98
+
99
+ it "should return an array of found data for array searches" do
100
+ Backend.expects(:empty_answer).returns([])
101
+ Backend.expects(:parse_answer).with("rspec::key", @scope).returns("rspec::key")
102
+ Backend.expects(:parse_answer).with("test::key", @scope).returns("test::key")
103
+ catalog = mock
104
+ catalog.expects(:classes).returns(["rspec", "test"])
105
+ @mockscope.expects(:catalog).returns(catalog)
106
+ @mockscope.expects(:function_include).never
107
+ @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec::key")
108
+ @mockscope.expects(:lookupvar).with("test::key").returns("test::key")
109
+
110
+ @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec", "test"])
111
+ @backend.lookup("key", @scope, nil, :array).should == ["rspec::key", "test::key"]
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+
@@ -0,0 +1,64 @@
1
+ require File.dirname(__FILE__) + '/../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
+
metadata CHANGED
@@ -1,38 +1,40 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-puppet
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 19
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
- - 2
8
+ - 3
8
9
  - 0
9
- version: 0.2.0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
- - R.I.Pienaar
13
+ - Puppet Labs
13
14
  autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-06-12 00:00:00 +01:00
18
- default_executable:
18
+ date: 2012-01-12 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: hiera
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ~>
26
27
  - !ruby/object:Gem::Version
28
+ hash: 19
27
29
  segments:
28
30
  - 0
29
- - 2
31
+ - 3
30
32
  - 0
31
- version: 0.2.0
33
+ version: 0.3.0
32
34
  type: :runtime
33
35
  version_requirements: *id001
34
36
  description: Store and query Hiera data from Puppet
35
- email: rip@devco.net
37
+ email: info@puppetlabs.com
36
38
  executables:
37
39
  - extlookup2hiera
38
40
  extensions: []
@@ -41,13 +43,16 @@ extra_rdoc_files: []
41
43
 
42
44
  files:
43
45
  - bin/extlookup2hiera
44
- - lib/puppet/parser/functions/hiera_array.rb
46
+ - lib/hiera/backend/puppet_backend.rb
47
+ - lib/hiera/scope.rb
45
48
  - lib/puppet/parser/functions/hiera.rb
49
+ - lib/puppet/parser/functions/hiera_array.rb
50
+ - lib/puppet/parser/functions/hiera_hash.rb
46
51
  - lib/puppet/parser/functions/hiera_include.rb
47
- - lib/hiera/scope.rb
48
- - lib/hiera/backend/puppet_backend.rb
49
- has_rdoc: true
50
- homepage: https://github.com/ripienaar/hiera-puppet
52
+ - spec/spec_helper.rb
53
+ - spec/unit/puppet_backend_spec.rb
54
+ - spec/unit/scope_spec.rb
55
+ homepage: https://github.com/puppetlabs/hiera-puppet
51
56
  licenses: []
52
57
 
53
58
  post_install_message:
@@ -56,25 +61,31 @@ rdoc_options: []
56
61
  require_paths:
57
62
  - lib
58
63
  required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
59
65
  requirements:
60
66
  - - ">="
61
67
  - !ruby/object:Gem::Version
68
+ hash: 3
62
69
  segments:
63
70
  - 0
64
71
  version: "0"
65
72
  required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
66
74
  requirements:
67
75
  - - ">="
68
76
  - !ruby/object:Gem::Version
77
+ hash: 3
69
78
  segments:
70
79
  - 0
71
80
  version: "0"
72
81
  requirements: []
73
82
 
74
83
  rubyforge_project:
75
- rubygems_version: 1.3.6
84
+ rubygems_version: 1.8.10
76
85
  signing_key:
77
86
  specification_version: 3
78
87
  summary: Puppet query interface and backend for Hiera
79
- test_files: []
80
-
88
+ test_files:
89
+ - spec/spec_helper.rb
90
+ - spec/unit/puppet_backend_spec.rb
91
+ - spec/unit/scope_spec.rb