nova/nova/tests/unit/objects/test_instance_device_metadata.py
Chris Dent def4b17934 Use nova.db.api directly
nova/db/__init__.py was importing * from nova.db.api. This meant that
any time any code anywhere within the nova.db package was imported
then nova.db.api was too, leading to a cascade of imports that may
not have been desired. Also, in general, code in __init__.py is a pain.

Therefore, this change adjusts code that so that either:

* nova.db.api is used directly
* nova.db.api is imported as 'db'

In either case, the functionality remains the same.

The primary goal of this change was to make it possible to import the
model files without having to import the db api. Moving the model files
to a different place in the directory hierarchy was considered, but
given that "code in __init__.py is a pain" this mode was chosen.

This looks like a very large change, but it is essentially adjusting
package names, many in mocks.

Change-Id: Ic1fd7c87ceda05eeb96735da2a415ef37060bb1a
2018-07-10 14:56:27 +00:00

103 lines
4.2 KiB
Python

# 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 mock
from oslo_serialization import jsonutils
from nova import objects
from nova.tests.unit.objects import test_objects
fake_net_interface_meta = objects.NetworkInterfaceMetadata(
mac='52:54:00:f6:35:8f',
tags=['mytag1'],
bus=objects.PCIDeviceBus(address='0000:00:03.0'),
vlan=1000)
fake_pci_disk_meta = objects.DiskMetadata(
bus=objects.PCIDeviceBus(address='0000:00:09.0'),
tags=['nfvfunc3'])
fake_obj_devices_metadata = objects.InstanceDeviceMetadata(
devices=[fake_net_interface_meta, fake_pci_disk_meta])
fake_devices_metadata = fake_obj_devices_metadata._to_json()
fake_db_metadata = {
'created_at': None,
'updated_at': None,
'deleted_at': None,
'deleted': 0,
'id': 1,
'device_metadata': fake_obj_devices_metadata._to_json()
}
fake_old_db_metadata = dict(fake_db_metadata) # copy
fake_old_db_metadata['device_metadata'] = jsonutils.dumps(
fake_devices_metadata)
def get_fake_obj_device_metadata(context):
fake_obj_devices_metadata_cpy = fake_obj_devices_metadata.obj_clone()
fake_obj_devices_metadata_cpy._context = context
return fake_obj_devices_metadata_cpy
class _TestInstanceDeviceMetadata(object):
def _check_object(self, obj_meta):
self.assertTrue(isinstance(obj_meta,
objects.NetworkInterfaceMetadata) or
isinstance(obj_meta, objects.DiskMetadata))
if isinstance(obj_meta, objects.NetworkInterfaceMetadata):
self.assertEqual(obj_meta.mac, '52:54:00:f6:35:8f')
self.assertEqual(obj_meta.tags, ['mytag1'])
self.assertIsInstance(obj_meta.bus, objects.PCIDeviceBus)
self.assertEqual(obj_meta.bus.address, '0000:00:03.0')
self.assertEqual(obj_meta.vlan, 1000)
elif isinstance(obj_meta, objects.DiskMetadata):
self.assertIsInstance(obj_meta.bus, objects.PCIDeviceBus)
self.assertEqual(obj_meta.bus.address, '0000:00:09.0')
self.assertEqual(obj_meta.tags, ['nfvfunc3'])
@mock.patch('nova.db.api.instance_extra_get_by_instance_uuid')
def test_get_by_instance_uuid(self, mock_get):
mock_get.return_value = fake_db_metadata
inst_meta = objects.InstanceDeviceMetadata
dev_meta = inst_meta.get_by_instance_uuid(
self.context, 'fake_uuid')
for obj_meta, fake_meta in zip(
dev_meta.devices,
fake_obj_devices_metadata.devices):
self._check_object(obj_meta)
def test_obj_from_db(self):
db_meta = fake_db_metadata['device_metadata']
metadata = objects.InstanceDeviceMetadata.obj_from_db(None, db_meta)
for obj_meta in metadata.devices:
self._check_object(obj_meta)
def test_net_if_compatible_pre_1_1(self):
vif_obj = objects.NetworkInterfaceMetadata(mac='52:54:00:f6:35:8f')
vif_obj.tags = ['test']
vif_obj.vlan = 1000
primitive = vif_obj.obj_to_primitive()
self.assertIn('vlan', primitive['nova_object.data'])
vif_obj.obj_make_compatible(primitive['nova_object.data'], '1.0')
self.assertNotIn('vlan', primitive['nova_object.data'])
class TestInstanceDeviceMetadata(test_objects._LocalTest,
_TestInstanceDeviceMetadata):
pass
class TestInstanceDeviceMetadataRemote(test_objects._RemoteTest,
_TestInstanceDeviceMetadata):
pass