Allow custom resources to access all other resources (#3108)

Signed-off-by: Jared Quick <jquick@chef.io>
This commit is contained in:
Jared Quick 2018-06-06 11:56:51 -04:00 committed by Clinton Wolfe
parent b1fa538521
commit 06e1aa5379
7 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,3 @@
# Example Custom Resource Profile
This example shows the implementation of an InSpec profile with custom resources.

View file

@ -0,0 +1,7 @@
# encoding: utf-8
describe gordon do
its('crime_rate') { should be < 5 }
it { should have_a_fabulous_mustache }
end

View file

@ -0,0 +1,8 @@
name: custom_resource_example
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: An InSpec Compliance Profile
version: 0.1.0

View file

@ -0,0 +1,20 @@
class Batsignal < Inspec.resource(1)
name 'batsignal'
example "
describe batsignal do
its('number_of_sightings)') { should eq '4' }
end
"
def number_of_sightings
local_command_call
end
private
def local_command_call
# call out to a core resource
inspec.command('echo 4').stdout.to_i
end
end

View file

@ -0,0 +1,21 @@
class Gordon < Inspec.resource(1)
name 'gordon'
example "
describe gordon do
its('crime_rate') { should be < 2 }
it { should have_a_fabulous_mustache }
end
"
def crime_rate
# call out ot another custom resource
inspec.batsignal.number_of_sightings
end
def has_a_fabulous_mustache?
# always true
true
end
end

View file

@ -50,8 +50,16 @@ module Inspec
define_method id.to_sym do |*args|
r.new(backend, id.to_s, *args)
end
# confirm backend custom resources have access to other custom resources
next if backend.respond_to?(id)
backend.class.send(:define_method, id.to_sym) do |*args|
r.new(backend, id.to_s, *args)
end
end
# attach backend so we have access to all resources and
# the train connection object
define_method :inspec do
backend
end

View file

@ -328,4 +328,13 @@ Test Summary: \e[38;5;41m2 successful\e[0m, 0 failures, 0 skipped\n"
controls.select { |c| c['results'][0]['status'] == 'passed' }.count.must_be :>, 1
end
end
describe 'when using multiple custom resources with each other' do
let(:out) { inspec('exec ' + File.join(examples_path, 'custom-resource') + ' --no-create-lockfile') }
it 'completes the run with failed controls but no exception' do
out.stderr.must_be_empty
out.exit_status.must_equal 0
end
end
end