Merge pull request #1008 from chef/vj/fix-cli-inherited-profiles

fix cli inherited profiles
This commit is contained in:
Christoph Hartmann 2016-09-04 18:51:15 +02:00 committed by GitHub
commit 5bfc234500
7 changed files with 97 additions and 5 deletions

View file

@ -164,10 +164,32 @@ class InspecRspecJson < InspecRspecMiniJson
[info[:name], info]
end
#
# TODO(ssd+vj): We should probably solve this by either ensuring the example has
# the profile_id of the top level profile when it is included as a dependency, or
# by registrying all dependent profiles with the formatter. The we could remove
# this heuristic matching.
#
def example2profile(example, profiles)
profiles.values.find { |p| profile_contains_example?(p, example) }
end
def profile_contains_example?(profile, example)
# Heuristic for finding the profile an example came from:
# Case 1: The profile_id on the example matches the name of the profile
# Case 2: The profile contains a control that matches the id of the example
if profile[:name] == example[:profile_id]
true
elsif profile[:controls] && profile[:controls].key?(example[:id])
true
else
false
end
end
def example2control(example, profiles)
profile = profiles[example[:profile_id]]
return nil if profile.nil? || profile[:controls].nil?
profile[:controls][example[:id]]
p = example2profile(example, profiles)
p[:controls][example[:id]] if p && p[:controls]
end
def format_example(example)

View file

@ -23,6 +23,7 @@ module FunctionalHelper
let(:example_control) { File.join(example_profile, 'controls', 'example.rb') }
let(:inheritance_profile) { File.join(examples_path, 'profile') }
let(:failure_control) { File.join(profile_path, 'failures', 'controls', 'failures.rb') }
let(:simple_inheritance) { File.join(profile_path, 'simple-inheritance') }
let(:dst) {
# create a temporary path, but we only want an auto-clean helper

View file

@ -147,4 +147,27 @@ Summary: \e[32m2 successful\e[0m, \e[31m0 failures\e[0m, \e[37m0 skipped\e[0m
out.stdout.must_include "undefined method `should_nota' "
end
end
describe 'given an inherited profile that has more that one test per control block' do
let(:out) { inspec('exec ' + simple_inheritance) }
it 'should print all the results' do
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ tmp-1.0: Create /tmp directory (1 failed)\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ should not be directory\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ undefined method `should_nota'"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ expected `File /tmp.directory?` to return false, got true\e[0m"
end
end
describe 'when passing in two profiles given an inherited profile that has more that one test per control block' do
let(:out) { inspec('exec ' + File.join(profile_path, 'dependencies', 'profile_d') + ' ' + simple_inheritance) }
it 'should print all the results' do
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ tmp-1.0: Create /tmp directory (1 failed)\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ should not be directory\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ undefined method `should_nota'"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✖ expected `File /tmp.directory?` to return false, got true\e[0m"
out.stdout.force_encoding(Encoding::UTF_8).must_include "✔ profiled-1: Create /tmp directory (profile d)"
end
end
end

View file

@ -2,10 +2,31 @@
# copyright: 2015, Chef Software, Inc.
# license: All rights reserved
title '/tmp profile'
title 'failures /tmp profile'
# you can also use plain tests
# control, first test passes, second fails
control "tmp-1.0" do # A unique ID for this control
impact 0.7 # The criticality, if this control fails.
title "Create /tmp directory" # A human-readable title
desc "An optional description..." # Describe why this is needed
tag data: "temp data" # A tag allows you to associate key information
tag "security" # to the test
ref "Document A-12", url: 'http://...' # Additional references
describe file('/tmp') do # The actual test
it { should be_directory }
it { should_not be_directory }
end
end
# anonymous describe block, first passes, second is syntax error
describe file('/tmp') do
it { should be_directory }
it { should_nota be_directory }
end
# anonymous describe block, first fails, second passes
describe file('/tmp') do
it { should_not be_directory }
it { should be_directory }
end

View file

@ -0,0 +1,8 @@
name: failures
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: All Rights Reserved
summary: An InSpec Compliance Profile
version: 0.1.0

View file

@ -0,0 +1,7 @@
# encoding: utf-8
include_controls 'failures'
describe file('/tmp') do
it { should be_directory }
end

View file

@ -0,0 +1,10 @@
name: simple inheritance
title: InSpec example simple inheritance
maintainer: Chef Software, Inc.
copyright: Chef Software, Inc.
copyright_email: support@chef.io
license: Apache 2 license
version: 1.0.0
depends:
- name: failures
path: ../failures