class Chef::Mixin::WhyRun::ResourceRequirements

ResourceRequirements provides a framework for making assertions about the host system’s state. It also provides a mechanism for making assumptions about what the system’s state might have been when running in why run mode.

For example, consider a recipe that consists of a package resource and a service resource. If the service’s init script is installed by the package, and Chef is running in why run mode, then the service resource would fail when attempting to run ‘/etc/init.d/software-name status`. In order to provide a more useful approximation of what would happen in a real chef run, we want to instead assume that the service was created but isn’t running. The logic would look like this:

# Hypothetical service provider demonstrating why run assumption logic.
# This isn't the actual API, it just shows the logic.
class HypotheticalServiceProvider < Chef::Provider

  def load_current_resource
    # Make sure we have the init script available:
    if ::File.exist?("/etc/init.d/some-service"
      # If the init script exists, proceed as normal:
      status_cmd = shell_out("/etc/init.d/some-service status")
      if status_cmd.success?
        @current_resource.status(:running)
      else
        @current_resource.status(:stopped)
      end
    else
      if whyrun_mode?
        # If the init script is not available, and we're in why run mode,
        # assume that some previous action would've created it:
        log("warning: init script '/etc/init.d/some-service' is not available")
        log("warning: assuming that the init script would have been created, assuming the state of 'some-service' is 'stopped'")
        @current_resource.status(:stopped)
      else
        raise "expected init script /etc/init.d/some-service doesn't exist"
      end
    end
  end

end

In short, the code above does the following:

ResourceRequirements encapsulates the above logic in a more declarative API.

Examples

Assertions and assumptions should be created through the WhyRun#assert method, which gets mixed in to providers. See that method’s documentation for examples.