Example #1
0
    def test_constructor_password(self):
        """ Test for the constructor for bool. """
        PluginConfigChecker([{
            'name': 'password',
            'type': 'password',
            'description': 'A password.'
        }])
        PluginConfigChecker([{'name': 'password', 'type': 'password'}])

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'type': 'password'}])
        self.assertTrue('name' in ctx.exception.message)
Example #2
0
    def test_constructor_int(self):
        """ Test for the constructor for int. """
        PluginConfigChecker([{
            'name': 'port',
            'type': 'int',
            'description': 'Port on the server.'
        }])
        PluginConfigChecker([{'name': 'port', 'type': 'int'}])

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'type': 'int'}])
        self.assertTrue('name' in ctx.exception.message)
Example #3
0
    def test_constructor_str(self):
        """ Test for the constructor for str. """
        PluginConfigChecker([{
            'name': 'hostname',
            'type': 'str',
            'description': 'The hostname of the server.'
        }])
        PluginConfigChecker([{'name': 'hostname', 'type': 'str'}])

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'type': 'str'}])
        self.assertTrue('name' in ctx.exception.message)
Example #4
0
    def test_constructor_bool(self):
        """ Test for the constructor for bool. """
        PluginConfigChecker([{
            'name':
            'use_auth',
            'type':
            'bool',
            'description':
            'Use authentication while connecting.'
        }])
        PluginConfigChecker([{'name': 'use_auth', 'type': 'bool'}])

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'type': 'bool'}])
        self.assertTrue('name' in ctx.exception.message)
Example #5
0
    def test_check_config_section(self):
        """ Test check_config for section. """
        checker = PluginConfigChecker([{
            'name':
            'outputs',
            'type':
            'section',
            'repeat':
            True,
            'min':
            1,
            'content': [{
                'name': 'output',
                'type': 'int'
            }]
        }])

        checker.check_config({'outputs': []})
        checker.check_config({'outputs': [{'output': 2}]})
        checker.check_config({'outputs': [{'output': 2}, {'output': 4}]})

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'outputs': 'test'})
        self.assertTrue('list' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'outputs': [{'test': 123}]})
        self.assertTrue('section' in ctx.exception.message
                        and 'output' in ctx.exception.message)
Example #6
0
    def test_check_config_int(self):
        """ Test check_config for int. """
        checker = PluginConfigChecker([{'name': 'port', 'type': 'int'}])
        checker.check_config({'port': 123})

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'port': "123"})
        self.assertTrue('int' in ctx.exception.message)
Example #7
0
    def test_check_config_bool(self):
        """ Test check_config for bool. """
        checker = PluginConfigChecker([{'name': 'use_auth', 'type': 'bool'}])
        checker.check_config({'use_auth': True})

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'use_auth': 234543})
        self.assertTrue('bool' in ctx.exception.message)
Example #8
0
    def test_check_config_str(self):
        """ Test check_config for str. """
        checker = PluginConfigChecker([{'name': 'hostname', 'type': 'str'}])
        checker.check_config({'hostname': 'cloud.openmotics.com'})

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'hostname': 123})
        self.assertTrue('str' in ctx.exception.message)
Example #9
0
    def __init__(self, webinterface, gateway_logger):
        self.setup_logging(log_function=gateway_logger)
        super(HelloWorldPlugin, self).__init__(webinterface, logger)
        logger.info('Starting %s plugin %s ...', self.name, self.version)

        # set config on default config and instantiate a validator
        self._config = self.default_config
        self._config_checker = PluginConfigChecker(self.config_description)

        logger.info("%s plugin started", self.name)
Example #10
0
    def test_check_config_error(self):
        """ Test check_config with an invalid data type """
        checker = PluginConfigChecker([{'name': 'hostname', 'type': 'str'}])

        with self.assertRaises(PluginException) as ctx:
            checker.check_config('string')
        self.assertTrue('dict' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({})
        self.assertTrue('hostname' in ctx.exception.message)
Example #11
0
    def test_check_config_password(self):
        """ Test check_config for bool. """
        checker = PluginConfigChecker([{
            'name': 'password',
            'type': 'password'
        }])
        checker.check_config({'password': '******'})

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'password': 123})
        self.assertTrue('str' in ctx.exception.message)
Example #12
0
 def test_simple(self):
     """ Test a simple valid configuration. """
     _ = self
     checker = PluginConfigChecker([{
         'name': 'log_inputs',
         'type': 'bool',
         'description': 'Log the input data.'
     }, {
         'name': 'log_outputs',
         'type': 'bool',
         'description': 'Log the output data.'
     }])
     checker.check_config({'log_inputs': True, 'log_outputs': False})
