mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Change config precedence so that env variables can override config.
Added a method to programmatically update the config Created tests
This commit is contained in:
parent
8f5173ea2d
commit
30d7185c38
@ -183,6 +183,20 @@ def set_config(config):
|
||||
bigchaindb.config['CONFIGURED'] = True
|
||||
|
||||
|
||||
def update_config(config):
|
||||
"""Update bigchaindb.config with whatever is in the provided config dict,
|
||||
and then set bigchaindb.config['CONFIGURED'] = True
|
||||
|
||||
Args:
|
||||
config (dict): the config dict to read for changes
|
||||
to the default config
|
||||
"""
|
||||
|
||||
# Update the default config with whatever is in the passed config
|
||||
update(bigchaindb.config, update_types(config, bigchaindb.config))
|
||||
bigchaindb.config['CONFIGURED'] = True
|
||||
|
||||
|
||||
def write_config(config, filename=None):
|
||||
"""Write the provided configuration to a specific location.
|
||||
|
||||
@ -206,13 +220,18 @@ def autoconfigure(filename=None, config=None, force=False):
|
||||
logger.debug('System already configured, skipping autoconfiguration')
|
||||
return
|
||||
|
||||
newconfig = env_config(bigchaindb.config)
|
||||
# start with the current configuration
|
||||
newconfig = bigchaindb.config
|
||||
|
||||
# update configuration from file
|
||||
try:
|
||||
newconfig = update(newconfig, file_config(filename=filename))
|
||||
except FileNotFoundError as e:
|
||||
logger.warning('Cannot find config file `%s`.' % e.filename)
|
||||
|
||||
# override configuration with env variables
|
||||
newconfig = env_config(newconfig)
|
||||
|
||||
if config:
|
||||
newconfig = update(newconfig, config)
|
||||
|
||||
|
@ -168,3 +168,41 @@ def test_autoconfigure_read_both_from_file_and_env(monkeypatch):
|
||||
'consensus_plugin': 'default',
|
||||
}
|
||||
|
||||
|
||||
def test_autoconfigure_env_precedence(monkeypatch):
|
||||
file_config = {
|
||||
'database': {'host': 'test-host', 'name': 'bigchaindb', 'port': 28015}
|
||||
}
|
||||
monkeypatch.setattr('bigchaindb.config_utils.file_config', lambda *args, **kwargs: file_config)
|
||||
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_NAME': 'test-dbname',
|
||||
'BIGCHAINDB_DATABASE_PORT': '4242',
|
||||
'BIGCHAINDB_SERVER_BIND': 'localhost:9985'})
|
||||
|
||||
import bigchaindb
|
||||
from bigchaindb import config_utils
|
||||
config_utils.autoconfigure()
|
||||
|
||||
assert bigchaindb.config['CONFIGURED']
|
||||
assert bigchaindb.config['database']['host'] == 'test-host'
|
||||
assert bigchaindb.config['database']['name'] == 'test-dbname'
|
||||
assert bigchaindb.config['database']['port'] == 4242
|
||||
assert bigchaindb.config['server']['bind'] == 'localhost:9985'
|
||||
|
||||
|
||||
def test_update_config(monkeypatch):
|
||||
import bigchaindb
|
||||
from bigchaindb import config_utils
|
||||
|
||||
file_config = {
|
||||
'database': {'host': 'test-host', 'name': 'bigchaindb', 'port': 28015}
|
||||
}
|
||||
monkeypatch.setattr('bigchaindb.config_utils.file_config', lambda *args, **kwargs: file_config)
|
||||
config_utils.autoconfigure()
|
||||
|
||||
# update configuration, retaining previous changes
|
||||
config_utils.update_config({'database': {'port': 28016, 'name': 'bigchaindb_other'}})
|
||||
|
||||
assert bigchaindb.config['database']['host'] == 'test-host'
|
||||
assert bigchaindb.config['database']['name'] == 'bigchaindb_other'
|
||||
assert bigchaindb.config['database']['port'] == 28016
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user