From 01dde6e1958d236d1e2bd70b3b7efb263bac6daf Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Tue, 29 Jan 2013 15:34:57 -0800 Subject: [CHEF-3806] add attribute value debug --- lib/chef/node/attribute.rb | 28 ++++++++++++++++++++++++++++ spec/unit/node/attribute_spec.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb index e2bb8b9cf9..a417406721 100644 --- a/lib/chef/node/attribute.rb +++ b/lib/chef/node/attribute.rb @@ -209,6 +209,34 @@ class Chef @combined_default = nil end + # Debug what's going on with an attribute. +args+ is a path spec to the + # attribute you're interested in. For example, to debug where the value + # of `node[:network][:default_interface]` is coming from, use: + # debug_value(:network, :default_interface). + # The return value is an Array of Arrays. The first element is + # `["set_unless_enabled?", Boolean]`, which describes whether the + # attribute collection is in "set_unless" mode. The rest of the Arrays + # are pairs of `["precedence_level", value]`, where precedence level is + # the component, such as role default, normal, etc. and value is the + # attribute value set at that precedence level. If there is no value at + # that precedence level, +value+ will be the symbol +:not_present+. + def debug_value(*args) + components = COMPONENTS.map do |component| + ivar = instance_variable_get(component) + value = args.inject(ivar) do |so_far, key| + if so_far == :not_present + :not_present + elsif so_far.has_key?(key) + so_far[key] + else + :not_present + end + end + [component.to_s.sub(/^@/,""), value] + end + [["set_unless_enabled?", @set_unless_present]] + components + end + # Enables or disables `||=`-like attribute setting. See, e.g., Node#set_unless def set_unless_value_present=(setting) @set_unless_present = setting diff --git a/spec/unit/node/attribute_spec.rb b/spec/unit/node/attribute_spec.rb index 435dc3230c..5aa563aedf 100644 --- a/spec/unit/node/attribute_spec.rb +++ b/spec/unit/node/attribute_spec.rb @@ -247,6 +247,37 @@ describe Chef::Node::Attribute do end + describe "when debugging attributes" do + before do + @attributes.default[:foo][:bar] = "default" + @attributes.env_default[:foo][:bar] = "env_default" + @attributes.role_default[:foo][:bar] = "role_default" + @attributes.force_default[:foo][:bar] = "force_default" + @attributes.normal[:foo][:bar] = "normal" + @attributes.override[:foo][:bar] = "override" + @attributes.role_override[:foo][:bar] = "role_override" + @attributes.env_override[:foo][:bar] = "env_override" + @attributes.force_override[:foo][:bar] = "force_override" + @attributes.automatic[:foo][:bar] = "automatic" + end + + it "gives the value at each level of precedence for a path spec" do + expected = [["set_unless_enabled?", false], + ["default", "default"], + ["env_default", "env_default"], + ["role_default", "role_default"], + ["force_default", "force_default"], + ["normal", "normal"], + ["override", "override"], + ["role_override", "role_override"], + ["env_override", "env_override"], + ["force_override", "force_override"], + ["automatic", "automatic"] + ] + @attributes.debug_value(:foo, :bar).should == expected + end + end + describe "when fetching values based on precedence" do before do @attributes.default["default"] = "cookbook default" -- cgit v1.2.1