diff options
author | Ivan Larionov <ivan.larionov@skype.net> | 2013-12-02 18:22:17 +0400 |
---|---|---|
committer | Ivan Larionov <ivan.larionov@skype.net> | 2014-05-31 03:21:59 +0400 |
commit | 91f15aa6d965b746a6a9598d8a9f78c536743842 (patch) | |
tree | cf4aab6e5e1f6a49a336c5ebe83224169475109d | |
parent | 7588211ecb83c9592065359622ff6cef98d773af (diff) | |
download | chef-91f15aa6d965b746a6a9598d8a9f78c536743842.tar.gz |
[CHEF-3399] Make data_bag_path an array like cookbook_path
-rw-r--r-- | lib/chef/data_bag.rb | 32 | ||||
-rw-r--r-- | spec/unit/data_bag_spec.rb | 41 |
2 files changed, 50 insertions, 23 deletions
diff --git a/lib/chef/data_bag.rb b/lib/chef/data_bag.rb index 639d71a74d..e940e6452b 100644 --- a/lib/chef/data_bag.rb +++ b/lib/chef/data_bag.rb @@ -83,11 +83,15 @@ class Chef def self.list(inflate=false) if Chef::Config[:solo] - unless File.directory?(Chef::Config[:data_bag_path]) - raise Chef::Exceptions::InvalidDataBagPath, "Data bag path '#{Chef::Config[:data_bag_path]}' is invalid" - end + paths = Array(Chef::Config[:data_bag_path]) + names = [] + paths.each do |path| + unless File.directory?(path) + raise Chef::Exceptions::InvalidDataBagPath, "Data bag path '#{path}' is invalid" + end - names = Dir.glob(File.join(Chef::Config[:data_bag_path], "*")).map{|f|File.basename(f)}.sort + names += Dir.glob(File.join(path, "*")).map{|f|File.basename(f)}.sort + end names.inject({}) {|h, n| h[n] = n; h} else if inflate @@ -105,15 +109,21 @@ class Chef # Load a Data Bag by name via either the RESTful API or local data_bag_path if run in solo mode def self.load(name) if Chef::Config[:solo] - unless File.directory?(Chef::Config[:data_bag_path]) - raise Chef::Exceptions::InvalidDataBagPath, "Data bag path '#{Chef::Config[:data_bag_path]}' is invalid" - end + paths = Array(Chef::Config[:data_bag_path]) + data_bag = {} + paths.each do |path| + unless File.directory?(path) + raise Chef::Exceptions::InvalidDataBagPath, "Data bag path '#{path}' is invalid" + end - Dir.glob(File.join(Chef::Config[:data_bag_path], "#{name}", "*.json")).inject({}) do |bag, f| - item = Chef::JSONCompat.from_json(IO.read(f)) - bag[item['id']] = item - bag + data = Dir.glob(File.join(path, name.to_s, "*.json")).inject({}) do |bag, f| + item = Chef::JSONCompat.from_json(IO.read(f)) + bag[item['id']] = item + bag + end + data_bag.merge! data end + return data_bag else Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("data/#{name}") end diff --git a/spec/unit/data_bag_spec.rb b/spec/unit/data_bag_spec.rb index 4ac843c869..1c74ad8adc 100644 --- a/spec/unit/data_bag_spec.rb +++ b/spec/unit/data_bag_spec.rb @@ -128,10 +128,11 @@ describe Chef::DataBag do end end - describe "in solo mode" do + shared_examples_for "data bag in solo mode" do |data_bag_path| before do Chef::Config[:solo] = true - Chef::Config[:data_bag_path] = '/var/chef/data_bags' + Chef::Config[:data_bag_path] = data_bag_path + @paths = Array(data_bag_path) end after do @@ -139,29 +140,37 @@ describe Chef::DataBag do end it "should get the data bag from the data_bag_path" do - File.should_receive(:directory?).with('/var/chef/data_bags').and_return(true) - Dir.should_receive(:glob).with('/var/chef/data_bags/foo/*.json').and_return([]) + @paths.each do |path| + File.should_receive(:directory?).with(path).and_return(true) + Dir.should_receive(:glob).with(File.join(path, 'foo/*.json')).and_return([]) + end Chef::DataBag.load('foo') end it "should get the data bag from the data_bag_path by symbolic name" do - File.should_receive(:directory?).with('/var/chef/data_bags').and_return(true) - Dir.should_receive(:glob).with('/var/chef/data_bags/foo/*.json').and_return([]) + @paths.each do |path| + File.should_receive(:directory?).with(path).and_return(true) + Dir.should_receive(:glob).with(File.join(path, 'foo/*.json')).and_return([]) + end Chef::DataBag.load(:foo) end it "should return the data bag" do - File.should_receive(:directory?).with('/var/chef/data_bags').and_return(true) - Dir.stub(:glob).and_return(["/var/chef/data_bags/foo/bar.json", "/var/chef/data_bags/foo/baz.json"]) - IO.should_receive(:read).with('/var/chef/data_bags/foo/bar.json').and_return('{"id": "bar", "name": "Bob Bar" }') - IO.should_receive(:read).with('/var/chef/data_bags/foo/baz.json').and_return('{"id": "baz", "name": "John Baz" }') + @paths.each do |path| + File.should_receive(:directory?).with(path).and_return(true) + Dir.should_receive(:glob).with(File.join(path, 'foo/*.json')).and_return([File.join(path, 'foo/bar.json'), File.join(path, 'foo/baz.json')]) + IO.should_receive(:read).with(File.join(path, 'foo/bar.json')).and_return('{"id": "bar", "name": "Bob Bar" }') + IO.should_receive(:read).with(File.join(path, 'foo/baz.json')).and_return('{"id": "baz", "name": "John Baz" }') + end data_bag = Chef::DataBag.load('foo') data_bag.should == { 'bar' => { 'id' => 'bar', 'name' => 'Bob Bar' }, 'baz' => { 'id' => 'baz', 'name' => 'John Baz' }} end it "should return the data bag list" do - File.should_receive(:directory?).with('/var/chef/data_bags').and_return(true) - Dir.should_receive(:glob).and_return(["/var/chef/data_bags/foo", "/var/chef/data_bags/bar"]) + @paths.each do |path| + File.should_receive(:directory?).with(path).and_return(true) + Dir.should_receive(:glob).and_return([File.join(path, 'foo'), File.join(path, 'bar')]) + end data_bag_list = Chef::DataBag.list data_bag_list.should == { 'bar' => 'bar', 'foo' => 'foo' } end @@ -175,6 +184,14 @@ describe Chef::DataBag do end end + + describe "data bag with string path" do + it_should_behave_like "data bag in solo mode", "/var/chef/data_bags" + end + + describe "data bag with array path" do + it_should_behave_like "data bag in solo mode", ["/var/chef/data_bags", "/var/chef/data_bags_2"] + end end end |