From 413b136671232a8a0a9e27c18c1b6547241276e7 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Fri, 10 Jun 2011 10:48:36 -0700 Subject: (#4416) Always remove old provider before recreating it In the case where provider class evaluation failed midway, the provider class would be created but not registered. Thus, when checking whether it should be removed, it wasn't found, and wasn't removed. This caused it to then fail to be recreated, because it collided with the existing class. Now we don't bother checking whether the provider is registered before we remove it, since rmclass has the appropriate checks to do the unregistration, and class removal safely. Removing a provider class that has been created but not registered should not be a problem since the only time this can happen is when the class is unusable because of parsing or other fatal errors in the provider itself. Paired-with: Jacob Helwig --- spec/unit/type_spec.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'spec') diff --git a/spec/unit/type_spec.rb b/spec/unit/type_spec.rb index 9b1f20500..442c759e7 100755 --- a/spec/unit/type_spec.rb +++ b/spec/unit/type_spec.rb @@ -165,6 +165,72 @@ describe Puppet::Type do end end + describe "when creating a provider" do + before :each do + @type = Puppet::Type.newtype(:provider_test_type) + end + + after :each do + @type.provider_hash.clear + end + + it "should create a subclass of Puppet::Provider for the provider" do + provider = @type.provide(:test_provider) + + provider.ancestors.should include(Puppet::Provider) + end + + it "should use a parent class if specified" do + parent_provider = @type.provide(:parent_provider) + child_provider = @type.provide(:child_provider, :parent => parent_provider) + + child_provider.ancestors.should include(parent_provider) + end + + it "should use a parent class if specified by name" do + parent_provider = @type.provide(:parent_provider) + child_provider = @type.provide(:child_provider, :parent => :parent_provider) + + child_provider.ancestors.should include(parent_provider) + end + + it "should raise an error when the parent class can't be found" do + expect { + @type.provide(:child_provider, :parent => :parent_provider) + }.to raise_error(Puppet::DevError, /Could not find parent provider.+parent_provider/) + end + + it "should ensure its type has a 'provider' parameter" do + @type.provide(:test_provider) + + @type.parameters.should include(:provider) + end + + it "should remove a previously registered provider with the same name" do + old_provider = @type.provide(:test_provider) + new_provider = @type.provide(:test_provider) + + old_provider.should_not equal(new_provider) + end + + it "should register itself as a provider for the type" do + provider = @type.provide(:test_provider) + + provider.should == @type.provider(:test_provider) + end + + it "should create a provider when a provider with the same name previously failed" do + @type.provide(:test_provider) do + raise "failed to create this provider" + end rescue nil + + provider = @type.provide(:test_provider) + + provider.ancestors.should include(Puppet::Provider) + provider.should == @type.provider(:test_provider) + end + end + describe "when choosing a default provider" do it "should choose the provider with the highest specificity" do # Make a fake type -- cgit From caca469976cc8b5ff6c7f68d7324eecd35399176 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Fri, 10 Jun 2011 10:58:19 -0700 Subject: (#4416) Ensure types are providified after reloading Previously, the 'provider' parameter for a type was only added when creating a provider for that type. This would cause a type to forget about its 'provider' parameter when only the type was reloaded. This was manifesting itself in pluginsync, when a provider plugin would be loaded before its type, causing the type to be autoloaded. The type plugin would then be loaded again by the plugin handler. Because the type => provider information is stored separately from the type, the providers don't need to be reloaded, and thus don't recreate the type's 'provider' parameter. Now we always "providify" the type (add its 'provider' parameter) upon creation, after trying to load its providers, if any providers are present. Paired-with: Jacob Helwig --- spec/integration/type_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'spec') diff --git a/spec/integration/type_spec.rb b/spec/integration/type_spec.rb index 957dfe344..74e268a3c 100755 --- a/spec/integration/type_spec.rb +++ b/spec/integration/type_spec.rb @@ -19,4 +19,15 @@ describe Puppet::Type do type.provider(:myprovider).should equal(provider) end + + it "should not lose its provider parameter when it is reloaded" do + type = Puppet::Type.newtype(:reload_test_type) + + provider = type.provide(:test_provider) + + # reload it + type = Puppet::Type.newtype(:reload_test_type) + + type.parameters.should include(:provider) + end end -- cgit