summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-05 14:47:18 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-08 09:01:55 -0700
commit3dcaddb86dc85a4cf730e675232ce83f97d5726b (patch)
tree950b87bae166f85b45edf7552dee6891153e8125
parentc65de79dbd662d895c8a10ef496d7eb9c69376c2 (diff)
downloadchef-3dcaddb86dc85a4cf730e675232ce83f97d5726b.tar.gz
Make sure resource_name :x only removes automatic provides from that class
-rw-r--r--lib/chef/node_map.rb4
-rw-r--r--lib/chef/platform/resource_priority_map.rb4
-rw-r--r--lib/chef/resource.rb13
-rw-r--r--spec/integration/recipes/recipe_dsl_spec.rb108
-rw-r--r--spec/integration/recipes/resource_definition_spec.rb18
-rw-r--r--spec/unit/lwrp_spec.rb6
-rw-r--r--spec/unit/runner_spec.rb4
7 files changed, 128 insertions, 29 deletions
diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb
index 18f55da835..9092316c1b 100644
--- a/lib/chef/node_map.rb
+++ b/lib/chef/node_map.rb
@@ -102,10 +102,10 @@ class Chef
# Seriously, don't use this, it's nearly certain to change on you
# @return remaining
# @api private
- def delete_canonical(key)
+ def delete_canonical(key, value)
remaining = @map[key]
if remaining
- remaining.delete_if { |matcher| matcher[:canonical] }
+ remaining.delete_if { |matcher| matcher[:canonical] && Array(matcher[:value]) == Array(value) }
if remaining.empty?
@map.delete(key)
remaining = nil
diff --git a/lib/chef/platform/resource_priority_map.rb b/lib/chef/platform/resource_priority_map.rb
index 5d96fd412b..fb08debc53 100644
--- a/lib/chef/platform/resource_priority_map.rb
+++ b/lib/chef/platform/resource_priority_map.rb
@@ -14,8 +14,8 @@ class Chef
end
# @api private
- def delete_canonical(resource_name)
- priority_map.delete_canonical(resource_name)
+ def delete_canonical(resource_name, resource_class)
+ priority_map.delete_canonical(resource_name, resource_class)
end
# @api private
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 3a9af0bcca..ed66bab916 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -911,10 +911,11 @@ class Chef
def self.resource_name(name=NULL_ARG)
# Setter
if name != NULL_ARG
+ remove_canonical_dsl
+
# Set the resource_name and call provides
if name
name = name.to_sym
- remove_canonical_dsl
# If our class is not already providing this name, provide it.
if !Chef::ResourceResolver.list(name).include?(self)
provides name, canonical: true
@@ -1327,10 +1328,18 @@ class Chef
#
resource_subclass = class_eval <<-EOM, __FILE__, __LINE__+1
class Chef::Resource::#{class_name} < resource_class
+ resource_name nil # we do not actually provide anything
def initialize(*args, &block)
Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 13 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.")
super
end
+ def self.resource_name(*args)
+ if args.empty?
+ @resource_name ||= superclass.resource_name
+ else
+ super
+ end
+ end
self
end
EOM
@@ -1379,7 +1388,7 @@ class Chef
def self.remove_canonical_dsl
if @resource_name
- remaining = Chef.resource_priority_map.delete_canonical(@resource_name)
+ remaining = Chef.resource_priority_map.delete_canonical(@resource_name, self)
if !remaining
Chef::DSL::Resources.remove_resource_dsl(@resource_name)
end
diff --git a/spec/integration/recipes/recipe_dsl_spec.rb b/spec/integration/recipes/recipe_dsl_spec.rb
index 403c879e74..9595653ad8 100644
--- a/spec/integration/recipes/recipe_dsl_spec.rb
+++ b/spec/integration/recipes/recipe_dsl_spec.rb
@@ -3,6 +3,14 @@ require 'support/shared/integration/integration_helper'
describe "Recipe DSL methods" do
include IntegrationSupport
+ module Namer
+ extend self
+ attr_accessor :current_index
+ end
+
+ before(:all) { Namer.current_index = 1 }
+ before { Namer.current_index += 1 }
+
context "With resource 'base_thingy' declared as BaseThingy" do
before(:context) {
@@ -403,6 +411,106 @@ describe "Recipe DSL methods" do
}.to raise_error(NoMethodError)
end
end
+
+ context "With a resource TwoClassesOneDsl" do
+ let(:class_name) { "TwoClassesOneDsl#{Namer.current_index}" }
+ let(:dsl_method) { "two_classes_one_dsl#{Namer.current_index}" }
+
+ before {
+ eval <<-EOM, nil, __FILE__, __LINE__+1
+ class #{class_name} < BaseThingy
+ end
+ EOM
+ }
+ context "And resource BlahModule::TwoClassesOneDsl" do
+ before {
+ eval <<-EOM, nil, __FILE__, __LINE__+1
+ module BlahModule
+ class #{class_name} < BaseThingy
+ end
+ end
+ EOM
+ }
+ it "two_classes_one_dsl resolves to BlahModule::TwoClassesOneDsl (last declared)" do
+ dsl_method = self.dsl_method
+ recipe = converge {
+ instance_eval("#{dsl_method} 'blah' do; end")
+ }
+ expect(recipe.logged_warnings).to eq ''
+ expect(BaseThingy.created_resource).to eq eval("BlahModule::#{class_name}")
+ end
+ end
+ context "And resource BlahModule::TwoClassesOneDsl with resource_name nil" do
+ before {
+ eval <<-EOM, nil, __FILE__, __LINE__+1
+ module BlahModule
+ class BlahModule::#{class_name} < BaseThingy
+ resource_name nil
+ end
+ end
+ EOM
+ }
+ it "two_classes_one_dsl resolves to ::TwoClassesOneDsl" do
+ dsl_method = self.dsl_method
+ recipe = converge {
+ instance_eval("#{dsl_method} 'blah' do; end")
+ }
+ expect(recipe.logged_warnings).to eq ''
+ expect(BaseThingy.created_resource).to eq eval("::#{class_name}")
+ end
+ end
+ context "And resource BlahModule::TwoClassesOneDsl with resource_name :argh" do
+ before {
+ eval <<-EOM, nil, __FILE__, __LINE__+1
+ module BlahModule
+ class BlahModule::#{class_name} < BaseThingy
+ resource_name :argh
+ end
+ end
+ EOM
+ }
+ it "two_classes_one_dsl resolves to ::TwoClassesOneDsl" do
+ dsl_method = self.dsl_method
+ recipe = converge {
+ instance_eval("#{dsl_method} 'blah' do; end")
+ }
+ expect(recipe.logged_warnings).to eq ''
+ expect(BaseThingy.created_resource).to eq eval("::#{class_name}")
+ end
+ end
+ context "And resource BlahModule::TwoClassesOneDsl with provides :two_classes_one_dsl, os: 'blarghle'" do
+ before {
+ eval <<-EOM, nil, __FILE__, __LINE__+1
+ module BlahModule
+ class BlahModule::#{class_name} < BaseThingy
+ provides #{dsl_method.inspect}, os: 'blarghle'
+ end
+ end
+ EOM
+ }
+ it "and os = blarghle, two_classes_one_dsl resolves to BlahModule::TwoClassesOneDsl" do
+ dsl_method = self.dsl_method
+ recipe = converge {
+ # this is an ugly way to test, make Cheffish expose node attrs
+ run_context.node.automatic[:os] = 'blarghle'
+ instance_eval("#{dsl_method} 'blah' do; end")
+ }
+ expect(recipe.logged_warnings).to eq ''
+ expect(BaseThingy.created_resource).to eq eval("BlahModule::#{class_name}")
+ end
+
+ it "and os = linux, two_classes_one_dsl resolves to BlahModule::TwoClassesOneDsl" do
+ dsl_method = self.dsl_method
+ recipe = converge {
+ # this is an ugly way to test, make Cheffish expose node attrs
+ run_context.node.automatic[:os] = 'linux'
+ instance_eval("#{dsl_method} 'blah' do; end")
+ }
+ expect(recipe.logged_warnings).to eq ''
+ expect(BaseThingy.created_resource).to eq eval("::#{class_name}")
+ end
+ end
+ end
end
end
diff --git a/spec/integration/recipes/resource_definition_spec.rb b/spec/integration/recipes/resource_definition_spec.rb
deleted file mode 100644
index 4e5bc53428..0000000000
--- a/spec/integration/recipes/resource_definition_spec.rb
+++ /dev/null
@@ -1,18 +0,0 @@
-require 'support/shared/integration/integration_helper'
-
-describe "Resource definition" do
- include IntegrationSupport
-
- context "With a resource with only provides lines and no resource_name" do
- before(:context) {
- class ResourceDefinitionNoNameTest < Chef::Resource
- provides :resource_definition_no_name_test
- end
- }
- it "Creating said resource with the resource builder fails with an exception" do
- expect_converge {
- resource_definition_no_name_test 'blah'
- }.to raise_error(Chef::Exceptions::InvalidResourceSpecification)
- end
- end
-end
diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb
index dbf76d3c13..34c6f6f1c5 100644
--- a/spec/unit/lwrp_spec.rb
+++ b/spec/unit/lwrp_spec.rb
@@ -38,7 +38,7 @@ describe "LWRP" do
end
def get_lwrp(name)
- Chef::Resource.resource_for_node(name, Chef::Node.new)
+ Chef::ResourceResolver.resolve(name)
end
def get_lwrp_provider(name)
@@ -146,7 +146,7 @@ describe "LWRP" do
content = IO.read(File.expand_path("../../data/lwrp/resources/foo.rb", __FILE__))
IO.write(@lwrp_path, content)
Chef::Resource::LWRPBase.build_from_file("lwrp", @lwrp_path, nil)
- @original_resource = Chef::Resource.resource_for_node(:lwrp_foo, Chef::Node.new)
+ @original_resource = Chef::ResourceResolver.resolve(:lwrp_foo)
end
after do
@@ -161,7 +161,7 @@ describe "LWRP" do
end
it "Should load the old content, and not the new" do
- resource = Chef::Resource.resource_for_node(:lwrp_foo, Chef::Node.new)
+ resource = Chef::ResourceResolver.resolve(:lwrp_foo)
expect(resource).to eq @original_resource
expect(resource.default_action).to eq(:pass_buck)
expect(Chef.method_defined?(:method_created_by_override_lwrp_foo)).to be_falsey
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb
index b30f818da1..82e57e068c 100644
--- a/spec/unit/runner_spec.rb
+++ b/spec/unit/runner_spec.rb
@@ -273,8 +273,8 @@ describe Chef::Runner do
expected_message =<<-E
Multiple failures occurred:
-* FailureProvider::ChefClientFail occurred in delayed notification: [explode] (dynamically defined) had an error: FailureProvider::ChefClientFail: chef had an error of some sort
-* FailureProvider::ChefClientFail occurred in delayed notification: [explode again] (dynamically defined) had an error: FailureProvider::ChefClientFail: chef had an error of some sort
+* FailureProvider::ChefClientFail occurred in delayed notification: failure_resource[explode] (dynamically defined) had an error: FailureProvider::ChefClientFail: chef had an error of some sort
+* FailureProvider::ChefClientFail occurred in delayed notification: failure_resource[explode again] (dynamically defined) had an error: FailureProvider::ChefClientFail: chef had an error of some sort
E
expect(exception.message).to eq(expected_message)