inspec/test/unit/plugin/v2/api_base_test.rb
Clinton Wolfe 811318f2f8 Plugins API v2: Loader, Base API, and Test Harness (#3278)
* Functional tests for userdir option
* Accepts --config-dir CLI option
* Actually loads a config file from the config dir, more cases to test
* Able to load config and verify contents from config-dir
* Functional tests to ensure precedence for config options
* Enable setting config dir via env var
* .inspec, not .inspec.d
* Begin converting PluginCtl to PluginLoader/Registry
* Able to load and partially validate the plugins.json file
* More work on the plugin loader
* Break the world, move next gen stuff to plugin/
* Be sure to require base cli in bundled plugins
* Move test file
* Revert changes to v1 plugin, so we can have a separate one
* Checkpoint commit
* Move v2 plugin work to v2 area
* Move plugins v1 code into an isolated directory
* rubocop fixes
* Rip out the stuff about a user-dir config file, just use a plugin file
* Two psuedocode test file
* Working base API, moock plugin type, and loader.
* Adjust load path to be more welcoming
* Silence circular depencency warning, which was breaking a unit test
* Linting
* Fix plugin type registry, add tests to cover
* Feedback from Jerry

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2018-08-16 18:16:32 -04:00

98 lines
3.9 KiB
Ruby

require 'minitest/autorun'
require 'minitest/test'
require_relative '../../../../lib/inspec/plugin/v2'
class PluginV2VersionedApiTests < MiniTest::Test
# you can call Inspec.plugin(2) and get the plugin base class
def test_calling_Inspec_dot_plugin_with_2_returns_the_plugin_base_class
klass = Inspec.plugin(2)
assert_kind_of Class, klass
assert_equal 'Inspec::Plugin::V2::PluginBase', klass.name
end
def test_calling_Inspec_dot_plugin_with_2_and_mock_plugin_returns_the_mock_plugin_base_class
klass = Inspec.plugin(2, :mock_plugin_type)
assert_kind_of Class, klass, '2-arg form of Inspec.plugin() should return a specific plugin type base class'
assert_equal 'Inspec::Plugin::V2::PluginType::Mock', klass.name
end
end
class PluginV2BaseMgmtMethods < MiniTest::Test
def test_plugin_v2_management_class_methods_present
[
:base_class_for_type,
:registry,
:register_plugin_type,
:plugin_name,
].each do |method_name|
klass = Inspec::Plugin::V2::PluginBase
assert_respond_to klass, method_name, "Base class plugin management class method: #{method_name}"
end
end
def test_plugin_type_base_classes_can_be_accessed_by_name
klass = Inspec::Plugin::V2::PluginBase.base_class_for_type(:mock_plugin_type)
assert_kind_of Class, klass, 'base_class_for_type should work for mock_plugin_type'
assert_equal 'Inspec::Plugin::V2::PluginType::Mock', klass.name
end
end
class PluginV2BaseDslMethods < MiniTest::Test
def test_plugin_v2_dsl_methods_present
[
:plugin_name,
:mock_plugin_type,
# [ :attribute_provider, :platform, :fetcher, :source_reader, :control_dsl, :reporter ]
].each do |method_name|
klass = Inspec::Plugin::V2::PluginBase
assert_respond_to klass, method_name, 'Plugin DSL methods'
end
end
def test_when_calling_plugin_name_the_plugin_is_registered
test_plugin_name = :dsl_plugin_name_test
reg = Inspec::Plugin::V2::Registry.instance
refute reg.known_plugin?(test_plugin_name), 'should not know plugin name in advance'
assert_equal 0, reg.loaded_count, 'Should start with no plugins loaded'
assert_equal 0, reg.known_count, 'Should start with no plugins known'
assert_raises(Inspec::Plugin::V2::LoadError, 'plugin definitions must include the plugin_name call') do
# Make a plugin class, including calling the plugin type DSL definition method, but do not call plugin_name
Class.new(Inspec.plugin(2)) do
# Plugin class body
mock_plugin_type :dsl_plugin_name_test do
Class.new(Inspec.plugin(2, :mock_plugin_type))
end
end
end
refute reg.known_plugin?(test_plugin_name), 'failing to load a nameless plugin should not somehow register the plugin'
assert_equal 0, reg.loaded_count, 'Should have no plugins loaded after failing to load a nameless plugin'
assert_equal 0, reg.known_count, 'Should have no plugins known after failing to load a nameless plugin'
# Now create another plugin class, but this time *do* call plugin_name
name_provided_class = Class.new(Inspec.plugin(2)) do
# Plugin class body
plugin_name :dsl_plugin_name_test
mock_plugin_type :dsl_plugin_name_test do
Class.new(Inspec.plugin(2, :mock_plugin_type))
end
end
assert reg.known_plugin?(test_plugin_name), 'plugin name should register the plugin'
assert_equal 0, reg.loaded_count, 'plugin_name should not load the plugin'
assert_equal 1, reg.known_count, 'plugin_name should cause one plugin to be known'
status = reg[test_plugin_name]
assert_equal name_provided_class, status.plugin_class
assert_equal 2, status.api_generation
assert_includes status.plugin_types, :mock_plugin_type
end
def test_plugin_type_registers_an_activation_dsl_method
klass = Inspec::Plugin::V2::PluginBase
assert_respond_to klass, :mock_plugin_type, 'Activation method for mock_plugin_type'
end
end