Tests to exercise only_if functionality

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2020-05-19 18:49:03 -04:00
parent 52392600d1
commit f39cf8c904
6 changed files with 135 additions and 0 deletions

View file

@ -0,0 +1,56 @@
control "control-start" do
desc "This control should not get skipped"
describe "a string" do
it { should cmp "a string" }
end
end
control "control-skip-no-message" do
desc "This control should get skipped"
only_if { false }
describe "a string" do
it { should cmp "a string" }
end
end
control "control-skip-with-message" do
desc "This control should get skipped"
only_if("here is a message") { false }
describe "a string" do
it { should cmp "a string" }
end
end
control "control-skip-test-body" do
desc "This control should demo that the test body does not get evaluated"
only_if { false }
describe "infinity" do
it { should cmp 1/0 }
end
end
control "control-skip-test-outer-error" do
desc "This control should demo that following test resources do not get evaluated"
only_if { false }
describe 1/0 do # does not error!
it { should cmp 1/0 }
end
end
control "control-skip-test-outer-error-test-first" do
desc "This control should demo that preceding test resources DO get evaluated"
describe 1/0 do # does error!
it { should cmp 1/0 }
end
only_if { false }
end
control "multi-skip" do
desc "This control should get skipped"
only_if("here is the intended message") { false }
only_if("here is a different message") { false }
describe "a string" do
it { should cmp "a string" }
end
end

View file

@ -0,0 +1,6 @@
name: skip-control
license: Apache-2.0
summary: A profile that skips controls using only_if
version: 0.1.0
supports:
platform: os

View file

@ -0,0 +1,6 @@
control "control-start-01" do
desc "This control should not get skipped"
describe "a string" do
it { should cmp "a string" }
end
end

View file

@ -0,0 +1,22 @@
control "control-start-02" do
desc "This control should get skipped"
describe "a string" do
it { should cmp "a string" }
end
end
only_if("I wonder what this will do") { false }
control "control-start-03" do
desc "This control should get skipped"
describe "a string" do
it { should cmp "a string" }
end
end
control "control-start-04" do
desc "This control should get skipped"
describe "a string" do
it { should cmp "a string" }
end
end

View file

@ -0,0 +1,6 @@
name: skip-file
license: Apache-2.0
summary: A profile that tries to skip an entire file of controls using only_if
version: 0.1.0
supports:
platform: os

View file

@ -943,4 +943,43 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
end
end
describe "when evaluating profiles with only_if" do
let(:run_result) { run_inspec_process("exec #{profile}", json: true) }
describe "when running a profile with a variety of skips" do
let(:profile) { "#{profile_path}/only_if/skip-control" }
it "should correctly skip in individual controls" do
run_result
_(@json.dig("profiles", 0, "controls", 0, "results", 0, "status")).must_equal "passed"
_(@json.dig("profiles", 0, "controls", 1, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 1, "results", 0, "skip_message")).must_equal "Skipped control due to only_if condition."
_(@json.dig("profiles", 0, "controls", 2, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 2, "results", 0, "skip_message")).must_equal "Skipped control due to only_if condition: here is a message"
# 1/0 in test body
_(@json.dig("profiles", 0, "controls", 3, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 3, "results", 0, "skip_message")).must_equal "Skipped control due to only_if condition."
# 1/0 in resource declaration but it follows the only_if
_(@json.dig("profiles", 0, "controls", 4, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 4, "results", 0, "skip_message")).must_equal "Skipped control due to only_if condition."
# 1/0 in resource declaration but it precedes the only_if
_(@json.dig("profiles", 0, "controls", 5, "results", 0, "status")).must_equal "failed"
_(@json.dig("profiles", 0, "controls", 5, "results", 0, "exception")).must_equal "RuntimeError"
# multiple only_ifs
_(@json.dig("profiles", 0, "controls", 6, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 6, "results", 0, "skip_message")).must_equal "Skipped control due to only_if condition: here is a different message"
end
end
describe "when running a profile with an only_if at the top-level" do
let(:profile) { "#{profile_path}/only_if/skip-file" }
it "should correctly skip entire files" do
run_result
# first control is in a separate file
_(@json.dig("profiles", 0, "controls", 0, "results", 0, "status")).must_equal "passed"
# Latter three are in the same file
_(@json.dig("profiles", 0, "controls", 1, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 2, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 3, "results", 0, "status")).must_equal "skipped"
end
end
end
end