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:
Rodolphe Marques 2016-06-21 13:19:02 +02:00
parent 8f5173ea2d
commit 30d7185c38
2 changed files with 58 additions and 1 deletions

View File

@ -183,6 +183,20 @@ def set_config(config):
bigchaindb.config['CONFIGURED'] = True 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): def write_config(config, filename=None):
"""Write the provided configuration to a specific location. """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') logger.debug('System already configured, skipping autoconfiguration')
return return
newconfig = env_config(bigchaindb.config) # start with the current configuration
newconfig = bigchaindb.config
# update configuration from file
try: try:
newconfig = update(newconfig, file_config(filename=filename)) newconfig = update(newconfig, file_config(filename=filename))
except FileNotFoundError as e: except FileNotFoundError as e:
logger.warning('Cannot find config file `%s`.' % e.filename) logger.warning('Cannot find config file `%s`.' % e.filename)
# override configuration with env variables
newconfig = env_config(newconfig)
if config: if config:
newconfig = update(newconfig, config) newconfig = update(newconfig, config)

View File

@ -168,3 +168,41 @@ def test_autoconfigure_read_both_from_file_and_env(monkeypatch):
'consensus_plugin': 'default', '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