2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/control_eval_context"
|
2019-06-07 23:33:56 +00:00
|
|
|
require "inspec/profile_context"
|
2016-09-14 13:57:26 +00:00
|
|
|
|
|
|
|
describe Inspec::ControlEvalContext do
|
|
|
|
module FakeDSL
|
|
|
|
def foobar
|
|
|
|
"wombat"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:control_content) do
|
|
|
|
<<~EOF
|
|
|
|
control 'foo' do
|
|
|
|
describe foobar do
|
|
|
|
end
|
|
|
|
end
|
2016-09-14 13:57:26 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
control 'bar' do
|
|
|
|
describe "wombat" do
|
|
|
|
it { should_equal foobar }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOF
|
2016-09-14 13:57:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:resource_dsl) { FakeDSL }
|
2019-07-09 00:20:30 +00:00
|
|
|
let(:backend) { mock }
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:profile_context) { Inspec::ProfileContext.new("test-profile", backend, {}) }
|
2016-09-14 13:57:26 +00:00
|
|
|
let(:eval_context) do
|
2017-12-05 13:13:41 +00:00
|
|
|
# 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
|
2019-12-11 02:07:42 +00:00
|
|
|
Inspec::ControlEvalContext.new(profile_context, resource_dsl, backend, {}, mock, mock, false)
|
2016-09-14 13:57:26 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides rules with access to the given DSL" do
|
|
|
|
profile_context.stubs(:current_load).returns({ file: "<test content>" })
|
2016-09-14 13:57:26 +00:00
|
|
|
eval_context.instance_eval(control_content)
|
|
|
|
profile_context.all_rules.each do |rule|
|
2016-09-15 07:54:15 +00:00
|
|
|
# Turn each rule into an example group and run it, none of the
|
|
|
|
# example content should raise an exception
|
2016-09-14 13:57:26 +00:00
|
|
|
Inspec::Rule.prepare_checks(rule).each do |m, a, b|
|
2016-09-15 07:54:15 +00:00
|
|
|
# if we require this at the top level, none of the other tests
|
|
|
|
# in this file will run. itsfine.jpg
|
2019-06-11 22:24:35 +00:00
|
|
|
require "rspec/core"
|
2016-09-14 13:57:26 +00:00
|
|
|
RSpec::Core::ExampleGroup.describe(*a, &b).run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-15 07:54:15 +00:00
|
|
|
|
2016-12-17 04:42:07 +00:00
|
|
|
describe "#resource_class" do
|
2016-09-15 07:54:15 +00:00
|
|
|
let(:resource_dsl) { Inspec::Resource.create_dsl(profile_context) }
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:inner_context) { Inspec::ProfileContext.new("inner-context", backend, {}) }
|
2019-07-09 00:20:30 +00:00
|
|
|
let(:newfoo) { mock }
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:control_content) do
|
|
|
|
<<~EOF
|
|
|
|
resource_class('profile_a', 'foobar')
|
|
|
|
EOF
|
2016-09-15 07:54:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if the requested profile can't be found" do
|
2019-06-11 22:24:35 +00:00
|
|
|
assert_raises(Inspec::ProfileNotFound) do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(eval_context.instance_eval(control_content)).must_raise
|
2019-06-11 22:24:35 +00:00
|
|
|
end
|
2016-09-15 07:54:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the resource from a subcontext" do
|
2019-06-11 22:24:35 +00:00
|
|
|
profile_context.expects(:subcontext_by_name).at_most_once.with("profile_a").returns(inner_context)
|
|
|
|
inner_context.expects(:resource_registry).returns({ "foobar" => newfoo })
|
2019-09-30 22:31:55 +00:00
|
|
|
_(eval_context.instance_eval(control_content)).must_equal newfoo
|
2016-09-15 07:54:15 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-14 13:57:26 +00:00
|
|
|
end
|