Input plugin types work

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-05-02 21:37:14 -04:00
parent 7fc50708d1
commit 53beaff372
4 changed files with 67 additions and 13 deletions

View file

@ -12,7 +12,7 @@ module Inspec
include Singleton
extend Forwardable
attr_reader :inputs_by_profile, :profile_aliases
attr_reader :inputs_by_profile, :profile_aliases, :plugins
def_delegator :inputs_by_profile, :each
def_delegator :inputs_by_profile, :[]
def_delegator :inputs_by_profile, :key?, :profile_known?
@ -25,6 +25,13 @@ module Inspec
# this is a list of optional profile name overrides set in the inspec.yml
@profile_aliases = {}
# Upon creation, activate all input plugins
@plugins = []
Inspec::Plugin::V2::Registry.instance.find_activators(plugin_type: :input).each do |activator|
activator.activate!
plugins << activator.implementation_class.new
end
end
#-------------------------------------------------------------#
@ -35,11 +42,23 @@ module Inspec
@profile_aliases[name] = alias_name
end
# Returns an Hash, name => Input that have actually been mentioned
def list_inputs_for_profile(profile)
inputs_by_profile[profile] = {} unless profile_known?(profile)
inputs_by_profile[profile]
end
# Returns an Array of input names. This includes input names
# that plugins may be able to fetch, but have not actually been
# mentioned in the control code.
def list_potential_input_names_for_profile(profile_name)
input_names = inputs_by_profile[profile_name].keys
plugins.each do |plugin|
input_names += plugin.list_inputs(profile_name)
end
input_names.uniq
end
#-------------------------------------------------------------#
# Support for Individual Inputs
#-------------------------------------------------------------#
@ -51,6 +70,7 @@ module Inspec
handle_late_arriving_alias(alias_name, profile_name) if profile_known?(alias_name)
end
# Find or create the input
inputs_by_profile[profile_name] ||= {}
if inputs_by_profile[profile_name].key?(input_name)
inputs_by_profile[profile_name][input_name].update(options)
@ -58,6 +78,12 @@ module Inspec
inputs_by_profile[profile_name][input_name] = Inspec::Input.new(input_name, options)
end
# Poll the plugins
plugins.each do |plugin|
evt = plugin.fetch(profile_name, input_name)
inputs_by_profile[profile_name][input_name].events << evt if evt
end
inputs_by_profile[profile_name][input_name]
end
@ -214,6 +240,7 @@ module Inspec
:find_or_register_input,
:register_profile_alias,
:list_inputs_for_profile,
:list_potential_input_names_for_profile,
:bind_profile_inputs,
].each do |meth|
define_singleton_method(meth) do |*args|

View file

@ -1,9 +1,40 @@
require 'inspec/objects/input'
module InspecPlugins::InputTestFixture
class InputImplementation < Inspec.plugin(2, :input)
# TODO
# fetch?
# default_priority?
# list_profiles?
# list_inputs?
def default_priority
65
end
def fetch(profile_name, input_name)
return nil unless test_fixture_data.key?(profile_name)
return nil unless test_fixture_data[profile_name].key?(input_name)
value = test_fixture_data[profile_name][input_name]
Inspec::Input::Event.new(
action: :fetch,
provider: :'inspec-input-test-fixture',
priority: default_priority,
value: value,
file: __FILE__,
hit: true,
)
end
def list_inputs(profile_name)
return [] unless test_fixture_data.key?(profile_name)
test_fixture_data[profile_name].keys
end
private
def test_fixture_data
{
'input-test-fixture' => {
'test_only_in_plugin' => 'only_in_plugin',
'test_collide_plugin_higher' => 'collide_plugin_higher',
'test_collide_inline_higher' => 'wrong',
'test_not_mentioned_inline' => 'anything',
}
}
end
end
end

View file

@ -5,7 +5,7 @@ module InspecPlugins
class Plugin < ::Inspec.plugin(2)
plugin_name :'inspec-input-test-fixture'
input :test_fixture do
require 'inspec-inspec-test-fixture/input'
require 'inspec-input-test-fixture/input'
InspecPlugins::InputTestFixture::InputImplementation
end
end

View file

@ -17,13 +17,9 @@ control 'collide_inline_higher' do
end
control 'list_inputs' do
inputs = Inspec::InputRegistry.list_inputs_for_profile(:'input-test-fixture')
describe inputs do
it { should_not be_nil }
it { should be_kind_of Hash }
end
inputs = Inspec::InputRegistry.list_potential_input_names_for_profile('input-test-fixture')
describe inputs.keys do
describe inputs do
[
'test_only_in_plugin',
'test_collide_inline_higher',