Refactor backup RPC API test cases

Our RPC API tests definitely need some refactoring. We have 3 different
implementations of such TestCase, each going to it's own direction.
Moreover these test cases aren't really asserting a lot and give us very
little possiblities in writing tests.

This commit initiates refactoring effort with the simplest API - backup.
It is moving the test case to a generic cinder.test.RPCAPITestcase class
and enhances the checks there. Following commits will repeat the efforts
for scheduler and volume RPC APIs.

Change-Id: Ie4760d540e44860d599494d504010b8024ea935e
This commit is contained in:
Michał Dulko 2016-09-27 12:30:58 +02:00
parent 585cf34ffb
commit 85c9826139
2 changed files with 42 additions and 98 deletions

View File

@ -64,7 +64,7 @@ class BackupAPI(rpc.RPCAPI):
volume_id=volume_id)
def delete_backup(self, ctxt, backup):
LOG.debug("delete_backup rpcapi backup_id %s", backup.id)
LOG.debug("delete_backup rpcapi backup_id %s", backup.id)
cctxt = self._get_cctxt(server=backup.host)
cctxt.cast(ctxt, 'delete_backup', backup=backup)

View File

@ -16,122 +16,66 @@
Unit Tests for cinder.backup.rpcapi
"""
import copy
import mock
from cinder.backup import rpcapi as backup_rpcapi
from cinder import context
from cinder import objects
from cinder import test
from cinder.tests.unit.backup import fake_backup
from cinder.tests.unit import fake_constants as fake
class BackupRpcAPITestCase(test.TestCase):
class BackupRPCAPITestCase(test.RPCAPITestCase):
def setUp(self):
super(BackupRpcAPITestCase, self).setUp()
self.context = context.RequestContext(fake.USER_ID, fake.PROJECT_ID)
super(BackupRPCAPITestCase, self).setUp()
self.rpcapi = backup_rpcapi.BackupAPI
self.fake_backup_obj = fake_backup.fake_backup_obj(self.context)
def _test_backup_api(self, method, rpc_method, server=None, fanout=False,
**kwargs):
rpcapi = backup_rpcapi.BackupAPI()
expected_retval = 'foo' if rpc_method == 'call' else None
target = {
"server": server,
"fanout": fanout,
"version": kwargs.pop('version', rpcapi.RPC_API_VERSION)
}
expected_msg = copy.deepcopy(kwargs)
self.fake_args = None
self.fake_kwargs = None
def _fake_prepare_method(*args, **kwds):
for kwd in kwds:
self.assertEqual(target[kwd], kwds[kwd])
return rpcapi.client
def _fake_rpc_method(*args, **kwargs):
self.fake_args = args
self.fake_kwargs = kwargs
if expected_retval:
return expected_retval
with mock.patch.object(rpcapi.client, "prepare") as mock_prepared:
mock_prepared.side_effect = _fake_prepare_method
with mock.patch.object(rpcapi.client, rpc_method) as mock_method:
mock_method.side_effect = _fake_rpc_method
retval = getattr(rpcapi, method)(self.context, **kwargs)
self.assertEqual(expected_retval, retval)
expected_args = [self.context, method, expected_msg]
for arg, expected_arg in zip(self.fake_args, expected_args):
self.assertEqual(expected_arg, arg)
for kwarg, value in self.fake_kwargs.items():
if isinstance(value, objects.Backup):
expected_back = expected_msg[kwarg].obj_to_primitive()
backup = value.obj_to_primitive()
self.assertEqual(expected_back, backup)
else:
self.assertEqual(expected_msg[kwarg], value)
def test_create_backup(self):
self._test_backup_api('create_backup',
rpc_method='cast',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj,
version='2.0')
self._test_rpc_api('create_backup',
rpc_method='cast',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj)
def test_restore_backup(self):
self._test_backup_api('restore_backup',
rpc_method='cast',
server='fake_volume_host',
volume_host='fake_volume_host',
backup=self.fake_backup_obj,
volume_id=fake.VOLUME_ID,
version='2.0')
self._test_rpc_api('restore_backup',
rpc_method='cast',
server='fake_volume_host',
volume_host='fake_volume_host',
backup=self.fake_backup_obj,
volume_id=fake.VOLUME_ID)
def test_delete_backup(self):
self._test_backup_api('delete_backup',
rpc_method='cast',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj,
version='2.0')
self._test_rpc_api('delete_backup',
rpc_method='cast',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj)
def test_export_record(self):
self._test_backup_api('export_record',
rpc_method='call',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj,
version='2.0')
self._test_rpc_api('export_record',
rpc_method='call',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj,
retval={'backup_service': 'fake_backup_driver',
'backup_url': 'http://fake_url'})
def test_import_record(self):
self._test_backup_api('import_record',
rpc_method='cast',
server='fake_volume_host',
host='fake_volume_host',
backup=self.fake_backup_obj,
backup_service='fake_service',
backup_url='fake_url',
backup_hosts=['fake_host1', 'fake_host2'],
version='2.0')
self._test_rpc_api('import_record',
rpc_method='cast',
server='fake_volume_host',
host='fake_volume_host',
backup=self.fake_backup_obj,
backup_service='fake_service',
backup_url='fake_url',
backup_hosts=['fake_host1', 'fake_host2'])
def test_reset_status(self):
self._test_backup_api('reset_status',
rpc_method='cast',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj,
status='error',
version='2.0')
self._test_rpc_api('reset_status',
rpc_method='cast',
server=self.fake_backup_obj.host,
backup=self.fake_backup_obj,
status='error')
def test_check_support_to_force_delete(self):
self._test_backup_api('check_support_to_force_delete',
rpc_method='call',
server='fake_volume_host',
host='fake_volume_host',
version='2.0')
self._test_rpc_api('check_support_to_force_delete',
rpc_method='call',
server='fake_volume_host',
host='fake_volume_host',
retval=True)