def test_loader(self): def load_to_list(config, val=1): config.append(val) return True config = [] with patch.dict('config_source._config_sources', clear=True): config_source('value', 'list')(load_to_list) res = load_to(config, 'value', 'list') assert res is True assert config == [1] res = load_to(config, 'value', 'list', val=123) assert res is True assert config == [1, 123]
def test_from_pyfile_obj(self): source = StringIO(u'ONE = 1\nTWO = "hello"\nthree = 3') config = dict() res = load_to(config, 'pyfile', 'dict', source) assert res is True # three won't load because it's lowercase. assert config == dict(ONE=1, TWO='hello')
def test_from_dict_skip_none(self): src = dict(PARAM1=1, PARAM_2=None, Y=None) config = dict(X='x', Y='y') res = load_to(config, 'dict', 'dict', src, skip_none=True) # NOTE: Y stays the same; PARAM_2 is not loaded. assert res is True assert config == dict(X='x', PARAM1=1, Y='y')
def test_mock_loader(self): loader = Mock(return_value=123) config = dict() with patch.dict('config_source._config_sources', clear=True): config_source('test_source')(loader) res = load_to(config, 'test_source', 'dict', 1, a=2) assert res == 123 assert loader.mock_calls == [call(config, 1, a=2)]
def test_from_pyfile(self, tmpdir): myconfig = tmpdir.join('myconfig.py') myconfig.write('ONE = 1\nTWO = "hello"\nthree = 3') config = dict() res = load_to(config, 'pyfile', 'dict', str(myconfig)) assert res is True # three won't load because it's lowercase. assert config == dict(ONE=1, TWO='hello')
def load_from_s3(config, filename, profile=None, access_key=None, secret_key=None, cache_filename=None, update_cache=False, silent=False): """Load configs from S3 bucket file. If ``cache_filename`` is not set then it downloads to memory, otherwise content is saved in ``cache_filename`` and reused on next calls unless ``update_cache`` is set. If ``update_cache`` is set then it downloads remote file and updates ``cache_filename``. Args: config: :class:`~configsource.Config` instance. filename: S3 file path ``s3://<bucket>/path/to/file``. profile: AWS credentials profile. access_key: AWS access key id. secret_key: AWS secret access key. cache_filename: Filename to store downloaded content. update_cache: Use cached file or force download. silent: Don't raise an error on missing remote files or file loading errors. Returns: ``True`` if config is loaded and ``False`` otherwise. See Also: https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html """ bucket, filename = split_s3_path(filename) if not filename: raise ValueError('Empty filename') try: if cache_filename is None: src = BytesIO() bucket = get_bucket(bucket, profile, access_key, secret_key) bucket.download_fileobj(filename, src) src.flush() src.seek(0) else: # If cache file is not set or doesn't exist then force download. src = cache_filename if not op.exists(cache_filename) or update_cache: bucket = get_bucket(bucket, profile, access_key, secret_key) bucket.download_file(filename, src) except Boto3Error: if silent: return False raise # If cache update is not required then just load configs from existing # cached file. return load_to(config, 'pyfile', 'dict', src, silent=silent)
def test_from_object_no_attrs(self): class Cfg: param1 = 1 param_2 = '2' lower_param = dict() config = dict() res = load_to(config, 'object', 'dict', Cfg) assert res is False assert config == dict()
def test_from_object(self): class Cfg: PARAM1 = 1 PARAM_2 = '2' lower_param = dict() # lowercase won't load. config = dict() res = load_to(config, 'object', 'dict', Cfg) assert res is True assert config == dict(PARAM1=1, PARAM_2='2')
def test_from_dict(self): src = dict( PARAM1=1, PARAM_2='2', PARAM_3=None, lower_param=None # lowercase won't load. ) config = dict(X='x') res = load_to(config, 'dict', 'dict', src) assert res is True assert config == dict(X='x', PARAM1=1, PARAM_2='2', PARAM_3=None)
def test_from_env_notrim(self): config = dict() load_to(config, 'env', 'dict', prefix='MYTEST_', trim_prefix=False) assert config == dict(MYTEST_ONE='12', MYTEST_TWO='hello')
def test_from_env(self): config = dict() res = load_to(config, 'env', 'dict', prefix='MYTEST_') assert res is True assert config == dict(ONE='12', TWO='hello')
def test_from_env_empty(self): config = dict() res = load_to(config, 'env', 'dict', prefix='MYTEST_') assert res is False assert config == dict()
def test_unknown_src(self): with patch.dict('config_source._config_sources', clear=True): configsource._config_sources['bla'] = dict() with pytest.raises(ConfigSourceError) as e: load_to({}, 'env', 'bla') assert str(e.value) == 'Unknown source: env (config type: bla)'
def test_unknown_type(self): with patch.dict('config_source._config_sources', clear=True): with pytest.raises(ConfigSourceError) as e: load_to({}, 'env', config_type='bla') assert str(e.value) == 'Unknown config type: bla'