class Kitchen::Verifier::Terraform

The verifier utilizes the {www.inspec.io/ InSpec infrastructure testing framework} to verify the behaviour and state of resources in the Terraform 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 executing the associated InSpec controls against the hosts of each system. The outputs of the Terraform state are retrieved and exposed as attributes to the InSpec controls.

Configuration Attributes

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

verifier:
  name: terraform
  a_configuration_attribute: some value

color

{include:Kitchen::Terraform::ConfigAttribute::Color}

fail_fast

{include:Kitchen::Terraform::ConfigAttribute::FailFast}

systems

{include:Kitchen::Terraform::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! ] }

@example Describe the verify command

kitchen help verify

@example Verify a Test Kitchen instance

kitchen verify default-ubuntu

@version 2

Constants

UNSUPPORTED_BASE_ATTRIBUTES

UNSUPPORTED_BASE_ATTRIBUTES is the list of attributes inherited from Kitchen::Verifier::Base which are not supported by Kitchen::Verifier::Terraform.

Attributes

action_failed[RW]
client_error[RW]
outputs[RW]
variables[RW]

Public Class Methods

new(config = {}) click to toggle source

initialize prepares a new instance of the class.

@param config [Hash] the verifier configuration. @return [Kitchen::Verifier::Terraform]

# File lib/kitchen/verifier/terraform.rb, line 130
def initialize(config = {})
  init_config config
  self.action_failed = ::Kitchen::Terraform::Raise::ActionFailed.new logger: logger
  self.client_error = ::Kitchen::Terraform::Raise::ClientError.new logger: logger
  self.outputs = {}
  self.variables = {}
end

Public Instance Methods

call(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 state [Hash] the mutable instance and verifier state. @raise [Kitchen::ActionFailed] if the result of the action is a failure. @return [void]

# File lib/kitchen/verifier/terraform.rb, line 109
def call(state)
  load_variables state: state
  load_outputs state: state
  verify_systems
rescue => error
  action_failed.call message: error.message
end
doctor(_state) click to toggle source

doctor checks the system and configuration for common errors.

@param _state [Hash] the mutable Kitchen instance state. @return [Boolean] true if any errors are found; false if no errors are found. @see github.com/test-kitchen/test-kitchen/blob/v1.21.2/lib/kitchen/verifier/base.rb#L85-L91

# File lib/kitchen/verifier/terraform.rb, line 122
def doctor(_state)
  false
end

Private Instance Methods

load_needed_dependencies!() click to toggle source

load_needed_dependencies! loads the InSpec libraries required to verify a Terraform 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/terraform.rb, line 152
def load_needed_dependencies!
  require "kitchen/terraform/inspec_runner"
  require "kitchen/terraform/system"
  ::Kitchen::Terraform::InSpecRunner.logger = logger
rescue ::LoadError => load_error
  client_error.call message: load_error.message
end
load_outputs(state:) click to toggle source
# File lib/kitchen/verifier/terraform.rb, line 160
def load_outputs(state:)
  logger.warn "Reading the Terraform output variables from the Kitchen instance state..."
  ::Kitchen::Terraform::OutputsManager.new.load outputs: outputs, state: state
  logger.warn "Finished reading the Terraform output variables from the Kitchen instance state."
end
load_variables(state:) click to toggle source
# File lib/kitchen/verifier/terraform.rb, line 142
def load_variables(state:)
  logger.warn "Reading the Terraform input variables from the Kitchen instance state..."
  ::Kitchen::Terraform::VariablesManager.new.load variables: variables, state: state
  logger.warn "Finished reading the Terraform input variables from the Kitchen instance state."
end
profile_locations() click to toggle source
# File lib/kitchen/verifier/terraform.rb, line 166
def profile_locations
  @profile_locations ||= [::File.join(config.fetch(:test_base_path), instance.suite.name)]
end
systems() click to toggle source
# File lib/kitchen/verifier/terraform.rb, line 170
def systems
  @systems ||= config_systems.map do |system|
    ::Kitchen::Terraform::System.new(
      configuration_attributes: { color: config_color, profile_locations: profile_locations }.merge(system),
      logger: logger,
    )
  end
end
systems_verifier() click to toggle source
# File lib/kitchen/verifier/terraform.rb, line 179
def systems_verifier
  @systems_verifier ||= ::Kitchen::Terraform::SystemsVerifierFactory.new(fail_fast: config_fail_fast).build(
    systems: systems,
  )
end
verify_systems() click to toggle source
# File lib/kitchen/verifier/terraform.rb, line 185
def verify_systems
  logger.warn "Verifying the systems..."
  systems_verifier.verify outputs: outputs, variables: variables
  logger.warn "Finished verifying the systems."
end