2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/profile_context"
|
|
|
|
require "inspec/resources/file"
|
|
|
|
require "inspec/resources/command"
|
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
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads an empty describe.one" do
|
|
|
|
profile.load(format(context_format, "describe.one"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks).must_equal([])
|
2016-02-25 12:44:57 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads an empty describe.one block" do
|
|
|
|
profile.load(format(context_format, "describe.one do; end"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks).must_equal([["describe.one", [], nil]])
|
2016-02-25 12:44:57 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a simple describe.one block" do
|
2016-02-25 12:44:57 +00:00
|
|
|
profile.load(format(context_format, '
|
|
|
|
describe.one do
|
|
|
|
describe true do; it { should eq true }; end
|
|
|
|
end'))
|
|
|
|
c = get_checks[0]
|
2019-09-30 22:31:55 +00:00
|
|
|
_(c[0]).must_equal "describe.one"
|
2016-02-25 12:44:57 +00:00
|
|
|
childs = c[1]
|
2019-09-30 22:31:55 +00:00
|
|
|
_(childs.length).must_equal 1
|
|
|
|
_(childs[0][0]).must_equal "describe"
|
|
|
|
_(childs[0][1]).must_equal [true]
|
2016-02-25 12:44:57 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads a complex describe.one block" do
|
2016-02-25 12:44:57 +00:00
|
|
|
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]
|
2019-09-30 22:31:55 +00:00
|
|
|
_(c[0]).must_equal "describe.one"
|
2016-02-25 12:44:57 +00:00
|
|
|
childs = c[1]
|
2019-09-30 22:31:55 +00:00
|
|
|
_(childs.length).must_equal 3
|
2016-02-25 12:44:57 +00:00
|
|
|
childs.each_with_index do |ci, idx|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(ci[0]).must_equal "describe"
|
|
|
|
_(ci[1]).must_equal [idx]
|
2016-02-25 12:44:57 +00:00
|
|
|
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
|
|
|
|
2017-10-17 12:47:30 +00:00
|
|
|
def get_checks(rule_index = 0)
|
|
|
|
rule = profile.rules.values[rule_index]
|
|
|
|
Inspec::Rule.prepare_checks(rule)
|
2016-02-25 12:44:57 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "must be able to load empty content" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.load("", "dummy", 1)).must_be_nil
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "its default DSL" do
|
2015-10-17 21:28:54 +00:00
|
|
|
def load(call)
|
2019-09-30 22:31:55 +00:00
|
|
|
_ { profile.load(call) }
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:context_format) { "%s" }
|
2016-02-25 12:44:57 +00:00
|
|
|
|
|
|
|
include DescribeOneTest
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "must provide os resource" do
|
|
|
|
load("print os[:family]").must_output "debian"
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "must provide file resource" do
|
|
|
|
load('print file("/etc/passwd").type').must_output "file"
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "must provide command resource" do
|
|
|
|
load('print command("").stdout').must_output ""
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports empty describe calls" do
|
|
|
|
load("describe").must_output ""
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.keys.length).must_equal 1
|
|
|
|
_(profile.rules.keys[0]).must_match(/^\(generated from \(eval\):1 [0-9a-f]+\)$/)
|
|
|
|
_(profile.rules.values[0]).must_be_kind_of Inspec::Rule
|
2016-02-25 13:19:29 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides the describe keyword in the global DSL" do
|
2019-09-30 22:31:55 +00:00
|
|
|
load("describe true do; it { should_eq true }; end").must_output ""
|
|
|
|
_(profile.rules.keys.length).must_equal 1
|
|
|
|
_(profile.rules.keys[0]).must_match(/^\(generated from \(eval\):1 [0-9a-f]+\)$/)
|
|
|
|
_(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
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "loads multiple computed calls to describe correctly" do
|
2015-11-19 13:22:42 +00:00
|
|
|
load("%w{1 2 3}.each do\ndescribe true do; it { should_eq true }; end\nend")
|
2019-06-11 22:24:35 +00:00
|
|
|
.must_output ""
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.keys.length).must_equal 3
|
2015-11-19 13:22:42 +00:00
|
|
|
[0, 1, 2].each do |i|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.keys[i]).must_match(/^\(generated from \(eval\):2 [0-9a-f]+\)$/)
|
|
|
|
_(profile.rules.values[i]).must_be_kind_of Inspec::Rule
|
2015-11-19 13:22:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does not provide the expect keyword in the global DSL" do
|
|
|
|
load("expect(true).to_eq true").must_raise NoMethodError
|
2015-10-17 23:09:47 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "global only_if" do
|
2016-04-05 13:07:49 +00:00
|
|
|
let(:if_true) { "only_if { true }\n" }
|
|
|
|
let(:if_false) { "only_if { false }\n" }
|
|
|
|
let(:describe) { "describe nil do its(:to_i) { should eq rand } end\n" }
|
2017-10-17 12:47:30 +00:00
|
|
|
let(:control) { "control 1 do\n#{describe}\nend\n" }
|
|
|
|
let(:control_2) { "control 2 do\n#{describe}\nend\n" }
|
2016-04-05 13:07:49 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides the keyword" do
|
2016-04-05 13:07:49 +00:00
|
|
|
profile.load(if_true)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules).must_equal({})
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt affect controls when positive" do
|
|
|
|
profile.load(if_true + "control 1")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.values[0]).must_be_kind_of Inspec::Rule
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt remove controls when negative" do
|
|
|
|
profile.load(if_false + "control 1")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.values[0]).must_be_kind_of Inspec::Rule
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "alters controls when positive" do
|
2016-04-05 13:07:49 +00:00
|
|
|
profile.load(if_false + control)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "alters non-controls when positive" do
|
2016-04-05 13:07:49 +00:00
|
|
|
profile.load(if_false + describe)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt alter controls when negative" do
|
2016-04-05 13:07:49 +00:00
|
|
|
profile.load(if_true + control)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0]).must_be_nil
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt alter non-controls when negative" do
|
2016-04-05 13:07:49 +00:00
|
|
|
profile.load(if_true + describe)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0]).must_be_nil
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
2016-04-05 15:28:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt overwrite falsy only_ifs" do
|
2016-04-05 15:28:00 +00:00
|
|
|
profile.load(if_false + if_true + control)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2016-04-05 15:28:00 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt overwrite falsy only_ifs" do
|
2016-04-05 15:28:00 +00:00
|
|
|
profile.load(if_true + if_false + control)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2017-10-17 12:47:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "allows specifying a message with true only_if" do
|
2018-08-09 19:12:47 +00:00
|
|
|
profile.load("only_if('this is a only_if skipped message') { false }\n" + control)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped" \
|
2019-06-11 22:24:35 +00:00
|
|
|
" control due to only_if condition: this is a only_if skipped message"
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2018-08-07 17:10:45 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt extend into other control files" do
|
2017-10-17 12:47:30 +00:00
|
|
|
fake_control_file = if_false + control
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.load_control_file(fake_control_file, "(eval)", nil)
|
|
|
|
profile.load_control_file(control_2, "(eval)", nil)
|
2017-10-17 12:47:30 +00:00
|
|
|
first_file_check = get_checks(0)
|
|
|
|
second_file_check = get_checks(1)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(first_file_check[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(second_file_check[0][1][0]).must_be_nil
|
2017-10-17 12:47:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "applies to the controls above it when at the bottom of the file" do
|
2017-10-17 12:47:30 +00:00
|
|
|
fake_control_file = control + if_false
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.load_control_file(fake_control_file, "(eval)", 1)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
2017-10-17 12:47:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "applies to the controls below it when at the top of the file" do
|
2017-10-17 12:47:30 +00:00
|
|
|
fake_control_file = if_false + control
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.load_control_file(fake_control_file, "(eval)", 1)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
2017-10-17 12:47:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "applies to the controls above and below it when at the middle of the file" do
|
2017-10-17 12:47:30 +00:00
|
|
|
fake_control_file = control + if_false + control_2
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.load_control_file(fake_control_file, "(eval)", 1)
|
2017-10-17 12:47:30 +00:00
|
|
|
check_top = get_checks(0)
|
|
|
|
check_bottom = get_checks(1)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check_top[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(check_bottom[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
2017-10-17 12:47:30 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "applies to the describe blocks above and below it when at the middle of the file" do
|
2017-10-17 12:47:30 +00:00
|
|
|
fake_control_file = describe + if_false + describe
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.load_control_file(fake_control_file, "(eval)", 1)
|
2017-10-17 12:47:30 +00:00
|
|
|
check_top = get_checks(0)
|
|
|
|
check_bottom = get_checks(1)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check_top[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(check_bottom[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
2016-04-05 15:28:00 +00:00
|
|
|
end
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides the control keyword in the global DSL" do
|
|
|
|
profile.load("control 1")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.keys).must_equal ["1"]
|
|
|
|
_(profile.rules.values[0]).must_be_kind_of Inspec::Rule
|
2016-04-05 13:07:49 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides the rule keyword in the global DSL (legacy mode)" do
|
|
|
|
profile.load("rule 1")
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.keys).must_equal ["1"]
|
|
|
|
_(profile.rules.values[0]).must_be_kind_of Inspec::Rule
|
2015-10-17 23:09:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "rule DSL" do
|
2015-10-17 23:09:47 +00:00
|
|
|
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
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt add any checks if none are provided" do
|
2015-10-17 23:09:47 +00:00
|
|
|
profile.load("rule #{rule_id.inspect}")
|
|
|
|
rule = profile.rules[rule_id]
|
2019-09-30 22:31:55 +00:00
|
|
|
_(::Inspec::Rule.prepare_checks(rule)).must_equal([])
|
2015-10-17 23:09:47 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "supports empty describe blocks" do
|
|
|
|
it "doesnt crash, but doesnt add anything either" do
|
|
|
|
profile.load(format(context_format, "describe"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.rules.keys).must_include(rule_id)
|
|
|
|
_(get_checks).must_equal([])
|
2016-02-25 13:19:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "adds a check via describe" do
|
|
|
|
let(:check) do
|
2016-02-25 13:19:29 +00:00
|
|
|
profile.load(format(context_format,
|
2019-07-09 00:20:30 +00:00
|
|
|
"describe(os[:family]) { it { must_equal 'debian' } }"))
|
2016-02-25 13:19:29 +00:00
|
|
|
get_checks[0]
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|
2015-10-17 23:09:47 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with describe" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[0]).must_equal "describe"
|
2015-10-17 23:09:47 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with the describe argument" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[1]).must_equal %w{debian}
|
2015-10-17 23:09:47 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with the provided proc" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[2]).must_be_kind_of Proc
|
2015-10-17 23:09:47 +00:00
|
|
|
end
|
|
|
|
end
|
2015-10-18 17:07:43 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "adds a check via expect" do
|
|
|
|
let(:check) do
|
2016-02-25 13:19:29 +00:00
|
|
|
profile.load(format(context_format,
|
2019-07-09 00:20:30 +00:00
|
|
|
"expect(os[:family]).to eq('debian')"))
|
2016-02-25 13:19:29 +00:00
|
|
|
get_checks[0]
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|
2015-10-18 17:07:43 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with describe" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[0]).must_equal "expect"
|
2015-10-18 17:07:43 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with the describe argument" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[1]).must_equal %w{debian}
|
2015-10-18 17:07:43 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with the provided proc" do
|
2019-09-30 22:31:55 +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
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "adds a check via describe + expect" do
|
|
|
|
let(:check) do
|
2016-02-25 13:19:29 +00:00
|
|
|
profile.load(format(context_format,
|
|
|
|
"describe 'the actual test' do
|
2016-08-03 17:18:24 +00:00
|
|
|
expect(os[:family]).to eq('debian')
|
2019-07-09 00:20:30 +00:00
|
|
|
end"))
|
2016-02-25 13:19:29 +00:00
|
|
|
get_checks[0]
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|
2015-10-19 07:49:40 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with describe" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[0]).must_equal "describe"
|
2015-10-19 07:49:40 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with the describe argument" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[1]).must_equal ["the actual test"]
|
2015-10-19 07:49:40 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "registers the check with the provided proc" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(check[2]).must_be_kind_of Proc
|
2015-10-19 07:49:40 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-05 15:01:08 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "with only_if" do
|
|
|
|
it "provides the only_if keyword" do
|
|
|
|
profile.load(format(context_format, "only_if"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks).must_equal([])
|
2016-04-05 15:01:08 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "skips with only_if == false" do
|
|
|
|
profile.load(format(context_format, "only_if { false }"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2016-04-05 15:01:08 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "does nothing with only_if == false" do
|
|
|
|
profile.load(format(context_format, "only_if { true }"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 0
|
2016-04-05 15:01:08 +00:00
|
|
|
end
|
2016-04-05 15:28:00 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt overwrite falsy only_ifs" do
|
2016-04-05 15:28:00 +00:00
|
|
|
profile.load(format(context_format, "only_if { false }\nonly_if { true }"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2016-04-05 15:28:00 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "doesnt overwrite falsy only_ifs" do
|
2016-04-05 15:28:00 +00:00
|
|
|
profile.load(format(context_format, "only_if { true }\nonly_if { false }"))
|
2019-09-30 22:31:55 +00:00
|
|
|
_(get_checks.length).must_equal 1
|
|
|
|
_(get_checks[0][1][0].resource_skipped?).must_equal true
|
|
|
|
_(get_checks[0][1][0].resource_exception_message).must_equal "Skipped control due to only_if condition."
|
|
|
|
_(get_checks[0][1][0].resource_failed?).must_equal false
|
2016-04-05 15:28:00 +00:00
|
|
|
end
|
2016-04-05 15:01:08 +00:00
|
|
|
end
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|
2016-06-03 19:36:16 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "library loading" do
|
|
|
|
it "supports simple ruby require statements" do
|
2016-06-03 19:36:16 +00:00
|
|
|
# Please note: we do discourage the use of Gems in inspec resources at
|
|
|
|
# this time. Resources should be well packaged whenever possible.
|
2019-09-30 22:31:55 +00:00
|
|
|
_ { profile.load("Net::POP3") }.must_raise NameError
|
2019-06-11 22:24:35 +00:00
|
|
|
profile.load_libraries([['require "net/pop"', "libraries/a.rb"]])
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile.load("Net::POP3").to_s).must_equal "Net::POP3"
|
2016-06-03 19:36:16 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports creating a simple library file (no require)" do
|
2018-01-02 20:41:01 +00:00
|
|
|
# this test will throw an exception if chaining doesn't work
|
|
|
|
profile.load_libraries([
|
2019-07-09 00:20:30 +00:00
|
|
|
["module A; end", "libraries/a.rb"],
|
2018-01-02 20:41:01 +00:00
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports loading across the library" do
|
2018-01-02 20:41:01 +00:00
|
|
|
# this test will throw an exception if chaining doesn't work
|
|
|
|
profile.load_libraries([
|
2019-06-11 22:24:35 +00:00
|
|
|
["require 'a'\nA", "libraries/b.rb"],
|
2019-07-09 00:20:30 +00:00
|
|
|
["module A; end", "libraries/a.rb"],
|
2018-01-02 20:41:01 +00:00
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports chain loading across the library" do
|
2018-01-02 20:41:01 +00:00
|
|
|
# this test will throw an exception if chaining doesn't work
|
2016-06-03 19:36:16 +00:00
|
|
|
profile.load_libraries([
|
2019-06-11 22:24:35 +00:00
|
|
|
["require 'b'\nA", "libraries/c.rb"],
|
|
|
|
["require 'a'\nA", "libraries/b.rb"],
|
2019-07-09 00:20:30 +00:00
|
|
|
["module A; end", "libraries/a.rb"],
|
2016-06-03 19:36:16 +00:00
|
|
|
])
|
2018-01-02 20:41:01 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "supports loading a regular ruby gem" do
|
2018-01-02 20:41:01 +00:00
|
|
|
profile.load_libraries([
|
2019-07-09 00:20:30 +00:00
|
|
|
["require 'erb'\nERB", "libraries/a.rb"],
|
2018-01-02 20:41:01 +00:00
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
# rubocop:disable Style/BlockDelimiters
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "fails if a required gem or lib doesnt exist" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_ {
|
2018-01-02 20:41:01 +00:00
|
|
|
profile.load_libraries([
|
2019-07-09 00:20:30 +00:00
|
|
|
["require 'erbluuuuub'", "libraries/a.rb"],
|
2018-01-02 20:41:01 +00:00
|
|
|
])
|
|
|
|
}.must_raise LoadError
|
2016-06-03 19:36:16 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "fails loading if reference error occur" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_ {
|
2016-06-03 19:36:16 +00:00
|
|
|
profile.load_libraries([
|
2019-06-11 22:24:35 +00:00
|
|
|
["require 'a'\nB", "libraries/b.rb"],
|
2019-07-09 00:20:30 +00:00
|
|
|
["module A; end", "libraries/a.rb"],
|
2016-06-03 19:36:16 +00:00
|
|
|
])
|
|
|
|
}.must_raise NameError
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "fails loading if a reference dependency isnt found" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_ {
|
2016-06-03 19:36:16 +00:00
|
|
|
profile.load_libraries([
|
2019-06-11 22:24:35 +00:00
|
|
|
["require 'a'\nA", "libraries/b.rb"],
|
2016-06-03 19:36:16 +00:00
|
|
|
])
|
|
|
|
}.must_raise LoadError
|
|
|
|
end
|
|
|
|
end
|
2015-10-17 21:28:54 +00:00
|
|
|
end
|