api: inspec.yml supports now adds tests w/o running

Instead of just removing all tests because of OS support, supports now acts by adding all tests to the execution context, but doesnt actually execute them. Instead tests are set to skip before they get to the actual execution context
This commit is contained in:
Dominik Richter 2016-04-05 18:07:00 +02:00 committed by Christoph Hartmann
parent 8656e673ef
commit fb54c4ea24
4 changed files with 44 additions and 2 deletions

View file

@ -53,9 +53,8 @@ module Inspec
end
def add_profile(profile, options = {})
return unless options[:ignore_supports] ||
profile.metadata.supports_transport?(@backend)
@test_collector.add_profile(profile)
options[:metadata] = profile.metadata
libs = profile.libraries.map do |k, v|
{ ref: k, content: v }
@ -87,6 +86,19 @@ module Inspec
tests = [tests] unless tests.is_a? Array
tests.each { |t| add_test_to_context(t, ctx) }
# skip based on support checks in metadata
meta = options[:metadata]
if !options[:ignore_supports] && !meta.nil? &&
!meta.supports_transport?(@backend)
os_info = @backend.os[:family].to_s
ctx.rules.values.each do |ctrl|
::Inspec::Rule.set_skip_rule(
ctrl,
"This OS/platform (#{os_info}) is not supported by this profile.",
)
end
end
# process the resulting rules
filter_controls(ctx.rules, options[:controls]).each do |rule_id, rule|
register_rule(rule_id, rule)

View file

@ -138,4 +138,26 @@ describe 'inspec exec' do
end
end
describe 'with a profile that is not supported on this OS/platform' do
let(:out) { inspec('exec ' + File.join(profile_path, 'skippy-profile-os') + ' --format fulljson') }
let(:json) { JSON.load(out.stdout) }
it 'exits cleanly' do
out.stderr.must_equal ''
out.exit_status.must_equal 0
end
it 'has one pending' do
json['summary']['pending_count'].must_equal 1
json['summary']['example_count'].must_equal 1
end
it 'delivers the pending message' do
json['examples'][0]['pending'].must_match %r{^This OS/platform \([^)]+\) is not supported by this profile\.$}
end
it 'never runs the actual resource' do
File.exist?('/tmp/inspec_test_DONT_CREATE').must_equal false
end
end
end

View file

@ -0,0 +1,3 @@
describe command('touch /tmp/inspec_test_DONT_CREATE') do
its(:exit_status) { should eq 123 }
end

View file

@ -0,0 +1,5 @@
name: skippy
title: skip-like functionality
version: 1.0.0
supports:
- os-family: definitely_not_supported