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.
This commit is contained in:
Christian Kellner 2020-09-14 12:29:23 +02:00 committed by Tom Gundersen
parent d8107f2347
commit 9e98f10afc
3 changed files with 54 additions and 33 deletions

46
test/unit/plugintest.py Normal file
View file

@ -0,0 +1,46 @@
#
# 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()