Added tests

This commit is contained in:
Rodolphe Marques 2017-01-25 12:36:08 +01:00
parent 69505a366b
commit 391da2cf60
5 changed files with 179 additions and 5 deletions

View File

@ -24,9 +24,11 @@ def set_replicas(connection, *, replicas):
@singledispatch @singledispatch
def add_replicas(connection, replicas): def add_replicas(connection, replicas):
raise NotImplementedError raise NotImplementedError('This command is specific to the '
'MongoDB backend.')
@singledispatch @singledispatch
def remove_replicas(connection, replicas): def remove_replicas(connection, replicas):
raise NotImplementedError raise NotImplementedError('This command is specific to the '
'MongoDB backend.')

View File

@ -277,7 +277,7 @@ def run_add_replicas(args):
try: try:
add_replicas(conn, args.replicas) add_replicas(conn, args.replicas)
except DatabaseOpFailedError as e: except (DatabaseOpFailedError, NotImplementedError) as e:
logger.warn(e) logger.warn(e)
else: else:
logger.info('Added {} to the replicaset.'.format(args.replicas)) logger.info('Added {} to the replicaset.'.format(args.replicas))
@ -290,7 +290,7 @@ def run_remove_replicas(args):
try: try:
remove_replicas(conn, args.replicas) remove_replicas(conn, args.replicas)
except DatabaseOpFailedError as e: except (DatabaseOpFailedError, NotImplementedError) as e:
logger.warn(e) logger.warn(e)
else: else:
logger.info('Removed {} from the replicaset.'.format(args.replicas)) logger.info('Removed {} from the replicaset.'.format(args.replicas))

View File

@ -0,0 +1,118 @@
"""Tests for the :mod:`bigchaindb.backend.mongodb.admin` module."""
import copy
from unittest import mock
import pytest
from pymongo.database import Database
from pymongo.errors import OperationFailure
@pytest.fixture
def mock_replicaset_config():
return {
'config': {
'_id': 'bigchain-rs',
'members': [
{
'_id': 0,
'arbiterOnly': False,
'buildIndexes': True,
'hidden': False,
'host': 'localhost:27017',
'priority': 1.0,
'slaveDelay': 0,
'tags': {},
'votes': 1
}
],
'version': 1
}
}
def test_add_replicas(mock_replicaset_config):
from bigchaindb.backend import connect
from bigchaindb.backend.admin import add_replicas
connection = connect()
# force the connection object to setup a connection to the database
# before we mock `Database.command`
connection.conn
expected_config = copy.deepcopy(mock_replicaset_config)
expected_config['config']['members'] += [
{'_id': 1, 'host': 'localhost:27018'},
{'_id': 2, 'host': 'localhost:27019'}
]
expected_config['config']['version'] += 1
with mock.patch.object(Database, 'command') as mock_command:
mock_command.return_value = mock_replicaset_config
add_replicas(connection, ['localhost:27018', 'localhost:27019'])
mock_command.assert_called_with('replSetReconfig',
expected_config['config'])
def test_add_replicas_raises(mock_replicaset_config):
from bigchaindb.backend import connect
from bigchaindb.backend.admin import add_replicas
from bigchaindb.backend.exceptions import DatabaseOpFailedError
connection = connect()
# force the connection object to setup a connection to the database
# before we mock `Database.command`
connection.conn
with mock.patch.object(Database, 'command') as mock_command:
mock_command.side_effect = [
mock_replicaset_config,
OperationFailure(error=1, details={'errmsg': ''})
]
with pytest.raises(DatabaseOpFailedError):
add_replicas(connection, ['localhost:27018'])
def test_remove_replicas(mock_replicaset_config):
from bigchaindb.backend import connect
from bigchaindb.backend.admin import remove_replicas
connection = connect()
# force the connection object to setup a connection to the database
# before we mock `Database.command`
connection.conn
expected_config = copy.deepcopy(mock_replicaset_config)
expected_config['config']['version'] += 1
# add some hosts to the configuration to remove
mock_replicaset_config['config']['members'] += [
{'_id': 1, 'host': 'localhost:27018'},
{'_id': 2, 'host': 'localhost:27019'}
]
with mock.patch.object(Database, 'command') as mock_command:
mock_command.return_value = mock_replicaset_config
remove_replicas(connection, ['localhost:27018', 'localhost:27019'])
mock_command.assert_called_with('replSetReconfig',
expected_config['config'])
def test_remove_replicas_raises(mock_replicaset_config):
from bigchaindb.backend import connect
from bigchaindb.backend.admin import remove_replicas
from bigchaindb.backend.exceptions import DatabaseOpFailedError
connection = connect()
# force the connection object to setup a connection to the database
# before we mock `Database.command`
connection.conn
with mock.patch.object(Database, 'command') as mock_command:
mock_command.side_effect = [
mock_replicaset_config,
OperationFailure(error=1, details={'errmsg': ''})
]
with pytest.raises(DatabaseOpFailedError):
remove_replicas(connection, ['localhost:27018'])

