diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2018-02-14 17:10:16 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2018-02-15 11:55:15 -0800 |
commit | bb87713458370485fb91be691356086fb98f055f (patch) | |
tree | de2edac6de5d9c7275a2b126e8d261a1ff96f6c6 | |
parent | e468c26794fb62a103e1fba3ccaf98f311c79e4d (diff) | |
download | chef-bb87713458370485fb91be691356086fb98f055f.tar.gz |
fix attributes specs + make run faster
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/node.rb | 10 | ||||
-rw-r--r-- | lib/chef/node/attribute.rb | 91 | ||||
-rw-r--r-- | spec/unit/node/attribute_spec.rb | 13 |
3 files changed, 73 insertions, 41 deletions
diff --git a/lib/chef/node.rb b/lib/chef/node.rb index 9540b367d8..8fd809067d 100644 --- a/lib/chef/node.rb +++ b/lib/chef/node.rb @@ -180,6 +180,9 @@ class Chef policy_group(policy_group) end + # @api private + attr_writer :attributes + def attributes @attributes ||= Chef::Node::Attribute.new({}, {}, {}, {}, self) end @@ -516,13 +519,12 @@ class Chef node = new node.name(o["name"]) node.chef_environment(o["chef_environment"]) + if o.has_key?("attributes") node.normal_attrs = o["attributes"] end - node.automatic_attrs = Mash.new(o["automatic"]) if o.has_key?("automatic") - node.normal_attrs = Mash.new(o["normal"]) if o.has_key?("normal") - node.default_attrs = Mash.new(o["default"]) if o.has_key?("default") - node.override_attrs = Mash.new(o["override"]) if o.has_key?("override") + + node.attributes = Chef::Node::Attribute.new(o["normal"] || o["attributes"], o["default"], o["override"], o["automatic"], node) if o.has_key?("run_list") node.run_list.reset!(o["run_list"]) diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb index 238d3744d7..44ea157bc9 100644 --- a/lib/chef/node/attribute.rb +++ b/lib/chef/node/attribute.rb @@ -188,21 +188,30 @@ class Chef attr_reader :automatic def initialize(normal, default, override, automatic, node = nil) - @default = VividMash.new(default, self, node, :default) - @env_default = VividMash.new({}, self, node, :env_default) - @role_default = VividMash.new({}, self, node, :role_default) - @force_default = VividMash.new({}, self, node, :force_default) + # Chef::Node and Chef::Node::Attribute are very tightly coupled + @__node__ = node + node.attributes = self - @normal = VividMash.new(normal, self, node, :normal) + defer_cache_resetting do + @default = VividMash.new({}, self, node, :default) + @env_default = VividMash.new({}, self, node, :env_default) + @role_default = VividMash.new({}, self, node, :role_default) + @force_default = VividMash.new({}, self, node, :force_default) - @override = VividMash.new(override, self, node, :override) - @role_override = VividMash.new({}, self, node, :role_override) - @env_override = VividMash.new({}, self, node, :env_override) - @force_override = VividMash.new({}, self, node, :force_override) + @normal = VividMash.new({}, self, node, :normal) - @automatic = VividMash.new(automatic, self, node, :automatic) + @override = VividMash.new({}, self, node, :override) + @role_override = VividMash.new({}, self, node, :role_override) + @env_override = VividMash.new({}, self, node, :env_override) + @force_override = VividMash.new({}, self, node, :force_override) - @__node__ = node + @automatic = VividMash.new({}, self, node, :automatic) + + @default = VividMash.new(default, self, node, :default) + @normal = VividMash.new(normal, self, node, :normal) + @override = VividMash.new(override, self, node, :override) + @automatic = VividMash.new(automatic, self, node, :automatic) + end end def deep_merge_cache @@ -239,6 +248,7 @@ class Chef end def reset_cache(*path) + return if @disable_cache_reset if path.empty? reset else @@ -255,60 +265,70 @@ class Chef # Set the cookbook level default attribute component to +new_data+. def default=(new_data) - reset - @default = VividMash.new(new_data, self, __node__, :default) + defer_cache_resetting do + @default = VividMash.new(new_data, self, __node__, :default) + end end # Set the role level default attribute component to +new_data+ def role_default=(new_data) - reset - @role_default = VividMash.new(new_data, self, __node__, :role_default) + defer_cache_resetting do + @role_default = VividMash.new(new_data, self, __node__, :role_default) + end end # Set the environment level default attribute component to +new_data+ def env_default=(new_data) - reset - @env_default = VividMash.new(new_data, self, __node__, :env_default) + defer_cache_resetting do + @env_default = VividMash.new(new_data, self, __node__, :env_default) + end end # Set the force_default (+default!+) level attributes to +new_data+ def force_default=(new_data) - reset - @force_default = VividMash.new(new_data, self, __node__, :force_default) + defer_cache_resetting do + @force_default = VividMash.new(new_data, self, __node__, :force_default) + end end # Set the normal level attribute component to +new_data+ def normal=(new_data) - reset - @normal = VividMash.new(new_data, self, __node__, :normal) + defer_cache_resetting do + @normal = VividMash.new(new_data, self, __node__, :normal) + end end # Set the cookbook level override attribute component to +new_data+ def override=(new_data) - reset - @override = VividMash.new(new_data, self, __node__, :override) + defer_cache_resetting do + @override = VividMash.new(new_data, self, __node__, :override) + end end # Set the role level override attribute component to +new_data+ def role_override=(new_data) - reset - @role_override = VividMash.new(new_data, self, __node__, :role_override) + defer_cache_resetting do + @role_override = VividMash.new(new_data, self, __node__, :role_override) + end end # Set the environment level override attribute component to +new_data+ def env_override=(new_data) - reset - @env_override = VividMash.new(new_data, self, __node__, :env_override) + defer_cache_resetting do + @env_override = VividMash.new(new_data, self, __node__, :env_override) + end end def force_override=(new_data) - reset - @force_override = VividMash.new(new_data, self, __node__, :force_override) + defer_cache_resetting do + @force_override = VividMash.new(new_data, self, __node__, :force_override) + end end def automatic=(new_data) - reset - @automatic = VividMash.new(new_data, self, __node__, :automatic) + defer_cache_resetting do + @automatic = VividMash.new(new_data, self, __node__, :automatic) + end end # @@ -649,6 +669,15 @@ class Chef end end + # avoid doing cache clearing work for an entire block of code, then nuke the entire cache + # @api private + def defer_cache_resetting + @disable_cache_reset = true + yield + @disable_cache_reset = false + reset + end + end end end diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index fabc491deb..2da5308092 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -21,11 +21,13 @@ require "spec_helper" require "chef/node/attribute" describe Chef::Node::Attribute do - let(:events) { instance_double(Chef::EventDispatch::Dispatcher) } - let(:run_context) { instance_double(Chef::RunContext, :events => events) } - let(:node) { instance_double(Chef::Node, :run_context => run_context) } + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:attributes) { Chef::Node::Attribute.new(@attribute_hash, @default_hash, @override_hash, @automatic_hash, node) } + before(:each) do - allow(events).to receive(:attribute_changed) + run_context # de-lazy the run_context + the event object + wire to the node @attribute_hash = { "dmi" => {}, "command" => { "ps" => "ps -ef" }, @@ -170,8 +172,7 @@ describe Chef::Node::Attribute do }, } @automatic_hash = { "week" => "friday" } - @attributes = Chef::Node::Attribute.new(@attribute_hash, @default_hash, @override_hash, @automatic_hash, node) - allow(node).to receive(:attributes).and_return(@attributes) + @attributes = attributes end describe "initialize" do |