diff options
| author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-08 14:16:39 -0800 |
|---|---|---|
| committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-08 14:16:39 -0800 |
| commit | 4650185d366874c175bd043d11fad6244ba33e6f (patch) | |
| tree | cacaeed332ba97b480833a82da218e9328234297 /spec/unit/lwrp_spec.rb | |
| parent | 416ab0c10f510c7d51d4c6c35741d0cca2a0bc21 (diff) | |
| download | chef-4650185d366874c175bd043d11fad6244ba33e6f.tar.gz | |
Chef-13: Chef::Resource cleanup
most of this deletes useless old code.
the change to lookup_provider_constant changes to more strictly
stop using class name based lookups and to go through the resource
resolver and provider resolver.
as a result in order to find the provider class for a given dsl name
you have to go through the resource resolver to find the resource in
order to be able to pass a resource instance through the provider
resolver. since the provider resolver api passes resources into blocks
passed into provides api you must construct a resource instance.
that means that providers need to be associated with resources in order
to be looked up (which makes sense in Real Life(tm) use of Chef, but
breaks quite a few lazy tests we had where we constructed providers
without doing the work of wrapping them in a resource.
note that as the deploy resource shows this filters into a changed
behavior of the `provider` syntax where before `provider :revision`
would look up Chef::Provider::Deploy::Revision via class-name based
magic. this breaks that API so that `provider :deploy_revision` is
used instead -- the symbol (or string) there is turned into a resource
first via the Chef::ResourceResolver and then looked up via the
Chef::ProviderResolver into Chef::Provider::Deploy::Revision. this
is a breaking change but is also a bug fix so that the symbol here
goes through the same lookup that you get when you type it in the DSL.
i had considered implementing a lookup from a resource_name symbol to
a provider, but in looking at how to implement that in the
ProviderResolver the issue is that we really need to have a resource
instance order to pass to the ProviderResolver.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'spec/unit/lwrp_spec.rb')
| -rw-r--r-- | spec/unit/lwrp_spec.rb | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index e167adb04c..28773a3c30 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -41,14 +41,11 @@ describe "LWRP" do Chef::ResourceResolver.resolve(name) end - def get_lwrp_provider(name) - old_treat_deprecation_warnings_as_errors = Chef::Config[:treat_deprecation_warnings_as_errors] - Chef::Config[:treat_deprecation_warnings_as_errors] = false - begin - Chef::Provider.const_get(convert_to_class_name(name.to_s)) - ensure - Chef::Config[:treat_deprecation_warnings_as_errors] = old_treat_deprecation_warnings_as_errors - end + def get_dynamic_lwrp_provider(name) + # need a node to do dynamic lookup, so also need a run_context and a resource instance + node = Chef::Node.new + run_context = Chef::RunContext.new(node, {}, nil) + Chef::Resource.new("name", run_context).lookup_provider_constant(name) end describe "when overriding an existing class" do @@ -386,7 +383,7 @@ describe "LWRP" do let(:runner) { Chef::Runner.new(run_context) } - let(:lwrp_cookbok_name) { "lwrp" } + let(:lwrp_cookbook_name) { "lwrp" } before do Chef::Provider::LWRPBase.class_eval { @loaded_lwrps = {} } @@ -394,18 +391,18 @@ describe "LWRP" do before(:each) do Dir[File.expand_path(File.expand_path("../../data/lwrp/resources/*", __FILE__))].each do |file| - Chef::Resource::LWRPBase.build_from_file(lwrp_cookbok_name, file, run_context) + Chef::Resource::LWRPBase.build_from_file(lwrp_cookbook_name, file, run_context) end Dir[File.expand_path(File.expand_path("../../data/lwrp/providers/*", __FILE__))].each do |file| - Chef::Provider::LWRPBase.build_from_file(lwrp_cookbok_name, file, run_context) + Chef::Provider::LWRPBase.build_from_file(lwrp_cookbook_name, file, run_context) end end it "should properly handle a new_resource reference" do resource = get_lwrp(:lwrp_foo).new("morpheus", run_context) resource.monkey("bob") - resource.provider(get_lwrp_provider(:lwrp_monkey_name_printer)) + resource.provider(get_dynamic_lwrp_provider(:lwrp_monkey_name_printer)) provider = resource.provider_for_action(:twiddle_thumbs) provider.action_twiddle_thumbs end @@ -426,8 +423,8 @@ describe "LWRP" do end it "should create a method for each action" do - expect(get_lwrp_provider(:lwrp_buck_passer).instance_methods).to include(:action_pass_buck) - expect(get_lwrp_provider(:lwrp_thumb_twiddler).instance_methods).to include(:action_twiddle_thumbs) + expect(get_dynamic_lwrp_provider(:lwrp_buck_passer).instance_methods).to include(:action_pass_buck) + expect(get_dynamic_lwrp_provider(:lwrp_thumb_twiddler).instance_methods).to include(:action_twiddle_thumbs) end it "sets itself as a provider for a resource of the same name" do @@ -435,30 +432,30 @@ describe "LWRP" do # we bypass the per-file loading to get the file to load each time, # which creates the LWRP class repeatedly. New things get prepended to # the list of providers. - expect(found_providers.first).to eq(get_lwrp_provider(:lwrp_buck_passer)) + expect(found_providers.first).to eq(get_dynamic_lwrp_provider(:lwrp_buck_passer)) end context "with a cookbook with an underscore in the name" do - let(:lwrp_cookbok_name) { "l_w_r_p" } + let(:lwrp_cookbook_name) { "l_w_r_p" } it "sets itself as a provider for a resource of the same name" do found_providers = Chef::Platform::ProviderHandlerMap.instance.list(node, :l_w_r_p_buck_passer) expect(found_providers.size).to eq(1) - expect(found_providers.last).to eq(get_lwrp_provider(:l_w_r_p_buck_passer)) + expect(found_providers.last).to eq(get_dynamic_lwrp_provider(:l_w_r_p_buck_passer)) end end context "with a cookbook with a hypen in the name" do - let(:lwrp_cookbok_name) { "l-w-r-p" } + let(:lwrp_cookbook_name) { "l-w-r-p" } it "sets itself as a provider for a resource of the same name" do incorrect_providers = Chef::Platform::ProviderHandlerMap.instance.list(node, :'l-w-r-p_buck_passer') expect(incorrect_providers).to eq([]) found_providers = Chef::Platform::ProviderHandlerMap.instance.list(node, :l_w_r_p_buck_passer) - expect(found_providers.first).to eq(get_lwrp_provider(:l_w_r_p_buck_passer)) + expect(found_providers.first).to eq(get_dynamic_lwrp_provider(:l_w_r_p_buck_passer)) end end end @@ -466,7 +463,7 @@ describe "LWRP" do it "should insert resources embedded in the provider into the middle of the resource collection" do injector = get_lwrp(:lwrp_foo).new("morpheus", run_context) injector.action(:pass_buck) - injector.provider(get_lwrp_provider(:lwrp_buck_passer)) + injector.provider(get_dynamic_lwrp_provider(:lwrp_buck_passer)) dummy = Chef::Resource::ZenMaster.new("keanu reeves", run_context) dummy.provider(Chef::Provider::Easy) run_context.resource_collection.insert(injector) @@ -483,11 +480,11 @@ describe "LWRP" do it "should insert embedded resources from multiple providers, including from the last position, properly into the resource collection" do injector = get_lwrp(:lwrp_foo).new("morpheus", run_context) injector.action(:pass_buck) - injector.provider(get_lwrp_provider(:lwrp_buck_passer)) + injector.provider(get_dynamic_lwrp_provider(:lwrp_buck_passer)) injector2 = get_lwrp(:lwrp_bar).new("tank", run_context) injector2.action(:pass_buck) - injector2.provider(get_lwrp_provider(:lwrp_buck_passer_2)) + injector2.provider(get_dynamic_lwrp_provider(:lwrp_buck_passer_2)) dummy = Chef::Resource::ZenMaster.new("keanu reeves", run_context) dummy.provider(Chef::Provider::Easy) @@ -510,7 +507,7 @@ describe "LWRP" do it "should properly handle a new_resource reference" do resource = get_lwrp(:lwrp_foo).new("morpheus", run_context) resource.monkey("bob") - resource.provider(get_lwrp_provider(:lwrp_monkey_name_printer)) + resource.provider(get_dynamic_lwrp_provider(:lwrp_monkey_name_printer)) provider = resource.provider_for_action(:twiddle_thumbs) provider.action_twiddle_thumbs @@ -521,7 +518,7 @@ describe "LWRP" do it "should properly handle an embedded Resource accessing the enclosing Provider's scope" do resource = get_lwrp(:lwrp_foo).new("morpheus", run_context) resource.monkey("bob") - resource.provider(get_lwrp_provider(:lwrp_embedded_resource_accesses_providers_scope)) + resource.provider(get_dynamic_lwrp_provider(:lwrp_embedded_resource_accesses_providers_scope)) provider = resource.provider_for_action(:twiddle_thumbs) #provider = @runner.build_provider(resource) @@ -541,7 +538,7 @@ describe "LWRP" do @resource = get_lwrp(:lwrp_foo).new("morpheus", run_context) @resource.allowed_actions << :test @resource.action(:test) - @resource.provider(get_lwrp_provider(:lwrp_inline_compiler)) + @resource.provider(get_dynamic_lwrp_provider(:lwrp_inline_compiler)) end it "does not add interior resources to the exterior resource collection" do |
