Document and test to verify that it skips resources in describe blocks

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2020-05-22 12:09:27 -04:00
parent 2b10aa8b47
commit 925363766e
3 changed files with 22 additions and 20 deletions

View file

@ -241,26 +241,17 @@ Mixing this with other conditionals (like checking existence of the files etc.)
Some notes about `only_if`:
* `only_if` applies to the entire `control`. If the results of the `only_if` block evaluate to false, the contents of the describe blocks will not be run. However, the describe block resources (`command('redis-cli SET test_inspec "HELLO"')` in the above example) that preceded the only_if WILL run. For this reason, `only_if` should usually come first in the control, prior to any `describe` blocks.
* `only_if` applies to the entire `control`. If the results of the `only_if` block evaluate to false, any CHef InSpec resources mentioned as part of a `describe` block will not be run. Additionally, the contents of the describe blocks will not be run. However, bare Ruby expressions and bare Chef InSpec resources (not assocated with a describe block) preceding the only_if statement will run.
To illustrate:
```ruby
control "don't do this" do
describe command("something-dangerous") do
# ...
end
# command("something-dangerous") already ran!!!
only_if { its_safe }
end
```
Instead, do this:
```ruby
control "do this instead" do
only_if { its_safe }
# command("something-dangerous") won't run unless it is safe
describe command("something-dangerous") do
# ...
control "whatruns" do
command("do_something") # This will ALWAYS run
describe command("do_another_thing") do # This will not run
command("do_yet_another_thing") # This will not run
end
only_if { false }
command("do_something_else") # This will not run
end
```

View file

@ -38,13 +38,21 @@ control "control-skip-test-outer-error" do
end
control "control-skip-test-outer-error-test-first" do
desc "This control should demo that preceding test resources DO get evaluated"
desc "This control should demo that preceding ruby expressions DO get evaluated"
describe 1/0 do # does error!
it { should cmp 1/0 }
end
only_if { false }
end
control "control-skip-test-outer-resource-test-first" do
desc "This control should demo that preceding test resources DO NOT get evaluated"
describe command("echo toldyaso") do # does exec
its("stdout") { should include "toldya" }
end
only_if { false }
end
control "multi-skip" do
desc "This control should get skipped"
only_if("here is the intended message") { false }

View file

@ -964,9 +964,12 @@ Test Summary: 2 successful, 0 failures, 0 skipped\n"
# 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
# resource declaration but it precedes the only_if
_(@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"
_(@json.dig("profiles", 0, "controls", 6, "results", 0, "skip_message")).must_equal "Skipped control due to only_if condition."
# multiple only_ifs
_(@json.dig("profiles", 0, "controls", 7, "results", 0, "status")).must_equal "skipped"
_(@json.dig("profiles", 0, "controls", 7, "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