mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Merge pull request #32 from bigchaindb/fix-exception
Fix exception when running `start`. Close #35
This commit is contained in:
commit
a685c15043
@ -8,6 +8,7 @@ import argparse
|
||||
import bigchaindb
|
||||
import bigchaindb.config_utils
|
||||
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
|
||||
@ -21,6 +22,9 @@ def run_show_config(args):
|
||||
"""Show the current configuration"""
|
||||
from pprint import pprint
|
||||
|
||||
# TODO Proposal: remove the "hidden" configuration. Only show config. If
|
||||
# the system needs to be configured, then display information on how to
|
||||
# configure the system.
|
||||
bigchaindb.config_utils.file_config(args.config)
|
||||
pprint(bigchaindb.config)
|
||||
|
||||
@ -32,13 +36,12 @@ def run_configure(args, skip_if_exists=False):
|
||||
skip_if_exists (bool): skip the function if a conf file already exists
|
||||
"""
|
||||
config_path = args.config or bigchaindb.config_utils.CONFIG_DEFAULT_PATH
|
||||
proceed = args.yes
|
||||
config_file_exists = os.path.exists(config_path)
|
||||
|
||||
if config_file_exists and skip_if_exists:
|
||||
return
|
||||
|
||||
if config_file_exists and not proceed:
|
||||
if config_file_exists and not args.yes:
|
||||
want = input('Config file `{}` exists, do you want to override it? '
|
||||
'(cannot be undone) [y/n]: '.format(config_path))
|
||||
if not want:
|
||||
@ -60,7 +63,14 @@ def run_configure(args, skip_if_exists=False):
|
||||
def run_init(args):
|
||||
"""Initialize the database"""
|
||||
bigchaindb.config_utils.file_config(args.config)
|
||||
db.init()
|
||||
# TODO Provide mechanism to:
|
||||
# 1. prompt the user to inquire whether they wish to drop the db
|
||||
# 2. force the init, (e.g., via -f flag)
|
||||
try:
|
||||
db.init()
|
||||
except DatabaseAlreadyExists:
|
||||
print('The database already exists.')
|
||||
print('If you wish to re-initialize it, first drop it.')
|
||||
|
||||
|
||||
def run_drop(args):
|
||||
@ -75,11 +85,11 @@ def run_start(args):
|
||||
bigchaindb.config_utils.file_config(args.config)
|
||||
try:
|
||||
db.init()
|
||||
except db.DatabaseAlreadyExistsException:
|
||||
except DatabaseAlreadyExists:
|
||||
pass
|
||||
p = Processes()
|
||||
processes = Processes()
|
||||
logger.info('Start bigchaindb main process')
|
||||
p.start()
|
||||
processes.start()
|
||||
|
||||
|
||||
def main():
|
||||
|
121
tests/test_commands.py
Normal file
121
tests/test_commands.py
Normal file
@ -0,0 +1,121 @@
|
||||
from argparse import Namespace
|
||||
from pprint import pprint
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_run_configure(monkeypatch):
|
||||
from bigchaindb.commands import bigchain
|
||||
monkeypatch.setattr(bigchain, 'run_configure', lambda *args, **kwargs: None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_file_config(monkeypatch):
|
||||
from bigchaindb import config_utils
|
||||
monkeypatch.setattr(config_utils, 'file_config', lambda *args: None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_write_config(monkeypatch):
|
||||
from bigchaindb import config_utils
|
||||
monkeypatch.setattr(config_utils, 'write_config', lambda *args: None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_db_init_with_existing_db(monkeypatch):
|
||||
from bigchaindb import db
|
||||
from bigchaindb.exceptions import DatabaseAlreadyExists
|
||||
|
||||
def mockreturn():
|
||||
raise DatabaseAlreadyExists
|
||||
|
||||
monkeypatch.setattr(db, 'init', mockreturn)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_processes_start(monkeypatch):
|
||||
from bigchaindb.processes import Processes
|
||||
monkeypatch.setattr(Processes, 'start', lambda *args: None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_rethink_db_drop(monkeypatch):
|
||||
def mockreturn(dbname):
|
||||
class MockDropped(object):
|
||||
def run(self, conn):
|
||||
return
|
||||
return MockDropped()
|
||||
monkeypatch.setattr('rethinkdb.db_drop', mockreturn)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_generate_key_pair(monkeypatch):
|
||||
from bigchaindb import crypto
|
||||
monkeypatch.setattr(crypto, 'generate_key_pair', lambda: ('privkey', 'pubkey'))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_bigchaindb_backup_config(monkeypatch):
|
||||
config = {
|
||||
'keypair': {},
|
||||
'database': {'host': 'host', 'port': 12345, 'name': 'adbname'},
|
||||
}
|
||||
monkeypatch.setattr('bigchaindb._config', config)
|
||||
|
||||
|
||||
def test_bigchain_run_start(mock_run_configure, mock_file_config,
|
||||
mock_processes_start, mock_db_init_with_existing_db):
|
||||
from bigchaindb.commands.bigchain import run_start
|
||||
args = Namespace(config=None, yes=True)
|
||||
run_start(args)
|
||||
|
||||
|
||||
# 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
|
||||
def test_bigchain_show_config(capsys, mock_file_config):
|
||||
from bigchaindb import config
|
||||
from bigchaindb.commands.bigchain import run_show_config
|
||||
args = Namespace(config=None)
|
||||
_, _ = capsys.readouterr()
|
||||
run_show_config(args)
|
||||
output_config, _ = capsys.readouterr()
|
||||
pprint(config)
|
||||
expected_outout_config, _ = capsys.readouterr()
|
||||
assert output_config == expected_outout_config
|
||||
|
||||
|
||||
def test_bigchain_run_init_when_db_exists(mock_file_config, mock_db_init_with_existing_db):
|
||||
from bigchaindb.commands.bigchain import run_init
|
||||
args = Namespace(config=None)
|
||||
run_init(args)
|
||||
|
||||
|
||||
def test_drop_existing_db(mock_file_config, mock_rethink_db_drop):
|
||||
from bigchaindb.commands.bigchain import run_drop
|
||||
args = Namespace(config=None, yes=True)
|
||||
run_drop(args)
|
||||
|
||||
|
||||
def test_run_configure_when_config_exists_and_skipping(monkeypatch):
|
||||
from bigchaindb.commands.bigchain import run_configure
|
||||
monkeypatch.setattr('os.path.exists', lambda path: True)
|
||||
args = Namespace(config='foo', yes=True)
|
||||
return_value = run_configure(args, skip_if_exists=True)
|
||||
assert return_value is None
|
||||
|
||||
|
||||
# TODO Beware if you are putting breakpoints in there, and using the '-s'
|
||||
# switch with pytest. It will just hang. Seems related to the monkeypatching of
|
||||
# input.
|
||||
def test_run_configure_when_config_does_not_exist(monkeypatch,
|
||||
mock_write_config,
|
||||
mock_generate_key_pair,
|
||||
mock_bigchaindb_backup_config):
|
||||
from bigchaindb.commands.bigchain import run_configure
|
||||
monkeypatch.setattr('os.path.exists', lambda path: False)
|
||||
monkeypatch.setattr('builtins.input', lambda question: '\n')
|
||||
args = Namespace(config='foo', yes=True)
|
||||
return_value = run_configure(args)
|
||||
assert return_value is None
|
Loading…
x
Reference in New Issue
Block a user