summaryrefslogtreecommitdiff
path: root/chef
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2012-10-19 14:50:11 -0700
committerdanielsdeleo <dan@opscode.com>2012-10-19 16:00:15 -0700
commitbf103e81ee679d6a24cab0b6c100e069abf4eb78 (patch)
tree253db91ee0679bf3e84c61c9554c862946ea141b /chef
parent3230ec8da18b8bca42c58d3effeaa7577e931880 (diff)
downloadchef-bf103e81ee679d6a24cab0b6c100e069abf4eb78.tar.gz
[CHEF-2737] add more tests for knockout detection
Diffstat (limited to 'chef')
-rw-r--r--chef/lib/chef/mixin/deep_merge.rb11
-rw-r--r--chef/spec/unit/mixin/deep_merge_spec.rb20
2 files changed, 30 insertions, 1 deletions
diff --git a/chef/lib/chef/mixin/deep_merge.rb b/chef/lib/chef/mixin/deep_merge.rb
index 12052a1461..c5bbc8d9e6 100644
--- a/chef/lib/chef/mixin/deep_merge.rb
+++ b/chef/lib/chef/mixin/deep_merge.rb
@@ -29,7 +29,13 @@ class Chef
class InvalidSubtractiveMerge < ArgumentError; end
+
OLD_KNOCKOUT_PREFIX = "!merge:".freeze
+
+ # Regex to match the "knockout prefix" that was used to indicate
+ # subtractive merging in Chef 10.x and previous. Subtractive merging is
+ # removed as of Chef 11, but we detect attempted use of it and raise an
+ # error (see: raise_if_knockout_used!)
OLD_KNOCKOUT_MATCH = %r[!merge].freeze
extend self
@@ -73,6 +79,7 @@ class Chef
end
raise_if_knockout_used!(source)
+ raise_if_knockout_used!(dest)
case source
when nil
dest
@@ -103,12 +110,16 @@ class Chef
dest
end # deep_merge!
+ # Checks for attempted use of subtractive merge, which was removed for
+ # Chef 11.0. If subtractive merge use is detected, will raise an
+ # InvalidSubtractiveMerge exception.
def raise_if_knockout_used!(obj)
if uses_knockout?(obj)
raise InvalidSubtractiveMerge, "subtractive merge with !merge is no longer supported"
end
end
+ # Checks for attempted use of subtractive merge in +obj+.
def uses_knockout?(obj)
case obj
when String
diff --git a/chef/spec/unit/mixin/deep_merge_spec.rb b/chef/spec/unit/mixin/deep_merge_spec.rb
index 2e759261c0..cbc9b1544f 100644
--- a/chef/spec/unit/mixin/deep_merge_spec.rb
+++ b/chef/spec/unit/mixin/deep_merge_spec.rb
@@ -287,10 +287,28 @@ describe Chef::Mixin::DeepMerge do
end
describe "role_merge" do
- it "errors out if knockout merge use is detected" do
+ it "errors out if knockout merge use is detected in an array" do
hash_dst = {"property" => ["2","4"]}
hash_src = {"property" => ["1","!merge:4"]}
lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge)
end
+
+ it "errors out if knockout merge use is detected in an array (reversed merge order)" do
+ hash_dst = {"property" => ["1","!merge:4"]}
+ hash_src = {"property" => ["2","4"]}
+ lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge)
+ end
+
+ it "errors out if knockout merge use is detected in a string" do
+ hash_dst = {"property" => ["2","4"]}
+ hash_src = {"property" => "!merge"}
+ lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge)
+ end
+
+ it "errors out if knockout merge use is detected in a string (reversed merge order)" do
+ hash_dst = {"property" => "!merge"}
+ hash_src= {"property" => ["2","4"]}
+ lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge)
+ end
end
end