Commit d78a0e6c authored by Jérome Perrin's avatar Jérome Perrin

test_interface: test that Manager classes implements their interfaces

parent d07e3ccf
...@@ -25,11 +25,15 @@ ...@@ -25,11 +25,15 @@
# #
############################################################################## ##############################################################################
import unittest import unittest
import os.path
import glob
from zope.interface.verify import verifyClass from zope.interface.verify import verifyClass
import zope.interface import zope.interface
from six import class_types from six import class_types
from slapos import slap
import slapos.slap
import slapos.manager
def getImplementationAssertionMethod(klass, interface): def getImplementationAssertionMethod(klass, interface):
...@@ -45,22 +49,29 @@ def getDeclarationAssertionMethod(klass): ...@@ -45,22 +49,29 @@ def getDeclarationAssertionMethod(klass):
self.fail('%s class does not respect its interface(s).' % klass.__name__) self.fail('%s class does not respect its interface(s).' % klass.__name__)
return testMethod return testMethod
def generateTestMethodListOnClass(klass, module): tested_classes = set() # to test aliases only once
def generateTestMethodListOnClass(klass, module, ignore_classes_without_interfaces=False):
"""Generate test method on klass""" """Generate test method on klass"""
for class_id in dir(module): for class_id in dir(module):
implementing_class = getattr(module, class_id) implementing_class = getattr(module, class_id)
if not isinstance(implementing_class, class_types): if not isinstance(implementing_class, class_types):
continue continue
if implementing_class in tested_classes:
continue
tested_classes.add(implementing_class)
# add methods to assert that publicly available classes are defining # add methods to assert that publicly available classes are defining
# interfaces # interfaces, except for some internal modules (slapos.manager).
method_name = 'test_%s_declares_interface' % (class_id,) if not ignore_classes_without_interfaces:
method_name = 'test_%s.%s_declares_interface' % (module.__name__, class_id)
setattr(klass, method_name, getDeclarationAssertionMethod( setattr(klass, method_name, getDeclarationAssertionMethod(
implementing_class)) implementing_class))
for interface in list(zope.interface.implementedBy(implementing_class)): for interface in list(zope.interface.implementedBy(implementing_class)):
# for each interface which class declares add a method which verify # for each interface which class declares add a method which verify
# implementation # implementation
method_name = 'test_%s_implements_%s' % (class_id, method_name = 'test_%s.%s_implements_%s' % (
module.__name__,
class_id,
interface.__identifier__) interface.__identifier__)
setattr(klass, method_name, getImplementationAssertionMethod( setattr(klass, method_name, getImplementationAssertionMethod(
implementing_class, interface)) implementing_class, interface))
...@@ -74,7 +85,18 @@ class TestInterface(unittest.TestCase): ...@@ -74,7 +85,18 @@ class TestInterface(unittest.TestCase):
""" """
# add methods to test class # add methods to test class
generateTestMethodListOnClass(TestInterface, slap) generateTestMethodListOnClass(TestInterface, slapos.slap)
# add manager classes by introspecting the modules
for module_name in glob.glob(os.path.join(os.path.dirname(slapos.manager.__file__), '*.py')):
module_name = os.path.splitext(os.path.basename(module_name))[0]
__import__('slapos.manager.' + module_name)
manager_module = getattr(slapos.manager, module_name)
generateTestMethodListOnClass(
TestInterface,
manager_module,
ignore_classes_without_interfaces=True)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment