diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-12-14 16:17:22 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-12-14 16:17:22 -0800 |
commit | 5d16e1847ad20818651af2b39c08285d94772e69 (patch) | |
tree | f48c2d62d58c100b49f057feae3f16f63b530e1d | |
parent | 7814a405d629b612cd0b93266db79e2427f59d7d (diff) | |
download | chef-5d16e1847ad20818651af2b39c08285d94772e69.tar.gz |
tags always an array; fix set_unless
previously if the node had a { normal: { tags: nil } } that we read
since the key existed (but was nil) we would not initialize with an
array. replacing the code with a call to node.set_unless revealed that
set_unless was similarly buggy. fixed both issues by fixing set_unless.
-rw-r--r-- | lib/chef/node.rb | 4 | ||||
-rw-r--r-- | lib/chef/node/attribute.rb | 2 | ||||
-rw-r--r-- | lib/chef/node/attribute_collections.rb | 4 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 14 |
4 files changed, 18 insertions, 6 deletions
diff --git a/lib/chef/node.rb b/lib/chef/node.rb index b04e607624..b2ef3aaf2f 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -3,7 +3,7 @@ # Author:: Christopher Brown (<cb@opscode.com>) # Author:: Christopher Walters (<cw@opscode.com>) # Author:: Tim Hinderliter (<tim@opscode.com>) -# Copyright:: Copyright (c) 2008-2011 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -372,7 +372,7 @@ class Chef # Lazy initializer for tags attribute def tags - normal[:tags] = [] unless attribute?(:tags) + normal_unless[:tags] = [] normal[:tags] end diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb index 45f2ef01ee..7ffac40bf4 100644 --- a/lib/chef/node/attribute.rb +++ b/lib/chef/node/attribute.rb @@ -1,7 +1,7 @@ #-- # Author:: Adam Jacob (<adam@opscode.com>) # Author:: AJ Christensen (<aj@opscode.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/lib/chef/node/attribute_collections.rb b/lib/chef/node/attribute_collections.rb index b912904534..1dcc950c6e 100644 --- a/lib/chef/node/attribute_collections.rb +++ b/lib/chef/node/attribute_collections.rb @@ -1,6 +1,6 @@ #-- # Author:: Daniel DeLeo (<dan@opscode.com>) -# Copyright:: Copyright (c) 2012 Opscode, Inc. +# Copyright:: Copyright (c) 2012-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -148,7 +148,7 @@ class Chef def []=(key, value) root.top_level_breadcrumb ||= key - if set_unless? && key?(key) + if set_unless? && key?(key) && !self[key].nil? self[key] else root.reset_cache(root.top_level_breadcrumb) diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index 4b57a93009..40e8cf8898 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@opscode.com>) -# Copyright:: Copyright (c) 2008 Opscode, Inc. +# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -264,6 +264,12 @@ describe Chef::Node do 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 + 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 @@ -798,6 +804,12 @@ describe Chef::Node do expect(node.tags).to eql([ "radiohead" ]) end + it "should set the tags attribute to an empty array if it is nil" do + node.attributes.normal = { 'tags' => nil } + node.consume_external_attrs(@ohai_data, {}) + expect(node.tags).to eql([]) + end + 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"}}) |