bigchaindb/tests/test_config_utils.py

243 lines
7.6 KiB
Python
Raw Normal View History

2016-02-10 19:55:33 +01:00
import copy
from unittest.mock import mock_open, patch
2016-02-10 19:55:33 +01:00
import pytest
import bigchaindb
ORIGINAL_CONFIG = copy.deepcopy(bigchaindb._config)
2016-02-10 19:55:33 +01:00
@pytest.fixture(scope='function', autouse=True)
def clean_config(monkeypatch):
monkeypatch.setattr('bigchaindb.config', copy.deepcopy(ORIGINAL_CONFIG))
2016-02-10 19:55:33 +01:00
def test_bigchain_instance_is_initialized_when_conf_provided():
2016-02-15 11:25:56 +01:00
from bigchaindb import config_utils
2016-02-10 19:55:33 +01:00
assert 'CONFIGURED' not in bigchaindb.config
2016-04-14 10:55:07 +02:00
config_utils.set_config({'keypair': {'public': 'a', 'private': 'b'}})
2016-02-10 19:55:33 +01:00
assert bigchaindb.config['CONFIGURED'] is True
2016-02-10 19:55:33 +01:00
b = bigchaindb.Bigchain()
assert b.me
assert b.me_private
def test_bigchain_instance_raises_when_not_configured(monkeypatch):
2016-02-15 11:25:56 +01:00
from bigchaindb import config_utils
from bigchaindb.common import exceptions
2016-02-10 19:55:33 +01:00
assert 'CONFIGURED' not in bigchaindb.config
# We need to disable ``bigchaindb.config_utils.autoconfigure`` to avoid reading
# from existing configurations
monkeypatch.setattr(config_utils, 'autoconfigure', lambda: 0)
2016-02-22 23:46:32 +01:00
with pytest.raises(exceptions.KeypairNotFoundException):
2016-02-10 19:55:33 +01:00
bigchaindb.Bigchain()
2016-03-14 21:27:17 +01:00
2016-03-24 01:41:00 +01:00
def test_map_leafs_iterator():
from bigchaindb import config_utils
mapping = {
'a': {'b': {'c': 1},
'd': {'z': 44}},
'b': {'d': 2},
'c': 3
}
result = config_utils.map_leafs(lambda x, path: x * 2, mapping)
assert result == {
'a': {'b': {'c': 2},
'd': {'z': 88}},
'b': {'d': 4},
'c': 6
}
result = config_utils.map_leafs(lambda x, path: path, mapping)
assert result == {
'a': {'b': {'c': ['a', 'b', 'c']},
'd': {'z': ['a', 'd', 'z']}},
'b': {'d': ['b', 'd']},
'c': ['c']
}
2016-04-07 18:06:04 +02:00
def test_update_types():
from bigchaindb import config_utils
raw = {
'a_string': 'test',
'an_int': '42',
'a_float': '3.14',
'a_list': 'a:b:c',
}
reference = {
'a_string': 'test',
'an_int': 42,
'a_float': 3.14,
'a_list': ['a', 'b', 'c'],
}
result = config_utils.update_types(raw, reference)
assert result == reference
2016-03-24 01:41:00 +01:00
def test_env_config(monkeypatch):
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_HOST': 'test-host',
'BIGCHAINDB_DATABASE_PORT': 'test-port'})
from bigchaindb import config_utils
result = config_utils.env_config({'database': {'host': None, 'port': None}})
expected = {'database': {'host': 'test-host', 'port': 'test-port'}}
assert result == expected
def test_autoconfigure_read_both_from_file_and_env(monkeypatch, request):
file_config = {
2017-01-06 11:23:21 +01:00
'database': {
'host': 'test-host',
'backend': request.config.getoption('--database-backend')
},
'backlog_reassign_delay': 5
}
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': '1.2.3.4:56',
'BIGCHAINDB_KEYRING': 'pubkey_0:pubkey_1:pubkey_2'})
2016-03-24 01:41:00 +01:00
import bigchaindb
from bigchaindb import config_utils
config_utils.autoconfigure()
assert bigchaindb.config == {
'CONFIGURED': True,
2016-04-07 14:31:34 +02:00
'server': {
'bind': '1.2.3.4:56',
2016-04-07 14:31:34 +02:00
'workers': None,
'threads': None,
},
2016-03-24 01:41:00 +01:00
'database': {
'backend': request.config.getoption('--database-backend'),
2016-03-24 01:41:00 +01:00
'host': 'test-host',
'port': 4242,
'name': 'test-dbname',
2016-03-24 01:41:00 +01:00
},
'keypair': {
'public': None,
'private': None,
},
'keyring': ['pubkey_0', 'pubkey_1', 'pubkey_2'],
2016-03-24 01:41:00 +01:00
'statsd': {
'host': 'localhost',
'port': 8125,
'rate': 0.01,
},
'backlog_reassign_delay': 5
2016-03-24 01:41:00 +01:00
}
2016-04-07 18:06:04 +02:00
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)
2016-07-19 12:07:47 +02:00
config_utils.autoconfigure(config=file_config)
# 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
def test_file_config():
from bigchaindb.config_utils import file_config, CONFIG_DEFAULT_PATH
with patch('builtins.open', mock_open(read_data='{}')) as m:
config = file_config()
m.assert_called_once_with(CONFIG_DEFAULT_PATH)
assert config == {}
2016-07-14 19:27:58 +02:00
def test_invalid_file_config():
Rebase/feat/586/integrate tx model (#641) * Adjust imports to bigchaindb_common * Adjust get_spent function signature * Adjust block serialization * Fix BigchainApi Test * Fix TestTransactionValidation tests * Fix TestBlockValidation tests * WIP: TestMultipleInputs * Adjust tests to tx-model interface changes - Fix old tests - Fix tests in TestMultipleInputs class * Remove fulfillment message tests * Fix TransactionMalleability tests * Remove Cryptoconditions tests * Remove create_transaction * Remove signing logic * Remove consensus plugin * Fix block_creation pipeline * Fix election pipeline * Replace some util functions with bdb_common ones - timestamp ==> gen_timestamp - serialize. * Implement Block model * Simplify function signatures for vote functions Change parameter interface for the following functions: - has_previous_vote - verify_vote_signature - block_election_status so that they take a block's id and voters instead of a fake block. * Integrate Block and Transaction model * Fix leftover tests and cleanup conftest * Add bigchaindb-common to install_requires * Delete transactions after block is written (#609) * delete transactions after block is written * cleanup transaction_exists * check for duplicate transactions * delete invalid tx from backlog * test duplicate transaction * Remove dead code * Test processes.py * Test invalid tx in on server * Fix tests for core.py * Fix models tests * Test commands main fn * Add final coverage to vote pipeline * Add more tests to voting pipeline * Remove consensus plugin docs and misc * Post rebase fixes * Fix rebase mess * Remove extra blank line * Improve docstring * Remove comment handled in bigchaindb/cryptoconditions#27; see https://github.com/bigchaindb/cryptoconditions/issues/27 * Fix block serialization in block creation * Add signed_ prefix to transfer_tx * Improve docs * Add library documentation page on pipelines * PR feedback for models.py * Impr. readability of get_last_voted_block * Use dict comprehension * Add docker-compose file to build and serve docs locally for development purposes * Change private_key for signing_key * Improve docstrings * Remove consensus docs * Document new consensus module * Create different transactions for the block * Cleanup variable names in block.py * Create different transactions for the block * Cleanup variable names in block.py
2016-09-29 10:29:41 +02:00
from bigchaindb.config_utils import file_config
from bigchaindb.common import exceptions
Rebase/feat/586/integrate tx model (#641) * Adjust imports to bigchaindb_common * Adjust get_spent function signature * Adjust block serialization * Fix BigchainApi Test * Fix TestTransactionValidation tests * Fix TestBlockValidation tests * WIP: TestMultipleInputs * Adjust tests to tx-model interface changes - Fix old tests - Fix tests in TestMultipleInputs class * Remove fulfillment message tests * Fix TransactionMalleability tests * Remove Cryptoconditions tests * Remove create_transaction * Remove signing logic * Remove consensus plugin * Fix block_creation pipeline * Fix election pipeline * Replace some util functions with bdb_common ones - timestamp ==> gen_timestamp - serialize. * Implement Block model * Simplify function signatures for vote functions Change parameter interface for the following functions: - has_previous_vote - verify_vote_signature - block_election_status so that they take a block's id and voters instead of a fake block. * Integrate Block and Transaction model * Fix leftover tests and cleanup conftest * Add bigchaindb-common to install_requires * Delete transactions after block is written (#609) * delete transactions after block is written * cleanup transaction_exists * check for duplicate transactions * delete invalid tx from backlog * test duplicate transaction * Remove dead code * Test processes.py * Test invalid tx in on server * Fix tests for core.py * Fix models tests * Test commands main fn * Add final coverage to vote pipeline * Add more tests to voting pipeline * Remove consensus plugin docs and misc * Post rebase fixes * Fix rebase mess * Remove extra blank line * Improve docstring * Remove comment handled in bigchaindb/cryptoconditions#27; see https://github.com/bigchaindb/cryptoconditions/issues/27 * Fix block serialization in block creation * Add signed_ prefix to transfer_tx * Improve docs * Add library documentation page on pipelines * PR feedback for models.py * Impr. readability of get_last_voted_block * Use dict comprehension * Add docker-compose file to build and serve docs locally for development purposes * Change private_key for signing_key * Improve docstrings * Remove consensus docs * Document new consensus module * Create different transactions for the block * Cleanup variable names in block.py * Create different transactions for the block * Cleanup variable names in block.py
2016-09-29 10:29:41 +02:00
with patch('builtins.open', mock_open(read_data='{_INVALID_JSON_}')):
with pytest.raises(exceptions.ConfigurationError):
file_config()
2016-07-14 19:27:58 +02:00
def test_write_config():
from bigchaindb.config_utils import write_config, CONFIG_DEFAULT_PATH
m = mock_open()
with patch('builtins.open', m):
write_config({})
m.assert_called_once_with(CONFIG_DEFAULT_PATH, 'w')
handle = m()
handle.write.assert_called_once_with('{}')
@pytest.mark.parametrize('env_name,env_value,config_key', (
('BIGCHAINDB_DATABASE_BACKEND', 'test-backend', 'backend'),
('BIGCHAINDB_DATABASE_HOST', 'test-host', 'host'),
('BIGCHAINDB_DATABASE_PORT', 4242, 'port'),
('BIGCHAINDB_DATABASE_NAME', 'test-db', 'name'),
))
def test_database_envs(env_name, env_value, config_key, monkeypatch):
import bigchaindb
monkeypatch.setattr('os.environ', {env_name: env_value})
bigchaindb.config_utils.autoconfigure()
expected_config = copy.deepcopy(bigchaindb.config)
expected_config['database'][config_key] = env_value
assert bigchaindb.config == expected_config
def test_database_envs_replicaset(monkeypatch):
# the replica set env is only used if the backend is mongodb
import bigchaindb
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_REPLICASET':
'test-replicaset'})
bigchaindb.config['database'] = bigchaindb._database_mongodb
bigchaindb.config_utils.autoconfigure()
expected_config = copy.deepcopy(bigchaindb.config)
expected_config['database']['replicaset'] = 'test-replicaset'
assert bigchaindb.config == expected_config