示例#1
0
    def test_type_checking_with_nulls(self):
        self.schema = {
            'type': 'object',
            'properties': {
                'foo': {
                    'type': ['integer', 'null']
                }
            }
        }
        self.config_man = mod.ConfigManager(self.schema)

        # confirm integers work
        self.config_man.add_iterable({'foo': 5})
        self.config_man.validate()

        # confirm None works
        self.config_man = mod.ConfigManager(self.schema)
        self.config_man.add_iterable({'foo': None})
        self.config_man.validate()

        # confirm it rejects strings
        with pytest.raises(ValueError):
            self.config_man = mod.ConfigManager(self.schema)
            self.config_man.add_iterable({'foo': 'Bad Data'})
            self.config_man.validate()
示例#2
0
 def test_using_properties_title_desc_and_default(self):
     """ Show we can include title, desc, default columns without breakig anything
     """
     self.schema = {
         'type': 'object',
         'title': 'config',
         'description': 'the config validation schema',
         'properties': {
             'foo': {
                 'required': True,
                 'blank': True,
                 'type': 'string',
                 'title': 'foo-title',
                 'description': 'foo-desc',
                 'default': 'bar'
             },
             'baz': {
                 'enum': ['spaz', 'fads']
             }
         },
         'additionalProperties': False
     }
     self.config_man = mod.ConfigManager(self.schema)
     sample_dict = {}
     sample_dict['foo'] = 'string'
     sample_dict['baz'] = 'spaz'
     self.config_man.add_iterable(sample_dict)
     assert self.config_man.validate() is True
示例#3
0
    def setup_method(self, method):
        parser = argparse.ArgumentParser()
        parser.add_argument('--foo', default='bar')
        parser.add_argument('--pythonpath', default='')
        parser.add_argument('--logdir')

        self.args = parser.parse_args([])
        self.config_man = mod.ConfigManager()
示例#4
0
 def setup_method(self, method):
     self.config_schema = {
         'type': 'object',
         'properties': {
             'pythonpath': '',
             'dir': '',
             'type': 'string'
         },
         'additionalProperties': False
     }
     self.config_man = mod.ConfigManager(self.config_schema)
示例#5
0
 def setup_method(self, method):
     self.schema = {
         'type': 'object',
         'properties': {
             'foo': {
                 'type': ['null', 'string']
             },
             'baz': {
                 'type': 'string'
             }
         },
         'additionalProperties': False
     }
     self.defaults = {'foo': 'foostuff'}
     self.config_man = mod.ConfigManager(self.schema)
示例#6
0
 def setup_method(self, method):
     self.schema = {
         'type': 'object',
         'properties': {
             'foo': {
                 'required': True,
                 'type': 'string'
             },
             'baz': {
                 'enum': ['spaz', 'fads']
             }
         },
         'additionalProperties': False
     }
     self.config_man = mod.ConfigManager(self.schema)
示例#7
0
    def setup_method(self, method):
        self.config_schema = {
            'type': 'object',
            'properties': {
                'home': '',
                'dir': '',
                'type': 'string',
                'cletus_foo': 'foo1'
            },
            'additionalProperties': False
        }

        self.config_man = mod.ConfigManager()
        os.environ['CLETUS_FOO'] = 'bar'
        self.var_list = []
        self.var_list.append('CLETUS_FOO')
示例#8
0
    def test_extra_fields_in_validation_schema(self):
        self.schema = {
            'type': 'object',
            'properties': {
                'foo': {
                    'required': True,
                    'type': 'string',
                    'foo': 'bar'
                },
                'baz': {
                    'enum': ['spaz', 'fads']
                }
            },
            'additionalProperties': False
        }
        self.config_man = mod.ConfigManager(self.schema)

        sample_dict = {}
        sample_dict['foo'] = 'bar'
        sample_dict['baz'] = 'spaz'
        self.config_man.add_iterable(sample_dict)
        assert self.config_man.validate() is True
示例#9
0
 def test_items_title_and_desc(self):
     """ Show we can include title & desc columns without breakig anything
     """
     self.schema = {
         'type': 'object',
         'title': 'config',
         'description': 'the config validation schema',
         'properties': {
             'results': {
                 'items': [{
                     "type": "integer",
                     "title": "field1",
                     "description": "blahblahblah"
                 }, {
                     "type": "string"
                 }]
             }
         }
     }
     self.config_man = mod.ConfigManager(self.schema)
     sample_dict = {}
     sample_dict["results"] = [1, "foo"]
     self.config_man.add_iterable(sample_dict)
     assert self.config_man.validate() is True
示例#10
0
def setup_config(args):
    """This is a helper function to keep the bulk of the ConfigManager and
       these comments out of the main().

       ConfigManager is being used to first add config items from a file,
       then from the argparse namespace.   Alternatively, we could add:
          - multiple config files (eg, server config plus this app config)
          - environmental variables
          - arguments & options from optparse, etc that uses dictionaries
            instead of namespaces
          - config items from a resource like zookeeper that are loaded
            via the add_iterable ConfigManger method.

       The order determines the precidence.  Since args are added after the
       config file any matching items will result in args overriding the
       config file.

       Validation is performed using validictory.  This is optional, but
       recommended.  All config items are stored in two places:  one
       dedicated to that method, and one consolidated dictionary:
          - cm_config_iterable
          - cm_config_file
          - cm_config_namespace
          - cm_config_env
          - cm_config           - the consolidated config
       Any of these dictionaries can be used for validation, just pass in
       its name (just drop the cm_ prefix) for the config_type argument.
       And you might want to provide a different schema for different configs,
       just pass that it using the schema argument.

       More info on Validictory is here:
          - https://readthedocs.org/projects/validictory/
       Note that any validation tool can be easily used with the dictionaries.
    """
    config_schema = {
        'type': 'object',
        'properties': {
            'dir': {
                'required': True,
                'type': 'string'
            },
            'log_level': {
                'enum': ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
            },
            'config_fqfn': {
                'required': False,
                'type': 'string'
            },
            'log_to_console': {
                'required': False,
                'type': 'boolean'
            }
        },
        'additionalProperties': False
    }
    config = conf.ConfigManager(config_schema)
    config.add_file(app_name=APP_NAME,
                    config_fqfn=args.config_fqfn,
                    config_fn='main.yml')
    config.add_namespace(args)
    config.validate()
    return config
示例#11
0
 def test_add_vars_using_no_keys(self):
     config_man = mod.ConfigManager()
     with pytest.raises(ValueError):
         config_man.add_env_vars()
示例#12
0
 def test_add_vars_using_schema(self):
     config_man = mod.ConfigManager(config_schema=self.config_schema)
     config_man.add_env_vars()
     pp(config_man.cm_config_env)
     assert config_man.cm_config_env['CLETUS_FOO'] == 'bar'
示例#13
0
 def setup_method(self, method):
     self.config_man = mod.ConfigManager()
示例#14
0
 def test_basic(self):
     self.config_man = mod.ConfigManager()
     self.config_man.add_file(config_dir=self.temp_dir,
                              config_fn='config.yml')
     assert self.config_man.cm_config_file['foo'] == 'bar'
     assert self.config_man.cm_config['foo'] == 'bar'
示例#15
0
 def test_empty_namespace(self):
     parser = argparse.ArgumentParser()
     args = parser.parse_args([])
     config_man = mod.ConfigManager()
     self.config_man.add_namespace(args)
     assert list(config_man.cm_config.keys()) == []
示例#16
0
 def test_empty_schema(self):
     config_man = mod.ConfigManager()
     assert config_man._get_schema_keys() == []