From 2e398f606f666bd9fa4f6285d46bf71cf635375f Mon Sep 17 00:00:00 2001 From: Sylvain Bellemare Date: Thu, 2 Mar 2017 14:36:52 +0100 Subject: [PATCH] Make some improvements to command line messages and error handling --- bigchaindb/commands/bigchain.py | 16 +++++++-------- tests/commands/rethinkdb/test_commands.py | 20 ++++++++---------- tests/commands/test_commands.py | 25 +++++++++++++++-------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/bigchaindb/commands/bigchain.py b/bigchaindb/commands/bigchain.py index 69124661..6b8bdb07 100644 --- a/bigchaindb/commands/bigchain.py +++ b/bigchaindb/commands/bigchain.py @@ -126,7 +126,6 @@ def run_configure(args, skip_if_exists=False): def run_export_my_pubkey(args): """Export this node's public key to standard output """ - print('bigchaindb args = {}'.format(args)) bigchaindb.config_utils.autoconfigure(filename=args.config, force=True) pubkey = bigchaindb.config['keypair']['public'] if pubkey is not None: @@ -145,9 +144,8 @@ def _run_init(): schema.init_database(connection=b.connection) - print('Create genesis block.') b.create_genesis_block() - print('Done, have fun!') + logger.info('Genesis block created.') def run_init(args): @@ -180,7 +178,7 @@ def run_drop(args): def run_start(args): """Start the processes to run the node""" - print('BigchainDB Version {}'.format(bigchaindb.__version__)) + logger.info('BigchainDB Version %s', bigchaindb.__version__) bigchaindb.config_utils.autoconfigure(filename=args.config, force=True) if args.allow_temp_keypair: @@ -231,7 +229,7 @@ def _run_load(tx_left, stats): def run_load(args): bigchaindb.config_utils.autoconfigure(filename=args.config, force=True) - print('Starting %s processes', args.multiprocess) + logger.info('Starting %s processes', args.multiprocess) stats = logstats.Logstats() logstats.thread.start(stats) @@ -250,7 +248,7 @@ def run_set_shards(args): try: set_shards(conn, shards=args.num_shards) except OperationError as e: - print(e) + sys.exit(str(e)) def run_set_replicas(args): @@ -258,7 +256,7 @@ def run_set_replicas(args): try: set_replicas(conn, replicas=args.num_replicas) except OperationError as e: - print(e) + sys.exit(str(e)) def run_add_replicas(args): @@ -269,7 +267,7 @@ def run_add_replicas(args): try: add_replicas(conn, args.replicas) except (OperationError, NotImplementedError) as e: - print(e) + sys.exit(str(e)) else: print('Added {} to the replicaset.'.format(args.replicas)) @@ -282,7 +280,7 @@ def run_remove_replicas(args): try: remove_replicas(conn, args.replicas) except (OperationError, NotImplementedError) as e: - print(e) + sys.exit(str(e)) else: print('Removed {} from the replicaset.'.format(args.replicas)) diff --git a/tests/commands/rethinkdb/test_commands.py b/tests/commands/rethinkdb/test_commands.py index 5fc0379e..5208587e 100644 --- a/tests/commands/rethinkdb/test_commands.py +++ b/tests/commands/rethinkdb/test_commands.py @@ -59,7 +59,7 @@ def test_set_shards(mock_reconfigure, monkeypatch, b): mock_reconfigure.assert_called_with(replicas=3, shards=3, dry_run=False) -def test_set_shards_raises_exception(monkeypatch, b, capsys): +def test_set_shards_raises_exception(monkeypatch, b): from bigchaindb.commands.bigchain import run_set_shards # test that we are correctly catching the exception @@ -73,11 +73,9 @@ def test_set_shards_raises_exception(monkeypatch, b, capsys): monkeypatch.setattr(rethinkdb.ast.Table, 'reconfigure', mock_raise) args = Namespace(num_shards=3) - run_set_shards(args) - - out, err = capsys.readouterr() - assert out[:-1] == 'Failed to reconfigure tables.' - assert not err + with pytest.raises(SystemExit) as exc: + run_set_shards(args) + assert exc.value.args == ('Failed to reconfigure tables.',) @patch('rethinkdb.ast.Table.reconfigure') @@ -104,7 +102,7 @@ def test_set_replicas(mock_reconfigure, monkeypatch, b): mock_reconfigure.assert_called_with(replicas=2, shards=3, dry_run=False) -def test_set_replicas_raises_exception(monkeypatch, b, capsys): +def test_set_replicas_raises_exception(monkeypatch, b): from bigchaindb.commands.bigchain import run_set_replicas # test that we are correctly catching the exception @@ -118,8 +116,6 @@ def test_set_replicas_raises_exception(monkeypatch, b, capsys): monkeypatch.setattr(rethinkdb.ast.Table, 'reconfigure', mock_raise) args = Namespace(num_replicas=2) - run_set_replicas(args) - - out, err = capsys.readouterr() - assert out[:-1] == 'Failed to reconfigure tables.' - assert not err + with pytest.raises(SystemExit) as exc: + run_set_replicas(args) + assert exc.value.args == ('Failed to reconfigure tables.',) diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py index 9d071131..991f4961 100644 --- a/tests/commands/test_commands.py +++ b/tests/commands/test_commands.py @@ -135,7 +135,6 @@ def test_bigchain_export_my_pubkey_when_pubkey_set(capsys, monkeypatch): lines = out.splitlines() assert config['keypair']['public'] in lines assert 'Charlie_Bucket' in lines - assert 'bigchaindb args = {}'.format(args) in lines def test_bigchain_export_my_pubkey_when_pubkey_not_set(monkeypatch): @@ -455,14 +454,18 @@ def test_run_add_replicas(mock_add_replicas): mock_add_replicas.reset_mock() # test add_replicas with `OperationError` - mock_add_replicas.side_effect = OperationError() - assert run_add_replicas(args) is None + mock_add_replicas.side_effect = OperationError('err') + with pytest.raises(SystemExit) as exc: + run_add_replicas(args) + assert exc.value.args == ('err',) assert mock_add_replicas.call_count == 1 mock_add_replicas.reset_mock() # test add_replicas with `NotImplementedError` - mock_add_replicas.side_effect = NotImplementedError() - assert run_add_replicas(args) is None + mock_add_replicas.side_effect = NotImplementedError('err') + with pytest.raises(SystemExit) as exc: + run_add_replicas(args) + assert exc.value.args == ('err',) assert mock_add_replicas.call_count == 1 mock_add_replicas.reset_mock() @@ -482,14 +485,18 @@ def test_run_remove_replicas(mock_remove_replicas): mock_remove_replicas.reset_mock() # test add_replicas with `OperationError` - mock_remove_replicas.side_effect = OperationError() - assert run_remove_replicas(args) is None + mock_remove_replicas.side_effect = OperationError('err') + with pytest.raises(SystemExit) as exc: + run_remove_replicas(args) + assert exc.value.args == ('err',) assert mock_remove_replicas.call_count == 1 mock_remove_replicas.reset_mock() # test add_replicas with `NotImplementedError` - mock_remove_replicas.side_effect = NotImplementedError() - assert run_remove_replicas(args) is None + mock_remove_replicas.side_effect = NotImplementedError('err') + with pytest.raises(SystemExit) as exc: + run_remove_replicas(args) + assert exc.value.args == ('err',) assert mock_remove_replicas.call_count == 1 mock_remove_replicas.reset_mock()