diff --git a/bigchaindb/config_utils.py b/bigchaindb/config_utils.py index c9da67b1..e1c45718 100644 --- a/bigchaindb/config_utils.py +++ b/bigchaindb/config_utils.py @@ -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) diff --git a/tests/utils/test_config_utils.py b/tests/utils/test_config_utils.py index 29aa5d0b..89572183 100644 --- a/tests/utils/test_config_utils.py +++ b/tests/utils/test_config_utils.py @@ -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 +