summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToomas Pelberg <toomas.pelberg@playtech.com>2010-08-09 15:09:02 +0300
committerDaniel DeLeo <dan@opscode.com>2010-08-22 11:37:58 -0700
commitabe2a9ffe2b151f963cd377e675cfb5f509b4a22 (patch)
tree8a39594f8c45c4ae72ad7056e09c946c8ecb3b60
parent907884b6630b4547775c0ea638ccae00eb0cd2fc (diff)
downloadchef-abe2a9ffe2b151f963cd377e675cfb5f509b4a22.tar.gz
Check for existance of keys not only their values ( CHEF-871 )
-rw-r--r--chef/lib/chef/node/attribute.rb9
-rw-r--r--chef/spec/unit/node/attribute_spec.rb18
2 files changed, 25 insertions, 2 deletions
diff --git a/chef/lib/chef/node/attribute.rb b/chef/lib/chef/node/attribute.rb
index a4799f8389..0b70e498a8 100644
--- a/chef/lib/chef/node/attribute.rb
+++ b/chef/lib/chef/node/attribute.rb
@@ -123,6 +123,10 @@ class Chef
end
def has_key?(key)
+ return true if exists_in_hash(@default.to_hash,key)
+ return true if exists_in_hash(@automatic.to_hash,key)
+ return true if exists_in_hash(@normal.to_hash,key)
+ return true if exists_in_hash(@override.to_hash,key)
attribute?(key)
end
@@ -448,6 +452,11 @@ class Chef
end
end
+ def exists_in_hash(data_hash,key)
+ return true if (@state.length == 0 && data_hash.has_key?(key))
+ return true if (data_hash.has_key?(@state.to_s) && data_hash[@state.to_s].respond_to?(:has_key?) && data_hash[@state.to_s].has_key?(key) )
+ end
+
end
end
end
diff --git a/chef/spec/unit/node/attribute_spec.rb b/chef/spec/unit/node/attribute_spec.rb
index 9d4c4b0626..0ca3d171aa 100644
--- a/chef/spec/unit/node/attribute_spec.rb
+++ b/chef/spec/unit/node/attribute_spec.rb
@@ -188,7 +188,7 @@ describe Chef::Node::Attribute do
"os_version"=>"9.7.0",
"hostname"=>"latte",
"macaddress"=>"00:23:6c:7f:67:6c",
- "music" => { "jimmy_eat_world" => "nice" }
+ "music" => { "jimmy_eat_world" => "nice", "apophis" => false }
}
@default_hash = {
"domain" => "opscode.com",
@@ -196,7 +196,8 @@ describe Chef::Node::Attribute do
"music" => {
"jimmy_eat_world" => "is fun!",
"mastodon" => "rocks",
- "mars_volta" => "is loud and nutty"
+ "mars_volta" => "is loud and nutty",
+ "gates_of_ishtar" => nil
}
}
@override_hash = {
@@ -415,6 +416,19 @@ describe Chef::Node::Attribute do
@attributes.has_key?("ninja").should == false
end
+ it "should return false if an attribute does not exist using dot notation" do
+ @attributes.has_key?("does_not_exist_at_all").should == false
+ end
+
+ it "should return true if an attribute exists but is set to nil using dot notation" do
+ @attributes.music.has_key?("gates_of_ishtar").should == true
+ end
+
+ it "should return true if an attribute exists but is set to false" do
+ @attributes["music"]
+ @attributes.has_key?("apophis").should == true
+ end
+
it "should be looking at the current position of the object" do
@attributes["music"]
@attributes.has_key?("mastodon").should == true