summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2020-05-20 12:40:39 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2020-05-20 12:43:45 -0700
commit6d14ba02ec07b18eb757315d9665b646470f6f56 (patch)
treec9fc136390c0bc12fc28c298fe466b14f13191fd
parent0aac0e30a5532e9d2d32764c8f3221efba475f86 (diff)
downloadchef-lcg/resource_name_fixes.tar.gz
Chef-16.2 breaking changelcg/resource_name_fixes
This is a breaking change to fix a breaking change in Chef 16.0. The new behavior is that a resource which relies upon the reource_name statement to wire the resource up to the DSL will fail (although the very old wiring of the "<cookbook_name>_<resource filename>" continues to work). So for a resource which does not get its DSL wiring from the filename, only declaring a resource_name will fail: ``` resource_name :foo ``` This can be fixed by adding an explicit `provides` line (which is backwards compatible): ``` resource_name :foo provides :foo ``` If backwards compatibility is not a concern, then post-16.0 the resource_name call can be completely dropped: ``` provides :foo ``` This is a vastly simpler backwards compatibility break than the unintentional break in #9885, which is difficult to describe and to detect. The rules going forward are fairly simple to explain: 1. The resource_name now only sets the resource_name. 2. The old behavior of the fallback resource_name and DSL wiring based on the filename is preserved (unless the values are set). 3. In Chef 16, The first provides line will set the resource_name if it has not already been set, allowing the resource_name to be omitted. It is recommended that all resources only set provides lines, the use of the fallback filename-based wiring is discouraged and explicitly setting the resource_name is discouraged. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--lib/chef/resource.rb11
-rw-r--r--spec/unit/resource_spec.rb14
2 files changed, 15 insertions, 10 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index cd96c4c1e9..2c63abeaa3 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -950,16 +950,7 @@ class Chef
def self.resource_name(name = NOT_PASSED)
# Setter
if name != NOT_PASSED
- if name
- @resource_name = name.to_sym
- name = name.to_sym
- # FIXME: determine a way to deprecate this magic behavior
- unless Chef::ResourceResolver.includes_handler?(name, self)
- provides name
- end
- else
- @resource_name = nil
- end
+ @resource_name = name.to_sym rescue nil
end
@resource_name = nil unless defined?(@resource_name)
diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb
index a2ba2b272f..3808d99a06 100644
--- a/spec/unit/resource_spec.rb
+++ b/spec/unit/resource_spec.rb
@@ -492,6 +492,20 @@ describe Chef::Resource do
expect(r.resource_name).to eq :blah
expect(r.declared_type).to eq :d
end
+
+ # This tests some somewhat confusing behavior that used to occur due to the resource_name call
+ # automatically wiring up the old canonical provides line.
+ it "setting resoure_name does not override provides in prior resource" do
+ c1 = Class.new(Chef::Resource) do
+ resource_name :self_resource_name_test_4
+ provides :self_resource_name_test_4
+ end
+ c2 = Class.new(Chef::Resource) do
+ resource_name :self_resource_name_test_4
+ provides(:self_resource_name_test_4) { false } # simulates any filter that does not match
+ end
+ expect(Chef::Resource.resource_for_node(:self_resource_name_test_4, node)).to eql(c1)
+ end
end
describe "to_json" do