Added functional test for FilterTable::ExceptionCatcher

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
Vasu1105 2021-03-24 14:15:06 +05:30
parent 45e9288e90
commit 0a62a6ac3b
3 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,5 @@
control 'exception_catcher_test' do
describe dummy([{'name' => 'foo'}]) do
its('valid?') { should eq '' }
end
end

View file

@ -0,0 +1,50 @@
module Dummy
class Test
attr_reader :filter_data
def initialize(data)
@filter_data = data
end
filter = FilterTable.create
filter.register_column(:names, field: 'name')
filter.register_column(
:tags,
field: :tags,
# ref: https://github.com/inspec/inspec/blob/master/docs/dev/filtertable-usage.md#lazy-loading
lazy: lambda do |row, _criterion, _table|
tags = row['tags']
if tags.nil?
raise(Inspec::Exceptions::ResourceFailed, '`tags` for resource is missing')
end
row[:tags] = tags
end
)
filter.install_filter_methods_on_resource(self, :filter_data)
end
end
class DummyTest < Inspec.resource(1)
name 'dummy'
attr_reader :data
def initialize(data)
@data = data
end
def valid?
tags
end
private
def tags
r_data.tags[0]
end
def r_data
Dummy::Test.new(data)
end
end

View file

@ -123,4 +123,15 @@ describe "filtertable functional tests" do
expect_clean_run(controls) expect_clean_run(controls)
end end
end end
describe "if control fails" do
it "should show the exact error message" do
controls = ["exception_catcher_test"]
cmd = "exec " + ft_profile_path + " --controls " + controls.join(" ") + " --no-sudo"
run_result = run_result_for_controls(controls)
outcome_hash = failed_control_test_outcomes(run_result)
_(outcome_hash["exception_catcher_test"]).must_include "`tags` for resource is missing"
assert_exit_code 100, run_result
end
end
end end