hiera-json 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hiera/backend/json_backend.rb +17 -34
- data/spec/json_backend_spec.rb +82 -0
- metadata +7 -7
@@ -1,18 +1,3 @@
|
|
1
|
-
#
|
2
|
-
# Copyright 2010, 2011 R.I.Pienaar
|
3
|
-
#
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
# you may not use this file except in compliance with the License.
|
6
|
-
# You may obtain a copy of the License at
|
7
|
-
#
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
#
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
# See the License for the specific language governing permissions and
|
14
|
-
# limitations under the License.
|
15
|
-
#
|
16
1
|
class Hiera
|
17
2
|
module Backend
|
18
3
|
class Json_backend
|
@@ -23,37 +8,35 @@ class Hiera
|
|
23
8
|
end
|
24
9
|
|
25
10
|
def lookup(key, scope, order_override, resolution_type)
|
26
|
-
answer =
|
11
|
+
answer = Backend.empty_answer(resolution_type)
|
27
12
|
|
28
|
-
Hiera.debug("Looking up #{key} in JSON
|
29
|
-
|
30
|
-
datadir = Backend.datadir(:json, scope)
|
31
|
-
|
32
|
-
raise "Cannot find data directory #{datadir}" unless File.directory?(datadir)
|
13
|
+
Hiera.debug("Looking up #{key} in JSON backend")
|
33
14
|
|
34
15
|
Backend.datasources(scope, order_override) do |source|
|
35
|
-
|
36
|
-
Hiera.debug("Looking for data source #{source}")
|
37
|
-
|
38
|
-
datafile = File.join([datadir, "#{source}.json"])
|
16
|
+
Hiera.debug("Looking for data source #{source}")
|
39
17
|
|
40
|
-
|
41
|
-
Hiera.warn("Cannot find datafile #{datafile}, skipping")
|
42
|
-
next
|
43
|
-
end
|
18
|
+
jsonfile = Backend.datafile(:json, scope, source, "json") || next
|
44
19
|
|
45
|
-
|
20
|
+
data = JSON.parse(File.read(jsonfile))
|
46
21
|
|
47
|
-
|
48
|
-
|
22
|
+
next if data.empty?
|
23
|
+
next unless data.include?(key)
|
49
24
|
|
50
|
-
|
25
|
+
# for array resolution we just append to the array whatever
|
26
|
+
# we find, we then goes onto the next file and keep adding to
|
27
|
+
# the array
|
28
|
+
#
|
29
|
+
# for priority searches we break after the first found data item
|
30
|
+
case resolution_type
|
31
|
+
when :array
|
32
|
+
answer << Backend.parse_answer(data[key], scope)
|
51
33
|
else
|
34
|
+
answer = Backend.parse_answer(data[key], scope)
|
52
35
|
break
|
53
36
|
end
|
54
37
|
end
|
55
38
|
|
56
|
-
answer
|
39
|
+
return answer
|
57
40
|
end
|
58
41
|
end
|
59
42
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
$:.insert(0, File.join([File.dirname(__FILE__), "..", "lib"]))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rspec'
|
5
|
+
require 'rspec/mocks'
|
6
|
+
require 'mocha'
|
7
|
+
require 'hiera/backend/json_backend'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.mock_with :mocha
|
11
|
+
end
|
12
|
+
|
13
|
+
class Hiera
|
14
|
+
module Backend
|
15
|
+
describe Json_backend do
|
16
|
+
before do
|
17
|
+
Hiera.stubs(:debug)
|
18
|
+
Hiera.stubs(:warn)
|
19
|
+
Hiera::Backend.stubs(:empty_answer).returns(nil)
|
20
|
+
@backend = Json_backend.new
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#initialize" do
|
24
|
+
it "should announce its creation" do # because other specs checks this
|
25
|
+
Hiera.expects(:debug).with("Hiera JSON backend starting")
|
26
|
+
Json_backend.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#lookup" do
|
31
|
+
it "should look for data in all sources" do
|
32
|
+
Backend.expects(:datasources).multiple_yields(["one"], ["two"])
|
33
|
+
Backend.expects(:datafile).with(:json, {}, "one", "json").returns(nil)
|
34
|
+
Backend.expects(:datafile).with(:json, {}, "two", "json").returns(nil)
|
35
|
+
|
36
|
+
@backend.lookup("key", {}, nil, :priority)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should pick data earliest source that has it for priority searches" do
|
40
|
+
scope = {"rspec" => "test"}
|
41
|
+
Backend.stubs(:parse_answer).with('answer', scope).returns("answer")
|
42
|
+
Backend.stubs(:parse_answer).with('test_%{rspec}', scope).returns("test_test")
|
43
|
+
Backend.expects(:datasources).multiple_yields(["one"], ["two"])
|
44
|
+
Backend.expects(:datafile).with(:json, scope, "one", "json").returns("/nonexisting/one.json")
|
45
|
+
Backend.expects(:datafile).with(:json, scope, "two", "json").returns(nil).never
|
46
|
+
File.expects(:read).with("/nonexisting/one.json").returns("one.json")
|
47
|
+
JSON.expects(:parse).with("one.json").returns({"key" => "test_%{rspec}"})
|
48
|
+
|
49
|
+
@backend.lookup("key", scope, nil, :priority).should == "test_test"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should build an array of all data sources for array searches" do
|
53
|
+
Hiera::Backend.stubs(:empty_answer).returns([])
|
54
|
+
Backend.stubs(:parse_answer).with('answer', {}).returns("answer")
|
55
|
+
Backend.expects(:datafile).with(:json, {}, "one", "json").returns("/nonexisting/one.json")
|
56
|
+
Backend.expects(:datafile).with(:json, {}, "two", "json").returns("/nonexisting/two.json")
|
57
|
+
|
58
|
+
Backend.expects(:datasources).multiple_yields(["one"], ["two"])
|
59
|
+
|
60
|
+
File.expects(:read).with("/nonexisting/one.json").returns("one.json")
|
61
|
+
File.expects(:read).with("/nonexisting/two.json").returns("two.json")
|
62
|
+
|
63
|
+
JSON.expects(:parse).with("one.json").returns({"key" => "answer"})
|
64
|
+
JSON.expects(:parse).with("two.json").returns({"key" => "answer"})
|
65
|
+
|
66
|
+
@backend.lookup("key", {}, nil, :array).should == ["answer", "answer"]
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should parse the answer for scope variables" do
|
70
|
+
Backend.stubs(:parse_answer).with('test_%{rspec}', {'rspec' => 'test'}).returns("test_test")
|
71
|
+
Backend.expects(:datasources).yields("one")
|
72
|
+
Backend.expects(:datafile).with(:json, {"rspec" => "test"}, "one", "json").returns("/nonexisting/one.json")
|
73
|
+
|
74
|
+
File.expects(:read).with("/nonexisting/one.json").returns("one.json")
|
75
|
+
JSON.expects(:parse).with("one.json").returns({"key" => "test_%{rspec}"})
|
76
|
+
|
77
|
+
@backend.lookup("key", {"rspec" => "test"}, nil, :priority).should == "test_test"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- R.I.Pienaar
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-06-
|
17
|
+
date: 2011-06-12 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,9 +26,9 @@ dependencies:
|
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
28
|
- 0
|
29
|
-
-
|
29
|
+
- 2
|
30
30
|
- 0
|
31
|
-
version: 0.
|
31
|
+
version: 0.2.0
|
32
32
|
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
34
|
- !ruby/object:Gem::Dependency
|
@@ -83,5 +83,5 @@ rubygems_version: 1.3.6
|
|
83
83
|
signing_key:
|
84
84
|
specification_version: 3
|
85
85
|
summary: JSON backend for the Hiera hierarcical data store
|
86
|
-
test_files:
|
87
|
-
|
86
|
+
test_files:
|
87
|
+
- spec/json_backend_spec.rb
|