summaryrefslogtreecommitdiff
path: root/spec/functional/notifications_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/functional/notifications_spec.rb')
-rw-r--r--spec/functional/notifications_spec.rb78
1 files changed, 74 insertions, 4 deletions
diff --git a/spec/functional/notifications_spec.rb b/spec/functional/notifications_spec.rb
index a02fdffe5e..42702a8bb2 100644
--- a/spec/functional/notifications_spec.rb
+++ b/spec/functional/notifications_spec.rb
@@ -74,6 +74,76 @@ describe "Notifications" do
runner.converge
end
+ it "should notify from one resource to another immediately_before" do
+ log_resource = recipe.declare_resource(:log, "log") do
+ message "This is a log message"
+ action :write
+ notifies :install, "package[vim]", :immediately_before
+ end
+ update_action(log_resource, 2)
+
+ package_resource = recipe.declare_resource(:package, "vim") do
+ action :nothing
+ end
+
+ actions = []
+ [ log_resource, package_resource ].each do |resource|
+ allow(resource).to receive(:run_action).and_wrap_original do |m, action, notification_type, notifying_resource|
+ actions << { resource: resource.to_s, action: action }
+ actions[-1][:why_run] = Chef::Config[:why_run] if Chef::Config[:why_run]
+ actions[-1][:notification_type] = notification_type if notification_type
+ actions[-1][:notifying_resource] = notifying_resource.to_s if notifying_resource
+ m.call(action, notification_type, notifying_resource)
+ end
+ end
+
+ runner.converge
+
+ expect(actions).to eq [
+ # First it runs why-run to check if the resource would update
+ { resource: log_resource.to_s, action: :write, why_run: true },
+ # Then it runs the immediately_before action
+ { resource: package_resource.to_s, action: :install, notification_type: :immediately_before, notifying_resource: log_resource.to_s },
+ # Then it runs the actual action
+ { resource: log_resource.to_s, action: :write },
+ { resource: package_resource.to_s, action: :nothing }
+ ]
+ end
+
+ it "should not notify from one resource to another immediately_before if the resource is not updated" do
+ log_resource = recipe.declare_resource(:log, "log") do
+ message "This is a log message"
+ action :write
+ notifies :install, "package[vim]", :immediately_before
+ end
+
+ package_resource = recipe.declare_resource(:package, "vim") do
+ action :nothing
+ end
+
+ actions = []
+ [ log_resource, package_resource ].each do |resource|
+ allow(resource).to receive(:run_action).and_wrap_original do |m, action, notification_type, notifying_resource|
+ actions << { resource: resource.to_s, action: action }
+ actions[-1][:why_run] = Chef::Config[:why_run] if Chef::Config[:why_run]
+ actions[-1][:notification_type] = notification_type if notification_type
+ actions[-1][:notifying_resource] = notifying_resource.to_s if notifying_resource
+ m.call(action, notification_type, notifying_resource)
+ end
+ end
+
+ runner.converge
+
+ expect(actions).to eq [
+ # First it runs why-run to check if the resource would update
+ { resource: log_resource.to_s, action: :write, why_run: true },
+ # Then it does NOT run the immediately_before action
+ # Then it runs the actual action
+ { resource: log_resource.to_s, action: :write },
+ { resource: package_resource.to_s, action: :nothing }
+ ]
+ end
+
it "should notify from one resource to another delayed" do
log_resource = recipe.declare_resource(:log, "log") do
message "This is a log message"
@@ -94,7 +164,7 @@ describe "Notifications" do
runner.converge
end
-
+
describe "when one resource is defined lazily" do
it "subscribes to a resource defined in a ruby block" do
@@ -158,10 +228,10 @@ describe "Notifications" do
end
# Mocks having the provider run successfully and update the resource
- def update_action(resource)
+ def update_action(resource, times=1)
p = Chef::Provider.new(resource, run_context)
- expect(resource).to receive(:provider_for_action).and_return(p)
- expect(p).to receive(:run_action) {
+ expect(resource).to receive(:provider_for_action).exactly(times).times.and_return(p)
+ expect(p).to receive(:run_action).exactly(times).times {
resource.updated_by_last_action(true)
}
end