Add tests for Pod and Service on dbapi

Did the following fix/modification to make the tests pass:
* Raise an exception on deleting non-existent pod or service.
* Rename the following methods:
  get_service_by_bay_uuid -> get_services_by_bay_uuid (db)
  get_by_bay_uuid -> list_by_bay_uuid (objects)

Change-Id: I7cbeb119550acecdbf0697df19172a8285eabca8
This commit is contained in:
Hongbin Lu 2015-01-03 21:09:45 +00:00
parent 6f3ed11181
commit e54428845b
7 changed files with 448 additions and 9 deletions

View File

@ -394,6 +394,14 @@ class Connection(object):
:returns: A pod.
"""
@abc.abstractmethod
def get_pods_by_bay_uuid(self, bay_uuid):
"""List all the pods for a given bay.
:param bay_uuid: The uuid of a bay.
:returns: A list of pods.
"""
@abc.abstractmethod
def destroy_pod(self, pod_id):
"""Destroy a pod and all associated interfaces.
@ -467,6 +475,14 @@ class Connection(object):
:returns: A service.
"""
@abc.abstractmethod
def get_services_by_bay_uuid(self, bay_uuid):
"""List all the services for a given bay.
:param bay_uuid: The uuid of a bay.
:returns: A list of services.
"""
@abc.abstractmethod
def destroy_service(self, service_id):
"""Destroy a service and all associated interfaces.

View File

@ -174,6 +174,7 @@ class Connection(api.Connection):
raise exception.InstanceAssociated(
instance_uuid=values['instance_uuid'],
bay=values['uuid'])
raise exception.BayAlreadyExists(uuid=values['uuid'])
return bay
@ -696,7 +697,9 @@ class Connection(api.Connection):
with session.begin():
query = model_query(models.Pod, session=session)
query = add_identity_filter(query, pod_id)
query.delete()
count = query.delete()
if count != 1:
raise exception.PodNotFound(pod_id)
def update_pod(self, pod_id, values):
# NOTE(dtantsur): this can lead to very strange errors
@ -799,7 +802,7 @@ class Connection(api.Connection):
except NoResultFound:
raise exception.ServiceNotFound(service=service_uuid)
def get_service_by_bay_uuid(self, bay_uuid):
def get_services_by_bay_uuid(self, bay_uuid):
query = model_query(models.Service).filter_by(bay_uuid=bay_uuid)
try:
return query.all()
@ -811,7 +814,9 @@ class Connection(api.Connection):
with session.begin():
query = model_query(models.Service, session=session)
query = add_identity_filter(query, service_id)
query.delete()
count = query.delete()
if count != 1:
raise exception.ServiceNotFound(service_id)
def update_service(self, service_id, values):
# NOTE(dtantsur): this can lead to very strange errors

View File

@ -105,8 +105,8 @@ class Pod(base.MagnumObject):
return pod
@base.remotable_classmethod
def get_by_bay_uuid(cls, context, bay_uuid):
"""Find a pods based on bay uuid and return a :class:`Pod` object.
def list_by_bay_uuid(cls, context, bay_uuid):
"""Return a list of :class:`Pod` objects associated with a given bay.
:param bay_uuid: the uuid of a bay.
:param context: Security context

View File

