mirror of
https://github.com/inspec/inspec
synced 2024-11-10 15:14:23 +00:00
Add only_if to Inspec objects (#3577)
* Add only_if attr to Inspec::test * Add only_if attr to Inspec::Control Signed-off-by: James Stocks <jstocks@chef.io>
This commit is contained in:
parent
d33d189d93
commit
5da1180780
3 changed files with 49 additions and 4 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
module Inspec
|
||||
class Control
|
||||
attr_accessor :id, :title, :descriptions, :impact, :tests, :tags, :refs
|
||||
attr_accessor :id, :title, :descriptions, :impact, :tests, :tags, :refs, :only_if
|
||||
def initialize
|
||||
@tests = []
|
||||
@tags = []
|
||||
|
@ -43,6 +43,7 @@ module Inspec
|
|||
res.push " impact #{impact}" unless impact.nil?
|
||||
tags.each { |t| res.push(indent(t.to_ruby, 2)) }
|
||||
refs.each { |t| res.push(" ref #{print_ref(t)}") }
|
||||
res.push " only_if { #{only_if} }" if only_if
|
||||
tests.each { |t| res.push(indent(t.to_ruby, 2)) }
|
||||
res.push 'end'
|
||||
res.join("\n")
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
module Inspec
|
||||
class Test
|
||||
attr_accessor :qualifier, :matcher, :expectation, :skip, :negated, :variables
|
||||
attr_accessor :qualifier, :matcher, :expectation, :skip, :negated, :variables, :only_if
|
||||
include RubyHelper
|
||||
|
||||
def initialize
|
||||
|
@ -61,6 +61,7 @@ module Inspec
|
|||
end
|
||||
|
||||
def rb_describe
|
||||
only_if_clause = "only_if { #{only_if} }\n" if only_if
|
||||
vars = variables.map(&:to_ruby).join("\n")
|
||||
vars += "\n" unless vars.empty?
|
||||
res, xtra = describe_chain
|
||||
|
@ -74,8 +75,8 @@ module Inspec
|
|||
elsif xpect != ''
|
||||
' ' + expectation.inspect
|
||||
end
|
||||
format("%sdescribe %s do\n %s { should%s %s%s }\nend",
|
||||
vars, res, itsy, naughty, matcher, xpect)
|
||||
format("%s%sdescribe %s do\n %s { should%s %s%s }\nend",
|
||||
only_if_clause, vars, res, itsy, naughty, matcher, xpect)
|
||||
end
|
||||
|
||||
def rb_skip
|
||||
|
|
|
@ -156,6 +156,19 @@ end
|
|||
describe service("avahi-daemon").info[\'properties\'][\'UnitFileState\'] do
|
||||
it { should eq "enabled" }
|
||||
end
|
||||
'.strip
|
||||
end
|
||||
|
||||
it 'constructs a simple resource + only_if' do
|
||||
obj.qualifier = [['resource'], ['version']]
|
||||
obj.matcher = 'cmp >='
|
||||
obj.expectation = '2.4.2'
|
||||
obj.only_if = "package('ntp').installed?"
|
||||
obj.to_ruby.must_equal '
|
||||
only_if { package(\'ntp\').installed? }
|
||||
describe resource do
|
||||
its("version") { should cmp >= "2.4.2" }
|
||||
end
|
||||
'.strip
|
||||
end
|
||||
end
|
||||
|
@ -297,6 +310,36 @@ end
|
|||
'.strip
|
||||
end
|
||||
|
||||
it 'constructs a control with only_if' do
|
||||
control = Inspec::Control.new
|
||||
control.add_test(obj1)
|
||||
control.only_if = "package('ntp').installed?"
|
||||
control.id = 'sample.control.id'
|
||||
control.title = 'Sample Control Important Title'
|
||||
control.descriptions = {
|
||||
default: 'The most critical control the world has ever seen',
|
||||
rationale: 'It is needed to save the planet',
|
||||
'more info': 'Insert clever joke here',
|
||||
}
|
||||
control.refs = ['simple ref', {ref: 'title', url: 'my url'}]
|
||||
control.impact = 1.0
|
||||
control.to_ruby.must_equal '
|
||||
control "sample.control.id" do
|
||||
title "Sample Control Important Title"
|
||||
desc "The most critical control the world has ever seen"
|
||||
desc "rationale", "It is needed to save the planet"
|
||||
desc "more info", "Insert clever joke here"
|
||||
impact 1.0
|
||||
ref "simple ref"
|
||||
ref ({:ref=>"title", :url=>"my url"})
|
||||
only_if { package(\'ntp\').installed? }
|
||||
describe command("ls /etc") do
|
||||
its("exit_status") { should eq 0 }
|
||||
end
|
||||
end
|
||||
'.strip
|
||||
end
|
||||
|
||||
it 'constructs a multiline desc in a control with indentation' do
|
||||
control = Inspec::Control.new
|
||||
control.descriptions[:default] = "Multiline\n control"
|
||||
|
|
Loading…
Reference in a new issue