2015-10-17 21:28:54 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
require 'helper'
|
2015-10-26 03:04:18 +00:00
|
|
|
require 'inspec/profile_context'
|
2015-10-17 21:28:54 +00:00
|
|
|
|
2016-02-25 12:44:57 +00:00
|
|
|
class Module
|
|
|
|
include Minitest::Spec::DSL
|
|
|
|
end
|
|
|
|
|
|
|
|
module DescribeOneTest
|
|
|
|
it 'loads an empty describe.one' do
|
|
|
|
profile.load(format(context_format, 'describe.one'))
|
|
|
|
get_checks.must_equal([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'loads an empty describe.one block' do
|
|
|
|
profile.load(format(context_format, 'describe.one do; end'))
|
|
|
|
get_checks.must_equal([['describe.one', [], nil]])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'loads a simple describe.one block' do
|
|
|
|
profile.load(format(context_format, '
|
|
|
|
describe.one do
|
|
|
|
describe true do; it { should eq true }; end
|
|
|
|
end'))
|
|
|
|
c = get_checks[0]
|
|
|
|
c[0].must_equal 'describe.one'
|
|
|
|
childs = c[1]
|
|
|
|
childs.length.must_equal 1
|
|
|
|
childs[0][0].must_equal 'describe'
|
|
|
|
childs[0][1].must_equal [true]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'loads a complex describe.one block' do
|
|
|
|
profile.load(format(context_format, '
|
|
|
|
describe.one do
|
|
|
|
describe 0 do; it { should eq true }; end
|
|
|
|
describe 1 do; it { should eq true }; end
|
|
|
|
describe 2 do; it { should eq true }; end
|
|
|
|
end'))
|
|
|
|
c = get_checks[0]
|
|
|
|
c[0].must_equal 'describe.one'
|
|
|
|
childs = c[1]
|
|
|
|
childs.length.must_equal 3
|
|
|
|
childs.each_with_index do |ci, idx|
|
|
|
|
ci[0].must_equal 'describe'
|
|
|
|
ci[1].must_equal [idx]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
describe Inspec::ProfileContext do
|
2015-10-17 21:28:54 +00:00
|
|
|
let(:backend) { MockLoader.new.backend }
|
2016-03-08 12:40:34 +00:00
|
|
|
let(:profile) { Inspec::ProfileContext.new(nil, backend, {}) }
|
2015-10-17 21:28:54 +00:00
|
|
|
|
2016-02-25 12:44:57 +00:00
|
|
|
def get_rule
|
|
|
|
profile.rules.values[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_checks
|
|
|
|
get_rule.instance_variable_get(:@checks)
|
|
|
|
end
|
|
|
|
|
2015-10-17 21:28:54 +00:00
|
|
|
it 'must be able to load empty content' do
|
|
|
|
profile.load('', 'dummy', 1).must_be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'its default DSL' do
|
|
|
|
def load(call)
|
|
|
|
proc { profile.load(call) }
|
|
|
|
end
|
|
|
|
|
2016-02-25 12:44:57 +00:00
|
|
|
let(:context_format) { '%s' }
|
|
|
|
|
|
|
|
include DescribeOneTest
|
|
|
|
|
2015-10-17 21:28:54 +00:00
|
|
|
it 'must provide os resource' do
|
|
|
|
load('print os[:family]').must_output 'ubuntu'
|
|
|
|
end
|
|
|
|
|
2015-11-19 13:17:18 +00:00
|
|
|
it 'must provide file resource' do
|
2015-10-17 21:28:54 +00:00
|
|
|
load('print file("").type').must_output 'unknown'
|
|
|
|
end
|
|
|
|
|
2015-11-19 13:17:18 +00:00
|
|
|
it 'must provide command resource' do
|
2015-10-17 21:28:54 +00:00
|
|
|
load('print command("").stdout').must_output ''
|
|
|
|
end
|
|
|
|
|
2016-02-25 13:19:29 +00:00
|
|
|
it 'supports empty describe calls' do
|
|
|
|
load('describe').must_output ''
|
|
|
|
profile.rules.keys.length.must_equal 1
|
2016-03-24 17:32:12 +00:00
|
|
|
profile.rules.keys[0].must_match /^\(generated from \(eval\):1 [0-9a-f]+\)$/
|
2016-02-25 13:19:29 +00:00
|
|
|
profile.rules.values[0].must_be_kind_of Inspec::Rule
|
|
|
|
end
|
|
|
|
|
2015-10-17 23:09:47 +00:00
|
|
|
it 'provides the describe keyword in the global DSL' do
|
2015-10-17 21:28:54 +00:00
|
|
|
load('describe true do; it { should_eq true }; end')
|
|
|
|
.must_output ''
|
2015-11-19 13:22:42 +00:00
|
|
|
profile.rules.keys.length.must_equal 1
|
2016-03-24 17:32:12 +00:00
|
|
|
profile.rules.keys[0].must_match /^\(generated from \(eval\):1 [0-9a-f]+\)$/
|
2015-10-26 03:04:18 +00:00
|
|
|
profile.rules.values[0].must_be_kind_of Inspec::Rule
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
2015-10-17 23:09:47 +00:00
|
|
|
|
2015-11-19 13:22:42 +00:00
|
|
|
it 'loads multiple computed calls to describe correctly' do
|
|
|
|
load("%w{1 2 3}.each do\ndescribe true do; it { should_eq true }; end\nend")
|
|
|
|
.must_output ''
|
|
|
|
profile.rules.keys.length.must_equal 3
|
|
|
|
[0, 1, 2].each do |i|
|
2016-03-24 17:32:12 +00:00
|
|
|
profile.rules.keys[i].must_match /^\(generated from \(eval\):2 [0-9a-f]+\)$/
|
2015-11-19 13:22:42 +00:00
|
|
|
profile.rules.values[i].must_be_kind_of Inspec::Rule
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-17 23:09:47 +00:00
|
|
|
it 'does not provide the expect keyword in the global DLS' do
|
|
|
|
load('expect(true).to_eq true').must_raise NoMethodError
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'provides the rule keyword in the global DSL' do
|
|
|
|
profile.load('rule 1')
|
|
|
|
profile.rules.keys.must_equal [1]
|
2015-10-26 03:04:18 +00:00
|
|
|
profile.rules.values[0].must_be_kind_of Inspec::Rule
|
2015-10-17 23:09:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'rule DSL' do
|
|
|
|
let(:rule_id) { rand.to_s }
|
2016-02-25 12:44:57 +00:00
|
|
|
let(:context_format) { "rule #{rule_id.inspect} do\n%s\nend" }
|
|
|
|
|
|
|
|
def get_rule
|
|
|
|
profile.rules[rule_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
include DescribeOneTest
|
2015-10-17 23:09:47 +00:00
|
|
|
|
|
|
|
it 'doesnt add any checks if none are provided' do
|
|
|
|
profile.load("rule #{rule_id.inspect}")
|
|
|
|
rule = profile.rules[rule_id]
|
|
|
|
rule.instance_variable_get(:@checks).must_equal([])
|
|
|
|
end
|
|
|
|
|
2016-02-25 13:19:29 +00:00
|
|
|
describe 'supports empty describe blocks' do
|
|
|
|
it 'doesnt crash, but doesnt add anything either' do
|
|
|
|
profile.load(format(context_format, 'describe'))
|
|
|
|
profile.rules.keys.must_include(rule_id)
|
|
|
|
get_checks.must_equal([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-17 23:09:47 +00:00
|
|
|
describe 'adds a check via describe' do
|
|
|
|
let(:check) {
|
2016-02-25 13:19:29 +00:00
|
|
|
profile.load(format(context_format,
|
|
|
|
"describe os[:family] { it { must_equal 'ubuntu' } }"
|
|
|
|
))
|
|
|
|
get_checks[0]
|
2015-10-17 23:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it 'registers the check with describe' do
|
|
|
|
check[0].must_equal 'describe'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers the check with the describe argument' do
|
|
|
|
check[1].must_equal %w{ubuntu}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers the check with the provided proc' do
|
|
|
|
check[2].must_be_kind_of Proc
|
|
|
|
end
|
|
|
|
end
|
2015-10-18 17:07:43 +00:00
|
|
|
|
|
|
|
describe 'adds a check via expect' do
|
|
|
|
let(:check) {
|
2016-02-25 13:19:29 +00:00
|
|
|
profile.load(format(context_format,
|
|
|
|
"expect(os[:family]).to eq('ubuntu')"
|
|
|
|
))
|
|
|
|
get_checks[0]
|
2015-10-18 17:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it 'registers the check with describe' do
|
|
|
|
check[0].must_equal 'expect'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers the check with the describe argument' do
|
|
|
|
check[1].must_equal %w{ubuntu}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers the check with the provided proc' do
|
2016-02-25 12:44:57 +00:00
|
|
|
check[2].must_be_kind_of Inspec::Expect
|
2015-10-18 17:07:43 +00:00
|
|
|
end
|
|
|
|
end
|
2015-10-19 07:49:40 +00:00
|
|
|
|
|
|
|
describe 'adds a check via describe + expect' do
|
|
|
|
let(:check) {
|
2016-02-25 13:19:29 +00:00
|
|
|
profile.load(format(context_format,
|
|
|
|
"describe 'the actual test' do
|
|
|
|
expect(os[:family]).to eq('ubuntu')
|
|
|
|
end"
|
|
|
|
))
|
|
|
|
get_checks[0]
|
2015-10-19 07:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it 'registers the check with describe' do
|
|
|
|
check[0].must_equal 'describe'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers the check with the describe argument' do
|
|
|
|
check[1].must_equal ['the actual test']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers the check with the provided proc' do
|
|
|
|
check[2].must_be_kind_of Proc
|
|
|
|
end
|
|
|
|
end
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
|
|
|
end
|