From c695f0950e7a1a6510b5f3ea5f39979f7ac30fb5 Mon Sep 17 00:00:00 2001 From: vrde Date: Thu, 18 Feb 2016 16:27:24 +0100 Subject: [PATCH 1/4] Allow non interactive first start --- bigchaindb/commands/bigchain.py | 11 +++++++---- tests/test_commands.py | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/bigchaindb/commands/bigchain.py b/bigchaindb/commands/bigchain.py index 7ea95851..c29e9e94 100644 --- a/bigchaindb/commands/bigchain.py +++ b/bigchaindb/commands/bigchain.py @@ -4,6 +4,7 @@ import os import logging import argparse +import copy import bigchaindb import bigchaindb.config_utils @@ -48,13 +49,15 @@ def run_configure(args, skip_if_exists=False): return # Patch the default configuration with the new values - conf = bigchaindb._config + conf = copy.deepcopy(bigchaindb._config) + print('Generating keypair') conf['keypair']['private'], conf['keypair']['public'] = generate_key_pair() - for key in ('host', 'port', 'name'): - val = conf['database'][key] - conf['database'][key] = input('Database {}? (default `{}`): '.format(key, val)) or val + if not args.yes: + for key in ('host', 'port', 'name'): + val = conf['database'][key] + conf['database'][key] = input('Database {}? (default `{}`): '.format(key, val)) or val bigchaindb.config_utils.write_config(conf, config_path) print('Ready to go!') diff --git a/tests/test_commands.py b/tests/test_commands.py index 3897129e..91ec3328 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1,5 +1,6 @@ from argparse import Namespace from pprint import pprint +import copy import pytest @@ -71,6 +72,30 @@ def test_bigchain_run_start(mock_run_configure, mock_file_config, run_start(args) +def test_bigchain_run_start_assume_yes_create_default_config(monkeypatch, mock_processes_start, + mock_generate_key_pair, mock_db_init_with_existing_db): + import bigchaindb + from bigchaindb.commands.bigchain import run_start + from bigchaindb import config_utils + + value = {} + expected_config = copy.deepcopy(bigchaindb._config) + expected_config['keypair']['public'] = 'pubkey' + expected_config['keypair']['private'] = 'privkey' + + def mock_write_config(newconfig, filename=None): + value['return'] = newconfig + + monkeypatch.setattr(config_utils, 'write_config', mock_write_config) + monkeypatch.setattr(config_utils, 'file_config', lambda x: config_utils.dict_config(value['return'])) + monkeypatch.setattr('os.path.exists', lambda path: False) + + args = Namespace(config=None, yes=True) + run_start(args) + + assert value['return'] == expected_config + + # TODO Please beware, that if debugging, the "-s" switch for pytest will # interfere with capsys. # See related issue: https://github.com/pytest-dev/pytest/issues/128 From aea99f8ce0737331c52fced222505df50a90c8ca Mon Sep 17 00:00:00 2001 From: vrde Date: Thu, 18 Feb 2016 22:14:58 +0100 Subject: [PATCH 2/4] Fix annoying fixture --- bigchaindb/commands/bigchain.py | 4 ++-- tests/test_commands.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/bigchaindb/commands/bigchain.py b/bigchaindb/commands/bigchain.py index c29e9e94..bcbf1926 100644 --- a/bigchaindb/commands/bigchain.py +++ b/bigchaindb/commands/bigchain.py @@ -12,7 +12,7 @@ from bigchaindb import db from bigchaindb.exceptions import DatabaseAlreadyExists from bigchaindb.commands.utils import base_parser, start from bigchaindb.processes import Processes -from bigchaindb.crypto import generate_key_pair +from bigchaindb import crypto logging.basicConfig(level=logging.INFO) @@ -52,7 +52,7 @@ def run_configure(args, skip_if_exists=False): conf = copy.deepcopy(bigchaindb._config) print('Generating keypair') - conf['keypair']['private'], conf['keypair']['public'] = generate_key_pair() + conf['keypair']['private'], conf['keypair']['public'] = crypto.generate_key_pair() if not args.yes: for key in ('host', 'port', 'name'): diff --git a/tests/test_commands.py b/tests/test_commands.py index 91ec3328..b3c1ae58 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -52,8 +52,7 @@ def mock_rethink_db_drop(monkeypatch): @pytest.fixture def mock_generate_key_pair(monkeypatch): - from bigchaindb import crypto - monkeypatch.setattr(crypto, 'generate_key_pair', lambda: ('privkey', 'pubkey')) + monkeypatch.setattr('bigchaindb.crypto.generate_key_pair', lambda: ('privkey', 'pubkey')) @pytest.fixture From 6ab2b03260d043b570c04c8c486a96983feafcd7 Mon Sep 17 00:00:00 2001 From: vrde Date: Fri, 19 Feb 2016 00:10:44 +0100 Subject: [PATCH 3/4] Use unique db names in tests to allow xdist --- tests/conftest.py | 8 ++++++-- tests/db/conftest.py | 43 +++++++++++++++++++++--------------------- tests/db/test_utils.py | 4 ++-- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a6006ac2..2191f73b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,12 +6,16 @@ Tasks: 2. delete test database after running the tests """ +import os + import pytest +DB_NAME = 'bigchain_test_{}'.format(os.getpid()) + config = { 'database': { - 'name': 'bigchain_test' + 'name': DB_NAME }, 'keypair': { 'private': '3i2FDXp87N9ExXSvWxqBAw9EgzoxxGTQNKbtxmWBpTyL', @@ -30,7 +34,7 @@ def restore_config(request, node_config): config_utils.dict_config(node_config) -@pytest.fixture +@pytest.fixture(scope='module') def node_config(): return config diff --git a/tests/db/conftest.py b/tests/db/conftest.py index a9a85d71..b0545464 100644 --- a/tests/db/conftest.py +++ b/tests/db/conftest.py @@ -13,9 +13,6 @@ from bigchaindb import Bigchain from bigchaindb.db import get_conn -NOOP = None - - @pytest.fixture(autouse=True) def restore_config(request, node_config): from bigchaindb import config_utils @@ -23,58 +20,60 @@ def restore_config(request, node_config): @pytest.fixture(scope='module', autouse=True) -def setup_database(request): +def setup_database(request, node_config): print('Initializing test db') + db_name = node_config['database']['name'] get_conn().repl() try: - r.db_create('bigchain_test').run() + r.db_create(db_name).run() except r.ReqlOpFailedError as e: - if e.message == 'Database `bigchain_test` already exists.': - r.db_drop('bigchain_test').run() - r.db_create('bigchain_test').run() + if e.message == 'Database `{}` already exists.'.format(db_name): + r.db_drop(db_name).run() + r.db_create(db_name).run() else: raise print('Finished initializing test db') # setup tables - r.db('bigchain_test').table_create('bigchain').run() - r.db('bigchain_test').table_create('backlog').run() + r.db(db_name).table_create('bigchain').run() + r.db(db_name).table_create('backlog').run() # create the secondary indexes # to order blocks by timestamp - r.db('bigchain_test').table('bigchain').index_create('block_timestamp', r.row['block']['timestamp']).run() + r.db(db_name).table('bigchain').index_create('block_timestamp', r.row['block']['timestamp']).run() # to order blocks by block number - r.db('bigchain_test').table('bigchain').index_create('block_number', r.row['block']['block_number']).run() + r.db(db_name).table('bigchain').index_create('block_number', r.row['block']['block_number']).run() # to order transactions by timestamp - r.db('bigchain_test').table('backlog').index_create('transaction_timestamp', r.row['transaction']['timestamp']).run() + r.db(db_name).table('backlog').index_create('transaction_timestamp', r.row['transaction']['timestamp']).run() # compound index to read transactions from the backlog per assignee - r.db('bigchain_test').table('backlog')\ + r.db(db_name).table('backlog')\ .index_create('assignee__transaction_timestamp', [r.row['assignee'], r.row['transaction']['timestamp']])\ .run() def fin(): - print('Deleting `bigchain_test` database') + print('Deleting `{}` database'.format(db_name)) get_conn().repl() try: - r.db_drop('bigchain_test').run() + r.db_drop(db_name).run() except r.ReqlOpFailedError as e: - if e.message != 'Database `bigchain_test` does not exist.': + if e.message != 'Database `{}` does not exist.'.format(db_name): raise - print('Finished deleting `bigchain_test`') + print('Finished deleting `{}`'.format(db_name)) request.addfinalizer(fin) @pytest.fixture(scope='function', autouse=True) -def cleanup_tables(request): +def cleanup_tables(request, node_config): + db_name = node_config['database']['name'] def fin(): get_conn().repl() try: - r.db('bigchain_test').table('bigchain').delete().run() - r.db('bigchain_test').table('backlog').delete().run() + r.db(db_name).table('bigchain').delete().run() + r.db(db_name).table('backlog').delete().run() except r.ReqlOpFailedError as e: - if e.message != 'Database `bigchain_test` does not exist.': + if e.message != 'Database `{}` does not exist.'.format(db_name): raise request.addfinalizer(fin) diff --git a/tests/db/test_utils.py b/tests/db/test_utils.py index c8caff60..287fdfcf 100644 --- a/tests/db/test_utils.py +++ b/tests/db/test_utils.py @@ -11,8 +11,8 @@ from .conftest import setup_database as _setup_database # Since we are testing database initialization and database drop, # we need to use the `setup_database` fixture on a function level @pytest.fixture(scope='function', autouse=True) -def setup_database(request): - _setup_database(request) +def setup_database(request, node_config): + _setup_database(request, node_config) def test_init_creates_db_tables_and_indexes(): From 1c96b15174c5e3ad7c96b424782ac9632189c512 Mon Sep 17 00:00:00 2001 From: vrde Date: Fri, 19 Feb 2016 00:11:44 +0100 Subject: [PATCH 4/4] Add xdist to travis --- .travis.yml | 2 +- setup.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bfd602e4..b6ae5153 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,6 @@ install: before_script: rethinkdb --daemon -script: py.test -v --cov=bigchaindb +script: py.test -n8 -v --cov=bigchaindb after_success: codecov diff --git a/setup.py b/setup.py index 901d84c7..e0130711 100644 --- a/setup.py +++ b/setup.py @@ -14,6 +14,7 @@ tests_require = [ 'pylint', 'pytest', 'pytest-cov', + 'pytest-xdist', ] dev_require = [