debian-koji-osbuild/test/unit/plugintest.py
Christian Kellner 9e98f10afc test/unit: extract common code
Create a new plugintest.PluginTest class that shares the plugin
loading code that is common to all plugin testing. Adapt the
existing (hub, builder) tests.
Also correct the name for test_builder.TestHubPlugin to
TestBuilderPlugin.
2020-09-14 13:06:52 +01:00

46 lines
1.2 KiB
Python

#
# Test Infrastructure
#
import imp
import os
import unittest
class PluginTest(unittest.TestCase):
"""Base class for Plugin tests
Use the PluginTest.load_plugin class decorator to automatically
load a plugin. If said decorator has not been specified, the
`plugin` property will be set to `None`.
"""
@staticmethod
def load_plugin(plugin_type):
def decorator(klass):
setattr(klass, "_plugin_type", plugin_type)
return klass
return decorator
def _load_plugin(self):
plugin_type = getattr(self, "_plugin_type", None)
if not plugin_type:
return None
root = os.getenv("GITHUB_WORKSPACE", os.getcwd())
haystack = os.path.join(root, "plugins", plugin_type)
fp, path, desc = imp.find_module("osbuild", [haystack])
try:
return imp.load_module("osbuild", fp, path, desc)
finally:
fp.close()
def setUp(self):
"""Setup plugin testing environment
Will load the specified plugin if the derived class has
been decorated with `Plugintest.load_plugin()`.
"""
self.plugin = self._load_plugin()