Merge "Clean the compute_node and compute_port objects and db interfaces"

This commit is contained in:
Jenkins 2017-07-18 05:42:11 +00:00 committed by Gerrit Code Review
commit ec7d5d0d69
16 changed files with 1 additions and 824 deletions

View File

@ -141,9 +141,6 @@ server_policies = [
policy.RuleDefault('mogan:flavor_access:get_all',
'rule:allow',
description='Retrieve all flavor access'),
policy.RuleDefault('mogan:node:get_all',
'rule:admin_api',
description='Retrieve all compute nodes'),
policy.RuleDefault('mogan:server:detach_interface',
'rule:default',
description='Detach a network interface'),

View File

@ -21,38 +21,12 @@ opts = [
cfg.StrOpt('scheduler_driver',
default='mogan.scheduler.filter_scheduler.FilterScheduler',
help=_('Default scheduler driver to use')),
cfg.StrOpt('scheduler_node_manager',
default='mogan.scheduler.node_manager.NodeManager',
help=_('The scheduler node manager class to use')),
cfg.IntOpt('scheduler_max_attempts',
default=3,
help=_('Maximum number of attempts to schedule a node')),
cfg.StrOpt('scheduler_json_config_location',
default='',
help=_('Absolute path to scheduler configuration JSON file.')),
cfg.ListOpt('scheduler_default_filters',
default=[
'AvailabilityZoneFilter',
'FlavorFilter',
'CapabilitiesFilter',
'PortsFilter'
],
help=_('Which filter class names to use for filtering nodes '
'when not specified in the request.')),
cfg.ListOpt('scheduler_default_weighers',
default=[
'PortWeigher',
],
help=_('Which weigher class names to use for weighing '
'nodes.')),
cfg.StrOpt('scheduler_weight_handler',
default='mogan.scheduler.weights.'
'OrderedNodeWeightHandler',
help=_('Which handler to use for selecting the node after '
'weighing')),
cfg.FloatOpt('port_weight_multiplier',
default=-1.0,
help=_('Node ports quantity weight multipler ratio.')),
]

View File

@ -82,56 +82,6 @@ class Connection(object):
def server_update(self, context, server_id, values):
"""Update a server."""
# Compute nodes
@abc.abstractmethod
def compute_node_create(self, context, values):
"""Create a new compute node."""
@abc.abstractmethod
def compute_node_get(self, context, node_uuid):
"""Get compute node by node uuid."""
@abc.abstractmethod
def compute_node_get_all(self, context):
"""Get all compute nodes."""
@abc.abstractmethod
def compute_node_get_all_available(self, context):
"""Get all available compute nodes."""
@abc.abstractmethod
def compute_node_destroy(self, context, node_uuid):
"""Delete a compute node."""
@abc.abstractmethod
def compute_node_update(self, context, node_uuid, values):
"""Update a compute node."""
# Compute ports
@abc.abstractmethod
def compute_port_create(self, context, values):
"""Create a new compute port."""
@abc.abstractmethod
def compute_port_get(self, context, port_uuid):
"""Get compute port by port uuid."""
@abc.abstractmethod
def compute_port_get_all(self, context):
"""Get all compute ports."""
@abc.abstractmethod
def compute_port_get_by_node_uuid(self, context, node_uuid):
"""Get compute ports by node_uuid."""
@abc.abstractmethod
def compute_port_destroy(self, context, port_uuid):
"""Delete a compute port."""
@abc.abstractmethod
def compute_port_update(self, context, port_uuid, values):
"""Update a compute port."""
# Flavor access
@abc.abstractmethod
def flavor_access_add(self, context, flavor_id, project_id):

View File

@ -81,39 +81,6 @@ def upgrade():
mysql_ENGINE='InnoDB',
mysql_DEFAULT_CHARSET='UTF8'
)
op.create_table(
'compute_nodes',
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('cpus', sa.Integer(), nullable=False),
sa.Column('memory_mb', sa.Integer(), nullable=False),
sa.Column('hypervisor_type', sa.String(length=255), nullable=False),
sa.Column('resource_class', sa.String(length=80), nullable=False),
sa.Column('availability_zone', sa.String(length=255), nullable=True),
sa.Column('node_uuid', sa.String(length=36), nullable=False),
sa.Column('extra_specs', sa.Text(), nullable=True),
sa.Column('used', sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('node_uuid', name='uniq_compute_nodes0node_uuid'),
mysql_ENGINE='InnoDB',
mysql_DEFAULT_CHARSET='UTF8'
)
op.create_table(
'compute_ports',
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('address', sa.String(length=18), nullable=False),
sa.Column('port_uuid', sa.String(length=36), nullable=False),
sa.Column('node_uuid', sa.String(length=36), nullable=False),
sa.Column('extra_specs', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('port_uuid', name='uniq_compute_ports0port_uuid'),
sa.ForeignKeyConstraint(['node_uuid'], ['compute_nodes.node_uuid'], ),
mysql_ENGINE='InnoDB',
mysql_DEFAULT_CHARSET='UTF8'
)
op.create_table(
'server_nics',
sa.Column('created_at', sa.DateTime(), nullable=True),

View File

@ -26,7 +26,6 @@ from oslo_utils import timeutils
from oslo_utils import uuidutils
from sqlalchemy import or_
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm import joinedload
from sqlalchemy.sql.expression import desc
from sqlalchemy.sql import true
@ -257,117 +256,6 @@ class Connection(api.Connection):
ref.update(values)
return ref
def compute_node_create(self, context, values):
compute_node = models.ComputeNode()
compute_node.update(values)
with _session_for_write() as session:
try:
session.add(compute_node)
session.flush()
except db_exc.DBDuplicateEntry:
raise exception.ComputeNodeAlreadyExists(
node=values['node_uuid'])
return compute_node
def compute_node_get(self, context, node_uuid):
query = model_query(
context,
models.ComputeNode).filter_by(node_uuid=node_uuid). \
options(joinedload('ports'))
try:
return query.one()
except NoResultFound:
raise exception.ComputeNodeNotFound(node=node_uuid)
def compute_node_get_all(self, context):
return model_query(
context,
models.ComputeNode).options(joinedload('ports'))
def compute_node_get_all_available(self, context):
return model_query(
context,
models.ComputeNode).filter_by(used=False). \
options(joinedload('ports'))
def compute_node_destroy(self, context, node_uuid):
with _session_for_write():
query = model_query(
context,
models.ComputeNode).filter_by(node_uuid=node_uuid)
port_query = model_query(context, models.ComputePort).filter_by(
node_uuid=node_uuid)
port_query.delete()
count = query.delete()
if count != 1:
raise exception.ComputeNodeNotFound(node=node_uuid)
def compute_node_update(self, context, node_uuid, values):
with _session_for_write():
query = model_query(
context,
models.ComputeNode).filter_by(node_uuid=node_uuid)
try:
ref = query.with_lockmode('update').one()
except NoResultFound:
raise exception.ComputeNodeNotFound(node=node_uuid)
ref.update(values)
return ref
def compute_port_create(self, context, values):
compute_port = models.ComputePort()
compute_port.update(values)
with _session_for_write() as session:
try:
session.add(compute_port)
session.flush()
except db_exc.DBDuplicateEntry:
raise exception.ComputePortAlreadyExists(
port=values['port_uuid'])
return compute_port
def compute_port_get(self, context, port_uuid):
query = model_query(
context,
models.ComputePort).filter_by(port_uuid=port_uuid)
try:
return query.one()
except NoResultFound:
raise exception.ComputePortNotFound(port=port_uuid)
def compute_port_get_all(self, context):
return model_query(context, models.ComputePort)
def compute_port_get_by_node_uuid(self, context, node_uuid):
return model_query(context, models.ComputePort).filter_by(
node_uuid=node_uuid).all()
def compute_port_destroy(self, context, port_uuid):
with _session_for_write():
query = model_query(
context,
models.ComputePort).filter_by(port_uuid=port_uuid)
count = query.delete()
if count != 1:
raise exception.ComputePortNotFound(port=port_uuid)
def compute_port_update(self, context, port_uuid, values):
with _session_for_write():
query = model_query(
context,
models.ComputePort).filter_by(port_uuid=port_uuid)
try:
ref = query.with_lockmode('update').one()
except NoResultFound:
raise exception.ComputePortNotFound(port=port_uuid)
ref.update(values)
return ref
def flavor_access_get(self, context, flavor_id):
return _flavor_access_query(context, flavor_id)

View File

@ -384,16 +384,7 @@ class API(object):
def list_availability_zones(self, context):
"""Get availability zone list."""
compute_nodes = objects.ComputeNodeList.get_all_available(context)
azs = set()
for node in compute_nodes:
az = node.availability_zone \
or CONF.engine.default_availability_zone
if az is not None:
azs.add(az)
return {'availability_zones': list(azs)}
return {'availability_zones': [CONF.engine.default_availability_zone]}
def lock(self, context, server):
"""Lock the given server."""

View File

@ -29,6 +29,4 @@ def register_all():
__import__('mogan.objects.server')
__import__('mogan.objects.server_nics')
__import__('mogan.objects.server_fault')
__import__('mogan.objects.compute_node')
__import__('mogan.objects.compute_port')
__import__('mogan.objects.keypair')

View File

@ -1,119 +0,0 @@
# Copyright 2017 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.
from oslo_versionedobjects import base as object_base
from mogan.db import api as dbapi
from mogan.objects import base
from mogan.objects import compute_port
from mogan.objects import fields as object_fields
@base.MoganObjectRegistry.register
class ComputeNode(base.MoganObject, object_base.VersionedObjectDictCompat):
# Version 1.0: Initial version
VERSION = '1.0'
dbapi = dbapi.get_instance()
fields = {
'id': object_fields.IntegerField(read_only=True),
'cpus': object_fields.IntegerField(),
'memory_mb': object_fields.IntegerField(),
'hypervisor_type': object_fields.StringField(),
'resource_class': object_fields.StringField(),
'availability_zone': object_fields.StringField(nullable=True),
'node_uuid': object_fields.UUIDField(read_only=True),
'ports': object_fields.ObjectField('ComputePortList', nullable=True),
'extra_specs': object_fields.FlexibleDictField(nullable=True),
'used': object_fields.BooleanField(default=False),
}
@staticmethod
def _from_db_object(context, node, db_node):
"""Converts a database entity to a formal object."""
for field in node.fields:
if field == 'ports':
node.ports = object_base.obj_make_list(
context, compute_port.ComputePortList(context),
compute_port.ComputePort, db_node['ports']
)
else:
node[field] = db_node[field]
node.obj_reset_changes()
return node
@classmethod
def get(cls, context, node_uuid):
"""Find a compute node and return a ComputeNode object."""
db_compute_node = cls.dbapi.compute_node_get(context, node_uuid)
compute_node = cls._from_db_object(
context, cls(context), db_compute_node)
return compute_node
def create(self, context=None):
"""Create a ComputeNode record in the DB."""
values = self.obj_get_changes()
self.dbapi.compute_node_create(context, values)
def destroy(self, context=None):
"""Delete the ComputeNode from the DB."""
self.dbapi.compute_node_destroy(context, self.node_uuid)
self.obj_reset_changes()
def save(self, context=None):
"""Save updates to this ComputeNode."""
updates = self.obj_get_changes()
self.dbapi.compute_node_update(context, self.node_uuid, updates)
self.obj_reset_changes()
def update_from_driver(self, node):
keys = ["cpus", "memory_mb", "hypervisor_type", "resource_class",
"availability_zone", "node_uuid", "extra_specs"]
for key in keys:
if key in node:
setattr(self, key, node[key])
@classmethod
def consume_node(cls, context, node_uuid):
updates = {'used': True}
cls.dbapi.compute_node_update(context, node_uuid, updates)
@base.MoganObjectRegistry.register
class ComputeNodeList(object_base.ObjectListBase, base.MoganObject,
object_base.VersionedObjectDictCompat):
# Version 1.0: Initial version
VERSION = '1.0'
dbapi = dbapi.get_instance()
fields = {
'objects': object_fields.ListOfObjectsField('ComputeNode')
}
@classmethod
def get_all(cls, context):
db_compute_nodes = cls.dbapi.compute_node_get_all(context)
return object_base.obj_make_list(context, cls(context),
ComputeNode, db_compute_nodes)
@classmethod
def get_all_available(cls, context):
db_compute_nodes = cls.dbapi.compute_node_get_all_available(context)
return object_base.obj_make_list(context, cls(context),
ComputeNode, db_compute_nodes)

View File

@ -1,64 +0,0 @@
# Copyright 2017 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 ComputeNodes via the DB API"""
from mogan.common import exception
from mogan.tests.unit.db import base
from mogan.tests.unit.db import utils
class DbComputeNodeTestCase(base.DbTestCase):
def test_compute_node_create(self):
utils.create_test_compute_node()
def test_compute_node_create_with_same_uuid(self):
utils.create_test_compute_node(node_uuid='uuid')
self.assertRaises(exception.ComputeNodeAlreadyExists,
utils.create_test_compute_node,
node_uuid='uuid')
def test_compute_node_get_by_uuid(self):
node = utils.create_test_compute_node()
res = self.dbapi.compute_node_get(self.context, node.node_uuid)
self.assertEqual(node.node_uuid, res.node_uuid)
def test_compute_node_get_not_exist(self):
self.assertRaises(exception.ComputeNodeNotFound,
self.dbapi.compute_node_get,
self.context,
'12345678-9999-0000-aaaa-123456789012')
def test_compute_node_destroy(self):
node = utils.create_test_compute_node()
self.dbapi.compute_node_destroy(self.context, node.node_uuid)
self.assertRaises(exception.ComputeNodeNotFound,
self.dbapi.compute_node_get,
self.context,
node.node_uuid)
def test_compute_node_destroy_not_exist(self):
self.assertRaises(exception.ComputeNodeNotFound,
self.dbapi.compute_node_destroy,
self.context,
'12345678-9999-0000-aaaa-123456789012')
def test_compute_node_update(self):
node = utils.create_test_compute_node()
res = self.dbapi.compute_node_update(self.context,
node.node_uuid,
{'hypervisor_type': 'foo'})
self.assertEqual('foo', res.hypervisor_type)

View File

@ -1,78 +0,0 @@
# Copyright 2017 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 ComputePorts via the DB API"""
from oslo_utils import uuidutils
import six
from mogan.common import exception
from mogan.tests.unit.db import base
from mogan.tests.unit.db import utils
class DbComputePortTestCase(base.DbTestCase):
def test_compute_port_create(self):
utils.create_test_compute_port()
def test_compute_port_create_with_same_uuid(self):
utils.create_test_compute_port(port_uuid='uuid')
self.assertRaises(exception.ComputePortAlreadyExists,
utils.create_test_compute_port,
port_uuid='uuid')
def test_compute_port_get_by_uuid(self):
port = utils.create_test_compute_port()
res = self.dbapi.compute_port_get(self.context, port.port_uuid)
self.assertEqual(port.port_uuid, res.port_uuid)
def test_compute_port_get_not_exist(self):
self.assertRaises(exception.ComputePortNotFound,
self.dbapi.compute_port_get,
self.context,
'12345678-9999-0000-aaaa-123456789012')
def test_compute_port_get_all(self):
port_uuids = []
for i in range(0, 3):
port = utils.create_test_compute_port(
port_uuid=uuidutils.generate_uuid())
port_uuids.append(six.text_type(port['port_uuid']))
res = self.dbapi.compute_port_get_all(self.context)
res_uuids = [r.port_uuid for r in res]
self.assertItemsEqual(port_uuids, res_uuids)
def test_compute_port_destroy(self):
port = utils.create_test_compute_port()
self.dbapi.compute_port_destroy(self.context, port.port_uuid)
self.assertRaises(exception.ComputePortNotFound,
self.dbapi.compute_port_get,
self.context,
port.port_uuid)
def test_compute_port_destroy_not_exist(self):
self.assertRaises(exception.ComputePortNotFound,
self.dbapi.compute_port_destroy,
self.context,
'12345678-9999-0000-aaaa-123456789012')
def test_compute_port_update(self):
port = utils.create_test_compute_port()
res = self.dbapi.compute_port_update(self.context,
port.port_uuid,
{'address': 'aa:bb:cc:dd:ee:ff'})
self.assertEqual('aa:bb:cc:dd:ee:ff', res.address)

View File

@ -84,80 +84,6 @@ def create_test_server(context={}, **kw):
return dbapi.server_create(context, server)
def get_test_compute_node(**kw):
return {
'id': kw.get('id', 123),
'cpus': kw.get('cpus', 16),
'memory_mb': kw.get('memory_mb', 10240),
'hypervisor_type': kw.get('hypervisor_type', 'ironic'),
'resource_class': kw.get('resource_class', 'gold'),
'availability_zone': kw.get('availability_zone', 'test_az'),
'node_uuid': kw.get('node_uuid',
'f978ef48-d4af-4dad-beec-e6174309bc71'),
'extra_specs': kw.get('extra_specs', {}),
'ports': kw.get('ports', []),
'used': kw.get('used', False),
'updated_at': kw.get('updated_at'),
'created_at': kw.get('created_at'),
}
def create_test_compute_node(context={}, **kw):
"""Create test compute node entry in DB and return ComputeNode DB object.
Function to be used to create test ComputeNode objects in the database.
:param context: The request context, for access checks.
:param kw: kwargs with overriding values for node's attributes.
:returns: Test ComputeNode DB object.
"""
node = get_test_compute_node(**kw)
# Let DB generate ID if it isn't specified explicitly
if 'id' not in kw:
del node['id']
# Create node with tags will raise an exception. If tags are not
# specified explicitly just delete it.
if 'ports' not in kw:
del node['ports']
dbapi = db_api.get_instance()
return dbapi.compute_node_create(context, node)
def get_test_compute_port(**kw):
return {
'id': kw.get('id', 123),
'address': kw.get('address', '52:54:00:cf:2d:31'),
'port_uuid': kw.get('port_uuid',
'f978ef48-d4af-4dad-beec-e6174309bc72'),
'node_uuid': kw.get('node_uuid',
'f978ef48-d4af-4dad-beec-e6174309bc71'),
'extra_specs': kw.get('extra_specs', {}),
'updated_at': kw.get('updated_at'),
'created_at': kw.get('created_at'),
}
def create_test_compute_port(context={}, **kw):
"""Create test compute port entry in DB and return ComputePort DB object.
Function to be used to create test ComputePort objects in the database.
:param context: The request context, for access checks.
:param kw: kwargs with overriding values for port's attributes.
:returns: Test ComputePort DB object.
"""
port = get_test_compute_port(**kw)
# Let DB generate ID if it isn't specified explicitly
if 'id' not in kw:
del port['id']
dbapi = db_api.get_instance()
return dbapi.compute_port_create(context, port)
def get_test_flavor(**kw):
return {
'uuid': kw.get('uuid', uuidutils.generate_uuid()),

View File

@ -17,7 +17,6 @@
import mock
from oslo_context import context
from oslo_utils import uuidutils
from mogan.common import exception
from mogan.common import states
@ -26,7 +25,6 @@ from mogan.engine import rpcapi as engine_rpcapi
from mogan import objects
from mogan.tests.unit.db import base
from mogan.tests.unit.db import utils as db_utils
from mogan.tests.unit.objects import utils as obj_utils
class ComputeAPIUnitTest(base.DbTestCase):
@ -359,20 +357,6 @@ class ComputeAPIUnitTest(base.DbTestCase):
self.engine_api.rebuild(self.context, fake_server_obj)
self.assertTrue(mock_rebuild.called)
def test_list_availability_zone(self):
uuid1 = uuidutils.generate_uuid()
uuid2 = uuidutils.generate_uuid()
obj_utils.create_test_compute_node(
self.context, availability_zone='az1')
obj_utils.create_test_compute_node(
self.context, node_uuid=uuid1, availability_zone='az2')
obj_utils.create_test_compute_node(
self.context, node_uuid=uuid2, availability_zone='az1')
azs = self.engine_api.list_availability_zones(self.context)
self.assertItemsEqual(['az1', 'az2'], azs['availability_zones'])
@mock.patch.object(engine_rpcapi.EngineAPI, 'detach_interface')
def test_detach_interface(self, mock_detach_interface):
fake_server = db_utils.get_test_server(

View File

@ -1,79 +0,0 @@
# Copyright 2017 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.
import copy
import mock
from oslo_context import context
from mogan import objects
from mogan.tests.unit.db import base
from mogan.tests.unit.db import utils
from mogan.tests.unit.objects import utils as obj_utils
class TestComputeNodeObject(base.DbTestCase):
def setUp(self):
super(TestComputeNodeObject, self).setUp()
self.ctxt = context.get_admin_context()
self.fake_node = utils.get_test_compute_node(
context=self.ctxt, ports=[utils.get_test_compute_port()])
self.node = obj_utils.get_test_compute_node(
self.ctxt, **self.fake_node)
def test_get(self):
node_uuid = self.fake_node['node_uuid']
with mock.patch.object(self.dbapi, 'compute_node_get',
autospec=True) as mock_node_get:
mock_node_get.return_value = self.fake_node
node = objects.ComputeNode.get(self.context, node_uuid)
mock_node_get.assert_called_once_with(self.context, node_uuid)
self.assertEqual(self.context, node._context)
def test_create(self):
with mock.patch.object(self.dbapi, 'compute_node_create',
autospec=True) as mock_node_create:
self.fake_node.pop('ports')
mock_node_create.return_value = self.fake_node
node = objects.ComputeNode(self.context, **self.fake_node)
node.create(self.context)
expected_called = copy.deepcopy(self.fake_node)
mock_node_create.assert_called_once_with(self.context,
expected_called)
self.assertEqual(self.fake_node['node_uuid'], node['node_uuid'])
def test_destroy(self):
uuid = self.fake_node['node_uuid']
with mock.patch.object(self.dbapi, 'compute_node_destroy',
autospec=True) as mock_node_destroy:
self.fake_node.pop('ports')
node = objects.ComputeNode(self.context, **self.fake_node)
node.destroy(self.context)
mock_node_destroy.assert_called_once_with(self.context, uuid)
def test_save(self):
uuid = self.fake_node['node_uuid']
with mock.patch.object(self.dbapi, 'compute_node_update',
autospec=True) as mock_node_update:
self.fake_node.pop('ports')
mock_node_update.return_value = self.fake_node
node = objects.ComputeNode(self.context, **self.fake_node)
updates = node.obj_get_changes()
node.save(self.context)
mock_node_update.assert_called_once_with(
self.context, uuid, updates)

View File

@ -1,93 +0,0 @@
# Copyright 2017 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.
import copy
import mock
from oslo_context import context
from mogan import objects
from mogan.tests.unit.db import base
from mogan.tests.unit.db import utils
from mogan.tests.unit.objects import utils as obj_utils
class TestComputePortObject(base.DbTestCase):
def setUp(self):
super(TestComputePortObject, self).setUp()
self.ctxt = context.get_admin_context()
self.fake_port = utils.get_test_compute_port(context=self.ctxt)
self.port = obj_utils.get_test_compute_port(
self.ctxt, **self.fake_port)
def test_get(self):
port_uuid = self.fake_port['port_uuid']
with mock.patch.object(self.dbapi, 'compute_port_get',
autospec=True) as mock_port_get:
mock_port_get.return_value = self.fake_port
port = objects.ComputePort.get(self.context, port_uuid)
mock_port_get.assert_called_once_with(self.context, port_uuid)
self.assertEqual(self.context, port._context)
def test_list(self):
with mock.patch.object(self.dbapi, 'compute_port_get_all',
autospec=True) as mock_port_get_all:
mock_port_get_all.return_value = [self.fake_port]
ports = objects.ComputePort.list(self.context)
mock_port_get_all.assert_called_once_with(self.context)
self.assertIsInstance(ports[0], objects.ComputePort)
self.assertEqual(self.context, ports[0]._context)
def test_create(self):
with mock.patch.object(self.dbapi, 'compute_port_create',
autospec=True) as mock_port_create:
mock_port_create.return_value = self.fake_port
port = objects.ComputePort(self.context, **self.fake_port)
port.obj_get_changes()
port.create(self.context)
expected_called = copy.deepcopy(self.fake_port)
mock_port_create.assert_called_once_with(self.context,
expected_called)
self.assertEqual(self.fake_port['port_uuid'], port['port_uuid'])
def test_destroy(self):
uuid = self.fake_port['port_uuid']
with mock.patch.object(self.dbapi, 'compute_port_destroy',
autospec=True) as mock_port_destroy:
port = objects.ComputePort(self.context, **self.fake_port)
port.destroy(self.context)
mock_port_destroy.assert_called_once_with(self.context, uuid)
def test_save(self):
uuid = self.fake_port['port_uuid']
with mock.patch.object(self.dbapi, 'compute_port_update',
autospec=True) as mock_port_update:
mock_port_update.return_value = self.fake_port
port = objects.ComputePort(self.context, **self.fake_port)
updates = port.obj_get_changes()
port.save(self.context)
mock_port_update.assert_called_once_with(
self.context, uuid, updates)
def test_save_after_refresh(self):
db_port = utils.create_test_compute_port(context=self.ctxt)
port = objects.ComputePort.get(self.context, db_port.port_uuid)
port.refresh(self.context)
port.save(self.context)

View File

@ -383,10 +383,6 @@ class _TestObject(object):
# The fingerprint values should only be changed if there is a version bump.
expected_object_fingerprints = {
'Server': '1.0-dc54162c0cc91fac43fed5304cd2c968',
'ComputeNode': '1.0-586e7eaadd4ec88a0506c4238ebdd7a5',
'ComputeNodeList': '1.0-33a2e1bb91ad4082f9f63429b77c1244',
'ComputePort': '1.0-996a1dc296ac109730e7976735488eb2',
'ComputePortList': '1.0-33a2e1bb91ad4082f9f63429b77c1244',
'ServerFault': '1.0-74349ff701259e4834b4e9dc2dac1b12',
'ServerFaultList': '1.0-43e8aad0258652921f929934e9e048fd',
'Flavor': '1.0-f53b71bd4aaaadea0d9284b811a82bb5',

View File

@ -92,64 +92,3 @@ def get_test_server_faults(**kw):
'updated_at': None},
]
}
def get_test_compute_node(ctxt, **kw):
"""Return a ComputeNode object with appropriate attributes.
NOTE: The object leaves the attributes marked as changed, such
that a create() could be used to commit it to the DB.
"""
kw['object_type'] = 'compute_node'
get_db_compute_node_checked = check_keyword_arguments(
db_utils.get_test_compute_node)
db_node = get_db_compute_node_checked(**kw)
# Let DB generate ID if it isn't specified explicitly
if 'id' not in kw:
del db_node['id']
node = objects.ComputeNode(ctxt)
for key in db_node:
if key != 'ports':
setattr(node, key, db_node[key])
return node
def create_test_compute_node(ctxt, **kw):
"""Create and return a test compute node object.
Create a compute node in the DB and return a ComputeNode object with
appropriate attributes.
"""
node = get_test_compute_node(ctxt, **kw)
node.create()
return node
def get_test_compute_port(ctxt, **kw):
"""Return a ComputePort object with appropriate attributes.
NOTE: The object leaves the attributes marked as changed, such
that a create() could be used to commit it to the DB.
"""
kw['object_type'] = 'compute_port'
get_db_compute_port_checked = check_keyword_arguments(
db_utils.get_test_compute_port)
db_port = get_db_compute_port_checked(**kw)
# Let DB generate ID if it isn't specified explicitly
if 'id' not in kw:
del db_port['id']
port = objects.ComputePort(ctxt, **db_port)
return port
def create_test_compute_port(ctxt, **kw):
"""Create and return a test compute port object.
Create a compute port in the DB and return a ComputePort object with
appropriate attributes.
"""
port = get_test_compute_port(ctxt, **kw)
port.create()
return port