class Kitchen::Verifier::Pulumi

The verifier utilizes the {www.inspec.io/ InSpec} infrastructure testing framework to verify the behaviour and state of resources in the Pulumi state.

Commands

The following command-line commands are provided by the verifier.

kitchen verify

A Kitchen instance is verified by iterating through the systems and running the associated InSpec controls against the hosts of each system. The outputs of the Pulumi state are retrieved and exposed as attributes to the InSpec controls.

Retrieving the Pulumi Output
pulumi stack output -json

Configuration Attributes

The configuration attributes of the verifier control the behaviour of the InSpec runner. Within the {kitchen.ci/docs/getting-started/kitchen-yml .kitchen.yml}, these attributes must be declared in the verifier mapping along with the plugin name.

verifier:
  name: pulumi
  a_configuration_attribute: some value

color

{include:Kitchen::Pulumi::ConfigAttribute::Color}

fail_fast

{include:Kitchen::Pulumi::ConfigAttribute::FailFast}

systems

{include:Kitchen::Pulumi::ConfigAttribute::Systems}

Ruby Interface

This class implements the interface of Kitchen::Configurable which requires the following Reek suppressions: :reek: MissingSafeMethod {

exclude: [ finalize_config!, load_needed_dependencies! ]

}

Attributes

error_messages[RW]
inspec_options_mapper[RW]
pulumi_inputs[RW]
pulumi_outputs[RW]

Public Class Methods

new(configuration = {}) click to toggle source
Calls superclass method
# File lib/kitchen/verifier/pulumi.rb, line 77
def initialize(configuration = {})
  super(configuration)
  @inspec_options_mapper = ::Kitchen::Pulumi::InSpecOptionsMapper.new
  @error_messages = []
  @pulumi_inputs = {}
  @pulumi_outputs = {}
end

Public Instance Methods

call(_kitchen_state) click to toggle source

The verifier enumerates through each host of each system and verifies the associated InSpec controls.

@example

`kitchen verify suite-name`

@param _kitchen_state [::Hash] the mutable instance and verifier state. @raise [::Kitchen::ActionFailed] if result of the action is failure. @return [void]

# File lib/kitchen/verifier/pulumi.rb, line 93
def call(_kitchen_state)
  load_variables
  verify_systems
  unless error_messages.empty?
    raise ::Kitchen::ActionFailed, error_messages.join("\n\n")
  end
rescue ::Kitchen::Pulumi::Error => e
  raise ::Kitchen::ActionFailed, e.message
end
doctor(_kitchen_state) click to toggle source

Checks the system and configuration for common errors.

@param _kitchen_state [::Hash] the mutable Kitchen instance state. @return [Boolean] false @see github.com/test-kitchen/test-kitchen/blob/v1.21.2/lib/kitchen/verifier/base.rb#L85-L91

# File lib/kitchen/verifier/pulumi.rb, line 108
def doctor(_kitchen_state)
  false
end

Private Instance Methods

handle_error(message:) click to toggle source

Raises an error immediately if the `fail_fast` config attribute is set on the

or collects all errors until execution has ended verifier

@return [void]

# File lib/kitchen/verifier/pulumi.rb, line 121
def handle_error(message:)
  raise ::Kitchen::Pulumi::Error, message if config_fail_fast

  logger.error message
  error_messages.push message
end
load_needed_dependencies!() click to toggle source

load_needed_dependencies! loads the InSpec libraries required to verify a Pulumi stack's state.

@raise [::Kitchen::ClientError] if loading the InSpec libraries fails. @see github.com/test-kitchen/test-kitchen/blob/v1.21.2/lib/kitchen/configurable.rb#L252-L274

# File lib/kitchen/verifier/pulumi.rb, line 147
def load_needed_dependencies!
  require 'kitchen/pulumi/inspec'
  require 'kitchen/pulumi/system'
  ::Kitchen::Pulumi::InSpec.logger = logger
rescue ::LoadError => e
  raise ::Kitchen::ClientError, e.message
end
load_variables() click to toggle source

Populates the `stack_inputs` and `stack_outputs` with the fully resolved stack

inputs and outputs produced by the appropriate Pulumi commands

@return [void]

# File lib/kitchen/verifier/pulumi.rb, line 132
def load_variables
  instance.driver.stack_outputs do |outputs:|
    @pulumi_outputs.replace(outputs)
  end

  instance.driver.stack_inputs do |inputs:|
    @pulumi_inputs.replace(inputs)
  end
end
system_inspec_options(system:) click to toggle source
# File lib/kitchen/verifier/pulumi.rb, line 155
def system_inspec_options(system:)
  inspec_options_mapper.map(
    options: { 'color' => config_color, 'distinct_exit' => false },
    system: system,
  )
end
verify(system:) click to toggle source

Runs verification logic of the given system

@param system [::Hash] the system to verify @return [void]

# File lib/kitchen/verifier/pulumi.rb, line 166
def verify(system:)
  ::Kitchen::Pulumi::System.new(
    mapping: {
      profile_locations: [
        ::File.join(config.fetch(:test_base_path), instance.suite.name),
      ],
    }.merge(system),
  ).verify(
    pulumi_inputs: @pulumi_inputs,
    pulumi_outputs: @pulumi_outputs,
    inspec_options: system_inspec_options(system: system),
  )
rescue StandardError => e
  handle_error message: e.message
end
verify_systems() click to toggle source

Runs verification logic for each system defined on the verifier's `systems` config

attribute

@return [void]

# File lib/kitchen/verifier/pulumi.rb, line 186
def verify_systems
  config_systems.each do |system|
    verify system: system
  end
end