diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-06-27 15:38:28 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-06-27 15:42:06 -0700 |
commit | 86fa507226b484a71a45ef6129840bc3928be6b7 (patch) | |
tree | b6f9efaedd634dbc61418ceb78817b91af1889df /spec | |
parent | 945f23be7a43d90ae7ed402d05363b3ff0c11bff (diff) | |
download | chef-86fa507226b484a71a45ef6129840bc3928be6b7.tar.gz |
Attributes v1.1 changeslcg/attributes-v1.1
- fixes *_unless behavior and set_unless_value_present hack from Chef 12
- simplifies rm_* code
- introduces functional read/write/unlink/exist? API
- deprecates method_missing access to attributes for Chef 13
- deprecates set/set_unless aliases for Chef 14
- removes MultiMash mess that I wrote for Chef 13
https://github.com/chef/chef/pull/5029 for more details
Diffstat (limited to 'spec')
20 files changed, 504 insertions, 77 deletions
diff --git a/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb b/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb index ef0967a4d2..e45e7d9f68 100644 --- a/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb +++ b/spec/data/run_context/cookbooks/circular-dep1/attributes/default.rb @@ -1,4 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "circular-dep1::default" - - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "circular-dep1::default" diff --git a/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb b/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb index f2ef012aa1..37f396b1f9 100644 --- a/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb +++ b/spec/data/run_context/cookbooks/circular-dep2/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "circular-dep2::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "circular-dep2::default" diff --git a/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb b/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb index e818d36a9e..3059494198 100644 --- a/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb +++ b/spec/data/run_context/cookbooks/dependency1/attributes/aa_first.rb @@ -1,2 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency1::aa_first" +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency1::aa_first" diff --git a/spec/data/run_context/cookbooks/dependency1/attributes/default.rb b/spec/data/run_context/cookbooks/dependency1/attributes/default.rb index 6875274e3f..a65a3345bc 100644 --- a/spec/data/run_context/cookbooks/dependency1/attributes/default.rb +++ b/spec/data/run_context/cookbooks/dependency1/attributes/default.rb @@ -1,2 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency1::default" +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency1::default" diff --git a/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb b/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb index 1a513b03d4..94ffb30133 100644 --- a/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb +++ b/spec/data/run_context/cookbooks/dependency1/attributes/zz_last.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency1::zz_last" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency1::zz_last" diff --git a/spec/data/run_context/cookbooks/dependency2/attributes/default.rb b/spec/data/run_context/cookbooks/dependency2/attributes/default.rb index 526751f422..8917bf9730 100644 --- a/spec/data/run_context/cookbooks/dependency2/attributes/default.rb +++ b/spec/data/run_context/cookbooks/dependency2/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "dependency2::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "dependency2::default" diff --git a/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb b/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb index 3ad2b925aa..07294665b2 100644 --- a/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb +++ b/spec/data/run_context/cookbooks/no-default-attr/attributes/server.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "no-default-attr::server" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "no-default-attr::server" diff --git a/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb b/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb index cca56bc61f..77309462b1 100644 --- a/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb +++ b/spec/data/run_context/cookbooks/test-with-circular-deps/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "test-with-circular-deps::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "test-with-circular-deps::default" diff --git a/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb b/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb index 4d71cc3cfe..c4cc8151a4 100644 --- a/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb +++ b/spec/data/run_context/cookbooks/test-with-deps/attributes/default.rb @@ -1,3 +1,2 @@ -set_unless[:attr_load_order] = [] -set[:attr_load_order] << "test-with-deps::default" - +normal_unless[:attr_load_order] = [] +normal[:attr_load_order] << "test-with-deps::default" diff --git a/spec/functional/resource/package_spec.rb b/spec/functional/resource/package_spec.rb index 6dc55f7f01..0f01a751ec 100644 --- a/spec/functional/resource/package_spec.rb +++ b/spec/functional/resource/package_spec.rb @@ -260,7 +260,7 @@ describe Chef::Resource::Package, metadata do end before do - node.set[:preseed_value] = "FROM TEMPLATE" + node.normal[:preseed_value] = "FROM TEMPLATE" end it "preseeds the package, then installs it" do diff --git a/spec/functional/resource/template_spec.rb b/spec/functional/resource/template_spec.rb index f270043f2c..32529fbb0c 100644 --- a/spec/functional/resource/template_spec.rb +++ b/spec/functional/resource/template_spec.rb @@ -110,7 +110,7 @@ describe Chef::Resource::Template do context "using single helper syntax referencing @node" do before do - node.set[:helper_test_attr] = "value from helper method" + node.normal[:helper_test_attr] = "value from helper method" resource.helper(:helper_method) { "#{@node[:helper_test_attr]}" } end @@ -131,7 +131,7 @@ describe Chef::Resource::Template do context "using an inline block referencing @node" do before do - node.set[:helper_test_attr] = "value from helper method" + node.normal[:helper_test_attr] = "value from helper method" resource.helpers do def helper_method @@ -168,7 +168,7 @@ describe Chef::Resource::Template do end before do - node.set[:helper_test_attr] = "value from helper method" + node.normal[:helper_test_attr] = "value from helper method" resource.helpers(ExampleModuleReferencingATNode) end diff --git a/spec/functional/shell_spec.rb b/spec/functional/shell_spec.rb index fe2abdb12a..636162fb16 100644 --- a/spec/functional/shell_spec.rb +++ b/spec/functional/shell_spec.rb @@ -137,7 +137,7 @@ describe Shell do it "sets the override_runlist from the command line" do output, exitstatus = run_chef_shell_with("-o 'override::foo,override::bar'") do |out, keyboard| - show_recipes_code = %q[puts "#{node.recipes.inspect}"] + show_recipes_code = %q[puts "#{node["recipes"].inspect}"] keyboard.puts(show_recipes_code) read_until(out, show_recipes_code) end diff --git a/spec/unit/cookbook_version_spec.rb b/spec/unit/cookbook_version_spec.rb index 7def2f5749..81ea161bfe 100644 --- a/spec/unit/cookbook_version_spec.rb +++ b/spec/unit/cookbook_version_spec.rb @@ -120,8 +120,8 @@ describe Chef::CookbookVersion do # Used to test file-specificity related file lookups let(:node) do Chef::Node.new.tap do |n| - n.set[:platform] = "ubuntu" - n.set[:platform_version] = "13.04" + n.normal[:platform] = "ubuntu" + n.normal[:platform_version] = "13.04" n.name("testing") end end @@ -203,8 +203,8 @@ describe Chef::CookbookVersion do # Used to test file-specificity related file lookups let(:node) do Chef::Node.new.tap do |n| - n.set[:platform] = "ubuntu" - n.set[:platform_version] = "13.04" + n.normal[:platform] = "ubuntu" + n.normal[:platform_version] = "13.04" n.name("testing") end end diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index 57ad3c0c25..e40f454c0b 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -218,7 +218,7 @@ describe Chef::Node::Attribute do end it "gives the value at each level of precedence for a path spec" do - expected = [["set_unless_enabled?", false], + expected = [ %w{default default}, %w{env_default env_default}, %w{role_default role_default}, @@ -417,12 +417,6 @@ describe Chef::Node::Attribute do expect(@attributes.normal["foo"]["bar"]).to eq(:baz) end - it "should optionally skip setting the value if one already exists" do - @attributes.set_unless_value_present = true - @attributes.normal["hostname"] = "bar" - expect(@attributes["hostname"]).to eq("latte") - end - it "does not support ||= when setting" do # This is a limitation of auto-vivification. # Users who need this behavior can use set_unless and friends @@ -493,6 +487,7 @@ describe Chef::Node::Attribute do end it "should return true if an attribute exists but is set to nil using dot notation" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect(@attributes.music.deeper.has_key?("gates_of_ishtar")).to eq(true) end @@ -533,10 +528,12 @@ describe Chef::Node::Attribute do describe "method_missing" do it "should behave like a [] lookup" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect(@attributes.music.mastodon).to eq("rocks") end it "should allow the last method to set a value if it has an = sign on the end" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false @attributes.normal.music.mastodon = %w{dream still shining} expect(@attributes.normal.music.mastodon).to eq(%w{dream still shining}) end @@ -577,7 +574,7 @@ describe Chef::Node::Attribute do it "should yield lower if we go deeper" do collect = Array.new - @attributes.one.keys.each do |k| + @attributes["one"].keys.each do |k| collect << k end expect(collect.include?("two")).to eq(true) @@ -587,7 +584,7 @@ describe Chef::Node::Attribute do end it "should not raise an exception if one of the hashes has a nil value on a deep lookup" do - expect { @attributes.place.keys { |k| } }.not_to raise_error + expect { @attributes["place"].keys { |k| } }.not_to raise_error end end @@ -1171,6 +1168,7 @@ describe Chef::Node::Attribute do end it "raises an error when using `attr=value`" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { @attributes.new_key = "new value" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end diff --git a/spec/unit/node/immutable_collections_spec.rb b/spec/unit/node/immutable_collections_spec.rb index f57ed459cd..fe4e50d1bd 100644 --- a/spec/unit/node/immutable_collections_spec.rb +++ b/spec/unit/node/immutable_collections_spec.rb @@ -95,6 +95,10 @@ describe Chef::Node::ImmutableMash do :replace, :select!, :shift, + :write, + :write!, + :unlink, + :unlink!, ].each do |mutator| it "doesn't allow mutation via `#{mutator}'" do expect { @immutable_mash.send(mutator) }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) diff --git a/spec/unit/node/vivid_mash_spec.rb b/spec/unit/node/vivid_mash_spec.rb new file mode 100644 index 0000000000..ca5eddce57 --- /dev/null +++ b/spec/unit/node/vivid_mash_spec.rb @@ -0,0 +1,344 @@ +# +# Copyright:: Copyright 2016, Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# + +require "spec_helper" +require "chef/node/attribute_collections" + +describe Chef::Node::VividMash do + class Root + attr_accessor :top_level_breadcrumb + end + + let(:root) { Root.new } + + let(:vivid) do + expect(root).to receive(:reset_cache).at_least(:once).with(nil) + Chef::Node::VividMash.new(root, + { "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil } + ) + end + + context "#read" do + before do + # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards + vivid + expect(root).not_to receive(:reset_cache) + end + + it "reads hashes deeply" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect(vivid.read("one", "two", "three")).to eql("four") + end + + it "does not trainwreck when hitting hash keys that do not exist" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect(vivid.read("one", "five", "six")).to eql(nil) + end + + it "does not trainwreck when hitting an array with an out of bounds index" do + expect(root).to receive(:top_level_breadcrumb=).with("array").and_call_original + expect(vivid.read("array", 5, "one")).to eql(nil) + end + + it "does not trainwreck when hitting an array with a string key" do + expect(root).to receive(:top_level_breadcrumb=).with("array").and_call_original + expect(vivid.read("array", "one", "two")).to eql(nil) + end + + it "does not trainwreck when traversing a nil" do + expect(root).to receive(:top_level_breadcrumb=).with("nil").and_call_original + expect(vivid.read("nil", "one", "two")).to eql(nil) + end + end + + context "#exist?" do + before do + # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards + vivid + expect(root).not_to receive(:reset_cache) + end + + it "true if there's a hash key there" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect(vivid.exist?("one", "two", "three")).to be true + end + + it "true for intermediate hashes" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect(vivid.exist?("one")).to be true + end + + it "true for arrays that exist" do + expect(root).to receive(:top_level_breadcrumb=).with("array").and_call_original + expect(vivid.exist?("array", 1)).to be true + end + + it "true when the value of the key is nil" do + expect(root).to receive(:top_level_breadcrumb=).with("nil").and_call_original + expect(vivid.exist?("nil")).to be true + end + + it "false when attributes don't exist" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect(vivid.exist?("one", "five", "six")).to be false + end + + it "false when traversing a non-container" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect(vivid.exist?("one", "two", "three", "four")).to be false + end + + it "false when an array index does not exist" do + expect(root).to receive(:top_level_breadcrumb=).with("array").and_call_original + expect(vivid.exist?("array", 3)).to be false + end + + it "false when traversing a nil" do + expect(root).to receive(:top_level_breadcrumb=).with("nil").and_call_original + expect(vivid.exist?("nil", "foo", "bar")).to be false + end + end + + context "#read!" do + before do + # vivify the vividmash, then we're read-only so the cache should never be cleared afterwards + vivid + expect(root).not_to receive(:reset_cache) + end + + it "reads hashes deeply" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect(vivid.read!("one", "two", "three")).to eql("four") + end + + it "reads arrays deeply" do + expect(root).to receive(:top_level_breadcrumb=).with("array").and_call_original + expect(vivid.read!("array", 1)).to eql(1) + end + + it "throws an exception when attributes do not exist" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect { vivid.read!("one", "five", "six") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + + it "throws an exception when traversing a non-container" do + expect(root).to receive(:top_level_breadcrumb=).with("one").and_call_original + expect { vivid.read!("one", "two", "three", "four") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + + it "throws an exception when an array element does not exist" do + expect(root).to receive(:top_level_breadcrumb=).with("array").and_call_original + expect { vivid.read!("array", 3) }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + end + + context "#write" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should write into hashes" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "five", "six") + expect(vivid["one"]["five"]).to eql("six") + end + + it "should deeply autovivify" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "five", "six", "seven", "eight", "nine", "ten") + expect(vivid["one"]["five"]["six"]["seven"]["eight"]["nine"]).to eql("ten") + end + + it "should raise an exception if you overwrite an array with a hash" do + expect(root).to receive(:reset_cache).at_least(:once).with("array") + vivid.write("array", "five", "six") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => { "five" => "six" }, "nil" => nil }) + end + + it "should raise an exception if you traverse through an array with a hash" do + expect(root).to receive(:reset_cache).at_least(:once).with("array") + vivid.write("array", "five", "six", "seven") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => { "five" => { "six" => "seven" } }, "nil" => nil }) + end + + it "should raise an exception if you overwrite a string with a hash" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "two", "three", "four", "five") + expect(vivid).to eql({ "one" => { "two" => { "three" => { "four" => "five" } } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through a string with a hash" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "two", "three", "four", "five", "six") + expect(vivid).to eql({ "one" => { "two" => { "three" => { "four" => { "five" => "six" } } } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you overwrite a nil with a hash" do + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + vivid.write("nil", "one", "two") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => { "one" => "two" } }) + end + + it "should raise an exception if you traverse through a nil with a hash" do + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + vivid.write("nil", "one", "two", "three") + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => { "one" => { "two" => "three" } } }) + end + + it "writes with a block" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write("one", "five") { "six" } + expect(vivid["one"]["five"]).to eql("six") + end + end + + context "#write!" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should write into hashes" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write!("one", "five", "six") + expect(vivid["one"]["five"]).to eql("six") + end + + it "should deeply autovivify" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write!("one", "five", "six", "seven", "eight", "nine", "ten") + expect(vivid["one"]["five"]["six"]["seven"]["eight"]["nine"]).to eql("ten") + end + + it "should raise an exception if you overwrite an array with a hash" do + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("array", "five", "six") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through an array with a hash" do + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("array", "five", "six", "seven") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you overwrite a string with a hash" do + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("one", "two", "three", "four", "five") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through a string with a hash" do + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("one", "two", "three", "four", "five", "six") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you overwrite a nil with a hash" do + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("nil", "one", "two") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should raise an exception if you traverse through a nil with a hash" do + expect(root).not_to receive(:reset_cache) + expect { vivid.write!("nil", "one", "two", "three") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "writes with a block" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + vivid.write!("one", "five") { "six" } + expect(vivid["one"]["five"]).to eql("six") + end + end + + context "#unlink" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should return nil if the keys already don't exist" do + expect(root).not_to receive(:reset_cache) + expect(vivid.unlink("five", "six", "seven", "eight")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink hashes" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + expect( vivid.unlink("one") ).to eql({ "two" => { "three" => "four" } }) + expect(vivid).to eql({ "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink array elements" do + expect(root).to receive(:reset_cache).at_least(:once).with("array") + expect(vivid.unlink("array", 2)).to eql(2) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1 ], "nil" => nil }) + end + + it "should unlink nil" do + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + expect(vivid.unlink("nil")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ] }) + end + + it "should traverse a nil and safely do nothing" do + expect(root).not_to receive(:reset_cache) + expect(vivid.unlink("nil", "foo")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + end + + context "#unlink!" do + before do + vivid + expect(root).not_to receive(:reset_cache).with(nil) + end + + it "should raise an exception if the keys already don't exist" do + expect(root).not_to receive(:reset_cache) + expect { vivid.unlink!("five", "six", "seven", "eight") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink! hashes" do + expect(root).to receive(:reset_cache).at_least(:once).with("one") + expect( vivid.unlink!("one") ).to eql({ "two" => { "three" => "four" } }) + expect(vivid).to eql({ "array" => [ 0, 1, 2 ], "nil" => nil }) + end + + it "should unlink! array elements" do + expect(root).to receive(:reset_cache).at_least(:once).with("array") + expect(vivid.unlink!("array", 2)).to eql(2) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1 ], "nil" => nil }) + end + + it "should unlink! nil" do + expect(root).to receive(:reset_cache).at_least(:once).with("nil") + expect(vivid.unlink!("nil")).to eql(nil) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ] }) + end + + it "should raise an exception if it traverses a nil" do + expect(root).not_to receive(:reset_cache) + expect { vivid.unlink!("nil", "foo") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + expect(vivid).to eql({ "one" => { "two" => { "three" => "four" } }, "array" => [ 0, 1, 2 ], "nil" => nil }) + end + end +end diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 923b488f72..af9a6e94fc 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -233,60 +233,63 @@ describe Chef::Node do end it "should let you go deep with attribute?" do - node.set["battles"]["people"]["wonkey"] = true + node.normal["battles"]["people"]["wonkey"] = true expect(node["battles"]["people"].attribute?("wonkey")).to eq(true) expect(node["battles"]["people"].attribute?("snozzberry")).to eq(false) end it "does not allow you to set an attribute via method_missing" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { node.sunshine = "is bright" }.to raise_error(Chef::Exceptions::ImmutableAttributeModification) end it "should allow you get get an attribute via method_missing" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default.sunshine = "is bright" expect(node.sunshine).to eql("is bright") end describe "normal attributes" do it "should allow you to set an attribute with set, without pre-declaring a hash" do - node.set[:snoopy][:is_a_puppy] = true + node.normal[:snoopy][:is_a_puppy] = true expect(node[:snoopy][:is_a_puppy]).to eq(true) end it "should allow you to set an attribute with set_unless" do - node.set_unless[:snoopy][:is_a_puppy] = false + node.normal_unless[:snoopy][:is_a_puppy] = false expect(node[:snoopy][:is_a_puppy]).to eq(false) end it "should not allow you to set an attribute with set_unless if it already exists" do - node.set[:snoopy][:is_a_puppy] = true - node.set_unless[:snoopy][:is_a_puppy] = false + node.normal[:snoopy][:is_a_puppy] = true + node.normal_unless[:snoopy][:is_a_puppy] = false expect(node[:snoopy][:is_a_puppy]).to eq(true) end it "should allow you to set an attribute with set_unless if is a nil value" do node.attributes.normal = { snoopy: { is_a_puppy: nil } } - node.set_unless[:snoopy][:is_a_puppy] = false + node.normal_unless[:snoopy][:is_a_puppy] = false expect(node[:snoopy][:is_a_puppy]).to eq(false) end it "should allow you to set a value after a set_unless" do # this tests for set_unless_present state bleeding between statements CHEF-3806 - node.set_unless[:snoopy][:is_a_puppy] = false - node.set[:snoopy][:is_a_puppy] = true + node.normal_unless[:snoopy][:is_a_puppy] = false + node.normal[:snoopy][:is_a_puppy] = true expect(node[:snoopy][:is_a_puppy]).to eq(true) end it "should let you set a value after a 'dangling' set_unless" do # this tests for set_unless_present state bleeding between statements CHEF-3806 - node.set[:snoopy][:is_a_puppy] = "what" - node.set_unless[:snoopy][:is_a_puppy] - node.set[:snoopy][:is_a_puppy] = true + node.normal[:snoopy][:is_a_puppy] = "what" + node.normal_unless[:snoopy][:is_a_puppy] + node.normal[:snoopy][:is_a_puppy] = true expect(node[:snoopy][:is_a_puppy]).to eq(true) end it "auto-vivifies attributes created via method syntax" do - node.set.fuu.bahrr.baz = "qux" + Chef::Config[:treat_deprecation_warnings_as_errors] = false + node.normal.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end @@ -295,6 +298,20 @@ describe Chef::Node do node.tag("three", "four") expect(node["tags"]).to eq(%w{one two three four}) end + + it "set is a deprecated alias for normal" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect(Chef).to receive(:log_deprecation).with(/set is deprecated/) + node.set[:snoopy][:is_a_puppy] = true + expect(node[:snoopy][:is_a_puppy]).to eq(true) + end + + it "set_unless is a deprecated alias for normal_unless" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + expect(Chef).to receive(:log_deprecation).with(/set_unless is deprecated/) + node.set_unless[:snoopy][:is_a_puppy] = false + expect(node[:snoopy][:is_a_puppy]).to eq(false) + end end describe "default attributes" do @@ -329,7 +346,14 @@ describe Chef::Node do expect(node[:snoopy][:is_a_puppy]).to eq(true) end + it "does not exhibit chef/chef/issues/5005 bug" do + node.env_default["a"]["r1"]["g"]["u"] = "u1" + node.default_unless["a"]["r1"]["g"]["r"] = "r" + expect(node["a"]["r1"]["g"]["u"]).to eql("u1") + end + it "auto-vivifies attributes created via method syntax" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end @@ -368,6 +392,7 @@ describe Chef::Node do end it "auto-vivifies attributes created via method syntax" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.override.fuu.bahrr.baz = "qux" expect(node.fuu.bahrr.baz).to eq("qux") end @@ -453,8 +478,9 @@ describe Chef::Node do 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?)") + it "when mistaking arrays for hashes, it considers the value removed and does nothing" do + node.rm("mysql", "server", "port") + expect(node["mysql"]["server"][0]["port"]).to eql(3456) end end end @@ -706,6 +732,7 @@ describe Chef::Node do # describe "deep merge attribute cache edge conditions" do it "does not error with complicated attribute substitution" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default["chef_attribute_hell"]["attr1"] = "attribute1" node.default["chef_attribute_hell"]["attr2"] = "#{node.chef_attribute_hell.attr1}/attr2" expect { node.default["chef_attribute_hell"]["attr3"] = "#{node.chef_attribute_hell.attr2}/attr3" }.not_to raise_error @@ -720,6 +747,7 @@ describe Chef::Node do end it "method interpolation syntax also works" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false node.default["passenger"]["version"] = "4.0.57" node.default["passenger"]["root_path"] = "passenger-#{node['passenger']['version']}" node.default["passenger"]["root_path_2"] = "passenger-#{node.passenger['version']}" @@ -729,6 +757,7 @@ describe Chef::Node do end it "should raise an ArgumentError if you ask for an attribute that doesn't exist via method_missing" do + Chef::Config[:treat_deprecation_warnings_as_errors] = false expect { node.sunshine }.to raise_error(NoMethodError) end @@ -744,6 +773,51 @@ describe Chef::Node do expect(seen_attributes["sunshine"]).to eq("is bright") expect(seen_attributes["canada"]).to eq("is a nice place") end + + describe "functional attribute API" do + # deeper functional testing of this API is in the VividMash spec tests + it "should have an exist? function" do + node.default["foo"]["bar"] = "baz" + expect(node.exist?("foo", "bar")).to be true + expect(node.exist?("bar", "foo")).to be false + end + + it "should have a read function" do + node.override["foo"]["bar"] = "baz" + expect(node.read("foo", "bar")).to eql("baz") + expect(node.read("bar", "foo")).to eql(nil) + end + + it "should have a read! function" do + node.override["foo"]["bar"] = "baz" + expect(node.read!("foo", "bar")).to eql("baz") + expect { node.read!("bar", "foo") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + + it "delegates write(:level) to node.level.write()" do + node.write(:default, "foo", "bar", "baz") + expect(node.default["foo"]["bar"]).to eql("baz") + end + + it "delegates write!(:level) to node.level.write!()" do + node.write!(:default, "foo", "bar", "baz") + expect(node.default["foo"]["bar"]).to eql("baz") + node.default["bar"] = true + expect { node.write!(:default, "bar", "foo", "baz") }.to raise_error(Chef::Exceptions::AttributeTypeMismatch) + end + + it "delegates unlink(:level) to node.level.unlink()" do + node.default["foo"]["bar"] = "baz" + expect(node.unlink(:default, "foo", "bar")).to eql("baz") + expect(node.unlink(:default, "bar", "foo")).to eql(nil) + end + + it "delegates unlink!(:level) to node.level.unlink!()" do + node.default["foo"]["bar"] = "baz" + expect(node.unlink!(:default, "foo", "bar")).to eql("baz") + expect { node.unlink!(:default, "bar", "foo") }.to raise_error(Chef::Exceptions::NoSuchAttribute) + end + end end describe "consuming json" do @@ -789,8 +863,8 @@ describe Chef::Node do it "should add json attributes to the node" do node.consume_external_attrs(@ohai_data, { "one" => "two", "three" => "four" }) - expect(node.one).to eql("two") - expect(node.three).to eql("four") + expect(node["one"]).to eql("two") + expect(node["three"]).to eql("four") end it "should set the tags attribute to an empty array if it is not already defined" do @@ -824,17 +898,17 @@ describe Chef::Node do it "deep merges attributes instead of overwriting them" do node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "four" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "four" } }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "four" } }) node.consume_external_attrs(@ohai_data, "one" => { "abc" => "123" }) node.consume_external_attrs(@ohai_data, "one" => { "two" => { "foo" => "bar" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "four", "foo" => "bar" }, "abc" => "123" }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "four", "foo" => "bar" }, "abc" => "123" }) end it "gives attributes from JSON priority when deep merging" do node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "four" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "four" } }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "four" } }) node.consume_external_attrs(@ohai_data, "one" => { "two" => { "three" => "forty-two" } }) - expect(node.one.to_hash).to eq({ "two" => { "three" => "forty-two" } }) + expect(node["one"].to_hash).to eq({ "two" => { "three" => "forty-two" } }) end end @@ -1036,10 +1110,10 @@ describe Chef::Node do end it "sets attributes from the files" do - expect(node.ldap_server).to eql("ops1prod") - expect(node.ldap_basedn).to eql("dc=hjksolutions,dc=com") - expect(node.ldap_replication_password).to eql("forsure") - expect(node.smokey).to eql("robinson") + expect(node["ldap_server"]).to eql("ops1prod") + expect(node["ldap_basedn"]).to eql("dc=hjksolutions,dc=com") + expect(node["ldap_replication_password"]).to eql("forsure") + expect(node["smokey"]).to eql("robinson") end it "gives a sensible error when attempting to load a missing attributes file" do @@ -1083,8 +1157,8 @@ describe Chef::Node do it "should load a node from a ruby file" do node.from_file(File.expand_path(File.join(CHEF_SPEC_DATA, "nodes", "test.rb"))) expect(node.name).to eql("test.example.com-short") - expect(node.sunshine).to eql("in") - expect(node.something).to eql("else") + expect(node["sunshine"]).to eql("in") + expect(node["something"]).to eql("else") expect(node.run_list).to eq(["operations-master", "operations-monitoring"]) end @@ -1562,4 +1636,19 @@ describe Chef::Node do end end + describe "method_missing handling" do + it "should have an #empty? method via Chef::Node::Attribute" do + node.default["foo"] = "bar" + expect(node.empty?).to be false + end + + it "it should correctly implement #respond_to?" do + expect(node.respond_to?(:empty?)).to be true + end + + it "it should correctly retrieve the method with #method" do + expect(node.method(:empty?)).to be_kind_of(Method) + end + end + end diff --git a/spec/unit/provider/powershell_script_spec.rb b/spec/unit/provider/powershell_script_spec.rb index 5b5c783986..96869ff31c 100644 --- a/spec/unit/provider/powershell_script_spec.rb +++ b/spec/unit/provider/powershell_script_spec.rb @@ -43,8 +43,8 @@ describe Chef::Provider::PowershellScript, "action_run" do allow(Chef::Platform).to receive(:windows_nano_server?).and_return(true) allow(provider).to receive(:is_forced_32bit).and_return(false) os_info_double = double("os_info") - allow(provider.run_context.node.kernel).to receive(:os_info).and_return(os_info_double) - allow(os_info_double).to receive(:system_directory).and_return("C:\\Windows\\system32") + allow(provider.run_context.node["kernel"]).to receive(:[]).with("os_info").and_return(os_info_double) + allow(os_info_double).to receive(:[]).with("system_directory").and_return("C:\\Windows\\system32") end it "sets the -Command flag as the last flag" do @@ -58,8 +58,8 @@ describe Chef::Provider::PowershellScript, "action_run" do allow(Chef::Platform).to receive(:windows_nano_server?).and_return(false) allow(provider).to receive(:is_forced_32bit).and_return(false) os_info_double = double("os_info") - allow(provider.run_context.node.kernel).to receive(:os_info).and_return(os_info_double) - allow(os_info_double).to receive(:system_directory).and_return("C:\\Windows\\system32") + allow(provider.run_context.node["kernel"]).to receive(:[]).with("os_info").and_return(os_info_double) + allow(os_info_double).to receive(:[]).with("system_directory").and_return("C:\\Windows\\system32") end it "sets the -File flag as the last flag" do diff --git a/spec/unit/run_context_spec.rb b/spec/unit/run_context_spec.rb index 7b2a27e9f6..234cd3c9e1 100644 --- a/spec/unit/run_context_spec.rb +++ b/spec/unit/run_context_spec.rb @@ -153,8 +153,8 @@ describe Chef::RunContext do let(:chef_repo_path) { File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")) } let(:node) { node = Chef::Node.new - node.set[:platform] = "ubuntu" - node.set[:platform_version] = "13.04" + node.normal[:platform] = "ubuntu" + node.normal[:platform_version] = "13.04" node.name("testing") node } diff --git a/spec/unit/shell/shell_session_spec.rb b/spec/unit/shell/shell_session_spec.rb index 22f3dc6f0c..259e6096a4 100644 --- a/spec/unit/shell/shell_session_spec.rb +++ b/spec/unit/shell/shell_session_spec.rb @@ -158,7 +158,7 @@ describe Shell::SoloSession do it "keeps json attribs and passes them to the node for consumption" do @session.node_attributes = { "besnard_lakes" => "are_the_dark_horse" } - expect(@session.node.besnard_lakes).to eq("are_the_dark_horse") + expect(@session.node["besnard_lakes"]).to eq("are_the_dark_horse") #pending "1) keep attribs in an ivar 2) pass them to the node 3) feed them to the node on reset" end |