summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-04-22 15:45:22 -0700
committerdanielsdeleo <dan@opscode.com>2013-04-22 15:45:22 -0700
commiteb1ef680f37a846a27604b5aaf28629c452df5c4 (patch)
tree961d02acd8a598db90b62fd023194387cdf89c85
parent5e73cccb87ab5ea24edbf8e058a60a716279ef8b (diff)
parent0839694ab1096122ac91bc3ed2c303797ba84040 (diff)
downloadchef-eb1ef680f37a846a27604b5aaf28629c452df5c4.tar.gz
Merge branch 'CHEF-3432-10-stable' into 10.24-patch
-rw-r--r--chef/lib/chef/resource.rb12
-rw-r--r--chef/spec/unit/lwrp_spec.rb16
-rw-r--r--chef/spec/unit/resource_spec.rb15
3 files changed, 40 insertions, 3 deletions
diff --git a/chef/lib/chef/resource.rb b/chef/lib/chef/resource.rb
index 931334c5b4..696cfcd874 100644
--- a/chef/lib/chef/resource.rb
+++ b/chef/lib/chef/resource.rb
@@ -122,7 +122,10 @@ F
# Track all subclasses of Resource. This is used so names can be looked up
# when attempting to deserialize from JSON. (See: json_compat)
def self.resource_classes
- @resource_classes ||= []
+ # Using a class variable here ensures we have one variable to track
+ # subclasses shared by the entire class hierarchy; without this, each
+ # subclass would have its own list of subclasses.
+ @@resource_classes ||= []
end
# Callback when subclass is defined. Adds subclass to list of subclasses.
@@ -728,8 +731,11 @@ F
# Add log entry if we override an existing light-weight resource.
class_name = convert_to_class_name(rname)
- overriding = Chef::Resource.const_defined?(class_name)
- Chef::Log.info("#{class_name} light-weight resource already initialized -- overriding!") if overriding
+ if Chef::Resource.const_defined?(class_name)
+ Chef::Log.info("#{class_name} light-weight resource already initialized -- overriding!")
+ old_class = Chef::Resource.send(:remove_const, class_name)
+ Chef::Resource.resource_classes.delete(old_class)
+ end
new_resource_class = Class.new self do |cls|
diff --git a/chef/spec/unit/lwrp_spec.rb b/chef/spec/unit/lwrp_spec.rb
index 0bd45769d0..8b0446f375 100644
--- a/chef/spec/unit/lwrp_spec.rb
+++ b/chef/spec/unit/lwrp_spec.rb
@@ -45,6 +45,22 @@ describe "override logging" do
end
end
+ it "removes the old LRWP resource class from the list of resource subclasses [CHEF-3432]" do
+ # CHEF-3432 regression test:
+ # Chef::Resource keeps a list of all subclasses to assist class inflation
+ # for json parsing (see Chef::JSONCompat). When replacing LWRP resources,
+ # we need to ensure the old resource class is remove from that list.
+ Dir[File.expand_path( "lwrp/resources/*", CHEF_SPEC_DATA)].each do |file|
+ Chef::Resource.build_from_file("lwrp", file, nil)
+ end
+ first_lwr_foo_class = Chef::Resource::LwrpFoo
+ Chef::Resource.resource_classes.should include(first_lwr_foo_class)
+ Dir[File.expand_path( "lwrp/resources/*", CHEF_SPEC_DATA)].each do |file|
+ Chef::Resource.build_from_file("lwrp", file, nil)
+ end
+ Chef::Resource.resource_classes.should_not include(first_lwr_foo_class)
+ end
+
end
describe "LWRP" do
diff --git a/chef/spec/unit/resource_spec.rb b/chef/spec/unit/resource_spec.rb
index a3bed5fce2..3c5c9d208e 100644
--- a/chef/spec/unit/resource_spec.rb
+++ b/chef/spec/unit/resource_spec.rb
@@ -35,6 +35,21 @@ describe Chef::Resource do
@resource = Chef::Resource.new("funk", @run_context)
end
+ describe "when inherited" do
+
+ it "adds an entry to a list of subclasses" do
+ subclass = Class.new(Chef::Resource)
+ Chef::Resource.resource_classes.should include(subclass)
+ end
+
+ it "keeps track of subclasses of subclasses" do
+ subclass = Class.new(Chef::Resource)
+ subclass_of_subclass = Class.new(subclass)
+ Chef::Resource.resource_classes.should include(subclass_of_subclass)
+ end
+
+ end
+
describe "when declaring the identity attribute" do
it "has no identity attribute by default" do
Chef::Resource.identity_attr.should be_nil