Fix unit test for replication controller

Replication Controller is missing unit test
for getting the replication controller object
by name. This patch adds the needed test. This
patch also adds the missing tenant filter that
was missing initially.

Change-Id: I28bb71f9fba4a335f67324d264bea06d0313d58d
Closes-Bug: #1488295
This commit is contained in:
Vilobh Meshram 2015-08-24 18:59:36 -07:00
parent fb168ffb47
commit afa4b49993
5 changed files with 20 additions and 8 deletions

View File

@ -638,9 +638,10 @@ class Connection(object):
"""
@abc.abstractmethod
def get_rc_by_name(self, rc_name):
def get_rc_by_name(self, context, rc_name):
"""Return a ReplicationController.
:param context: The security context
:param rc_name: The name of a ReplicationController.
:returns: A ReplicationController.
"""

View File

@ -894,9 +894,10 @@ class Connection(api.Connection):
except NoResultFound:
raise exception.ReplicationControllerNotFound(bay=bay_uuid)
def get_rc_by_name(self, rc_name):
query = model_query(models.ReplicationController).filter_by(
name=rc_name)
def get_rc_by_name(self, context, rc_name):
query = model_query(models.ReplicationController)
query = self._add_tenant_filters(context, query)
query = query.filter_by(name=rc_name)
try:
return query.one()
except MultipleResultsFound:

View File

@ -95,7 +95,7 @@ class ReplicationController(base.MagnumPersistentObject, base.MagnumObject,
:param context: Security context
:returns: a :class:`ReplicationController` object.
"""
db_rc = cls.dbapi.get_rc_by_name(name)
db_rc = cls.dbapi.get_rc_by_name(context, name)
rc = ReplicationController._from_db_object(cls(context), db_rc)
return rc

View File

@ -49,7 +49,7 @@ class DbRCTestCase(base.DbTestCase):
self.assertEqual(self.rc.uuid, rc.uuid)
def test_get_rc_by_name(self):
res = self.dbapi.get_rc_by_name(self.rc.name)
res = self.dbapi.get_rc_by_name(self.context, self.rc.name)
self.assertEqual(self.rc.name, res.name)
self.assertEqual(self.rc.uuid, res.uuid)
@ -57,11 +57,11 @@ class DbRCTestCase(base.DbTestCase):
utils.create_test_rc(bay_uuid=self.bay.uuid,
uuid=magnum_utils.generate_uuid())
self.assertRaises(exception.Conflict, self.dbapi.get_rc_by_name,
self.rc.name)
self.context, self.rc.name)
def test_get_rc_by_name_not_found(self):
self.assertRaises(exception.ReplicationControllerNotFound,
self.dbapi.get_rc_by_name,
self.dbapi.get_rc_by_name, self.context,
'not_found')
def test_get_rc_that_does_not_exist(self):

View File

@ -48,6 +48,16 @@ class TestReplicationControllerObject(base.DbTestCase):
mock_get_rc.assert_called_once_with(self.context, uuid)
self.assertEqual(self.context, rc._context)
def test_get_by_name(self):
name = self.fake_rc['name']
with mock.patch.object(self.dbapi, 'get_rc_by_name',
autospec=True) as mock_get_rc:
mock_get_rc.return_value = self.fake_rc
rc = objects.ReplicationController.get_by_name(self.context,
name)
mock_get_rc.assert_called_once_with(self.context, name)
self.assertEqual(self.context, rc._context)
def test_list(self):
with mock.patch.object(self.dbapi, 'get_rc_list',
autospec=True) as mock_get_list: