From 1db710e55ec53d12222654818f577044f63168d1 Mon Sep 17 00:00:00 2001 From: neha-p6 Date: Thu, 27 Jan 2022 17:26:24 +0530 Subject: Validate resource spec passed to subscribes Signed-off-by: Neha Pansare --- lib/chef/resource.rb | 1 + spec/unit/resource_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 681927f1d0..f9ab292b83 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -341,6 +341,7 @@ class Chef def subscribes(action, resources, timing = :delayed) resources = [resources].flatten resources.each do |resource| + validate_resource_spec!(resource) if resource.is_a?(String) resource = UnresolvedSubscribes.new(resource, run_context) end diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index f20a6d3bc6..86e666c00e 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -300,6 +300,14 @@ describe Chef::Resource do end describe "subscribes" do + context "with syntax error in resources parameter" do + it "raises an exception immediately" do + expect do + resource.subscribes(:run, "typo[missing-closing-bracket") + end.to raise_error(Chef::Exceptions::InvalidResourceSpecification) + end + end + it "should make resources appear in the actions hash of subscribed nodes" do run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee") zr = run_context.resource_collection.find(zen_master: "coffee") -- cgit v1.2.1 From e8ee2c6f76efb7c954ef4adeac9a7d2a00ec8ad0 Mon Sep 17 00:00:00 2001 From: Neha Pansare Date: Tue, 1 Feb 2022 16:24:15 +0530 Subject: Fix existing unit test cases Signed-off-by: Neha Pansare --- spec/unit/resource_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 86e666c00e..95aed42f5b 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -312,7 +312,7 @@ describe Chef::Resource do run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee") zr = run_context.resource_collection.find(zen_master: "coffee") resource.subscribes :reload, zr - expect(zr.delayed_notifications.detect { |e| e.resource.name == "funk" && e.action == :reload }).not_to be_nil + expect(zr.delayed_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil end it "should make resources appear in the actions hash of subscribed nodes" do @@ -323,8 +323,8 @@ describe Chef::Resource do run_context.resource_collection << Chef::Resource::ZenMaster.new("bean") zrb = run_context.resource_collection.find(zen_master: "bean") - zrb.subscribes :reload, zr - expect(zr.delayed_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil + resource.subscribes :reload, zrb + expect(zrb.delayed_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil end it "should make subscribed resources be capable of acting immediately" do -- cgit v1.2.1 From 9c3f1d4871566dc86aff7e3494a41a9fd6814608 Mon Sep 17 00:00:00 2001 From: Neha Pansare Date: Wed, 9 Feb 2022 18:25:48 +0530 Subject: Update resource build calls to use resource_context for Subscribes specs Signed-off-by: Neha Pansare --- spec/unit/resource_spec.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 95aed42f5b..f0a624d5db 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -309,26 +309,26 @@ describe Chef::Resource do end it "should make resources appear in the actions hash of subscribed nodes" do - run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee") + run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee", run_context) zr = run_context.resource_collection.find(zen_master: "coffee") resource.subscribes :reload, zr - expect(zr.delayed_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil + expect(zr.delayed_notifications.detect { |e| e.resource.name == "funk" && e.action == :reload }).not_to be_nil end it "should make resources appear in the actions hash of subscribed nodes" do - run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee") + run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee", run_context) zr = run_context.resource_collection.find(zen_master: "coffee") resource.subscribes :reload, zr expect(zr.delayed_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil - run_context.resource_collection << Chef::Resource::ZenMaster.new("bean") + run_context.resource_collection << Chef::Resource::ZenMaster.new("bean", run_context) zrb = run_context.resource_collection.find(zen_master: "bean") - resource.subscribes :reload, zrb - expect(zrb.delayed_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil + zrb.subscribes :reload, zr + expect(zr.delayed_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil end it "should make subscribed resources be capable of acting immediately" do - run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee") + run_context.resource_collection << Chef::Resource::ZenMaster.new("coffee", run_context) zr = run_context.resource_collection.find(zen_master: "coffee") resource.subscribes :reload, zr, :immediately expect(zr.immediate_notifications.detect { |e| e.resource.name == resource.name && e.action == :reload }).not_to be_nil -- cgit v1.2.1