inspec/test/unit/mock/profiles/profile-with-resource-exceptions/libraries/example_resource.rb
Jerry Aldrich III 43b71ff132 Add non-halting exception support to resources (#2235)
* Add non-halting exception support to resources

This adds two `Inspec::Exceptions` that can be used within resources to
either skip or fail a test without halting execution.

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
2017-11-06 13:28:53 -05:00

57 lines
1.4 KiB
Ruby

# encoding: utf-8
class ExceptionResourceTest < Inspec.resource(1)
name 'exception_resource_test'
desc '
Used to test resource exceptions.
'
example "
# Should execute always and pass
describe exception_resource_test('foo') do
its('value') { should eq 'foo' }
end
# Should execute always and fail
describe exception_resource_test('foo') do
its('value') { should eq 'bar' }
end
# Should raise Inspec::Exceptions::SkipResource but not halt run
# Example: Command not found
describe exception_resource_test('foo', :skip_me) do
its('value') { should eq 'foo' }
end
# Should raise Inspec::Exceptions::FailResource but not halt run
# Example: Command failed
describe exception_resource_test('foo', :fail_me) do
its('value') { should eq 'foo' }
end
"
attr_reader :value
def initialize(value, qualifier = nil)
@value = value
@inside_matcher = inside_matcher
case qualifier
when :skip_me
raise Inspec::Exceptions::ResourceSkipped, 'Skipping because reasons'
when :fail_me
raise Inspec::Exceptions::ResourceFailed, 'Failing because reasons'
end
end
def inside_matcher
case @value
when 'fail inside matcher'
raise Inspec::Exceptions::ResourceFailed, 'Failing inside matcher'
when 'skip inside matcher'
raise Inspec::Exceptions::ResourceSkipped, 'Skipping inside matcher'
else
'inside_matcher'
end
end
end