Fix deployable attribute object issues and unit test failures.

Change-Id: I969e80a5ea241b8838027712ed642af4ad9f0140
This commit is contained in:
wangxu 2018-08-14 19:45:42 +08:00
parent 719f3dee01
commit 4d5809d540
5 changed files with 42 additions and 28 deletions

View File

@ -101,7 +101,7 @@ class Connection(object):
"""Get requested deployable by filters with attributes."""
# attributes
@abc.abstractmethod
def attribute_create(self, context, key, value):
def attribute_create(self, context, values):
"""Create a new attribute."""
@abc.abstractmethod

View File

@ -425,7 +425,7 @@ class Connection(api.Connection):
session.flush()
except db_exc.DBDuplicateEntry:
raise exception.AttributeAlreadyExists(
uuid=update_fields['uuid'])
uuid=values['uuid'])
return attribute
def attribute_get(self, context, uuid):
@ -441,10 +441,7 @@ class Connection(api.Connection):
query = model_query(
context,
models.Attribute).filter_by(deployable_id=deployable_id)
try:
return query.all()
except NoResultFound:
raise exception.AttributeNotFound(uuid=uuid)
return query.all()
def attribute_get_by_filter(self, context, filters):
"""Return attributes that matches the filters

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
from oslo_log import log as logging
from oslo_versionedobjects import base as object_base
@ -157,7 +156,7 @@ class Deployable(base.CyborgObject, object_base.VersionedObjectDictCompat):
if key == exist_attr.key:
LOG.warning("The attribute already exists")
if value != exist_attr.value:
exist_attr.value = attribute.value
exist_attr.value = value
exist_attr.save(context)
return None
# The attribute does not exist yet. Create it.

View File

@ -12,24 +12,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import mock
import netaddr
from oslo_db import exception as db_exc
from oslo_serialization import jsonutils
from oslo_utils import timeutils
from oslo_context import context
from cyborg import db
from cyborg.common import exception
from cyborg import objects
from cyborg.objects import base
from cyborg import tests as test
from cyborg.tests.unit import fake_attribute
from cyborg.tests.unit import fake_deployable
from cyborg.tests.unit import fake_accelerator
from cyborg.tests.unit.objects import test_objects
from cyborg.tests.unit.db.base import DbTestCase
@ -205,3 +193,29 @@ class _TestDeployableObject(DbTestCase):
attr_get_list = objects.Attribute.get_by_filter(
self.context, attr_filter)
self.assertEqual(len(attr_get_list), 0)
def test_create_exists(self):
db_acc = self.fake_accelerator
acc = objects.Accelerator(context=self.context,
**db_acc)
acc.create(self.context)
acc_get = objects.Accelerator.get(self.context, acc.uuid)
db_dpl = self.fake_deployable
dpl = objects.Deployable(context=self.context,
**db_dpl)
dpl.accelerator_id = acc_get.id
dpl.create(self.context)
dpl_get = objects.Deployable.get(self.context, dpl.uuid)
db_attr = self.fake_attribute
attr = objects.Attribute(context=self.context,
**db_attr)
attr.deployable_id = dpl_get.id
attr.create(self.context)
attr2 = objects.Attribute(context=self.context,
**db_attr)
attr2.deployable_id = dpl_get.id
self.assertRaises(exception.AttributeAlreadyExists,
attr2.create, self.context)

View File

@ -12,20 +12,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import mock
import netaddr
from oslo_db import exception as db_exc
from oslo_serialization import jsonutils
from oslo_utils import timeutils
from oslo_context import context
from cyborg import db
from cyborg.common import exception
from cyborg import objects
from cyborg.objects import base
from cyborg import tests as test
from cyborg.tests.unit import fake_accelerator
from cyborg.tests.unit import fake_deployable
from cyborg.tests.unit import fake_attribute
@ -167,6 +159,18 @@ class _TestDeployableObject(DbTestCase):
dpl_get = objects.Deployable.get(self.context, dpl.uuid)
self.assertEqual(len(dpl_get.attributes_list), 1)
self.assertEqual(dpl_get.attributes_list[0].key,
db_attr['key'])
self.assertEqual(dpl_get.attributes_list[0].value,
db_attr['value'])
dpl.add_attribute(self.context, db_attr['key'], 'change_value')
dpl_get = objects.Deployable.get(self.context, dpl.uuid)
self.assertEqual(len(dpl_get.attributes_list), 1)
self.assertEqual(dpl_get.attributes_list[0].key,
db_attr['key'])
self.assertEqual(dpl_get.attributes_list[0].value,
'change_value')
def test_delete_attribute(self):
db_acc = self.fake_accelerator