lib/inspec/runner.rb in inspec-0.35.0 vs lib/inspec/runner.rb in inspec-1.0.0.beta2
- old
+ new
@@ -9,10 +9,11 @@
require 'inspec/backend'
require 'inspec/profile_context'
require 'inspec/profile'
require 'inspec/metadata'
require 'inspec/secrets'
+require 'inspec/dependencies/cache'
# spec requirements
module Inspec
#
# Inspec::Runner coordinates the running of tests and is the main
@@ -30,21 +31,21 @@
#
class Runner # rubocop:disable Metrics/ClassLength
extend Forwardable
def_delegator :@test_collector, :report
- def_delegator :@test_collector, :reset
attr_reader :backend, :rules, :attributes
def initialize(conf = {})
@rules = []
@conf = conf.dup
@conf[:logger] ||= Logger.new(nil)
@target_profiles = []
@controls = @conf[:controls] || []
@ignore_supports = @conf[:ignore_supports]
-
+ @create_lockfile = @conf[:create_lockfile]
+ @cache = Inspec::Cache.new(@conf[:cache])
@test_collector = @conf.delete(:test_collector) || begin
require 'inspec/runner_rspec'
RunnerRspec.new(@conf)
end
@@ -62,26 +63,54 @@
def configure_transport
@backend = Inspec::Backend.create(@conf)
@test_collector.backend = @backend
end
- def run(with = nil)
- Inspec::Log.debug "Starting run with targets: #{@target_profiles.map(&:to_s)}"
+ def reset
+ @test_collector.reset
+ @target_profiles.each do |profile|
+ profile.runner_context.rules = {}
+ end
+ @rules = []
+ end
+
+ def load
all_controls = []
@target_profiles.each do |profile|
@test_collector.add_profile(profile)
+ write_lockfile(profile) if @create_lockfile
profile.locked_dependencies
profile.load_libraries
@attributes |= profile.runner_context.attributes
all_controls += profile.collect_tests
end
all_controls.each do |rule|
register_rule(rule)
end
+ end
+ def run(with = nil)
+ Inspec::Log.debug "Starting run with targets: #{@target_profiles.map(&:to_s)}"
+ load
+ run_tests(with)
+ end
+
+ def write_lockfile(profile)
+ return false if !profile.writable?
+
+ if profile.lockfile_exists?
+ Inspec::Log.debug "Using existing lockfile #{profile.lockfile_path}"
+ else
+ Inspec::Log.debug "Creating lockfile: #{profile.lockfile_path}"
+ lockfile = profile.generate_lockfile
+ File.write(profile.lockfile_path, lockfile.to_yaml)
+ end
+ end
+
+ def run_tests(with = nil)
@test_collector.run(with)
end
# determine all attributes before the execution, fetch data from secrets backend
def load_attributes(options)
@@ -124,31 +153,18 @@
#
# @eturns [Inspec::ProfileContext]
#
def add_target(target, _opts = [])
profile = Inspec::Profile.for_target(target,
+ cache: @cache,
backend: @backend,
controls: @controls,
attributes: @conf[:attributes])
fail "Could not resolve #{target} to valid input." if profile.nil?
@target_profiles << profile if supports_profile?(profile)
end
- #
- # This is used by inspec-shell and inspec-detect. This should
- # probably be cleaned up a bit.
- #
- # @params [Hash] Options
- # @returns [Inspec::ProfileContext]
- #
- def create_context(options = {})
- meta = options[:metadata]
- profile_id = nil
- profile_id = meta.params[:name] unless meta.nil?
- Inspec::ProfileContext.new(profile_id, @backend, @conf.merge(options))
- end
-
def supports_profile?(profile)
return true if @ignore_supports
if !profile.supports_runtime?
fail 'This profile requires InSpec version '\
@@ -176,9 +192,17 @@
next if block_given? && !(yield rule_id, rule)
new_tests = true
register_rule(rule)
end
new_tests
+ end
+
+ def eval_with_virtual_profile(command)
+ require 'fetchers/mock'
+ add_target({ 'inspec.yml' => 'name: inspec-shell' })
+ our_profile = @target_profiles.first
+ ctx = our_profile.runner_context
+ ctx.load(command)
end
private
def block_source_info(block)