class Kitchen::Terraform::InSpecRunner

InSpecRunner is the class of objects which act as interfaces to the InSpec gem.

Attributes

host[RW]
runner[RW]

Public Class Methods

logger=(logger) click to toggle source

.logger= sets the logger for all InSpec processes.

The logdev of the logger is extended to conform to interface expected by InSpec.

@param logger [Kitchen::Logger] the logger to use. @return [Kitchen::Logger] the logger.

# File lib/kitchen/terraform/inspec_runner.rb, line 31
def logger=(logger)
  logger.logdev.define_singleton_method :filename do
    false
  end

  ::Inspec::Log.logger = logger
end
new(options:, profile_locations:) click to toggle source

initialize prepares a new instance of the class.

@param options [Hash] options to configure the runner. @param profile_locations [Array<String>] a list of pathnames of profiles. @return [Kitchen::Terraform::InSpecRunner]

# File lib/kitchen/terraform/inspec_runner.rb, line 59
def initialize(options:, profile_locations:)
  self.host = options.fetch :host do
    ""
  end

  ::Inspec::Plugin::V2::Loader.new.tap do |loader|
    loader.load_all
    loader.exit_on_load_error
  end

  self.runner = ::Inspec::Runner.new options.merge logger: ::Inspec::Log.logger

  profile_locations.each do |profile_location|
    runner.add_target profile_location
  end
end

Public Instance Methods

exec() click to toggle source

exec executes InSpec.

@raise [Kitchen::TransientFailure] if the execution of InSpec fails. @return [self]

# File lib/kitchen/terraform/inspec_runner.rb, line 44
def exec
  run do |exit_code:|
    if 0 != exit_code
      raise ::Kitchen::TransientFailure, "#{action} failed due to a non-zero exit code of #{exit_code}."
    end
  end

  self
end

Private Instance Methods

action() click to toggle source
# File lib/kitchen/terraform/inspec_runner.rb, line 80
def action
  if host.empty?
    "Running InSpec"
  else
    "Running InSpec against the '#{host}' host"
  end
end
run() { |exit_code: run| ... } click to toggle source
# File lib/kitchen/terraform/inspec_runner.rb, line 88
def run
  yield exit_code: runner.run
rescue => error
  raise ::Kitchen::TransientFailure, "#{action} failed:\n\t\t#{error.message}"
end