Example #13
0
    def test_constructor_enum(self):
        """ Test for the constructor for enum. """
        PluginConfigChecker([{
            'name': 'enumtest',
            'type': 'enum',
            'description': 'Test for enum',
            'choices': ['First', 'Second']
        }])
        PluginConfigChecker([{
            'name': 'enumtest',
            'type': 'enum',
            'choices': ['First', 'Second']
        }])

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'enumtest',
                'type': 'enum',
                'choices': 'First'
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'list' in ctx.exception.message)
Example #14
0
    def test_check_config_nested_enum(self):
        """ Test check_config for nested_enum. """
        checker = PluginConfigChecker([{
            'name':
            'network',
            'type':
            'nested_enum',
            'choices': [{
                'value': 'Facebook',
                'content': [{
                    'name': 'likes',
                    'type': 'int'
                }]
            }, {
                'value': 'Twitter',
                'content': [{
                    'name': 'followers',
                    'type': 'int'
                }]
            }]
        }])

        checker.check_config({'network': ['Twitter', {'followers': 3}]})
        checker.check_config({'network': ['Facebook', {'likes': 3}]})

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'network': 'test'})
        self.assertTrue('list' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'network': []})
        self.assertTrue('list' in ctx.exception.message
                        and '2' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'network': ['something else', {}]})
        self.assertTrue('choices' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            checker.check_config({'network': ['Twitter', {}]})
        self.assertTrue('nested_enum dict' in ctx.exception.message
                        and 'followers' in ctx.exception.message)
Example #15
0
    def test_constructor_error(self):
        """ Test with an invalid data type """
        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker({'test': 123})
        self.assertTrue('list' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'test': 123}])
        self.assertTrue('name' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'name': 123}])
        self.assertTrue('name' in ctx.exception.message
                        and 'string' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'name': 'test'}])
        self.assertTrue('type' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'name': 'test', 'type': 123}])
        self.assertTrue('type' in ctx.exception.message
                        and 'string' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{'name': 'test', 'type': 'something_else'}])
        self.assertTrue('type' in ctx.exception.message
                        and 'something_else' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'test',
                'type': 'str',
                'description': []
            }])
        self.assertTrue('description' in ctx.exception.message
                        and 'string' in ctx.exception.message)
Example #16
0
 def test_constructor(self):
     """ Test for the constructor. """
     _ = self
     PluginConfigChecker(FULL_DESCR)
Example #17
0
    def test_constructor_section(self):
        """ Test for the constructor for section. """
        PluginConfigChecker([{
            'name': 'outputs',
            'type': 'section',
            'repeat': True,
            'min': 1,
            'content': [{
                'name': 'output',
                'type': 'int'
            }]
        }])
        PluginConfigChecker([{
            'name': 'outputs',
            'type': 'section',
            'repeat': False,
            'content': [{
                'name': 'output',
                'type': 'int'
            }]
        }])
        PluginConfigChecker([{
            'name': 'outputs',
            'type': 'section',
            'content': [{
                'name': 'output',
                'type': 'int'
            }]
        }])

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'outputs',
                'type': 'section',
                'repeat': 'hello',
                'content': [{
                    'name': 'output',
                    'type': 'int'
                }]
            }])
        self.assertTrue('repeat' in ctx.exception.message
                        and 'bool' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'outputs',
                'type': 'section',
                'min': 1,
                'content': [{
                    'name': 'output',
                    'type': 'int'
                }]
            }])
        self.assertTrue('min' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'outputs',
                'type': 'section',
                'content': 'error'
            }])
        self.assertTrue('content' in ctx.exception.message
                        and 'list' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'outputs',
                'type': 'section',
                'content': [{
                    'name': 123
                }]
            }])
        self.assertTrue('content' in ctx.exception.message
                        and 'name' in ctx.exception.message
                        and 'string' in ctx.exception.message)
Example #18
0
    def test_constructor_nested_enum(self):
        """ Test for constructor for nested enum. """
        PluginConfigChecker([{
            'name':
            'network',
            'type':
            'nested_enum',
            'choices': [{
                'value': 'Facebook',
                'content': [{
                    'name': 'likes',
                    'type': 'int'
                }]
            }, {
                'value': 'Twitter',
                'content': [{
                    'name': 'followers',
                    'type': 'int'
                }]
            }]
        }])

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'network',
                'type': 'nested_enum',
                'choices': 'test'
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'list' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'network',
                'type': 'nested_enum',
                'choices': ['test']
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'dict' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'network',
                'type': 'nested_enum',
                'choices': [{}]
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'value' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'network',
                'type': 'nested_enum',
                'choices': [{
                    'value': 123
                }]
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'network' in ctx.exception.message
                        and 'content' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name': 'network',
                'type': 'nested_enum',
                'choices': [{
                    'value': 'test'
                }]
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'content' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name':
                'network',
                'type':
                'nested_enum',
                'choices': [{
                    'value': 'test',
                    'content': 'test'
                }]
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'content' in ctx.exception.message
                        and 'list' in ctx.exception.message)

        with self.assertRaises(PluginException) as ctx:
            PluginConfigChecker([{
                'name':
                'network',
                'type':
                'nested_enum',
                'choices': [{
                    'value': 'test',
                    'content': [{}]
                }]
            }])
        self.assertTrue('choices' in ctx.exception.message
                        and 'content' in ctx.exception.message
                        and 'name' in ctx.exception.message)