mirror of
https://github.com/inspec/inspec
synced 2024-12-19 17:43:44 +00:00
58 lines
1.4 KiB
Ruby
58 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
|