Merge "Add db instance_type unit test cases"

This commit is contained in:
Jenkins 2016-11-11 15:20:19 +00:00 committed by Gerrit Code Review
commit 08c8744cf5
7 changed files with 137 additions and 18 deletions

View File

@ -43,38 +43,45 @@ class Connection(object):
# Instance Types
@abc.abstractmethod
def instance_type_create(self, values):
def instance_type_create(self, context, values):
"""Create a new instance type."""
@abc.abstractmethod
def instance_type_get(uuid):
def instance_type_get(self, context, instance_type_uuid):
"""Get instance type by uuid."""
def instance_type_get_all():
@abc.abstractmethod
def instance_type_get_all(self, context):
"""Get all instance types."""
@abc.abstractmethod
def instance_type_destroy(uuid):
def instance_type_destroy(self, context, instance_type_uuid):
"""Delete an instance type."""
# Instances
@abc.abstractmethod
def instance_create(self, values):
def instance_create(self, context, values):
"""Create a new instance."""
@abc.abstractmethod
def instance_get(uuid):
def instance_get(self, context, instance_id):
"""Get instance by name."""
def instance_get_all():
@abc.abstractmethod
def instance_get_all(self, context):
"""Get all instances."""
@abc.abstractmethod
def instance_destroy(name):
def instance_destroy(self, context, instance_id):
"""Delete an instance."""
@abc.abstractmethod
def extra_specs_update_or_create(instance_type_id, extra_specs):
def instance_update(self, context, instance_id, values):
"""Update an instance."""
@abc.abstractmethod
def extra_specs_update_or_create(self, context,
instance_type_id, extra_specs):
"""Create or update instance type extra specs.
This adds or modifies the key/value pairs specified in the
@ -82,7 +89,11 @@ class Connection(object):
"""
@abc.abstractmethod
def type_extra_specs_delete(instance_type_id, key):
def instance_type_extra_specs_get(self, context, type_id):
"""Get instance type extra specs"""
@abc.abstractmethod
def type_extra_specs_delete(self, context, instance_type_id, key):
"""Delete instance type extra specs.
This deletes the key/value pairs specified in the

View File

@ -104,7 +104,7 @@ class Connection(api.Connection):
def __init__(self):
pass
def instance_type_create(self, values):
def instance_type_create(self, context, values):
if not values.get('uuid'):
values['uuid'] = uuidutils.generate_uuid()
@ -190,7 +190,7 @@ class Connection(api.Connection):
if count != 1:
raise exception.InstanceNotFound(instance=instance_id)
def update_instance(self, context, instance_id, values):
def instance_update(self, context, instance_id, values):
if 'uuid' in values:
msg = _("Cannot overwrite UUID for an existing Instance.")
raise exception.InvalidParameterValue(err=msg)
@ -275,10 +275,10 @@ def _type_get_id_from_type_query(context, type_id):
def _type_get_id_from_type(context, type_id):
result = _type_get_id_from_type_query(context, type_id).first().id
result = _type_get_id_from_type_query(context, type_id).first()
if not result:
raise exception.InstanceTypeNotFound(type_id=type_id)
return result
return result.id
def _type_extra_specs_get_query(context, type_id):

View File

@ -79,5 +79,5 @@ class Instance(base.NimbleObject, object_base.VersionedObjectDictCompat):
def save(self, context=None):
"""Save updates to this Instance."""
updates = self.obj_get_changes()
self.dbapi.update_instance(context, self.uuid, updates)
self.dbapi.instance_update(context, self.uuid, updates)
self.obj_reset_changes()

View File

@ -82,7 +82,7 @@ class InstanceType(base.NimbleObject, object_base.VersionedObjectDictCompat):
def create(self, context=None):
"""Create a Instance Type record in the DB."""
values = self.obj_get_changes()
db_instance_type = self.dbapi.instance_type_create(values)
db_instance_type = self.dbapi.instance_type_create(context, values)
self._from_db_object(self, db_instance_type)
def destroy(self, context=None):

View File

@ -0,0 +1,77 @@
# Copyright 2016 Huawei Technologies Co.,LTD.
# 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 Instance Types via the DB API"""
from oslo_utils import uuidutils
import six
from nimble.common import exception
from nimble.tests.unit.db import base
from nimble.tests.unit.db import utils
class DbInstanceTypeTestCase(base.DbTestCase):
def setUp(self):
super(DbInstanceTypeTestCase, self).setUp()
self.context = {}
self.instance_type = utils.create_test_instance_type()
def test_create_instance_type(self):
utils.create_test_instance_type(name='testing')
def test_create_instance_type_already_exists(self):
self.assertRaises(exception.InstanceTypeAlreadyExists,
utils.create_test_instance_type,
uuid=self.instance_type['uuid'])
def test_get_instance_type_list(self):
uuids = [self.instance_type['uuid']]
for i in range(1, 6):
inst_type = utils.create_test_instance_type(
uuid=uuidutils.generate_uuid(),
name=six.text_type(i))
uuids.append(six.text_type(inst_type['uuid']))
res = self.dbapi.instance_type_get_all(self.context)
res_uuids = [r['uuid'] for r in res]
six.assertCountEqual(self, uuids, res_uuids)
def test_get_instance_type(self):
instance_type = self.dbapi.instance_type_get(
self.context, self.instance_type['uuid'])
self.assertEqual(self.instance_type['uuid'], instance_type['uuid'])
def test_get_instance_type_that_does_not_exist(self):
self.assertRaises(exception.InstanceTypeNotFound,
self.dbapi.instance_type_get,
self.context,
uuidutils.generate_uuid())
def test_destroy_instance_type(self):
self.dbapi.instance_type_destroy(self.context,
self.instance_type['uuid'])
self.assertRaises(exception.InstanceTypeNotFound,
self.dbapi.instance_type_destroy,
self.context,
self.instance_type['uuid'])
def test_destroy_instance_type_that_does_not_exist(self):
self.assertRaises(exception.InstanceTypeNotFound,
self.dbapi.instance_type_destroy,
self.context,
uuidutils.generate_uuid())

View File

@ -13,13 +13,13 @@
# License for the specific language governing permissions and limitations
# under the License.
"""Tests for manipulating Nodes via the DB API"""
"""Tests for manipulating Instances via the DB API"""
from nimble.tests.unit.db import base
from nimble.tests.unit.db import utils
class DbNodeTestCase(base.DbTestCase):
class DbInstanceTestCase(base.DbTestCase):
def test_instance_create(self):
utils.create_test_instance()

View File

@ -78,3 +78,34 @@ def create_test_instance(context={}, **kw):
dbapi = db_api.get_instance()
return dbapi.instance_create(context, instance)
def get_test_instance_type(**kw):
return {
'id': kw.get('id', 123),
'uuid': kw.get('uuid', 'e5ddde02-f84d-4da7-ade3-957c55072986'),
'name': kw.get('name', 'test'),
'description': kw.get('description', 'test'),
'is_public': kw.get('is_public', 1),
'updated_at': kw.get('updated_at'),
'created_at': kw.get('created_at'),
}
def create_test_instance_type(context={}, **kw):
"""Create test instance type entry in DB and return the DB object.
Function to be used to create test Instance Type objects in the database.
:param context: The request context, for access checks.
:param kw: kwargs with overriding values for instance type's attributes.
:returns: Test Instance Type DB object.
"""
instance_type = get_test_instance_type(**kw)
# Let DB generate ID if it isn't specified explicitly
if 'id' not in kw:
del instance_type['id']
dbapi = db_api.get_instance()
return dbapi.instance_type_create(context, instance_type)