@ -94,14 +94,14 @@ class Service(base.MagnumObject):
return service
@base.remotable_classmethod
def get_by_bay_uuid(cls, context, bay_uuid):
"""Find a service based on bay uuid and return a :class:`Pod` object.
def list_by_bay_uuid(cls, context, bay_uuid):
"""Return a list of :class:`Service` objects associated with a given bay.
:param bay_uuid: the uuid of a bay.
:param context: Security context
:returns: a list of class:`Service` object.
"""
db_services = cls.dbapi.get_service_by_bay_uuid(bay_uuid)
db_services = cls.dbapi.get_services_by_bay_uuid(bay_uuid)
return Service._from_db_object_list(db_services, cls, context)
@base.remotable_classmethod

View File

@ -39,7 +39,7 @@ class DbBaymodelTestCase(base.DbTestCase):
uuids.append(six.text_type(bm['uuid']))
res = self.dbapi.get_baymodel_list()
res_uuids = [r.uuid for r in res]
self.assertEqual(uuids.sort(), res_uuids.sort())
self.assertEqual(sorted(uuids), sorted(res_uuids))
def test_get_baymodel_by_id(self):
bm = self._create_test_baymodel()

212
magnum/tests/db/test_pod.py Normal file
View File

@ -0,0 +1,212 @@
# Copyright 2015 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Tests for manipulating Pods via the DB API"""
import six
from magnum.common import exception
from magnum.common import utils as magnum_utils
from magnum.tests.db import base
from magnum.tests.db import utils as utils
class DbPodTestCase(base.DbTestCase):
def setUp(self):
# This method creates a pod for every test and
# replaces a test for creating a pod.
super(DbPodTestCase, self).setUp()
self.bay = utils.create_test_bay()
self.pod = utils.create_test_pod(bay_uuid=self.bay.uuid)
def test_create_pod_duplicated_uuid(self):
self.assertRaises(exception.PodAlreadyExists,
utils.create_test_pod,
uuid=self.pod.uuid,
bay_uuid=self.bay.uuid)
def test_get_pod_by_id(self):
res = self.dbapi.get_pod_by_id(self.pod.id)
self.assertEqual(self.pod.id, res.id)
self.assertEqual(self.pod.uuid, res.uuid)
def test_get_pod_by_uuid(self):
res = self.dbapi.get_pod_by_uuid(self.pod.uuid)
self.assertEqual(self.pod.id, res.id)
self.assertEqual(self.pod.uuid, res.uuid)
def test_get_pod_by_name(self):
res = self.dbapi.get_pod_by_name(self.pod.name)
self.assertEqual(self.pod.id, res.id)
self.assertEqual(self.pod.uuid, res.uuid)
def test_get_pod_that_does_not_exist(self):
self.assertRaises(exception.PodNotFound,
self.dbapi.get_pod_by_id, 999)
self.assertRaises(exception.PodNotFound,
self.dbapi.get_pod_by_uuid,
magnum_utils.generate_uuid())
self.assertRaises(exception.PodNotFound,
self.dbapi.get_pod_by_name,
'bad-name')
def test_get_podinfo_list_defaults(self):
pod_id_list = [self.pod.id]
for i in range(1, 6):
pod = utils.create_test_pod(bay_uuid=self.bay.uuid,
uuid=magnum_utils.generate_uuid())
pod_id_list.append(pod.id)
res = [i[0] for i in self.dbapi.get_podinfo_list()]
self.assertEqual(sorted(res), sorted(pod_id_list))
def test_get_podinfo_list_with_cols(self):
uuids = {self.pod.id: self.pod.uuid}
names = {self.pod.id: self.pod.name}
for i in range(1, 6):
uuid = magnum_utils.generate_uuid()
name = 'pod' + str(i)
pod = utils.create_test_pod(name=name, uuid=uuid,
bay_uuid=self.bay.uuid)
uuids[pod.id] = uuid
names[pod.id] = name
res = self.dbapi.get_podinfo_list(columns=['id', 'name', 'uuid'])
self.assertEqual(names, dict((r[0], r[1]) for r in res))
self.assertEqual(uuids, dict((r[0], r[2]) for r in res))
def test_get_podinfo_list_with_filters(self):
bay1 = utils.get_test_bay(id=11, uuid=magnum_utils.generate_uuid())
bay2 = utils.get_test_bay(id=12, uuid=magnum_utils.generate_uuid())
self.dbapi.create_bay(bay1)
self.dbapi.create_bay(bay2)
pod1 = utils.create_test_pod(name='pod-one',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay1['uuid'],
status='status1')
pod2 = utils.create_test_pod(name='pod-two',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay2['uuid'],
status='status2')
res = self.dbapi.get_podinfo_list(filters={'bay_uuid': bay1['uuid']})
self.assertEqual([pod1.id], [r.id for r in res])
res = self.dbapi.get_podinfo_list(filters={'bay_uuid': bay2['uuid']})
self.assertEqual([pod2.id], [r.id for r in res])
res = self.dbapi.get_podinfo_list(filters={'name': 'pod-one'})
self.assertEqual([pod1.id], [r[0] for r in res])
res = self.dbapi.get_podinfo_list(filters={'name': 'bad-pod'})
self.assertEqual([], [r[0] for r in res])
res = self.dbapi.get_podinfo_list(filters={'status': 'status1'})
self.assertEqual([pod1.id], [r[0] for r in res])
res = self.dbapi.get_podinfo_list(filters={'status': 'status2'})
self.assertEqual([pod2.id], [r[0] for r in res])
def test_get_pod_list(self):
uuids = [self.pod.uuid]
for i in range(1, 6):
pod = utils.create_test_pod(uuid=magnum_utils.generate_uuid(),
bay_uuid=self.bay.uuid)
uuids.append(six.text_type(pod.uuid))
res = self.dbapi.get_pod_list()
res_uuids = [r.uuid for r in res]
self.assertEqual(sorted(uuids), sorted(res_uuids))
def test_get_pod_list_with_filters(self):
bay1 = utils.get_test_bay(id=11, uuid=magnum_utils.generate_uuid())
bay2 = utils.get_test_bay(id=12, uuid=magnum_utils.generate_uuid())
self.dbapi.create_bay(bay1)
self.dbapi.create_bay(bay2)
pod1 = utils.create_test_pod(name='pod-one',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay1['uuid'],
status='status1')
pod2 = utils.create_test_pod(name='pod-two',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay2['uuid'],
status='status2')
res = self.dbapi.get_pod_list(filters={'bay_uuid': bay1['uuid']})
self.assertEqual([pod1.id], [r.id for r in res])
res = self.dbapi.get_pod_list(filters={'bay_uuid': bay2['uuid']})
self.assertEqual([pod2.id], [r.id for r in res])
res = self.dbapi.get_pod_list(filters={'name': 'pod-one'})
self.assertEqual([pod1.id], [r.id for r in res])
res = self.dbapi.get_pod_list(filters={'name': 'bad-pod'})
self.assertEqual([], [r.id for r in res])
res = self.dbapi.get_pod_list(filters={'status': 'status1'})
self.assertEqual([pod1.id], [r.id for r in res])
res = self.dbapi.get_pod_list(filters={'status': 'status2'})
self.assertEqual([pod2.id], [r.id for r in res])
def test_get_pod_list_bay_not_exist(self):
res = self.dbapi.get_pod_list({'bay_uuid': self.bay.uuid})
self.assertEqual(1, len(res))
res = self.dbapi.get_pod_list({
'bay_uuid': magnum_utils.generate_uuid()})
self.assertEqual(0, len(res))
def test_get_pods_by_bay_uuid(self):
res = self.dbapi.get_pods_by_bay_uuid(self.bay.uuid)
self.assertEqual(self.pod.id, res[0].id)
def test_get_pods_by_bay_uuid_that_does_not_exist(self):
res = self.dbapi.get_pods_by_bay_uuid(magnum_utils.generate_uuid())
self.assertEqual([], res)
def test_destroy_pod(self):
self.dbapi.destroy_pod(self.pod.id)
self.assertRaises(exception.PodNotFound,
self.dbapi.get_pod_by_id, self.pod.id)
def test_destroy_pod_by_uuid(self):
self.assertIsNotNone(self.dbapi.get_pod_by_uuid(self.pod.uuid))
self.dbapi.destroy_pod(self.pod.uuid)
self.assertRaises(exception.PodNotFound,
self.dbapi.get_pod_by_uuid, self.pod.uuid)
def test_destroy_pod_that_does_not_exist(self):
self.assertRaises(exception.PodNotFound,
self.dbapi.destroy_pod,
magnum_utils.generate_uuid())
def test_update_pod(self):
old_name = self.pod.name
new_name = 'new-pod'
self.assertNotEqual(old_name, new_name)
res = self.dbapi.update_pod(self.pod.id, {'name': new_name})
self.assertEqual(new_name, res.name)
def test_update_pod_not_found(self):
pod_uuid = magnum_utils.generate_uuid()
self.assertRaises(exception.PodNotFound, self.dbapi.update_pod,
pod_uuid, {'status': 'Running'})
def test_update_pod_uuid(self):
self.assertRaises(exception.InvalidParameterValue,
self.dbapi.update_pod, self.pod.id,
{'uuid': ''})

