Class: Kitchen::Verifier::Inspec

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/verifier/inspec.rb

Overview

InSpec verifier for Kitchen.

Author:

Instance Method Summary collapse

Instance Method Details

#call(state) ⇒ Object

Raises:

  • (ActionFailed)

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kitchen/verifier/inspec.rb', line 66

def call(state)
  logger.debug("Initialize InSpec")

  # gather connection options
  opts = runner_options(instance.transport, state, instance.platform.name, instance.suite.name)
  logger.debug "Options #{opts.inspect}"

  # add inputs
  setup_inputs(opts, config)

  # setup logger
  ::Inspec::Log.init(STDERR)
  ::Inspec::Log.level = Kitchen::Util.from_logger_level(logger.level)

  # initialize runner
  runner = ::Inspec::Runner.new(opts)

  # add each profile to runner
  tests = collect_tests
  profile_ctx = nil
  tests.each do |target|
    profile_ctx = runner.add_target(target, opts)
  end

  profile_ctx ||= []
  profile_ctx.each do |profile|
    logger.info("Loaded #{profile.name} ")
  end

  exit_code = runner.run
  # 101 is a success as well (exit with no fails but has skipped controls)
  return if exit_code == 0 || exit_code == 101
  raise ActionFailed, "InSpec Runner returns #{exit_code}"
end

#finalize_config!(instance) ⇒ self

A lifecycle method that should be invoked when the object is about ready to be used. A reference to an Instance is required as configuration dependant data may be access through an Instance. This also acts as a hook point where the object may wish to perform other last minute checks, validations, or configuration expansions.

Parameters:

  • instance (Instance)

    an associated instance

Returns:

  • (self)

    itself, for use in chaining

Raises:

  • (ClientError)

    if instance parameter is nil


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kitchen/verifier/inspec.rb', line 49

def finalize_config!(instance)
  super

  # We want to switch kitchen-inspec to look for its tests in
  # `cookbook_dir/test/recipes` instead of `cookbook_dir/test/integration`
  # Unfortunately there is no way to read `test_base_path` from the
  # .kitchen.yml, it can only be provided on the CLI.
  # See https://github.com/test-kitchen/test-kitchen/issues/1077
  inspec_test_dir = File.join(config[:kitchen_root], "test", "recipes")
  if File.directory?(inspec_test_dir)
    config[:test_base_path] = inspec_test_dir
  end

  self
end