Add dbapi functions

1. add get_nodes_by_chassis
2. add chassis_list
3. add tests for new dbapi function
4. remove object created in setUp function in test

We need chassis_list when we get /chassis,
and we need get_nodes_by_chassis when we get /chassis/chassis_id/nodes.

I remove object created in setUp function because the object created in
setUp function will always exist in the db util test is over, so the
result of test_chassis_list will be effected by the object created in
setUp.

Change-Id: Ic2a743b764761935f847de0e0cdb0a2ebcec2486
This commit is contained in:
jiangwt100 2013-07-05 01:31:43 +08:00
parent c343d15707
commit 9cb58b4109
5 changed files with 90 additions and 20 deletions

View File

@ -120,6 +120,14 @@ class Connection(object):
:returns: A node.
"""
@abc.abstractmethod
def get_nodes_by_chassis(self, chassis):
"""List all the nodes for a given chassis.
:param chassis: The id or uuid of a chassis.
:returns: A list of nodes.
"""
@abc.abstractmethod
def destroy_node(self, node):
"""Destroy a node and all associated interfaces.
@ -208,6 +216,10 @@ class Connection(object):
:returns: A chassis.
"""
@abc.abstractmethod
def get_chassis_list(self):
"""Return a list of chassis UUIDs."""
@abc.abstractmethod
def update_chassis(self, chassis, values):
"""Update properties of an chassis.

View File

@ -94,12 +94,21 @@ def add_port_filter(query, value):
def add_port_filter_by_node(query, value):
if utils.is_int_like(value):
return query.filter_by(node_id=value)
else:
query = query.join(models.Node,
models.Port.node_id == models.Node.id)
return query.filter(models.Node.uuid == value)
if utils.is_int_like(value):
return query.filter_by(node_id=value)
else:
query = query.join(models.Node,
models.Port.node_id == models.Node.id)
return query.filter(models.Node.uuid == value)
def add_node_filter_by_chassis(query, value):
if utils.is_int_like(value):
return query.filter_by(chassis_id=value)
else:
query = query.join(models.Chassis,
models.Node.chassis_id == models.Chassis.id)
return query.filter(models.Chassis.uuid == value)
class Connection(api.Connection):
@ -116,6 +125,13 @@ class Connection(api.Connection):
query = model_query(models.Node.uuid)
return [i[0] for i in query.all()]
@objects.objectify(objects.Node)
def get_nodes_by_chassis(self, chassis):
query = model_query(models.Node)
query = add_node_filter_by_chassis(query, chassis)
return query.all()
@objects.objectify(objects.Node)
def get_associated_nodes(self):
pass
@ -324,6 +340,10 @@ class Connection(api.Connection):
except NoResultFound:
raise exception.ChassisNotFound(chassis=chassis)
def get_chassis_list(self):
query = model_query(models.Chassis.uuid)
return [i[0] for i in query.all()]
@objects.objectify(objects.Chassis)
def create_chassis(self, values):
chassis = models.Chassis()

View File

@ -29,28 +29,45 @@ class DbChassisTestCase(base.DbTestCase):
def setUp(self):
super(DbChassisTestCase, self).setUp()
self.dbapi = dbapi.get_instance()
self.ch = utils.get_test_chassis()
self.ch = self.dbapi.create_chassis(self.ch)
def _create_test_chassis(self, **kwargs):
ch = utils.get_test_chassis(**kwargs)
self.dbapi.create_chassis(ch)
return ch
def test_get_chassis_list(self):
uuids = []
for i in xrange(1, 6):
n = utils.get_test_chassis(id=i, uuid=uuidutils.generate_uuid())
self.dbapi.create_chassis(n)
uuids.append(unicode(n['uuid']))
res = self.dbapi.get_chassis_list()
uuids.sort()
res.sort()
self.assertEqual(uuids, res)
def test_get_chassis_by_id(self):
chassis = self.dbapi.get_chassis('42')
self.assertEqual(chassis['id'], self.ch['id'])
self.assertEqual(chassis['uuid'], self.ch['uuid'])
ch = self._create_test_chassis()
chassis = self.dbapi.get_chassis(ch['id'])
self.assertEqual(chassis['uuid'], ch['uuid'])
def test_get_chassis_by_uuid(self):
chassis = self.dbapi.get_chassis('42')
self.assertEqual(chassis['id'], self.ch['id'])
self.assertEqual(chassis['uuid'], self.ch['uuid'])
ch = self._create_test_chassis()
chassis = self.dbapi.get_chassis(ch['uuid'])
self.assertEqual(chassis['id'], ch['id'])
def test_get_chassis_that_does_not_exist(self):
self.assertRaises(exception.ChassisNotFound,
self.dbapi.get_chassis, 666)
def test_update_chassis(self):
ch = self._create_test_chassis()
new_uuid = uuidutils.generate_uuid()
self.ch['uuid'] = new_uuid
res = self.dbapi.update_chassis(self.ch['id'], {'uuid': new_uuid})
ch['uuid'] = new_uuid
res = self.dbapi.update_chassis(ch['id'], {'uuid': new_uuid})
self.assertEqual(res['uuid'], new_uuid)
@ -61,10 +78,11 @@ class DbChassisTestCase(base.DbTestCase):
self.dbapi.update_chassis, 666, {'uuid': new_uuid})
def test_destroy_chassis(self):
self.dbapi.destroy_chassis(self.ch['id'])
ch = self._create_test_chassis()
self.dbapi.destroy_chassis(ch['id'])
self.assertRaises(exception.ChassisNotFound,
self.dbapi.get_chassis, self.ch['id'])
self.dbapi.get_chassis, ch['id'])
def test_destroy_chassis_that_does_not_exist(self):
self.assertRaises(exception.ChassisNotFound,

View File

@ -47,6 +47,27 @@ class DbNodeTestCase(base.DbTestCase):
def test_create_node(self):
self._create_test_node()
def test_get_nodes_by_chassis_id(self):
ch = utils.get_test_chassis()
ch = self.dbapi.create_chassis(ch)
n = self._create_test_node()
nodes = self.dbapi.get_nodes_by_chassis(ch['id'])
self.assertEqual(n['uuid'], nodes[0]['uuid'])
def test_get_nodes_by_chassis_uuid(self):
ch = utils.get_test_chassis()
ch = self.dbapi.create_chassis(ch)
n = self._create_test_node()
nodes = self.dbapi.get_nodes_by_chassis(ch['uuid'])
self.assertEqual(n['id'], nodes[0]['id'])
def test_get_nodes_by_chassis_that_does_not_exist(self):
nodes = self.dbapi.get_nodes_by_chassis(33)
self.assertEqual(0, len(nodes))
nodes = self.dbapi.get_nodes_by_chassis("1231231-123123-123123")
self.assertEqual(0, len(nodes))
def test_get_node_by_id(self):
n = self._create_test_node()
res = self.dbapi.get_node(n['id'])
@ -54,7 +75,6 @@ class DbNodeTestCase(base.DbTestCase):
def test_get_node_by_uuid(self):
n = self._create_test_node()
res = self.dbapi.get_node(n['uuid'])
self.assertEqual(n['id'], res['id'])

View File

@ -70,7 +70,7 @@ def get_test_node(**kw):
node = {
'id': kw.get('id', 123),
'uuid': kw.get('uuid', '1be26c0b-03f2-4d2e-ae87-c02d7f33c123'),
'chassis_id': 5,
'chassis_id': 42,
'task_start': None,
'task_state': kw.get('task_state', 'NOSTATE'),
'instance_uuid': kw.get('instance_uuid',