Skip to content

Commit a3b67f2

Browse files
mleinartdanielsdeleo
authored andcommitted
CHEF-1519 - only_if and not_if not checked on notifies
1 parent b6c2509 commit a3b67f2

2 files changed

Lines changed: 34 additions & 23 deletions

File tree

‎chef/lib/chef/runner.rb‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,37 @@ def build_provider(resource)
4747
# Determine the appropriate provider for the given resource, then
4848
# execute it.
4949
def run_action(resource, action, delayed_actions)
50+
# Check if this resource has an only_if block -- if it does,
51+
# evaluate the only_if block and skip the resource if
52+
# appropriate.
53+
if resource.only_if
54+
unless Chef::Mixin::Command.only_if(resource.only_if, resource.only_if_args)
55+
Chef::Log.debug("Skipping #{resource} due to only_if")
56+
return
57+
end
58+
end
59+
60+
# Check if this resource has a not_if block -- if it does,
61+
# evaluate the not_if block and skip the resource if
62+
# appropriate.
63+
if resource.not_if
64+
unless Chef::Mixin::Command.not_if(resource.not_if, resource.not_if_args)
65+
Chef::Log.debug("Skipping #{resource} due to not_if")
66+
return
67+
end
68+
end
69+
5070
provider = build_provider(resource)
5171
provider.send("action_#{action}")
52-
72+
5373
# Execute any immediate and queue up any delayed notifications
5474
# associated with the resource.
5575
if resource.updated
5676
resource.notifies_immediate.each do |notify|
5777
Chef::Log.info("#{resource} sending #{notify.action} action to #{notify.resource} (immediate)")
5878
run_action(notify.resource, notify.action, delayed_actions)
5979
end
60-
80+
6181
resource.notifies_delayed.each do |notify|
6282
unless delayed_actions.include?(notify)
6383
delayed_actions << notify
@@ -83,26 +103,6 @@ def converge
83103
begin
84104
Chef::Log.debug("Processing #{resource} on #{run_context.node.name}")
85105

86-
# Check if this resource has an only_if block -- if it does,
87-
# evaluate the only_if block and skip the resource if
88-
# appropriate.
89-
if resource.only_if
90-
unless Chef::Mixin::Command.only_if(resource.only_if, resource.only_if_args)
91-
Chef::Log.debug("Skipping #{resource} due to only_if")
92-
next
93-
end
94-
end
95-
96-
# Check if this resource has a not_if block -- if it does,
97-
# evaluate the not_if block and skip the resource if
98-
# appropriate.
99-
if resource.not_if
100-
unless Chef::Mixin::Command.not_if(resource.not_if, resource.not_if_args)
101-
Chef::Log.debug("Skipping #{resource} due to not_if")
102-
next
103-
end
104-
end
105-
106106
# Execute each of this resource's actions.
107107
action_list = resource.action.kind_of?(Array) ? resource.action : [ resource.action ]
108108
action_list.each do |action|

‎chef/spec/unit/runner_spec.rb‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ def new_runner
179179
provider.should_receive(:action_pur).once.ordered
180180
@runner.converge
181181
end
182-
182+
183+
it "should check a resource's only_if and not_if if notified by another resource" do
184+
provider = Chef::Provider::SnakeOil.new(@run_context.resource_collection[0], @run_context)
185+
@run_context.resource_collection[0].action = :nothing
186+
@run_context.resource_collection << Chef::Resource::Cat.new("carmel", @run_context)
187+
@run_context.resource_collection[1].notifies :buy, @run_context.resource_collection[0], :delayed
188+
@run_context.resource_collection[1].updated = true
189+
# hits only_if first time when the resource is run in order, second on notify
190+
@run_context.resource_collection[0].should_receive(:only_if).exactly(2).times.and_return(nil)
191+
@run_context.resource_collection[0].should_receive(:not_if).exactly(2).times.and_return(nil)
192+
@runner.converge
193+
end
183194

184195
end

0 commit comments

Comments
 (0)