summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/mixin/why_run.rb10
-rw-r--r--lib/chef/provider.rb2
-rw-r--r--spec/unit/mixin/why_run_spec.rb53
3 files changed, 62 insertions, 3 deletions
diff --git a/lib/chef/mixin/why_run.rb b/lib/chef/mixin/why_run.rb
index efe327168e..1954f574d3 100644
--- a/lib/chef/mixin/why_run.rb
+++ b/lib/chef/mixin/why_run.rb
@@ -242,8 +242,12 @@ class Chef
end
end
- def initialize(resource, run_context)
- @resource, @run_context = resource, run_context
+ attr_accessor :action
+
+ def initialize(resource, run_context, action)
+ @resource = resource
+ @run_context = run_context
+ @action = action
@assertions = Hash.new { |h, k| h[k] = [] }
@blocked_actions = []
end
@@ -305,6 +309,8 @@ class Chef
# "You don't have sufficient privileges to delete #{@new_resource.path}")
# end
def assert(*actions)
+ return unless actions.include?(action.to_sym)
+
assertion = Assertion.new
yield assertion
actions.each { |action| @assertions[action] << assertion }
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb
index e7d7ca84ff..296a22222c 100644
--- a/lib/chef/provider.rb
+++ b/lib/chef/provider.rb
@@ -269,7 +269,7 @@ class Chef
end
def requirements
- @requirements ||= ResourceRequirements.new(@new_resource, run_context)
+ @requirements ||= ResourceRequirements.new(@new_resource, run_context, action)
end
def description(description = "NOT_PASSED")
diff --git a/spec/unit/mixin/why_run_spec.rb b/spec/unit/mixin/why_run_spec.rb
new file mode 100644
index 0000000000..16ee50fec4
--- /dev/null
+++ b/spec/unit/mixin/why_run_spec.rb
@@ -0,0 +1,53 @@
+#
+# Copyright:: Copyright (c) Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+
+describe Chef::Mixin::WhyRun::ResourceRequirements do
+ class TestResource < Chef::Resource
+ action_class do
+ def define_resource_requirements
+ requirements.assert(:boom) do |a|
+ a.assertion { raise "boom1" }
+ a.failure_message("#{raise "boom2"}")
+ a.whyrun("#{raise "boom3"}")
+ end
+ end
+ end
+
+ action :boom do
+ # nothing
+ end
+
+ action :noboom do
+ # nothing
+ end
+ end
+
+ let(:node) { Chef::Node.new }
+ let(:events) { Chef::EventDispatch::Dispatcher.new }
+ let(:run_context) { Chef::RunContext.new(node, {}, events) }
+ let(:resource) { TestResource.new("name", run_context) }
+
+ it "raises an exception for an action where the assertions raise exceptions" do
+ expect { resource.run_action(:boom) }.to raise_error(StandardError)
+ end
+
+ it "does not raise an exception for an action which has no assertions" do
+ resource.run_action(:noboom)
+ end
+end