diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-09-29 11:29:00 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-11-08 11:30:22 -0800 |
commit | 58b343284a4f5c9e4ea7fa81cd115fef6be8f2cb (patch) | |
tree | 201500aa675bb91ae731a24676d18eccf440de77 /spec | |
parent | c92393e52977b468bff720ae84de644da933ac03 (diff) | |
download | chef-58b343284a4f5c9e4ea7fa81cd115fef6be8f2cb.tar.gz |
chef-12 attribute changes
- adding rm, rm_default, rm_normal, rm_override APIs
- adding default!, normal! and override! APIs
- changing force_default! and force_override! APIs
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/node/attribute_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 326 |
2 files changed, 315 insertions, 17 deletions
diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index c5d0f2398e..6cdf60550f 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -285,7 +285,7 @@ describe Chef::Node::Attribute do end it "prefers 'forced default' over any other default" do - @attributes.default!["default"] = "force default" + @attributes.force_default["default"] = "force default" @attributes.role_default["default"] = "role default" @attributes.env_default["default"] = "environment default" expect(@attributes["default"]).to eq("force default") @@ -307,7 +307,7 @@ describe Chef::Node::Attribute do end it "prefers 'forced overrides' over role or cookbook overrides" do - @attributes.override!["override"] = "force override" + @attributes.force_override["override"] = "force override" @attributes.env_override["override"] = "environment override" @attributes.role_override["override"] = "role override" expect(@attributes["override"]).to eq("force override") @@ -939,7 +939,6 @@ describe Chef::Node::Attribute do end - describe "values" do before do @attributes = Chef::Node::Attribute.new( @@ -1185,4 +1184,3 @@ describe Chef::Node::Attribute do end end - diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 24209d367f..92d4422c3d 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -247,13 +247,6 @@ describe Chef::Node do node.default.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end - - it "accesses force defaults via default!" do - node.default![:foo] = "wet bar" - node.default[:foo] = "bar" - expect(node[:foo]).to eq("wet bar") - end - end describe "override attributes" do @@ -292,13 +285,322 @@ describe Chef::Node do node.override.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end + end + + describe "globally deleting attributes" do + context "with hash values" do + before do + node.role_default["mysql"]["server"]["port"] = 1234 + node.normal["mysql"]["server"]["port"] = 2345 + node.override["mysql"]["server"]["port"] = 3456 + end + + it "deletes all the values and returns the value with the highest precidence" do + expect( node.rm("mysql", "server", "port") ).to eql(3456) + expect( node["mysql"]["server"]["port"] ).to be_nil + expect( node["mysql"]["server"] ).to eql({}) + end + + it "deletes nested things correctly" do + node.default["mysql"]["client"]["client_setting"] = "foo" + expect( node.rm("mysql", "server") ).to eql( {"port" => 3456} ) + expect( node["mysql"] ).to eql( { "client" => { "client_setting" => "foo" } } ) + end + + it "returns nil if the node attribute does not exist" do + expect( node.rm("no", "such", "thing") ).to be_nil + end + + it "can delete the entire tree" do + expect( node.rm("mysql") ).to eql({"server"=>{"port"=>3456}}) + end + end + + context "when trying to delete through a thing that isn't an array-like or hash-like object" do + before do + node.default["mysql"] = true + end + + it "returns nil when you're two levels deeper" do + expect( node.rm("mysql", "server", "port") ).to eql(nil) + end + + it "returns nil when you're one level deeper" do + expect( node.rm("mysql", "server") ).to eql(nil) + end - it "sets force_overrides via override!" do - node.override![:foo] = "wet bar" - node.override[:foo] = "bar" - expect(node[:foo]).to eq("wet bar") + it "correctly deletes at the top level" do + expect( node.rm("mysql") ).to eql(true) + end + end + + context "with array indexes" do + before do + node.role_default["mysql"]["server"][0]["port"] = 1234 + node.normal["mysql"]["server"][0]["port"] = 2345 + node.override["mysql"]["server"][0]["port"] = 3456 + node.override["mysql"]["server"][1]["port"] = 3456 + end + + it "deletes the array element" do + expect( node.rm("mysql", "server", 0, "port") ).to eql(3456) + expect( node["mysql"]["server"][0]["port"] ).to be_nil + expect( node["mysql"]["server"][1]["port"] ).to eql(3456) + end end + context "with real arrays" do + before do + node.role_default["mysql"]["server"] = [ { + "port" => 1234, + } ] + node.normal["mysql"]["server"] = [ { + "port" => 2345, + } ] + node.override["mysql"]["server"] = [ { + "port" => 3456, + } ] + end + + it "deletes the array element" do + expect( node.rm("mysql", "server", 0, "port") ).to eql(3456) + expect( node["mysql"]["server"][0]["port"] ).to be_nil + end + + it "does not have a horrible error message when mistaking arrays for hashes" do + expect { node.rm("mysql", "server", "port") }.to raise_error(TypeError, "Wrong type in index of attribute (did you use a Hash index on an Array?)") + end + end + end + + describe "granular deleting attributes" do + context "when only defaults exist" do + before do + node.role_default["mysql"]["server"]["port"] = 1234 + node.default["mysql"]["server"]["port"] = 2345 + node.force_default["mysql"]["server"]["port"] = 3456 + end + + it "returns the deleted values" do + expect( node.rm_default("mysql", "server", "port") ).to eql(3456) + end + + it "returns nil for the combined attribues" do + expect( node.rm_default("mysql", "server", "port") ).to eql(3456) + expect( node["mysql"]["server"]["port"] ).to eql(nil) + end + + it "returns an empty hash for the default attrs" do + expect( node.rm_default("mysql", "server", "port") ).to eql(3456) + # this auto-vivifies, should it? + expect( node.default_attrs["mysql"]["server"]["port"] ).to eql({}) + end + + it "returns an empty hash after the last key is deleted" do + expect( node.rm_default("mysql", "server", "port") ).to eql(3456) + expect( node["mysql"]["server"] ).to eql({}) + end + end + + context "when trying to delete through a thing that isn't an array-like or hash-like object" do + before do + node.default["mysql"] = true + end + + it "returns nil when you're two levels deeper" do + expect( node.rm_default("mysql", "server", "port") ).to eql(nil) + end + + it "returns nil when you're one level deeper" do + expect( node.rm_default("mysql", "server") ).to eql(nil) + end + + it "correctly deletes at the top level" do + expect( node.rm_default("mysql") ).to eql(true) + end + end + + context "when a higher precedence exists" do + before do + node.role_default["mysql"]["server"]["port"] = 1234 + node.default["mysql"]["server"]["port"] = 2345 + node.force_default["mysql"]["server"]["port"] = 3456 + + node.override["mysql"]["server"]["port"] = 9999 + end + + it "returns the deleted values" do + expect( node.rm_default("mysql", "server", "port") ).to eql(3456) + end + + it "returns the higher precedence values after the delete" do + expect( node.rm_default("mysql", "server", "port") ).to eql(3456) + expect( node["mysql"]["server"]["port"] ).to eql(9999) + end + + it "returns an empty has for the default attrs" do + expect( node.rm_default("mysql", "server", "port") ).to eql(3456) + # this auto-vivifies, should it? + expect( node.default_attrs["mysql"]["server"]["port"] ).to eql({}) + end + end + + context "when a lower precedence exists" do + before do + node.default["mysql"]["server"]["port"] = 2345 + node.override["mysql"]["server"]["port"] = 9999 + node.role_override["mysql"]["server"]["port"] = 9876 + node.force_override["mysql"]["server"]["port"] = 6669 + end + + it "returns the deleted values" do + expect( node.rm_override("mysql", "server", "port") ).to eql(6669) + end + + it "returns the lower precedence levels after the delete" do + expect( node.rm_override("mysql", "server", "port") ).to eql(6669) + expect( node["mysql"]["server"]["port"] ).to eql(2345) + end + + it "returns an empty has for the override attrs" do + expect( node.rm_override("mysql", "server", "port") ).to eql(6669) + # this auto-vivifies, should it? + expect( node.override_attrs["mysql"]["server"]["port"] ).to eql({}) + end + end + + it "rm_default returns nil on deleting non-existent values" do + expect( node.rm_default("no", "such", "thing") ).to be_nil + end + + it "rm_normal returns nil on deleting non-existent values" do + expect( node.rm_normal("no", "such", "thing") ).to be_nil + end + + it "rm_override returns nil on deleting non-existent values" do + expect( node.rm_override("no", "such", "thing") ).to be_nil + end + end + + describe "granular replacing attributes" do + it "removes everything at the level of the last key" do + node.default["mysql"]["server"]["port"] = 2345 + + node.default!["mysql"]["server"] = { "data_dir" => "/my_raid_volume/lib/mysql" } + + expect( node["mysql"]["server"] ).to eql({ "data_dir" => "/my_raid_volume/lib/mysql" }) + end + + it "replaces a value at the cookbook sub-level of the atributes only" do + node.default["mysql"]["server"]["port"] = 2345 + node.default["mysql"]["server"]["service_name"] = "fancypants-sql" + node.role_default["mysql"]["server"]["port"] = 1234 + node.force_default["mysql"]["server"]["port"] = 3456 + + node.default!["mysql"]["server"] = { "data_dir" => "/my_raid_volume/lib/mysql" } + + expect( node["mysql"]["server"]["port"] ).to eql(3456) + expect( node["mysql"]["server"]["service_name"] ).to be_nil + expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") + expect( node["mysql"]["server"] ).to eql({ "port" => 3456, "data_dir" => "/my_raid_volume/lib/mysql" }) + end + + it "higher precedence values aren't removed" do + node.role_default["mysql"]["server"]["port"] = 1234 + node.default["mysql"]["server"]["port"] = 2345 + node.force_default["mysql"]["server"]["port"] = 3456 + node.override["mysql"]["server"]["service_name"] = "fancypants-sql" + + node.default!["mysql"]["server"] = { "data_dir" => "/my_raid_volume/lib/mysql" } + + expect( node["mysql"]["server"]["port"] ).to eql(3456) + expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") + expect( node["mysql"]["server"] ).to eql({ "service_name" => "fancypants-sql", "port" => 3456, "data_dir" => "/my_raid_volume/lib/mysql" }) + end + end + + describe "granular force replacing attributes" do + it "removes everything at the level of the last key" do + node.force_default["mysql"]["server"]["port"] = 2345 + + node.force_default!["mysql"]["server"] = { + "data_dir" => "/my_raid_volume/lib/mysql", + } + + expect( node["mysql"]["server"] ).to eql({ + "data_dir" => "/my_raid_volume/lib/mysql", + }) + end + + it "removes all values from the precedence level when setting" do + node.role_default["mysql"]["server"]["port"] = 1234 + node.default["mysql"]["server"]["port"] = 2345 + node.force_default["mysql"]["server"]["port"] = 3456 + + node.force_default!["mysql"]["server"] = { + "data_dir" => "/my_raid_volume/lib/mysql", + } + + expect( node["mysql"]["server"]["port"] ).to be_nil + expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") + expect( node["mysql"]["server"] ).to eql({ + "data_dir" => "/my_raid_volume/lib/mysql", + }) + end + + it "higher precedence levels are not removed" do + node.role_default["mysql"]["server"]["port"] = 1234 + node.default["mysql"]["server"]["port"] = 2345 + node.force_default["mysql"]["server"]["port"] = 3456 + node.override["mysql"]["server"]["service_name"] = "fancypants-sql" + + node.force_default!["mysql"]["server"] = { + "data_dir" => "/my_raid_volume/lib/mysql", + } + + expect( node["mysql"]["server"]["port"] ).to be_nil + expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") + expect( node["mysql"]["server"] ).to eql({ + "service_name" => "fancypants-sql", + "data_dir" => "/my_raid_volume/lib/mysql", + }) + end + + it "lower precedence levels aren't removed" do + node.role_override["mysql"]["server"]["port"] = 1234 + node.override["mysql"]["server"]["port"] = 2345 + node.force_override["mysql"]["server"]["port"] = 3456 + node.default["mysql"]["server"]["service_name"] = "fancypants-sql" + + node.force_override!["mysql"]["server"] = { + "data_dir" => "/my_raid_volume/lib/mysql", + } + + expect( node["mysql"]["server"]["port"] ).to be_nil + expect( node["mysql"]["server"]["data_dir"] ).to eql("/my_raid_volume/lib/mysql") + expect( node["mysql"]["server"] ).to eql({ + "service_name" => "fancypants-sql", + "data_dir" => "/my_raid_volume/lib/mysql", + }) + end + + it "when overwriting a non-hash/array" do + node.override["mysql"] = false + node.force_override["mysql"] = true + expect { node.force_override!["mysql"]["server"] = { + "data_dir" => "/my_raid_volume/lib/mysql", + } }.to raise_error(TypeError) + end + + it "when overwriting an array with a hash" do + node.force_override["mysql"][0] = true + node.force_override!["mysql"]["server"] = { + "data_dir" => "/my_raid_volume/lib/mysql", + } + expect( node["mysql"]["server"] ).to eql({ + "data_dir" => "/my_raid_volume/lib/mysql", + }) + end end it "should raise an ArgumentError if you ask for an attribute that doesn't exist via method_missing" do @@ -536,7 +838,6 @@ describe Chef::Node do @expansion.default_attrs.replace({:default => "from role", :d_role => "role only"}) @expansion.override_attrs.replace({:override => "from role", :o_role => "role only"}) - @environment = Chef::Environment.new @environment.default_attributes = {:default => "from env", :d_env => "env only" } @environment.override_attributes = {:override => "from env", :o_env => "env only"} @@ -753,7 +1054,6 @@ describe Chef::Node do expect(node_for_json["default"]["env default"]).to eq("env default") end - it "should deserialize itself from json", :json => true do node.from_file(File.expand_path("nodes/test.example.com.rb", CHEF_SPEC_DATA)) json = Chef::JSONCompat.to_json(node) |