summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerdar Sutay <serdar@opscode.com>2014-08-13 14:22:08 -0700
committerSerdar Sutay <serdar@opscode.com>2014-08-13 14:22:08 -0700
commitda2abd0709f7c7e6c6c83ccf622a4fc5f3424586 (patch)
treecb3b6e8f56806e1d38b5cedabd389daca1421970
parent4f6c1fa1fab67e9aef46af0fe920acdff613d966 (diff)
parent68e6db9717f434eac1f5165155c4838e17ff44d6 (diff)
downloadchef-da2abd0709f7c7e6c6c83ccf622a4fc5f3424586.tar.gz
Merge pull request #1812 from opscode/sersut/chef-1773
Name :freebsd_package resource `package` to provide compat with ChefSpec.
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/chef/dsl/recipe.rb14
-rw-r--r--lib/chef/resource/freebsd_package.rb12
-rw-r--r--spec/support/lib/chef/resource/zen_follower.rb46
-rw-r--r--spec/unit/recipe_spec.rb41
5 files changed, 112 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1fc5858b06..d71e70627b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -95,6 +95,7 @@
* Enable Travis to run Test Kitchen with Kitchen EC2.
* Fix a bug in reporting not to post negative duration values.
* Add password setting support for Mac 10.7, 10.8 and 10.9 to the dscl user provider.
+* ChefSpec can find freebsd_package resource correctly when a package resource is declared on Freebsd.
## Last Release: 11.14.2
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