Permalink
Browse files

Add "force" level precedence to Node::Attribute

  • Loading branch information...
danielsdeleo committed Nov 8, 2012
1 parent 238c212 commit 5f19be6a6bb96ae54507a64ca4742d50ab68d63f
Showing with 53 additions and 2 deletions.
  1. +35 −2 lib/chef/node/attribute.rb
  2. +18 −0 spec/unit/node/attribute_spec.rb
View
@@ -42,24 +42,28 @@ class Attribute < Mash
:@default,
:@env_default,
:@role_default,
+ :@force_default,
:@normal,
:@override,
:@role_override,
:@env_override,
+ :@force_override,
:@automatic
].freeze
DEFAULT_COMPONENTS = [
:@default,
:@env_default,
- :@role_default
+ :@role_default,
+ :@force_default
]
OVERRIDE_COMPONENTS = [
:@override,
:@role_override,
- :@env_override
+ :@env_override,
+ :@force_override
]
attr_reader :serial_number
@@ -154,6 +158,14 @@ def #{delegated_method}(*args, &block)
# return the environment level default attribute component
attr_reader :env_default
+ # return the force_default level attribute component
+ attr_reader :force_default
+
+ # default! is the "advertised" method for force_default, but is
+ # implemented as an alias because instance variables can't (easily) have
+ # +!+ characters.
+ alias :default! :force_default
+
# return the "normal" level attribute component
attr_reader :normal
@@ -166,6 +178,14 @@ def #{delegated_method}(*args, &block)
# return the enviroment level override attribute component
attr_reader :env_override
+ # return the force override level attribute component
+ attr_reader :force_override
+
+ # +override!+ is the "advertised" method for +force_override+ but is
+ # implemented as an alias because instance variables can't easily have
+ # +!+ characters.
+ alias :override! :force_override
+
# return the automatic level attribute component
attr_reader :automatic
@@ -176,12 +196,14 @@ def initialize(normal, default, override, automatic)
@default = VividMash.new(self, default)
@env_default = VividMash.new(self, {})
@role_default = VividMash.new(self, {})
+ @force_default = VividMash.new(self, {})
@normal = VividMash.new(self, normal)
@override = VividMash.new(self, override)
@role_override = VividMash.new(self, {})
@env_override = VividMash.new(self, {})
+ @force_override = VividMash.new(self, {})
@automatic = VividMash.new(self, automatic)
@@ -226,6 +248,12 @@ def env_default=(new_data)
@env_default = VividMash.new(self, new_data)
end
+ # Set the force_default (+default!+) level attributes to +new_data+
+ def force_default=(new_data)
+ reset
+ @force_default = VividMash.new(self, new_data)
+ end
+
# Set the normal level attribute component to +new_data+
def normal=(new_data)
reset
@@ -250,6 +278,11 @@ def env_override=(new_data)
@env_override = VividMash.new(self, new_data)
end
+ def force_override=(new_data)
+ reset
+ @force_override = VividMash.new(self, new_data)
+ end
+
def automatic=(new_data)
reset
@automatic = VividMash.new(self, new_data)
@@ -240,6 +240,13 @@
@attributes.override["override"] = "cookbook override"
end
+ it "prefers 'forced default' over any other default" do
+ @attributes.default!["default"] = "force default"
+ @attributes.role_default["default"] = "role default"
+ @attributes.env_default["default"] = "environment default"
+ @attributes["default"].should == "force default"
+ end
+
it "prefers role_default over environment or cookbook default" do
@attributes.role_default["default"] = "role default"
@attributes.env_default["default"] = "environment default"
@@ -255,6 +262,13 @@
@attributes["default"].should == "cookbook default"
end
+ it "prefers 'forced overrides' over role or cookbook overrides" do
+ @attributes.override!["override"] = "force override"
+ @attributes.env_override["override"] = "environment override"
+ @attributes.role_override["override"] = "role override"
+ @attributes["override"].should == "force override"
+ end
+
it "prefers environment overrides over role or cookbook overrides" do
@attributes.env_override["override"] = "environment override"
@attributes.role_override["override"] = "role override"
@@ -277,21 +291,25 @@
@attributes.default["cd"] = "cookbook default"
@attributes.role_default["rd"] = "role default"
@attributes.env_default["ed"] = "env default"
+ @attributes.default!["fd"] = "force default"
@attributes.override["co"] = "cookbook override"
@attributes.role_override["ro"] = "role override"
@attributes.env_override["eo"] = "env override"
+ @attributes.override!["fo"] = "force override"
end
it "merges all types of overrides into a combined override" do
@attributes.combined_override["co"].should == "cookbook override"
@attributes.combined_override["ro"].should == "role override"
@attributes.combined_override["eo"].should == "env override"
+ @attributes.combined_override["fo"].should == "force override"
end
it "merges all types of defaults into a combined default" do
@attributes.combined_default["cd"].should == "cookbook default"
@attributes.combined_default["rd"].should == "role default"
@attributes.combined_default["ed"].should == "env default"
+ @attributes.combined_default["fd"].should == "force default"
end
end

0 comments on commit 5f19be6

Please sign in to comment.