Class: Inspec::Runner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/inspec/runner.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = {}) ⇒ Runner

Returns a new instance of Runner.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/inspec/runner.rb', line 19

def initialize(conf = {})
  @rules = {}
  @profile_id = conf[:id]
  @conf = conf.dup
  @conf[:logger] ||= Logger.new(nil)

  @test_collector = @conf.delete(:test_collector) || begin
    require 'inspec/runner_rspec'
    RunnerRspec.new(@conf)
  end

  configure_transport
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



18
19
20
# File 'lib/inspec/runner.rb', line 18

def backend
  @backend
end

#rulesObject (readonly)

Returns the value of attribute rules.



18
19
20
# File 'lib/inspec/runner.rb', line 18

def rules
  @rules
end

Instance Method Details

#add_content(tests, libs, options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/inspec/runner.rb', line 94

def add_content(tests, libs, options = {})
  return if tests.nil? || tests.empty?

  # load all libraries
  ctx = create_context(options)
  libs.each do |lib|
    ctx.load(lib[:content].to_s, lib[:ref], lib[:line] || 1)
    ctx.reload_dsl
  end

  # evaluate the test content
  tests = [tests] unless tests.is_a? Array
  tests.each { |t| add_test_to_context(t, ctx) }

  # process the resulting rules
  filter_controls(ctx.rules, options[:controls]).each do |rule_id, rule|
    register_rule(rule_id, rule)
  end
end

#add_profile(profile, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/inspec/runner.rb', line 72

def add_profile(profile, options = {})
  return if !options[:ignore_supports] && !supports_profile?(profile)

  @test_collector.add_profile(profile)
  options[:metadata] = profile.

  libs = profile.libraries.map do |k, v|
    { ref: k, content: v }
  end

  tests = profile.tests.map do |ref, content|
    r = profile.source_reader.target.abs_path(ref)
    { ref: r, content: content }
  end

  add_content(tests, libs, options)
end

#add_target(target, options = {}) ⇒ Object



49
50
51
52
53
# File 'lib/inspec/runner.rb', line 49

def add_target(target, options = {})
  profile = Inspec::Profile.for_target(target, options)
  fail "Could not resolve #{target} to valid input." if profile.nil?
  add_profile(profile, options)
end

#configure_transportObject



45
46
47
# File 'lib/inspec/runner.rb', line 45

def configure_transport
  @backend = Inspec::Backend.create(@conf)
end

#create_context(options = {}) ⇒ Object



90
91
92
# File 'lib/inspec/runner.rb', line 90

def create_context(options = {})
  Inspec::ProfileContext.new(@profile_id, @backend, @conf.merge(options))
end

#normalize_map(hm) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/inspec/runner.rb', line 37

def normalize_map(hm)
  res = {}
  hm.each {|k, v|
    res[k.to_s] = v
  }
  res
end

#supports_profile?(profile) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/inspec/runner.rb', line 55

def supports_profile?(profile)
  return true if profile..nil?

  if !profile..supports_runtime?
    fail 'This profile requires InSpec version '\
         "#{profile..inspec_requirement}. You are running "\
         "InSpec v#{Inspec::VERSION}.\n"
  end

  if !profile..supports_transport?(@backend)
    os_info = @backend.os[:family].to_s
    fail "This OS/platform (#{os_info}) is not supported by this profile."
  end

  true
end

#testsObject



33
34
35
# File 'lib/inspec/runner.rb', line 33

def tests
  @test_collector.tests
end