def test_wrong_arguments(self): """ Test a plugin with wrong arguments to a method. """ class P1(OMPluginBase): """ Plugin with interface and methods with the wrong arguments. """ name = 'P1' version = '1.0' interfaces = [('config', '1.0')] @om_expose(auth=True) def get_config_description(self): """ Method arguments are fine. """ pass @om_expose(auth=True) def get_config(self): """ Method arguments are fine. """ pass @om_expose(auth=True) def set_config(self, test): """ Method arguments: expected config instead of test. """ pass with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals( 'Plugin \'P1\': the arguments for method \'set_config\': [\'test\'] do not match the interface arguments: [\'config\'].', ctx.exception.message)
def check_plugin(plugin_class): """ Check if the plugin class has name, version and interfaces attributes. Raises PluginException when the attributes are not present. """ if not hasattr(plugin_class, 'name'): raise PluginException( 'Attribute \'name\' is missing from the plugin class') # Check if valid plugin name if not re.match(r'^[a-zA-Z0-9_]+$', plugin_class.name): raise PluginException( 'Plugin name \'{0}\' is malformed: can only contain letters, numbers and underscores.' .format(plugin_class.name)) if not hasattr(plugin_class, 'version'): raise PluginException( 'Attribute \'version\' is missing from the plugin class') # Check if valid version (a.b.c) if not re.match(r'^[0-9]+\.[0-9]+\.[0-9]+$', plugin_class.version): raise PluginException( 'Plugin version \'{0}\' is malformed: expected \'a.b.c\' where a, b and c are numbers.' .format(plugin_class.version)) if not hasattr(plugin_class, 'interfaces'): raise PluginException( 'Attribute \'interfaces\' is missing from the plugin class') check_interfaces(plugin_class)
def test_ok(self): """ Test an interface check that succeeds. """ _ = self class P1(OMPluginBase): """ Plugin with multiple interfaces that are well implemented. """ name = 'P1' version = '1.0' interfaces = [('config', '1.0'), ('webui', '1.0')] @om_expose(auth=True) def get_config_description(self): """ No implementation. """ pass @om_expose(auth=True) def get_config(self): """ No implementation. """ pass @om_expose(auth=True) def set_config(self, config): """ No implementation. """ pass @om_expose(auth=True) def html_index(self): """ No implementation. """ pass check_interfaces(P1)
def test_wrong_arguments(self): """ Test a plugin with wrong arguments to a method. """ class P1(OMPluginBase): """ Plugin with interface and methods with the wrong arguments. """ name = 'P1' version = '1.0' interfaces = [('config', '1.0')] @om_expose(auth=True) def get_config_description(self): """ Method arguments are fine. """ pass @om_expose(auth=True) def get_config(self): """ Method arguments are fine. """ pass @om_expose(auth=True) def set_config(self, test): """ Method arguments: expected config instead of test. """ pass with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('Plugin \'P1\': the arguments for method \'set_config\': [\'test\'] do not match the interface arguments: [\'config\'].', ctx.exception.message)
def test_interface_not_found(self): """ Test a plugin with an interface that is not known. """ class P1(OMPluginBase): """ Plugin with unknown interface. """ name = 'P1' version = '1.0' interfaces = [('myinterface', '2.0')] with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('Interface \'myinterface\' with version \'2.0\' was not found.', ctx.exception.message)
def test_no_interfaces(self): """ Test a plugin without interfaces. """ _ = self class P1(OMPluginBase): """ Plugin without interfaces. """ name = 'P1' version = '1.0' interfaces = [] check_interfaces(P1) # Should not raise exceptions
def test_missing_method_interface(self): """ Test a plugin with a missing method. """ class P1(OMPluginBase): """ Plugin with valid interface and missing methods. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('Plugin \'P1\' has no method named \'html_index\'', ctx.exception.message)
def test_not_a_method(self): """ Test where a name of an interface method is used for something else. """ class P1(OMPluginBase): """ Plugin with valid interface and missing methods. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] html_index = 'hello' with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('Plugin \'P1\' has no method named \'html_index\'', ctx.exception.message)
def test_interface_not_found(self): """ Test a plugin with an interface that is not known. """ class P1(OMPluginBase): """ Plugin with unknown interface. """ name = 'P1' version = '1.0' interfaces = [('myinterface', '2.0')] with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals( 'Interface \'myinterface\' with version \'2.0\' was not found.', ctx.exception.message)
def test_not_exposed_interface(self): """ Test a non-exposed method on a plugin. """ class P1(OMPluginBase): """ Plugin with valid interface and unexposed methods. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] def html_index(self): _ = self return 'hello' with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('Plugin \'P1\' does not expose method \'html_index\'', ctx.exception.message)
def test_wrong_authentication_interface(self): """ Test a plugin with wrong authentication on a method. """ class P1(OMPluginBase): """ Plugin with valid interface and methods without authentication. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] @om_expose(auth=False) def html_index(self): return 'hello' with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('Plugin \'P1\': authentication for method \'html_index\' does not match the interface authentication (required).', ctx.exception.message)
def test_missing_self(self): """ Test a plugin that is missing 'self' for a method. """ class P1(OMPluginBase): """ Plugin with interface method without self. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] @om_expose(auth=True) def html_index(): # pylint: disable=E0211 """ Without self. """ pass with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('Method \'html_index\' on plugin \'P1\' lacks \'self\' as first argument.', ctx.exception.message)
def test_not_exposed_interface(self): """ Test a non-exposed method on a plugin. """ class P1(OMPluginBase): """ Plugin with valid interface and unexposed methods. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] def html_index(self): _ = self return 'hello' with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEqual('Plugin \'P1\' does not expose method \'html_index\'', str(ctx.exception))
def test_wrong_authentication_interface(self): """ Test a plugin with wrong authentication on a method. """ class P1(OMPluginBase): """ Plugin with valid interface and methods without authentication. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] @om_expose(auth=False) def html_index(self): return 'hello' with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals( 'Plugin \'P1\': authentication for method \'html_index\' does not match the interface authentication (required).', ctx.exception.message)
def test_missing_self(self): """ Test a plugin that is missing 'self' for a method. """ class P1(OMPluginBase): """ Plugin with interface method without self. """ name = 'P1' version = '1.0' interfaces = [('webui', '1.0')] @om_expose(auth=True) def html_index(): # pylint: disable=E0211 """ Without self. """ pass with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals( 'Method \'html_index\' on plugin \'P1\' lacks \'self\' as first argument.', ctx.exception.message)
def test_wrong_interface_format(self): """ Test a plugin with the wrong interface format. """ class P1(OMPluginBase): """ Plugin with invalid interface. """ name = 'P1' version = '1.0' interfaces = 'interface1' with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals('The interfaces attribute on plugin \'P1\' is not a list.', ctx.exception.message) class P2(OMPluginBase): """ Plugin with invalid interface. """ name = 'P2' version = '1.0' interfaces = ['interface1'] with self.assertRaises(PluginException) as ctx: check_interfaces(P2) self.assertEquals('Interface \'interface1\' on plugin \'P2\' is not a tuple of (name, version).', ctx.exception.message) class P3(OMPluginBase): """ Plugin with invalid interface. """ name = 'P3' version = '1.0' interfaces = [('interface1',)] with self.assertRaises(PluginException) as ctx: check_interfaces(P3) self.assertEquals('Interface \'(\'interface1\',)\' on plugin \'P3\' is not a tuple of (name, version).', ctx.exception.message)
def check_plugin(plugin_class): """ Check if the plugin class has name, version and interfaces attributes. Raises PluginException when the attributes are not present. """ if not hasattr(plugin_class, 'name'): raise PluginException('Attribute \'name\' is missing from the plugin class') # Check if valid plugin name if not re.match(r'^[a-zA-Z0-9_]+$', plugin_class.name): raise PluginException('Plugin name \'{0}\' is malformed: can only contain letters, numbers and underscores.'.format(plugin_class.name)) if not hasattr(plugin_class, 'version'): raise PluginException('Attribute \'version\' is missing from the plugin class') # Check if valid version (a.b.c) if not re.match(r'^[0-9]+\.[0-9]+\.[0-9]+$', plugin_class.version): raise PluginException('Plugin version \'{0}\' is malformed: expected \'a.b.c\' where a, b and c are numbers.'.format(plugin_class.version)) if not hasattr(plugin_class, 'interfaces'): raise PluginException('Attribute \'interfaces\' is missing from the plugin class') check_interfaces(plugin_class)
def test_wrong_interface_format(self): """ Test a plugin with the wrong interface format. """ class P1(OMPluginBase): """ Plugin with invalid interface. """ name = 'P1' version = '1.0' interfaces = 'interface1' with self.assertRaises(PluginException) as ctx: check_interfaces(P1) self.assertEquals( 'The interfaces attribute on plugin \'P1\' is not a list.', ctx.exception.message) class P2(OMPluginBase): """ Plugin with invalid interface. """ name = 'P2' version = '1.0' interfaces = ['interface1'] with self.assertRaises(PluginException) as ctx: check_interfaces(P2) self.assertEquals( 'Interface \'interface1\' on plugin \'P2\' is not a tuple of (name, version).', ctx.exception.message) class P3(OMPluginBase): """ Plugin with invalid interface. """ name = 'P3' version = '1.0' interfaces = [('interface1', )] with self.assertRaises(PluginException) as ctx: check_interfaces(P3) self.assertEquals( 'Interface \'(\'interface1\',)\' on plugin \'P3\' is not a tuple of (name, version).', ctx.exception.message)