summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2013-04-18 12:53:20 -0700
committerdanielsdeleo <dan@opscode.com>2013-04-18 12:53:20 -0700
commit2cc7d616caccc2fd0cb0e0b851e05ba33b95af80 (patch)
tree8841fbbffb77417e980bc062f4cae59be105b8f0
parent20e2b37faf73a41b80b47d9f0e3172f5c97723d4 (diff)
downloadchef-2cc7d616caccc2fd0cb0e0b851e05ba33b95af80.tar.gz
[CHEF-3432] fix leak of LWRP resource classes
-rw-r--r--lib/chef/resource/lwrp_base.rb9
-rw-r--r--spec/unit/lwrp_spec.rb18
2 files changed, 24 insertions, 3 deletions
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb
index 8a714e75b7..370ccd8c10 100644
--- a/lib/chef/resource/lwrp_base.rb
+++ b/lib/chef/resource/lwrp_base.rb
@@ -39,8 +39,13 @@ class Chef
# 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 Resource.const_defined?(class_name)
+ old_class = Resource.send(:remove_const, class_name)
+ # CHEF-3432 -- Chef::Resource keeps a list of subclasses; need to
+ # remove old ones from the list when replacing.
+ resource_classes.delete(old_class)
+ Chef::Log.info("#{class_name} light-weight resource already initialized -- overriding!")
+ end
resource_class = Class.new(self)
diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb
index 14501c51c7..3e807bc06c 100644
--- a/spec/unit/lwrp_spec.rb
+++ b/spec/unit/lwrp_spec.rb
@@ -34,7 +34,7 @@ describe "LWRP" do
$stderr.stub!(:write)
end
- it "should log if attempting to load resource of same name", :focus do
+ it "should log if attempting to load resource of same name" do
Dir[File.expand_path( "lwrp/resources/*", CHEF_SPEC_DATA)].each do |file|
Chef::Resource::LWRPBase.build_from_file("lwrp", file, nil)
end
@@ -56,6 +56,22 @@ describe "LWRP" 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::LWRPBase.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::LWRPBase.build_from_file("lwrp", file, nil)
+ end
+ Chef::Resource.resource_classes.should_not include(first_lwr_foo_class)
+ end
+
end
describe "Lightweight Chef::Resource" do