Class | Chef::Mixin::WhyRun::ResourceRequirements::Assertion
In: | lib/chef/mixin/why_run.rb
Parent: | Object

Implements the logic for a single assertion/assumption. See the documentation for ResourceRequirements for full discussion.

### Methods

assertion assertion_failed? block_action! block_action? failure_message new run whyrun

### Classes and Modules

Class Chef::Mixin::WhyRun::ResourceRequirements::Assertion::AssertionFailure

### Public Class methods

new()

### Public Instance methods

assertion(&assertion_proc)

Defines the code block that determines if a requirement is met. The block should return a truthy value to indicate that the requirement is met, and a falsey value if the requirement is not met.

      # in a provider:
      assert(:some_action) do |a|
        # This provider requires the file /tmp/foo to exist:
        a.assertion { ::File.exist?("/tmp/foo") }
      end

assertion_failed?()
block_action!()

Prevents associated actions from being invoked in whyrun mode. This will also stop further processing of assertions for a given action.

An example from the template provider: if the source template doesn‘t exist we can‘t parse it in the action_create block of template - something that we do even in whyrun mode. Because the soruce template may have been created in an earlier step, we still want to keep going in whyrun mode.

assert(:create, :create_if_missing) do |a|

      a.assertion { File::exists?(@new_resource.source) }
      a.whyrun "Template source file does not exist, assuming it would have been created."
      a.block_action!

end

block_action?()
failure_message(*args)

Defines the failure message, and optionally the Exception class to use when a requirement is not met. It works like `raise`:

      # in a provider:
      assert(:some_action) do |a|
        # This example shows usage with 1 or 2 args by calling #failure_message twice.
        # In practice you should only call this once per Assertion.

        # Set the Exception class explicitly
        a.failure_message(Chef::Exceptions::MissingRequiredFile, "File /tmp/foo doesn't exist")
        # Fallback to the default error class (AssertionFailure)
        a.failure_message("File /tmp/foo" doesn't exist")
      end

run(action, events, resource)

Runs the assertion/assumption logic. Will raise an Exception of the type specified in failure_message (or AssertionFailure by default) if the requirement is not met and Chef is not running in why run mode. An exception will also be raised if running in why run mode and no why run message or block has been declared.

whyrun(message, &resource_modifier)

Defines a message and optionally provides a code block to execute when the requirement is not met and Chef is executing in why run mode

If no failure_message is provided (above), then execution will be allowed to continue in both whyrun an dnon-whyrun mode

With a service resource that requires /etc/init.d/service-name to exist:

      # in a provider
      assert(:start, :restart) do |a|
        a.assertion { ::File.exist?("/etc/init.d/service-name") }
        a.whyrun("Init script '/etc/init.d/service-name' doesn't exist, assuming a prior action would have created it.") do
          # blindly assume that the service exists but is stopped in why run mode:
          @new_resource.status(:stopped)
        end
      end

[Validate]