Fixed Inspec::Plugin::V2::Registry#detect_system_plugins

+ Falls back to checking for inspec-core if inspec not found.
+ Fails gracefully if neither are found.

Signed-off-by: Ryan Davis <zenspider@chef.io>
This commit is contained in:
Ryan Davis 2019-09-24 16:45:03 -07:00
parent 9e641e9e94
commit ca4844210d
2 changed files with 99 additions and 3 deletions

View file

@ -1,4 +1,5 @@
require "inspec/log"
require "inspec/version"
require "inspec/plugin/v2/config_file"
require "inspec/plugin/v2/filter"
@ -274,9 +275,22 @@ module Inspec::Plugin::V2
end
end
def find_inspec_gemspec(name, ver)
Gem::Specification.find_by_name(name, ver)
rescue Gem::MissingSpecError
nil
end
def detect_system_plugins
# Find the gemspec for inspec
inspec_gemspec = Gem::Specification.find_by_name("inspec", "=#{Inspec::VERSION}")
inspec_gemspec =
find_inspec_gemspec("inspec", "=#{Inspec::VERSION}") ||
find_inspec_gemspec("inspec-core", "=#{Inspec::VERSION}")
unless inspec_gemspec
Inspec::Log.warn "inspec gem not found, skipping detecting of system plugins"
return
end
# Make a RequestSet that represents the dependencies of inspec
inspec_deps_request_set = Gem::RequestSet.new(*inspec_gemspec.dependencies)

View file

@ -1,10 +1,14 @@
# Unit tests for Inspec::PluginLoader and Registry
require "minitest/autorun"
require_relative "../../../../lib/inspec/plugin/v2"
require "helper"
require "inspec/plugin/v2"
require "train" # Needed for Train plugin testing
class Inspec::Plugin::V2::Loader
public :detect_system_plugins
end
class PluginLoaderTests < Minitest::Test
@@orig_home = Dir.home
@ -244,4 +248,82 @@ class PluginLoaderTests < Minitest::Test
assert_includes(Train::Plugins.registry.keys, "test-fixture", "After requiring the gem, the Train Registry should know the plugin is loaded")
assert(reg.loaded_plugin?(plugin_name), "After requiring, InSpec Registry should know the the plugin is loaded")
end
REG_INST = Inspec::Plugin::V2::Registry.instance
def with_empty_registry
old_reg = REG_INST.registry.dup
REG_INST.registry.clear
yield
ensure
REG_INST.registry.replace old_reg
end
def with_logger
old_logger = Inspec::Log.logger
io = StringIO.new
Inspec::Log.logger = Logger.new(io)
yield
io.string
ensure
Inspec::Log.logger = old_logger
end
def using_bundler?
Gem::Specification.find_by_name("inspec")
rescue Gem::MissingSpecError
nil
end
def assert_detect_system_plugins(exp_keys, exp_err)
# rubocop:disable Style/HashSyntax
loader = Inspec::Plugin::V2::Loader.new(:omit_user_plugins => true,
:omit_bundles => true,
:omit_core_plugins => true,
:omit_sys_plugins => true)
assert_empty REG_INST.registry
yield loader
log_out = with_logger do
assert_silent do
loader.detect_system_plugins
end
end
assert_equal exp_keys, REG_INST.registry.keys.sort
assert_match exp_err, log_out
end
def test_detect_system_plugins
with_empty_registry do
exp = []
exp_err = "inspec gem not found, skipping detecting of system plugins\n"
assert_detect_system_plugins exp, exp_err do |loader|
def loader.find_inspec_gemspec(*)
nil
end
end
end
end
def test_detect_system_plugins_inspec
skip "not valid in this env" unless using_bundler?
with_empty_registry do
exp = %i{ train-aws train-habitat train-winrm }
exp_err = ""
assert_detect_system_plugins exp, exp_err do |loader|
def loader.find_inspec_gemspec(name, version)
super if name == "inspec"
end
end
end
end
end