View File

@ -100,6 +100,8 @@ def test_init_database(mock_create_database, mock_create_tables,
('reconfigure', {'table': None, 'shards': None, 'replicas': None}), ('reconfigure', {'table': None, 'shards': None, 'replicas': None}),
('set_shards', {'shards': None}), ('set_shards', {'shards': None}),
('set_replicas', {'replicas': None}), ('set_replicas', {'replicas': None}),
('add_replicas', {'replicas': None}),
('remove_replicas', {'replicas': None}),
)) ))
def test_admin(admin_func_name, kwargs): def test_admin(admin_func_name, kwargs):
from bigchaindb.backend import admin from bigchaindb.backend import admin

View File

@ -1,6 +1,6 @@
import json import json
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from argparse import Namespace from argparse import Namespace, ArgumentTypeError
import copy import copy
import pytest import pytest
@ -376,3 +376,55 @@ def test_calling_main(start_mock, base_parser_mock, parse_args_mock,
'distributed equally to all ' 'distributed equally to all '
'the processes') 'the processes')
assert start_mock.called is True assert start_mock.called is True
@patch('bigchaindb.backend.admin.add_replicas')
def test_run_add_replicas(mock_add_replicas):
from bigchaindb.commands.bigchain import run_add_replicas
from bigchaindb.backend.exceptions import DatabaseOpFailedError
args = Namespace(config=None, replicas=['localhost:27017'])
# test add_replicas no raises
mock_add_replicas.return_value = None
assert run_add_replicas(args) is None
# test add_replicas with `DatabaseOpFailedError`
mock_add_replicas.side_effect = DatabaseOpFailedError()
assert run_add_replicas(args) is None
# test add_replicas with `NotImplementedError`
mock_add_replicas.side_effect = NotImplementedError()
assert run_add_replicas(args) is None
@patch('bigchaindb.backend.admin.remove_replicas')
def test_run_remove_replicas(mock_remove_replicas):
from bigchaindb.commands.bigchain import run_remove_replicas
from bigchaindb.backend.exceptions import DatabaseOpFailedError
args = Namespace(config=None, replicas=['localhost:27017'])
# test add_replicas no raises
mock_remove_replicas.return_value = None
assert run_remove_replicas(args) is None
# test add_replicas with `DatabaseOpFailedError`
mock_remove_replicas.side_effect = DatabaseOpFailedError()
assert run_remove_replicas(args) is None
# test add_replicas with `NotImplementedError`
mock_remove_replicas.side_effect = NotImplementedError()
assert run_remove_replicas(args) is None
def test_mongodb_host_type():
from bigchaindb.commands.utils import mongodb_host
# bad port provided
with pytest.raises(ArgumentTypeError):
mongodb_host('localhost:11111111111')
# no port information provided
with pytest.raises(ArgumentTypeError):
mongodb_host('localhost')