From a1ddbe4cf2b8546bf407690d49a4a64a3ace62e8 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Sun, 10 Jan 2016 23:59:57 -0500 Subject: [PATCH] explicitly ignore supports for `inspec check` --- bin/inspec | 1 + lib/inspec/metadata.rb | 4 ---- lib/inspec/profile.rb | 3 +-- lib/inspec/runner.rb | 8 ++++---- test/unit/profile_test.rb | 28 ++++++++++++++++------------ 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/bin/inspec b/bin/inspec index cf2efbdab..dd22d4099 100755 --- a/bin/inspec +++ b/bin/inspec @@ -73,6 +73,7 @@ class InspecCLI < Thor # rubocop:disable Metrics/ClassLength o = opts.dup o[:logger] = Logger.new(STDOUT) + o[:ignore_supports] = true # we check for integrety only profile = Inspec::Profile.from_path(path, o) exit 1 unless profile.check end diff --git a/lib/inspec/metadata.rb b/lib/inspec/metadata.rb index d30aad87c..9af6c9972 100644 --- a/lib/inspec/metadata.rb +++ b/lib/inspec/metadata.rb @@ -86,10 +86,6 @@ module Inspec end def supports_transport?(backend) - # make a happy face when presented the mock backend - # FIXME(sr) this is not beautiful - return true if backend.backend.is_a? Train::Transports::Mock::Connection - # make sure the supports field is always an array supp = params[:supports] supp = supp.is_a?(Hash) ? [supp] : Array(supp) diff --git a/lib/inspec/profile.rb b/lib/inspec/profile.rb index f22162a99..bce4a2504 100644 --- a/lib/inspec/profile.rb +++ b/lib/inspec/profile.rb @@ -38,8 +38,7 @@ module Inspec id: @profile_id, backend: :mock, ) - - @runner.add_tests([@path]) + @runner.add_tests([@path], @options) @runner.rules.each do |id, rule| file = rule.instance_variable_get(:@__file) rules[file] ||= {} diff --git a/lib/inspec/runner.rb b/lib/inspec/runner.rb index 434c66aaf..05050ab39 100644 --- a/lib/inspec/runner.rb +++ b/lib/inspec/runner.rb @@ -47,22 +47,22 @@ module Inspec @backend = Inspec::Backend.create(@conf) end - def add_test_profile(test) + def add_test_profile(test, ignore_supports = false) assets = Inspec::Targets.resolve(test, @conf) meta_assets = assets.find_all { |a| a[:type] == :metadata } metas = meta_assets.map do |x| Inspec::Metadata.from_ref(x[:ref], x[:content], @profile_id, @conf[:logger]) end metas.each do |meta| - return [] unless meta.supports_transport?(@backend) + return [] if !ignore_supports && !meta.supports_transport?(@backend) end assets end - def add_tests(tests) + def add_tests(tests, options = {}) # retrieve the raw ruby code of all tests items = tests.map do |test| - add_test_profile(test) + add_test_profile(test, options[:ignore_supports]) end.flatten tests = items.find_all { |i| i[:type] == :test } diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 1d3dd0c46..1453afbd6 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -11,7 +11,7 @@ describe Inspec::Profile do # currently it's stopped at test runner conflicts class Inspec::Profile::Runner def initialize(opts) end - def add_tests(tests) end + def add_tests(tests, options=nil) end def rules {} end @@ -85,18 +85,22 @@ describe Inspec::Profile do end describe 'a complete metadata profile with controls' do - let(:profile) { load_profile('complete-profile', {logger: logger}) } + let(:profile) { load_profile('complete-profile', {logger: logger, ignore_supports: true}) } - # TODO(sr): this test fails, while it works on the command line - # it 'prints ok messages and counts the rules' do - # logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/complete-profile"] - # logger.expect :info, nil, ['Metadata OK.'] - # logger.expect :info, nil, ['Found 1 rules.'] - # logger.expect :info, nil, ['Rule definitions OK.'] - # - # profile.check - # logger.verify - # end + it 'prints ok messages and counts the rules' do + logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/complete-profile"] + logger.expect :info, nil, ['Metadata OK.'] + + # TODO: cannot load rspec in unit tests, therefore we get a loading warn + # RSpec does not work with minitest tests + logger.expect :warn, nil, ['No controls or tests were defined.'] + # we expect that this should work: + # logger.expect :info, nil, ['Found 1 rules.'] + # logger.expect :info, nil, ['Rule definitions OK.'] + + profile.check + logger.verify + end end end end