inspec/test/unit/profiles/control_eval_context_test.rb
Jerry Aldrich III 49d36de0f3 Allow inspec check to ignore only_if (#2250)
* Allow `inspec check` to ignore `only_if`

When using `inspec check` a mock Train backend is created. This means
that the following would raise an error because `os.name` is `nil`

```
only_if { os.name.include?('anything') }
```

Since `inspec check` isn't concerned with the evaluation of `only_if`
this skips those checks if the block given raises an error.

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>

* Remove unnecessary `e` in rescue

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>

* Modify implementation to use `check_mode`

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>

* Move `check_mode` concept to the Profile scope

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>

* Fix lint after rubocop upgrade

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>

* Add comment for mocked ControlEvalContext options

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
2017-12-05 14:13:41 +01:00

79 lines
2.3 KiB
Ruby

# encoding: utf-8
# author: Steven Danna
require 'helper'
require 'inspec/control_eval_context'
describe Inspec::ControlEvalContext do
module FakeDSL
def foobar
"wombat"
end
end
let(:control_content) { <<EOF
control 'foo' do
describe foobar do
end
end
control 'bar' do
describe "wombat" do
it { should_equal foobar }
end
end
EOF
}
let(:resource_dsl) { FakeDSL }
let(:backend) { mock() }
let(:profile_context) { Inspec::ProfileContext.new('test-profile', backend, {}) }
let(:eval_context) do
c = Inspec::ControlEvalContext.create(profile_context, resource_dsl)
# Options that are mocked below are:
# backend, conf, dependencies, require_loader, and skip_only_if_eval
# See: `lib/inspec/control_eval_context.rb` for more details
c.new(backend, {}, mock(), mock(), false)
end
it 'accepts a context and a resource_dsl' do
Inspec::ControlEvalContext.create(profile_context, resource_dsl)
end
it 'provides rules with access to the given DSL' do
profile_context.stubs(:current_load).returns({file: "<test content>"})
eval_context.instance_eval(control_content)
profile_context.all_rules.each do |rule|
# Turn each rule into an example group and run it, none of the
# example content should raise an exception
Inspec::Rule.prepare_checks(rule).each do |m, a, b|
# if we require this at the top level, none of the other tests
# in this file will run. itsfine.jpg
require 'rspec/core'
RSpec::Core::ExampleGroup.describe(*a, &b).run
end
end
end
describe "#resource_class" do
let(:resource_dsl) { Inspec::Resource.create_dsl(profile_context) }
let(:inner_context) { Inspec::ProfileContext.new('inner-context', backend, {}) }
let(:newfoo) { mock() }
let(:control_content) do <<EOF
resource_class('profile_a', 'foobar')
EOF
end
it "fails if the requested profile can't be found" do
assert_raises(Inspec::ProfileNotFound) {
eval_context.instance_eval(control_content).must_raise
}
end
it "returns the resource from a subcontext" do
profile_context.expects(:subcontext_by_name).at_most_once.with('profile_a').returns(inner_context)
inner_context.expects(:resource_registry).returns({ 'foobar' => newfoo })
eval_context.instance_eval(control_content).must_equal newfoo
end
end
end