summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2018-02-14 17:10:16 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2018-02-21 14:32:18 -0800
commit61286a1e775910711a6c31c01ea3a7bf748252bd (patch)
tree6d777b83f478d9af5f7438928ad6ddebf9f69640
parent39d760cbbe8ffe65dd5d3f25fc7a56f508e59f44 (diff)
downloadchef-61286a1e775910711a6c31c01ea3a7bf748252bd.tar.gz
fix attributes specs + make run faster
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--lib/chef/node.rb10
-rw-r--r--lib/chef/node/attribute.rb91
-rw-r--r--spec/unit/node/attribute_spec.rb13
3 files changed, 73 insertions, 41 deletions
diff --git a/lib/chef/node.rb b/lib/chef/node.rb
index 0a96787849..9e490d9286 100644
--- a/lib/chef/node.rb
+++ b/lib/chef/node.rb
@@ -179,6 +179,9 @@ class Chef
policy_group(policy_group)
end
+ # @api private
+ attr_writer :attributes
+
def attributes
@attributes ||= Chef::Node::Attribute.new({}, {}, {}, {}, self)
end
@@ -509,13 +512,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 dd6f645dfa..3338a07b2f 100644
--- a/lib/chef/node/attribute.rb
+++ b/lib/chef/node/attribute.rb
@@ -186,21 +186,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
@@ -233,6 +242,7 @@ class Chef
end
def reset_cache(*path)
+ return if @disable_cache_reset
if path.empty?
reset
else
@@ -249,60 +259,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
#
@@ -619,6 +639,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 97483524ae..de8f3adb52 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