diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-06-03 12:48:01 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-06-04 09:34:08 -0700 |
commit | e4c52e53a4b9caebebf09fb8ff944f7a2397fe33 (patch) | |
tree | cc0d295be849ff2abbbaa6d63c856c823eb7f9a0 | |
parent | 0c5556b8d1aceee30a25855a965a7ec13d8fa13b (diff) | |
download | chef-e4c52e53a4b9caebebf09fb8ff944f7a2397fe33.tar.gz |
allow include_recipe from LWRP provider code
allows include_recipe "build-essential" to work from provider
code.
-rw-r--r-- | lib/chef/provider/lwrp_base.rb | 4 | ||||
-rw-r--r-- | spec/support/lib/chef/provider/openldap_includer.rb | 29 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/openldap_includer.rb | 28 | ||||
-rw-r--r-- | spec/unit/recipe_spec.rb | 30 |
4 files changed, 91 insertions, 0 deletions
diff --git a/lib/chef/provider/lwrp_base.rb b/lib/chef/provider/lwrp_base.rb index 31f93248c6..b5efbb284d 100644 --- a/lib/chef/provider/lwrp_base.rb +++ b/lib/chef/provider/lwrp_base.rb @@ -19,6 +19,7 @@ # require 'chef/provider' +require 'chef/dsl/include_recipe' class Chef class Provider @@ -77,6 +78,9 @@ class Chef include Chef::DSL::PlatformIntrospection include Chef::DSL::DataQuery + # Allow include_recipe from within LWRP provider code + include Chef::DSL::IncludeRecipe + # no-op `load_current_resource`. Allows simple LWRP providers to work # without defining this method explicitly (silences # Chef::Exceptions::Override exception) diff --git a/spec/support/lib/chef/provider/openldap_includer.rb b/spec/support/lib/chef/provider/openldap_includer.rb new file mode 100644 index 0000000000..afb0c7cf01 --- /dev/null +++ b/spec/support/lib/chef/provider/openldap_includer.rb @@ -0,0 +1,29 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Copyright:: Copyright (c) 2008 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +class Chef + class Provider + class OpenldapIncluder < Chef::Provider::LWRPBase + provides :openldap_includer + + def action_run + include_recipe "openldap::default" + end + end + end +end diff --git a/spec/support/lib/chef/resource/openldap_includer.rb b/spec/support/lib/chef/resource/openldap_includer.rb new file mode 100644 index 0000000000..421c95532d --- /dev/null +++ b/spec/support/lib/chef/resource/openldap_includer.rb @@ -0,0 +1,28 @@ +# +# Author:: Adam Jacob (<adam@opscode.com>) +# Copyright:: Copyright (c) 2008, 2010 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +class Chef + class Resource + class OpenldapIncluder < Chef::Resource::LWRPBase + use_automatic_resource_name + allowed_actions :run + default_action :run + end + end +end diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 618c742998..72d5cc6f63 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -578,6 +578,36 @@ describe Chef::Recipe do expect(cookbook_collection[:openldap]).not_to receive(:load_recipe).with("default", run_context) openldap_recipe.include_recipe "::default" end + + it "will not load a recipe twice when called first from an LWRP provider" do + openldap_recipe = Chef::Recipe.new("openldap", "test", run_context) + expect(node).to receive(:loaded_recipe).with(:openldap, "default").exactly(:once) + allow(run_context).to receive(:unreachable_cookbook?).with(:openldap).and_return(false) + expect(cookbook_collection[:openldap]).to receive(:load_recipe).with("default", run_context) + openldap_recipe.include_recipe "::default" + expect(cookbook_collection[:openldap]).not_to receive(:load_recipe).with("default", run_context) + openldap_recipe.openldap_includer("do it").run_action(:run) + end + + it "will not load a recipe twice when called last from an LWRP provider" do + openldap_recipe = Chef::Recipe.new("openldap", "test", run_context) + expect(node).to receive(:loaded_recipe).with(:openldap, "default").exactly(:once) + allow(run_context).to receive(:unreachable_cookbook?).with(:openldap).and_return(false) + expect(cookbook_collection[:openldap]).to receive(:load_recipe).with("default", run_context) + openldap_recipe.openldap_includer("do it").run_action(:run) + expect(cookbook_collection[:openldap]).not_to receive(:load_recipe).with("default", run_context) + openldap_recipe.include_recipe "::default" + end + + it "will not load a recipe twice when called both times from an LWRP provider" do + openldap_recipe = Chef::Recipe.new("openldap", "test", run_context) + expect(node).to receive(:loaded_recipe).with(:openldap, "default").exactly(:once) + allow(run_context).to receive(:unreachable_cookbook?).with(:openldap).and_return(false) + expect(cookbook_collection[:openldap]).to receive(:load_recipe).with("default", run_context) + openldap_recipe.openldap_includer("do it").run_action(:run) + expect(cookbook_collection[:openldap]).not_to receive(:load_recipe).with("default", run_context) + openldap_recipe.openldap_includer("do it").run_action(:run) + end end describe "tags" do |