diff options
author | Serdar Sutay <serdar@opscode.com> | 2014-08-13 11:56:59 -0700 |
---|---|---|
committer | Serdar Sutay <serdar@opscode.com> | 2014-08-13 11:57:08 -0700 |
commit | 8d3dfdc77591b8f35ca92a94046ff629ac1f23fb (patch) | |
tree | a1b48fe35f66bd99d033e325e168af834b29a535 | |
parent | 6ad12bac9cd0b736915dcafe9232d168bc1e0dc6 (diff) | |
download | chef-8d3dfdc77591b8f35ca92a94046ff629ac1f23fb.tar.gz |
Make sure freebsd_package is captured with the resource name it's declared with in resource collection.
-rw-r--r-- | lib/chef/dsl/recipe.rb | 14 | ||||
-rw-r--r-- | lib/chef/resource/freebsd_package.rb | 12 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/zen_follower.rb | 46 | ||||
-rw-r--r-- | spec/unit/recipe_spec.rb | 41 |
4 files changed, 111 insertions, 2 deletions
diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb index 23cfbd558c..3282320b8c 100644 --- a/lib/chef/dsl/recipe.rb +++ b/lib/chef/dsl/recipe.rb @@ -85,6 +85,20 @@ class Chef resource = build_resource(type, name, created_at, &resource_attrs_block) + # Some resources (freebsd_package) can be invoked with multiple names + # (package || freebsd_package). + # https://github.com/opscode/chef/issues/1773 + # For these resources we want to make sure + # their key in resource collection is same as the name they are declared + # as. Since this might be a breaking change for resources that define + # customer to_s methods, we are working around the issue by letting + # resources know of their created_as_type until this issue is fixed in + # Chef 12: + # https://github.com/opscode/chef/issues/1817 + if resource.respond_to?(:created_as_type=) + resource.created_as_type = type + end + run_context.resource_collection.insert(resource) resource end diff --git a/lib/chef/resource/freebsd_package.rb b/lib/chef/resource/freebsd_package.rb index c7a8d44181..70ef62ae8a 100644 --- a/lib/chef/resource/freebsd_package.rb +++ b/lib/chef/resource/freebsd_package.rb @@ -31,17 +31,26 @@ class Chef provides :package, :on_platforms => ["freebsd"] + attr_accessor :created_as_type def initialize(name, run_context=nil) super @resource_name = :freebsd_package + @created_as_type = "freebsd_package" end def after_created assign_provider end - + # This resource can be invoked with multiple names package & freebsd_package. + # We override the to_s method to ensure the key in resource collection + # matches the type resource is declared as using created_as_type. This + # logic can be removed once Chef does this for all resource in Chef 12: + # https://github.com/opscode/chef/issues/1817 + def to_s + "#{created_as_type}[#{name}]" + end private @@ -68,4 +77,3 @@ class Chef end end end - diff --git a/spec/support/lib/chef/resource/zen_follower.rb b/spec/support/lib/chef/resource/zen_follower.rb new file mode 100644 index 0000000000..0fa0c4af5b --- /dev/null +++ b/spec/support/lib/chef/resource/zen_follower.rb @@ -0,0 +1,46 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software, 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. +# + +require 'chef/knife' +require 'chef/json_compat' + +class Chef + class Resource + class ZenFollower < Chef::Resource + attr_accessor :created_as_type + + provides :follower, :on_platforms => ["zen"] + + def initialize(name, run_context=nil) + @resource_name = :zen_follower + @created_as_type = "zen_follower" + super + end + + def to_s + "#{created_as_type}[#{name}]" + end + + def master(arg=nil) + if !arg.nil? + @master = arg + end + @master + end + end + end +end diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 2bdf470143..c795ba3788 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -182,7 +182,48 @@ describe Chef::Recipe do zm_resource # force let binding evaluation run_context.resource_collection.resources(:zen_master => "klopp").should == zm_resource end + end + + describe "creating a resource with short name" do + # zen_follower resource has this: + # provides :follower, :on_platforms => ["zen"] + before do + node.stub(:[]) do |key| + case key + when :platform + :zen + when :platform_version + "1.0.0" + else + nil + end + end + end + + let(:resource_follower) do + recipe.declare_resource(:follower, "srst") do + master "none" + end + end + it "defines the resource using the declaration name with short name" do + resource_follower + run_context.resource_collection.lookup("follower[srst]").should_not be_nil + end + end + + describe "creating a resource with a long name" do + let(:resource_zn_follower) do + recipe.declare_resource(:zen_follower, "srst") do + master "none" + end + end + + + it "defines the resource using the declaration name with long name" do + resource_zn_follower + run_context.resource_collection.lookup("zen_follower[srst]").should_not be_nil + end end describe "when attempting to create a resource of an invalid type" do |