You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.7 KiB
83 lines
2.7 KiB
# pylint: disable=redefined-outer-name
|
|
import sys
|
|
import pytest
|
|
from shepherd.agent import plugin
|
|
|
|
|
|
def clear_plugin_state(plugin_name):
|
|
# Make sure it's loading a fresh copy
|
|
sys.modules.pop(plugin_name, None)
|
|
plugin._loaded_plugins = {}
|
|
plugin.plugin_interfaces = {}
|
|
plugin.plugin_functions = {}
|
|
|
|
|
|
@pytest.fixture
|
|
def simple_plugin(request):
|
|
interface = plugin.load_plugin("simpletestplugin", request.fspath.dirname)
|
|
return interface
|
|
|
|
|
|
def test_simple_plugin_load(simple_plugin: plugin.PluginInterface):
|
|
# If successful, will load as if it's a custom plugin
|
|
assert simple_plugin._plugin_name == "simpletestplugin"
|
|
|
|
|
|
def test_simple_interface_function_load(simple_plugin: plugin.PluginInterface):
|
|
|
|
# Check register_function()
|
|
assert "my_interface_function" in simple_plugin._functions
|
|
|
|
|
|
@pytest.fixture
|
|
def simple_running_plugin(request):
|
|
clear_plugin_state("simpletestplugin")
|
|
interface = plugin.load_plugin("simpletestplugin", request.fspath.dirname)
|
|
template_config = interface.confspec.get_template()
|
|
plugin.init_plugins({"simpletestplugin": template_config}, {"ckey": "cval"})
|
|
return interface
|
|
|
|
|
|
def test_simple_plugin_init(simple_running_plugin):
|
|
assert "simpletestplugin" in plugin.plugin_interfaces
|
|
|
|
|
|
def test_simple_interface_functions(simple_running_plugin):
|
|
|
|
# Check module level function dict
|
|
assert plugin.plugin_functions["simpletestplugin"]["my_interface_function"]() == 42
|
|
|
|
# Check functions handed back to plugin
|
|
assert simple_running_plugin.plugins["simpletestplugin"].my_interface_function() == 42
|
|
|
|
|
|
def test_dirty_plugin_load(request):
|
|
"""
|
|
Corner cases in plugin load
|
|
"""
|
|
interface = plugin.load_plugin("dirtytestplugin", request.fspath.dirname)
|
|
|
|
# Should prefer the confspec actually registered, even if declared after
|
|
assert "spec2" in interface.confspec.spec_dict
|
|
|
|
|
|
@pytest.fixture
|
|
def running_class_plugin(request):
|
|
clear_plugin_state("classtestplugin")
|
|
interface = plugin.load_plugin("classtestplugin", request.fspath.dirname)
|
|
template_config = interface.confspec.get_template()
|
|
plugin.init_plugins({"classtestplugin": template_config}, {"core_conf": "core_conf_vals"})
|
|
return interface
|
|
|
|
|
|
def test_class_plugin_init(running_class_plugin):
|
|
assert "classtestplugin" in plugin.plugin_interfaces
|
|
|
|
|
|
def test_class_interface_functions(running_class_plugin):
|
|
ifuncs = running_class_plugin.plugins["classtestplugin"]
|
|
assert ifuncs.module_function("a") == "module func return"
|
|
assert ifuncs.instance_method("a") == "instance method return"
|
|
assert ifuncs.class_method("a") == "class method return"
|
|
assert ifuncs.static_method("a") == "static method return"
|