Permalink
Browse files

Add support for array searches

  • Loading branch information...
ripienaar committed Jun 11, 2011
1 parent daa6578 commit e9fc0fad55a218d7a3f3a8223ba563282aeb523b
Showing with 66 additions and 83 deletions.
  1. +2 −0 CHANGES.txt
  2. +17 −34 lib/hiera/backend/json_backend.rb
  3. +47 −49 spec/json_backend_spec.rb
View
@@ -0,0 +1,2 @@
+2011/06/11 - Add array search support and use new helper methods
+2011/06/07 - Change warn level messages when a datafile is missing to debug
@@ -1,18 +1,3 @@
-#
-# Copyright 2010, 2011 R.I.Pienaar
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
class Hiera
module Backend
class Json_backend
@@ -23,37 +8,35 @@ def initialize
end
def lookup(key, scope, order_override, resolution_type)
- answer = nil
+ answer = Backend.empty_answer(resolution_type)
- Hiera.debug("Looking up #{key} in JSON backup")
-
- datadir = Backend.datadir(:json, scope)
-
- raise "Cannot find data directory #{datadir}" unless File.directory?(datadir)
+ Hiera.debug("Looking up #{key} in JSON backend")
Backend.datasources(scope, order_override) do |source|
- unless answer
- Hiera.debug("Looking for data source #{source}")
-
- datafile = File.join([datadir, "#{source}.json"])
+ Hiera.debug("Looking for data source #{source}")
- unless File.exist?(datafile)
- Hiera.warn("Cannot find datafile #{datafile}, skipping")
- next
- end
+ jsonfile = Backend.datafile(:json, scope, source, "json") || next
- data = JSON.parse(File.read(datafile))
+ data = JSON.parse(File.read(jsonfile))
- next if data.empty?
- next unless data.include?(key)
+ next if data.empty?
+ next unless data.include?(key)
- answer = Backend.parse_string(data[key], scope)
+ # for array resolution we just append to the array whatever
+ # we find, we then goes onto the next file and keep adding to
+ # the array
+ #
+ # for priority searches we break after the first found data item
+ case resolution_type
+ when :array
+ answer << Backend.parse_answer(data[key], scope)
else
+ answer = Backend.parse_answer(data[key], scope)
break
end
end
- answer
+ return answer
end
end
end
View
@@ -2,9 +2,9 @@
require 'rubygems'
require 'rspec'
-require 'hiera/backend/json_backend'
require 'rspec/mocks'
require 'mocha'
+require 'hiera/backend/json_backend'
RSpec.configure do |config|
config.mock_with :mocha
@@ -14,70 +14,68 @@ class Hiera
module Backend
describe Json_backend do
before do
- Hiera.stubs(:warn)
Hiera.stubs(:debug)
- Backend.stubs(:parse_string)
- Backend.stubs(:datasources).yields([])
-
- Backend.expects(:datadir).with(:json, {}).returns("/nonexisting")
+ Hiera.stubs(:warn)
+ Hiera::Backend.stubs(:empty_answer).returns(nil)
@backend = Json_backend.new
end
- it "should use the configured datadir" do
- File.expects(:directory?).with("/nonexisting").returns(true)
-
- @backend.lookup("test", {}, nil, nil)
+ describe "#initialize" do
+ it "should announce its creation" do # because other specs checks this
+ Hiera.expects(:debug).with("Hiera JSON backend starting")
+ Json_backend.new
+ end
end
- it "should fail for missing data directories" do
- File.expects(:directory?).with("/nonexisting").returns(false)
+ describe "#lookup" do
+ it "should look for data in all sources" do
+ Backend.expects(:datasources).multiple_yields(["one"], ["two"])
+ Backend.expects(:datafile).with(:json, {}, "one", "json").returns(nil)
+ Backend.expects(:datafile).with(:json, {}, "two", "json").returns(nil)
- expect {
- @backend.lookup("test", {}, nil, nil)
- }.to raise_error("Cannot find data directory /nonexisting")
- end
+ @backend.lookup("key", {}, nil, :priority)
+ end
- it "should look for data in all data sources" do
- File.expects(:directory?).with("/nonexisting").returns(true)
- Backend.expects(:datasources).with({}, nil).multiple_yields(["one"], ["two"])
- File.expects(:exist?).with("/nonexisting/one.json")
- File.expects(:exist?).with("/nonexisting/two.json")
- @backend.lookup("test", {}, nil, nil)
- end
+ it "should pick data earliest source that has it for priority searches" do
+ scope = {"rspec" => "test"}
+ Backend.stubs(:parse_answer).with('answer', scope).returns("answer")
+ Backend.stubs(:parse_answer).with('test_%{rspec}', scope).returns("test_test")
+ Backend.expects(:datasources).multiple_yields(["one"], ["two"])
+ Backend.expects(:datafile).with(:json, scope, "one", "json").returns("/nonexisting/one.json")
+ Backend.expects(:datafile).with(:json, scope, "two", "json").returns(nil).never
+ File.expects(:read).with("/nonexisting/one.json").returns("one.json")
+ JSON.expects(:parse).with("one.json").returns({"key" => "test_%{rspec}"})
- it "should warn about missing data files and continue" do
- File.expects(:directory?).with("/nonexisting").returns(true)
- Backend.expects(:datasources).with({}, nil).multiple_yields(["one"], ["two"])
- File.expects(:exist?).with("/nonexisting/one.json").returns(false)
- File.expects(:exist?).with("/nonexisting/two.json").returns(false)
+ @backend.lookup("key", scope, nil, :priority).should == "test_test"
+ end
- Hiera.expects(:warn).with("Cannot find datafile /nonexisting/one.json, skipping")
- Hiera.expects(:warn).with("Cannot find datafile /nonexisting/two.json, skipping")
+ it "should build an array of all data sources for array searches" do
+ Hiera::Backend.stubs(:empty_answer).returns([])
+ Backend.stubs(:parse_answer).with('answer', {}).returns("answer")
+ Backend.expects(:datafile).with(:json, {}, "one", "json").returns("/nonexisting/one.json")
+ Backend.expects(:datafile).with(:json, {}, "two", "json").returns("/nonexisting/two.json")
- @backend.lookup("test", {}, nil, nil)
- end
+ Backend.expects(:datasources).multiple_yields(["one"], ["two"])
- it "should parse string data for interprolation" do
- File.expects(:directory?).with("/nonexisting").returns(true)
- Backend.expects(:datasources).with({}, nil).yields("one")
- File.expects(:exist?).with("/nonexisting/one.json").returns(true)
- File.expects(:read).with("/nonexisting/one.json").returns('{"test":"data"}')
- Backend.expects(:parse_string).with("data", {})
+ File.expects(:read).with("/nonexisting/one.json").returns("one.json")
+ File.expects(:read).with("/nonexisting/two.json").returns("two.json")
- @backend.lookup("test", {}, nil, nil)
- end
+ JSON.expects(:parse).with("one.json").returns({"key" => "answer"})
+ JSON.expects(:parse).with("two.json").returns({"key" => "answer"})
+
+ @backend.lookup("key", {}, nil, :array).should == ["answer", "answer"]
+ end
- it "should return the first answer found" do
- File.expects(:directory?).with("/nonexisting").returns(true)
- Backend.expects(:datasources).with({}, nil).multiple_yields(["one"], ["two"])
- File.expects(:exist?).with("/nonexisting/one.json").returns(true)
- File.expects(:read).with("/nonexisting/one.json").returns('{"test":"data1"}')
- Backend.expects(:parse_string).with("data1", {}).returns("data1")
+ it "should parse the answer for scope variables" do
+ Backend.stubs(:parse_answer).with('test_%{rspec}', {'rspec' => 'test'}).returns("test_test")
+ Backend.expects(:datasources).yields("one")
+ Backend.expects(:datafile).with(:json, {"rspec" => "test"}, "one", "json").returns("/nonexisting/one.json")
- File.expects(:exist?).with("/nonexisting/two.json").never
- File.expects(:read).with("/nonexisting/two.json").never
+ File.expects(:read).with("/nonexisting/one.json").returns("one.json")
+ JSON.expects(:parse).with("one.json").returns({"key" => "test_%{rspec}"})
- @backend.lookup("test", {}, nil, nil).should == "data1"
+ @backend.lookup("key", {"rspec" => "test"}, nil, :priority).should == "test_test"
+ end
end
end
end

0 comments on commit e9fc0fa

Please sign in to comment.