Merge pull request #64 from bigchaindb/feat/58/non-interactive-first-start

Allow non interactive first start
This commit is contained in:
Alberto Granzotto 2016-02-21 22:38:11 +01:00
commit 272a899c9e
2 changed files with 36 additions and 9 deletions

View File

@ -4,6 +4,7 @@
import os
import logging
import argparse
import copy
import bigchaindb
import bigchaindb.config_utils
@ -11,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)
@ -48,13 +49,15 @@ def run_configure(args, skip_if_exists=False):
return
# Patch the default configuration with the new values
conf = bigchaindb._config
print('Generating keypair')
conf['keypair']['private'], conf['keypair']['public'] = generate_key_pair()
conf = copy.deepcopy(bigchaindb._config)
for key in ('host', 'port', 'name'):
val = conf['database'][key]
conf['database'][key] = input('Database {}? (default `{}`): '.format(key, val)) or val
print('Generating keypair')
conf['keypair']['private'], conf['keypair']['public'] = crypto.generate_key_pair()
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!')

View File

@ -1,5 +1,6 @@
from argparse import Namespace
from pprint import pprint
import copy
import pytest
@ -51,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
@ -71,6 +71,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