Class: Inspec::Runner
- Inherits:
-
Object
- Object
- Inspec::Runner
- Extended by:
- Forwardable
- Defined in:
- lib/inspec/runner.rb
Overview
Inspec::Runner coordinates the running of tests and is the main entry point to the application.
Users are expected to insantiate a runner, add targets to be run, and then call the run method:
“‘ r = Inspec::Runner.new() r.add_target(“/path/to/some/profile”) r.add_target(“url/to/some/profile”) r.run “`
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#target_profiles ⇒ Object
Returns the value of attribute target_profiles.
-
#test_collector ⇒ Object
Returns the value of attribute test_collector.
Instance Method Summary collapse
-
#add_target(target, _opts = []) ⇒ Object
add_target allows the user to add a target whose tests will be run when the user calls the run method.
-
#all_rules ⇒ Object
In some places we read the rules off of the runner, in other places we read it off of the profile context.
- #attributes ⇒ Object
- #configure_transport ⇒ Object
- #eval_with_virtual_profile(command) ⇒ Object
-
#initialize(conf = {}) ⇒ Runner
constructor
A new instance of Runner.
- #load ⇒ Object
- #register_rules(ctx) ⇒ Object
- #render_output(run_data) ⇒ Object
- #report ⇒ Object
- #reset ⇒ Object
- #run(with = nil) ⇒ Object
- #run_tests(with = nil) ⇒ Object
- #supports_profile?(profile) ⇒ Boolean
- #tests ⇒ Object
- #verify_target_profiles_if_signed(target_profiles) ⇒ Object
- #write_lockfile(profile) ⇒ Object
Constructor Details
#initialize(conf = {}) ⇒ Runner
Returns a new instance of Runner.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/inspec/runner.rb', line 45 def initialize(conf = {}) @rules = [] # If we were handed a Hash config (by audit cookbook or kitchen-inspec), # upgrade it to a proper config. This handles a lot of config finalization, # like reporter parsing. @conf = conf.is_a?(Hash) ? Inspec::Config.new(conf) : conf @conf[:logger] ||= Logger.new(nil) @target_profiles = [] @controls = @conf[:controls] || [] @tags = @conf[:tags] || [] @depends = @conf[:depends] || [] @create_lockfile = @conf[:create_lockfile] @cache = Inspec::Cache.new(@conf[:vendor_cache]) @test_collector = @conf.delete(:test_collector) || begin RunnerRspec.new(@conf) end if @conf[:waiver_file] Inspec.with_feature("inspec-waivers") { @conf[:waiver_file].each do |file| unless File.file?(file) raise Inspec::Exceptions::WaiversFileDoesNotExist, "Waiver file #{file} does not exist." end end } end # About reading inputs: # @conf gets passed around a lot, eventually to # Inspec::InputRegistry.register_external_inputs. # # @conf may contain the key :attributes or :inputs, which is to be a Hash # of values passed in from the Runner API. # This is how kitchen-inspec and the audit_cookbook pass in inputs. # # @conf may contain the key :attrs or :input_file, which is to be an Array # of file paths, each a YAML file. This how --input-file works. configure_transport end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
35 36 37 |
# File 'lib/inspec/runner.rb', line 35 def backend @backend end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
35 36 37 |
# File 'lib/inspec/runner.rb', line 35 def rules @rules end |
#target_profiles ⇒ Object
Returns the value of attribute target_profiles.
36 37 38 |
# File 'lib/inspec/runner.rb', line 36 def target_profiles @target_profiles end |
#test_collector ⇒ Object
Returns the value of attribute test_collector.
38 39 40 |
# File 'lib/inspec/runner.rb', line 38 def test_collector @test_collector end |
Instance Method Details
#add_target(target, _opts = []) ⇒ Object
add_target allows the user to add a target whose tests will be run when the user calls the run method.
A target is a path or URL that points to a profile. Using this target we generate a Profile and a ProfileContext. The content (libraries, tests, and inputs) from the Profile are loaded into the ProfileContext.
If the profile depends on other profiles, those profiles will be loaded on-demand when include_content or required_content are called using similar code in Inspec::DSL.
Once the we’ve loaded all of the tests files in the profile, we query the profile for the full list of rules. Those rules are registered with the @test_collector which is ultimately responsible for actually running the tests.
TODO: Deduplicate/clarify the loading code that exists in here, the ProfileContext, the Profile, and Inspec::DSL
254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/inspec/runner.rb', line 254 def add_target(target, _opts = []) profile = Inspec::Profile.for_target(target, vendor_cache: @cache, backend: @backend, controls: @controls, tags: @tags, runner_conf: @conf) raise "Could not resolve #{target} to valid input." if profile.nil? @target_profiles << profile if supports_profile?(profile) end |
#all_rules ⇒ Object
In some places we read the rules off of the runner, in other places we read it off of the profile context. To keep the API’s the same, we provide an #all_rules method here as well.
279 280 281 |
# File 'lib/inspec/runner.rb', line 279 def all_rules @rules end |
#attributes ⇒ Object
40 41 42 43 |
# File 'lib/inspec/runner.rb', line 40 def attributes Inspec.deprecate(:rename_attributes_to_inputs, "Don't call runner.attributes, call runner.inputs") inputs end |
#configure_transport ⇒ Object
91 92 93 94 |
# File 'lib/inspec/runner.rb', line 91 def configure_transport @backend = Inspec::Backend.create(@conf) @test_collector.backend = @backend end |
#eval_with_virtual_profile(command) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/inspec/runner.rb', line 294 def eval_with_virtual_profile(command) require "inspec/fetcher/mock" add_target({ "inspec.yml" => "name: inspec-shell" }) our_profile = @target_profiles.first ctx = our_profile.runner_context # Load local profile dependencies. This is used in inspec shell # to provide access to local profiles that add resources. @depends.each do |dep| # support for windows paths dep = dep.tr("\\", "/") Inspec::Profile.for_path(dep, { profile_context: ctx }).load_libraries end ctx.load(command) end |
#load ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/inspec/runner.rb', line 104 def load all_controls = [] @target_profiles.each do |profile| @test_collector.add_profile(profile) next unless profile.supports_platform? write_lockfile(profile) if @create_lockfile profile.locked_dependencies profile.load_gem_dependencies profile_context = profile.load_libraries profile_context.dependencies.list.values.each do |requirement| unless requirement.profile.supports_platform? Inspec::Log.warn "Skipping profile: '#{requirement.profile.name}'" \ " on unsupported platform: '#{@backend.platform.name}/#{@backend.platform.release}'." next end @test_collector.add_profile(requirement.profile) end begin tests = profile.collect_tests all_controls += tests unless tests.nil? rescue Inspec::Exceptions::ProfileLoadFailed => e Inspec::Log.error "Failed to load profile #{profile.name}: #{e}" profile. e.to_s next end end controls_count = 0 control_checks_count_map = {} all_controls.each do |rule| unless rule.nil? register_rule(rule) total_checks = 0 control_describe_checks = ::Inspec::Rule.prepare_checks(rule) examples = control_describe_checks.flat_map do |m, a, b| get_check_example(m, a, b) end.compact examples.map { |example| total_checks += example.examples.count } unless control_describe_checks.empty? # controls with empty tests are avoided # checks represent tests within control controls_count += 1 if control_checks_count_map[rule.to_s].nil? control_checks_count_map[rule.to_s] = control_checks_count_map[rule.to_s].to_i + total_checks end end end # this sets data via runner-rspec into base RSpec formatter object, which gets used up within streaming plugins @test_collector.set_controls_count(controls_count) @test_collector.set_control_checks_count_map(control_checks_count_map) end |
#register_rules(ctx) ⇒ Object
283 284 285 286 287 288 289 290 291 292 |
# File 'lib/inspec/runner.rb', line 283 def register_rules(ctx) new_tests = false ctx.rules.each do |rule_id, rule| next if block_given? && !(yield rule_id, rule) new_tests = true register_rule(rule) end new_tests end |
#render_output(run_data) ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/inspec/runner.rb', line 195 def render_output(run_data) return if @conf["reporter"].nil? @conf["reporter"].each do |reporter| enhanced_outcome_flag = @conf["enhanced_outcomes"] result = Inspec::Reporters.render(reporter, run_data, enhanced_outcome_flag) raise Inspec::ReporterError, "Error generating reporter '#{reporter[0]}'" if result == false end end |
#report ⇒ Object
205 206 207 |
# File 'lib/inspec/runner.rb', line 205 def report Inspec::Reporters.report(@conf["reporter"].first, @run_data) end |
#reset ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/inspec/runner.rb', line 96 def reset @test_collector.reset @target_profiles.each do |profile| profile.runner_context.rules = {} end @rules = [] end |
#run(with = nil) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/inspec/runner.rb', line 164 def run(with = nil) ChefLicensing.check_software_entitlement! if Inspec::Dist::EXEC_NAME == "inspec" # Validate if profiles are signed and verified # Additional check is required to provide error message in case of inspec exec command (exec command can use multiple profiles as well) # Only runs this block when preview flag CHEF_PREVIEW_MANDATORY_PROFILE_SIGNING is set Inspec.with_feature("inspec-mandatory-profile-signing") { unless @conf.allow_unsigned_profiles? verify_target_profiles_if_signed(@target_profiles) end } Inspec::Log.debug "Starting run with targets: #{@target_profiles.map(&:to_s)}" load run_tests(with) rescue ChefLicensing::SoftwareNotEntitled Inspec::Log.error "License is not entitled to use InSpec." Inspec::UI.new.exit(:license_not_entitled) rescue ChefLicensing::Error => e Inspec::Log.error e. Inspec::UI.new.exit(:usage_error) end |
#run_tests(with = nil) ⇒ Object
221 222 223 224 225 226 |
# File 'lib/inspec/runner.rb', line 221 def run_tests(with = nil) @run_data = @test_collector.run(with) # dont output anything if we want a report render_output(@run_data) unless @conf["report"] @test_collector.exit_code end |
#supports_profile?(profile) ⇒ Boolean
266 267 268 269 270 271 272 273 274 |
# File 'lib/inspec/runner.rb', line 266 def supports_profile?(profile) unless profile.supports_runtime? raise "This profile requires #{Inspec::Dist::PRODUCT_NAME} version "\ "#{profile..inspec_requirement}. You are running "\ "#{Inspec::Dist::PRODUCT_NAME} v#{Inspec::VERSION}.\n" end true end |
#tests ⇒ Object
87 88 89 |
# File 'lib/inspec/runner.rb', line 87 def tests @test_collector.tests end |
#verify_target_profiles_if_signed(target_profiles) ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/inspec/runner.rb', line 187 def verify_target_profiles_if_signed(target_profiles) unsigned_profiles = [] target_profiles.each do |profile| unsigned_profiles << profile.name unless profile.verify_if_signed end raise Inspec::ProfileSignatureRequired, "Signature required for profile/s: #{unsigned_profiles.join(", ")}. Please provide a signed profile. Or set CHEF_ALLOW_UNSIGNED_PROFILES in the environment. Or use `--allow-unsigned-profiles` flag with InSpec CLI. " unless unsigned_profiles.empty? end |
#write_lockfile(profile) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/inspec/runner.rb', line 209 def write_lockfile(profile) return false unless 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 |