hiera 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of hiera might be problematic. Click here for more details.

data/bin/hiera CHANGED
@@ -1,4 +1,4 @@
1
- #!/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  # CLI client for Hiera.
4
4
  #
@@ -0,0 +1 @@
1
+ --format s --colour --backtrace
@@ -0,0 +1,19 @@
1
+ $:.insert(0, File.join([File.dirname(__FILE__), "..", "..", "lib"]))
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+ require 'hiera'
6
+ require 'rspec/mocks'
7
+ require 'mocha'
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_with :mocha
11
+ end
12
+
13
+ class Puppet
14
+ class Parser
15
+ class Functions
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ require 'hiera/backend/yaml_backend'
4
+
5
+ class Hiera
6
+ module Backend
7
+ describe Yaml_backend do
8
+ describe "#initialize" do
9
+ it "should announce its creation" do # because other specs checks this
10
+ Hiera.expects(:debug).with("Hiera YAML backend starting")
11
+ Yaml_backend.new
12
+ end
13
+ end
14
+
15
+ describe "#lookup" do
16
+ before do
17
+ Hiera.stubs(:debug)
18
+ Hiera.stubs(:warn)
19
+ @backend = Yaml_backend.new
20
+ end
21
+
22
+ it "should fail for missing datadir" do
23
+ Backend.expects(:datadir).with(:yaml, {}).returns("/nonexisting")
24
+
25
+ expect {
26
+ @backend.lookup("key", {}, nil, nil)
27
+ }.to raise_error("Cannot find data directory /nonexisting")
28
+ end
29
+
30
+ it "should look for data in all sources" do
31
+ Backend.expects(:datadir).with(:yaml, {}).returns("/nonexisting")
32
+ File.expects(:directory?).with("/nonexisting").returns(true)
33
+ Backend.expects(:datasources).multiple_yields(["one"], ["two"])
34
+ File.expects(:exist?).with("/nonexisting/one.yaml").returns(false)
35
+ File.expects(:exist?).with("/nonexisting/two.yaml").returns(false)
36
+ @backend.lookup("key", {}, nil, nil)
37
+ end
38
+
39
+ it "should pick data earliest source that has it" do
40
+ Backend.expects(:datadir).with(:yaml, {}).returns("/nonexisting")
41
+ File.expects(:directory?).with("/nonexisting").returns(true)
42
+ Backend.expects(:datasources).multiple_yields(["one"], ["two"])
43
+ File.expects(:exist?).with("/nonexisting/one.yaml").returns(true)
44
+ YAML.expects(:load_file).with("/nonexisting/one.yaml").returns({"key" => "answer"})
45
+
46
+ File.expects(:exist?).with("/nonexisting/two.yaml").never
47
+
48
+ @backend.lookup("key", {}, nil, nil).should == "answer"
49
+ end
50
+
51
+ it "should parse the answer for scope variables" do
52
+ Backend.expects(:datadir).with(:yaml, {"rspec" => "test"}).returns("/nonexisting")
53
+ File.expects(:directory?).with("/nonexisting").returns(true)
54
+ Backend.expects(:datasources).yields("one")
55
+ File.expects(:exist?).with("/nonexisting/one.yaml").returns(true)
56
+ YAML.expects(:load_file).with("/nonexisting/one.yaml").returns({"key" => "test_%{rspec}"})
57
+
58
+ @backend.lookup("key", {"rspec" => "test"}, nil, nil).should == "test_test"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,158 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class Hiera
4
+ describe Backend do
5
+ describe "#datadir" do
6
+ it "should use the backend configured dir" do
7
+ Config.load({:rspec => {:datadir => "/tmp"}})
8
+ Backend.expects(:parse_string).with("/tmp", {})
9
+ Backend.datadir(:rspec, {})
10
+ end
11
+
12
+ it "should default to /var/lib/hiera" do
13
+ Config.load({})
14
+ Backend.expects(:parse_string).with("/var/lib/hiera", {})
15
+ Backend.datadir(:rspec, {})
16
+ end
17
+ end
18
+
19
+ describe "#datasources" do
20
+ it "should use the supplied hierarchy" do
21
+ expected = ["one", "two"]
22
+ Backend.datasources({}, nil, ["one", "two"]) do |backend|
23
+ backend.should == expected.delete_at(0)
24
+ end
25
+
26
+ expected.empty?.should == true
27
+ end
28
+
29
+ it "should use the configured hierarchy if none is supplied" do
30
+ Config.load(:hierarchy => "test")
31
+
32
+ Backend.datasources({}) do |backend|
33
+ backend.should == "test"
34
+ end
35
+ end
36
+
37
+ it "should default to common if not configured or supplied" do
38
+ Config.load({})
39
+
40
+ Backend.datasources({}) do |backend|
41
+ backend.should == "common"
42
+ end
43
+ end
44
+
45
+ it "should insert the override if provided" do
46
+ Config.load({})
47
+
48
+ expected = ["override", "common"]
49
+ Backend.datasources({}, "override") do |backend|
50
+ backend.should == expected.delete_at(0)
51
+ end
52
+
53
+ expected.empty?.should == true
54
+ end
55
+
56
+ it "should parse the sources based on scope" do
57
+ Backend.expects(:parse_string).with("common", {:rspec => :tests})
58
+ Backend.datasources({:rspec => :tests}) { }
59
+ end
60
+
61
+ it "should not return empty sources" do
62
+ Config.load({})
63
+
64
+ expected = ["common"]
65
+ Backend.datasources({}, "%{rspec}") do |backend|
66
+ backend.should == expected.delete_at(0)
67
+ end
68
+
69
+ expected.empty?.should == true
70
+ end
71
+ end
72
+
73
+ describe "#parse_string" do
74
+ it "should not try to parse invalid data" do
75
+ Backend.parse_string(nil, {}).should == nil
76
+ end
77
+
78
+ it "should clone the supplied data" do
79
+ data = ""
80
+ data.expects(:clone).returns("")
81
+ Backend.parse_string(data, {})
82
+ end
83
+
84
+ it "should only parse string data" do
85
+ data = ""
86
+ data.expects(:is_a?).with(String)
87
+ Backend.parse_string(data, {})
88
+ end
89
+
90
+ it "should match data from scope" do
91
+ input = "test_%{rspec}_test"
92
+ Backend.parse_string(input, {"rspec" => "test"}).should == "test_test_test"
93
+ end
94
+
95
+ it "should match data from extra_data" do
96
+ input = "test_%{rspec}_test"
97
+ Backend.parse_string(input, {}, {"rspec" => "test"}).should == "test_test_test"
98
+ end
99
+
100
+ it "should prefer scope over extra_data" do
101
+ input = "test_%{rspec}_test"
102
+ Backend.parse_string(input, {"rspec" => "test"}, {"rspec" => "fail"}).should == "test_test_test"
103
+ end
104
+ end
105
+
106
+ describe "#lookup" do
107
+ before do
108
+ Hiera.stubs(:debug)
109
+ Hiera.stubs(:warn)
110
+ end
111
+
112
+ it "should cache backends" do
113
+ Hiera.stubs(:debug)
114
+ Hiera.expects(:debug).with(regexp_matches(/Hiera YAML backend starting/)).once
115
+
116
+ Config.load({:yaml => {:datadir => "/tmp"}})
117
+ Config.load_backends
118
+
119
+ Backend.lookup("key", "default", {}, nil, nil)
120
+ Backend.lookup("key", "default", {}, nil, nil)
121
+ end
122
+
123
+ it "should return the answer from the backend" do
124
+ Hiera.stubs(:debug)
125
+ Config.load({:yaml => {:datadir => "/tmp"}})
126
+ Config.load_backends
127
+
128
+ Backend::Yaml_backend.any_instance.expects(:lookup).with("key", {}, nil, nil).returns("answer")
129
+
130
+ Backend.lookup("key", "default", {}, nil, nil).should == "answer"
131
+ end
132
+
133
+ it "should call to all backends till an answer is found" do
134
+ backend = mock
135
+ backend.expects(:lookup).returns("answer")
136
+ Hiera.stubs(:debug)
137
+ Config.load({})
138
+ Config.instance_variable_set("@config", {:backends => ["yaml", "rspec"]})
139
+ Backend.instance_variable_set("@backends", {"rspec" => backend})
140
+ Backend::Yaml_backend.any_instance.expects(:lookup).with("key", {"rspec" => "test"}, nil, nil)
141
+ Backend.expects(:constants).returns(["Yaml_backend", "Rspec_backend"]).twice
142
+
143
+ Backend.lookup("key", "test_%{rspec}", {"rspec" => "test"}, nil, nil).should == "answer"
144
+
145
+ end
146
+
147
+ it "should return the default with variables parsed if nothing is found" do
148
+ Hiera.stubs(:debug)
149
+ Config.load({:yaml => {:datadir => "/tmp"}})
150
+ Config.load_backends
151
+
152
+ Backend::Yaml_backend.any_instance.expects(:lookup).with("key", {"rspec" => "test"}, nil, nil)
153
+
154
+ Backend.lookup("key", "test_%{rspec}", {"rspec" => "test"}, nil, nil).should == "test_test"
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,70 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class Hiera
4
+ describe Config do
5
+ describe "#load" do
6
+ it "should treat string sources as a filename" do
7
+ expect {
8
+ Config.load("/nonexisting")
9
+ }.to raise_error("Config file /nonexisting not found")
10
+ end
11
+
12
+ it "should attempt to YAML load config files" do
13
+ File.expects(:exist?).with("/nonexisting").returns(true)
14
+ YAML.expects(:load_file).with("/nonexisting").returns({})
15
+
16
+ Config.load("/nonexisting")
17
+ end
18
+
19
+ it "should use hash data as source if supplied" do
20
+ config = Config.load({"rspec" => "test"})
21
+ config["rspec"].should == "test"
22
+ end
23
+
24
+ it "should merge defaults with the loaded or supplied config" do
25
+ config = Config.load({})
26
+ config.should == {:backends => ["yaml"], :hierarchy => "common", :logger => "console"}
27
+ end
28
+
29
+ it "should force :backends to be a flattened array" do
30
+ Config.load({:backends => [["foo", ["bar"]]]}).should == {:backends => ["foo", "bar"], :hierarchy => "common", :logger => "console"}
31
+ end
32
+
33
+ it "should load the supplied logger" do
34
+ Hiera.expects(:logger=).with("foo")
35
+ Config.load({:logger => "foo"})
36
+ end
37
+
38
+ it "should default to the console logger" do
39
+ Hiera.expects(:logger=).with("console")
40
+ Config.load({})
41
+ end
42
+ end
43
+
44
+ describe "#load_backends" do
45
+ it "should load each backend" do
46
+ Config.load(:backends => ["One", "Two"])
47
+ Config.expects(:require).with("hiera/backend/one_backend")
48
+ Config.expects(:require).with("hiera/backend/two_backend")
49
+ Config.load_backends
50
+ end
51
+
52
+ it "should warn if it cant load a backend" do
53
+ Config.load(:backends => ["one"])
54
+ Config.expects(:require).with("hiera/backend/one_backend").raises("fail")
55
+
56
+ expect {
57
+ Config.load_backends
58
+ }.to raise_error("fail")
59
+ end
60
+ end
61
+
62
+ describe "#include?" do
63
+ it "should correctly report inclusion" do
64
+ Config.load({})
65
+ Config.include?(:foo).should == false
66
+ Config.include?(:logger).should == true
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class Hiera
4
+ describe Console_logger do
5
+ describe "#warn" do
6
+ it "should warn to STDERR" do
7
+ STDERR.expects(:puts).with("WARN: 0: foo")
8
+ Time.expects(:now).returns(0)
9
+ Console_logger.warn("foo")
10
+ end
11
+
12
+ it "should debug to STDERR" do
13
+ STDERR.expects(:puts).with("DEBUG: 0: foo")
14
+ Time.expects(:now).returns(0)
15
+ Console_logger.debug("foo")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Hiera" do
4
+ describe "#logger=" do
5
+ it "should attempt to load the supplied logger" do
6
+ Hiera.stubs(:warn)
7
+ Hiera.expects(:require).with("hiera/foo_logger").raises("fail")
8
+ Hiera.logger = "foo"
9
+ end
10
+
11
+ it "should fall back to the Console logger on failure" do
12
+ Hiera.expects(:warn).with("Failed to load foo logger: LoadError: no such file to load -- hiera/foo_logger")
13
+ Hiera.logger = "foo"
14
+ end
15
+ end
16
+
17
+ describe "#warn" do
18
+ it "should call the supplied logger" do
19
+ Hiera::Console_logger.expects(:warn).with("rspec")
20
+ Hiera.warn("rspec")
21
+ end
22
+ end
23
+
24
+ describe "#debug" do
25
+ it "should call the supplied logger" do
26
+ Hiera::Console_logger.expects(:debug).with("rspec")
27
+ Hiera.debug("rspec")
28
+ end
29
+ end
30
+
31
+ describe "#initialize" do
32
+ it "should default to /etc/hiera.yaml for config" do
33
+ Hiera::Config.expects(:load).with("/etc/hiera.yaml")
34
+ Hiera::Config.stubs(:load_backends)
35
+ Hiera.new
36
+ end
37
+
38
+ it "should pass the supplied config to the config class" do
39
+ Hiera::Config.expects(:load).with({"test" => "rspec"})
40
+ Hiera::Config.stubs(:load_backends)
41
+ Hiera.new(:config => {"test" => "rspec"})
42
+ end
43
+
44
+ it "should load all backends on start" do
45
+ Hiera::Config.stubs(:load)
46
+ Hiera::Config.expects(:load_backends)
47
+ Hiera.new
48
+ end
49
+ end
50
+
51
+ describe "#lookup" do
52
+ it "should proxy to the Backend#lookup method" do
53
+ Hiera::Config.stubs(:load)
54
+ Hiera::Config.stubs(:load_backends)
55
+ Hiera::Backend.expects(:lookup).with(:key, :default, :scope, :order_override, :resolution_type)
56
+ Hiera.new.lookup(:key, :default, :scope, :order_override, :resolution_type)
57
+ end
58
+ end
59
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - R.I.Pienaar
@@ -34,7 +34,7 @@ files:
34
34
  - lib/hiera/console_logger.rb
35
35
  - lib/hiera/backend/yaml_backend.rb
36
36
  has_rdoc: true
37
- homepage: http://devco.net/
37
+ homepage: https://github.com/ripienaar/hiera/
38
38
  licenses: []
39
39
 
40
40
  post_install_message:
@@ -63,5 +63,11 @@ rubygems_version: 1.3.6
63
63
  signing_key:
64
64
  specification_version: 3
65
65
  summary: Light weight hierarcical data store
66
- test_files: []
67
-
66
+ test_files:
67
+ - spec/spec.opts
68
+ - spec/spec_helper.rb
69
+ - spec/unit/backend_spec.rb
70
+ - spec/unit/console_logger_spec.rb
71
+ - spec/unit/hiera_spec.rb
72
+ - spec/unit/config_spec.rb
73
+ - spec/unit/backend/yaml_backend_spec.rb