View File

@ -0,0 +1,206 @@
# Copyright 2015 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Tests for manipulating Services via the DB API"""
import six
from magnum.common import exception
from magnum.common import utils as magnum_utils
from magnum.tests.db import base
from magnum.tests.db import utils as utils
class DbServiceTestCase(base.DbTestCase):
def setUp(self):
# This method creates a service for every test and
# replaces a test for creating a service.
super(DbServiceTestCase, self).setUp()
self.bay = utils.create_test_bay()
self.service = utils.create_test_service(bay_uuid=self.bay.uuid)
def test_create_service_duplicated_uuid(self):
self.assertRaises(exception.ServiceAlreadyExists,
utils.create_test_service,
uuid=self.service.uuid,
bay_uuid=self.bay.uuid)
def test_get_service_by_id(self):
res = self.dbapi.get_service_by_id(self.service.id)
self.assertEqual(self.service.id, res.id)
self.assertEqual(self.service.uuid, res.uuid)
def test_get_service_by_uuid(self):
res = self.dbapi.get_service_by_uuid(self.service.uuid)
self.assertEqual(self.service.id, res.id)
self.assertEqual(self.service.uuid, res.uuid)
def test_get_service_that_does_not_exist(self):
self.assertRaises(exception.ServiceNotFound,
self.dbapi.get_service_by_id, 999)
self.assertRaises(exception.ServiceNotFound,
self.dbapi.get_service_by_uuid,
magnum_utils.generate_uuid())
def test_get_serviceinfo_list_defaults(self):
service_id_list = [self.service.id]
for i in range(1, 6):
service = utils.create_test_service(bay_uuid=self.bay.uuid,
uuid=magnum_utils.generate_uuid())
service_id_list.append(service.id)
res = [i[0] for i in self.dbapi.get_serviceinfo_list()]
self.assertEqual(sorted(res), sorted(service_id_list))
def test_get_serviceinfo_list_with_cols(self):
uuids = {self.service.id: self.service.uuid}
ports = {self.service.id: self.service.port}
for i in range(1, 6):
uuid = magnum_utils.generate_uuid()
port = i
service = utils.create_test_service(port=port, uuid=uuid,
bay_uuid=self.bay.uuid)
uuids[service.id] = uuid
ports[service.id] = port
res = self.dbapi.get_serviceinfo_list(columns=['id', 'port', 'uuid'])
self.assertEqual(ports, dict((r[0], r[1]) for r in res))
self.assertEqual(uuids, dict((r[0], r[2]) for r in res))
def test_get_serviceinfo_list_with_filters(self):
bay1 = utils.get_test_bay(id=11, uuid=magnum_utils.generate_uuid())
bay2 = utils.get_test_bay(id=12, uuid=magnum_utils.generate_uuid())
self.dbapi.create_bay(bay1)
self.dbapi.create_bay(bay2)
service1 = utils.create_test_service(name='service-one',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay1['uuid'],
port=8000)
service2 = utils.create_test_service(name='service-two',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay2['uuid'],
port=8001)
res = self.dbapi.get_serviceinfo_list(
filters={'bay_uuid': bay1['uuid']})
self.assertEqual([service1.id], [r.id for r in res])
res = self.dbapi.get_serviceinfo_list(
filters={'bay_uuid': bay2['uuid']})
self.assertEqual([service2.id], [r.id for r in res])
res = self.dbapi.get_serviceinfo_list(filters={'name': 'service-one'})
self.assertEqual([service1.id], [r[0] for r in res])
res = self.dbapi.get_serviceinfo_list(filters={'name': 'bad-service'})
self.assertEqual([], [r[0] for r in res])
res = self.dbapi.get_serviceinfo_list(filters={'port': 8000})
self.assertEqual([service1.id], [r[0] for r in res])
res = self.dbapi.get_serviceinfo_list(filters={'port': 8001})
self.assertEqual([service2.id], [r[0] for r in res])
def test_get_service_list(self):
uuids = [self.service.uuid]
for i in range(1, 6):
service = utils.create_test_service(bay_uuid=self.bay.uuid,
uuid=magnum_utils.generate_uuid())
uuids.append(six.text_type(service.uuid))
res = self.dbapi.get_service_list()
res_uuids = [r.uuid for r in res]
self.assertEqual(sorted(uuids), sorted(res_uuids))
def test_get_service_list_with_filters(self):
bay1 = utils.get_test_bay(id=11, uuid=magnum_utils.generate_uuid())
bay2 = utils.get_test_bay(id=12, uuid=magnum_utils.generate_uuid())
self.dbapi.create_bay(bay1)
self.dbapi.create_bay(bay2)
service1 = utils.create_test_service(name='service-one',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay1['uuid'],
port=8000)
service2 = utils.create_test_service(name='service-two',
uuid=magnum_utils.generate_uuid(),
bay_uuid=bay2['uuid'],
port=8001)
res = self.dbapi.get_service_list(filters={'bay_uuid': bay1['uuid']})
self.assertEqual([service1.id], [r.id for r in res])
res = self.dbapi.get_service_list(filters={'bay_uuid': bay2['uuid']})
self.assertEqual([service2.id], [r.id for r in res])
res = self.dbapi.get_service_list(filters={'name': 'service-one'})
self.assertEqual([service1.id], [r.id for r in res])
res = self.dbapi.get_service_list(filters={'name': 'bad-service'})
self.assertEqual([], [r.id for r in res])
res = self.dbapi.get_service_list(filters={'port': 8000})
self.assertEqual([service1.id], [r.id for r in res])
res = self.dbapi.get_service_list(filters={'port': 8001})
self.assertEqual([service2.id], [r.id for r in res])
def test_get_service_list_bay_not_exist(self):
res = self.dbapi.get_service_list({'bay_uuid': self.bay.uuid})
self.assertEqual(1, len(res))
res = self.dbapi.get_service_list({
'bay_uuid': magnum_utils.generate_uuid()})
self.assertEqual(0, len(res))
def test_get_services_by_bay_uuid(self):
res = self.dbapi.get_services_by_bay_uuid(self.bay.uuid)
self.assertEqual(self.service.id, res[0].id)
def test_get_services_by_bay_uuid_that_does_not_exist(self):
res = self.dbapi.get_services_by_bay_uuid(magnum_utils.generate_uuid())
self.assertEqual([], res)
def test_destroy_service(self):
self.dbapi.destroy_service(self.service.id)
self.assertRaises(exception.ServiceNotFound,
self.dbapi.get_service_by_id, self.service.id)
def test_destroy_service_by_uuid(self):
self.assertIsNotNone(self.dbapi.get_service_by_uuid(self.service.uuid))
self.dbapi.destroy_service(self.service.uuid)
self.assertRaises(exception.ServiceNotFound,
self.dbapi.get_service_by_uuid, self.service.uuid)
def test_destroy_service_that_does_not_exist(self):
self.assertRaises(exception.ServiceNotFound,
self.dbapi.destroy_service,
magnum_utils.generate_uuid())
def test_update_service(self):
old_name = self.service.name
new_name = 'new-service'
self.assertNotEqual(old_name, new_name)
res = self.dbapi.update_service(self.service.id, {'name': new_name})
self.assertEqual(new_name, res.name)
def test_update_service_not_found(self):
service_uuid = magnum_utils.generate_uuid()
self.assertRaises(exception.ServiceNotFound, self.dbapi.update_service,
service_uuid, {'port': 80})
def test_update_service_uuid(self):
self.assertRaises(exception.InvalidParameterValue,
self.dbapi.update_service, self.service.id,
{'uuid': ''})