summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorIvan Larionov <ivan.larionov@skype.net>2014-05-31 03:21:17 +0400
committerIvan Larionov <ivan.larionov@skype.net>2014-05-31 03:21:59 +0400
commit58de779995ec2f3adff739ec5170aedf0366ada2 (patch)
treed8581022249cc558f095310a091a48cd402ab8da /spec
parentab216f0ee10022b2d9c2a65b6c13c9e75d456ad4 (diff)
downloadchef-58de779995ec2f3adff739ec5170aedf0366ada2.tar.gz
[CHEF-3399] Raise in case of data bag items conflict. Tests.
Diffstat (limited to 'spec')
-rw-r--r--spec/unit/data_bag_spec.rb56
1 files changed, 50 insertions, 6 deletions
diff --git a/spec/unit/data_bag_spec.rb b/spec/unit/data_bag_spec.rb
index 08860fa018..a35e3d2aa6 100644
--- a/spec/unit/data_bag_spec.rb
+++ b/spec/unit/data_bag_spec.rb
@@ -163,17 +163,61 @@ describe Chef::DataBag do
Chef::DataBag.load(:foo)
end
- it "should return merged data bag with first seen values" do
+ it "should return the data bag" do
+ @paths.each do |path|
+ file_dir_stub(path)
+ if path == @paths.first
+ dir_glob_stub(path, [File.join(path, 'foo/bar.json'), File.join(path, 'foo/baz.json')])
+ else
+ dir_glob_stub(path)
+ end
+ end
+ IO.should_receive(:read).with(File.join(@paths.first, 'foo/bar.json')).and_return('{"id": "bar", "name": "Bob Bar" }')
+ IO.should_receive(:read).with(File.join(@paths.first, 'foo/baz.json')).and_return('{"id": "baz", "name": "John Baz" }')
+ data_bag = Chef::DataBag.load('foo')
+ data_bag.should == { 'bar' => { 'id' => 'bar', 'name' => 'Bob Bar' }, 'baz' => { 'id' => 'baz', 'name' => 'John Baz' }}
+ end
+
+ it "should raise if data bag has items with similar names but different content" do
+ @paths.each do |path|
+ file_dir_stub(path)
+ item_with_different_content = "{\"id\": \"bar\", \"name\": \"Bob Bar\", \"path\": \"#{path}\"}"
+ IO.should_receive(:read).with(File.join(path, 'foo/bar.json')).and_return(item_with_different_content)
+ if @paths.size == 1
+ dir_glob_stub(path, [File.join(path, 'foo/bar.json'), File.join(path, 'foo/baz.json')])
+ item_2_with_different_content = '{"id": "bar", "name": "John Baz"}'
+ IO.should_receive(:read).with(File.join(path, 'foo/baz.json')).and_return(item_2_with_different_content)
+ else
+ dir_glob_stub(path, [File.join(path, 'foo/bar.json')])
+ end
+ end
+ expect { Chef::DataBag.load('foo') }.to raise_error(Chef::Exceptions::DuplicateDataBagItem)
+ end
+
+ it "should return data bag if it has items with similar names and the same content" do
+ @paths.each do |path|
+ file_dir_stub(path)
+ dir_glob_stub(path, [File.join(path, 'foo/bar.json'), File.join(path, 'foo/baz.json')])
+ item_with_same_content = '{"id": "bar", "name": "Bob Bar"}'
+ IO.should_receive(:read).with(File.join(path, 'foo/bar.json')).and_return(item_with_same_content)
+ IO.should_receive(:read).with(File.join(path, 'foo/baz.json')).and_return(item_with_same_content)
+ end
+ data_bag = Chef::DataBag.load('foo')
+ test_data_bag = { 'bar' => { 'id' => 'bar', 'name' => 'Bob Bar'} }
+ data_bag.should == test_data_bag
+ end
+
+ it "should merge data bag items if there are no conflicts" do
@paths.each_with_index do |path, index|
file_dir_stub(path)
dir_glob_stub(path, [File.join(path, 'foo/bar.json'), File.join(path, 'foo/baz.json')])
- test_dup_key = "{\"id\": \"bar\", \"name\": \"Bob Bar\", \"path\": \"#{path}\"}"
- IO.should_receive(:read).with(File.join(path, 'foo/bar.json')).and_return(test_dup_key)
- test_uniq_key = "{\"id\": \"baz_#{index}\", \"name\": \"John Baz\", \"path\": \"#{path}\"}"
- IO.should_receive(:read).with(File.join(path, 'foo/baz.json')).and_return(test_uniq_key)
+ test_item_with_same_content = '{"id": "bar", "name": "Bob Bar"}'
+ IO.should_receive(:read).with(File.join(path, 'foo/bar.json')).and_return(test_item_with_same_content)
+ test_uniq_item = "{\"id\": \"baz_#{index}\", \"name\": \"John Baz\", \"path\": \"#{path}\"}"
+ IO.should_receive(:read).with(File.join(path, 'foo/baz.json')).and_return(test_uniq_item)
end
data_bag = Chef::DataBag.load('foo')
- test_data_bag = { 'bar' => { 'id' => 'bar', 'name' => 'Bob Bar', 'path' => @paths.first} }
+ test_data_bag = { 'bar' => { 'id' => 'bar', 'name' => 'Bob Bar'} }
@paths.each_with_index do |path, index|
test_data_bag["baz_#{index}"] = { "id" => "baz_#{index}", "name" => "John Baz", "path" => path }
end