summaryrefslogtreecommitdiff
path: root/nova/tests/unit/objects/test_migration_context.py
blob: 12becaee38c3dd68491f4f95efd1fbe0f303a551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#    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 unittest import mock

from oslo_serialization import jsonutils
from oslo_utils.fixture import uuidsentinel as uuids

from nova import context
from nova import exception
from nova import objects
from nova.tests.unit.objects import test_instance_numa
from nova.tests.unit.objects import test_objects


fake_instance_uuid = uuids.fake

fake_migration_context_obj = objects.MigrationContext()
fake_migration_context_obj.instance_uuid = fake_instance_uuid
fake_migration_context_obj.migration_id = 42
fake_migration_context_obj.new_numa_topology = (
    test_instance_numa.fake_obj_numa_topology.obj_clone())
fake_migration_context_obj.old_numa_topology = None
fake_migration_context_obj.new_pci_devices = objects.PciDeviceList()
fake_migration_context_obj.old_pci_devices = None
fake_migration_context_obj.new_pci_requests = (
    objects.InstancePCIRequests(requests=[
        objects.InstancePCIRequest(count=123, spec=[])]))
fake_migration_context_obj.old_pci_requests = None
fake_migration_context_obj.new_resources = objects.ResourceList()
fake_migration_context_obj.old_resources = None

fake_db_context = {
    'created_at': None,
    'updated_at': None,
    'deleted_at': None,
    'deleted': 0,
    'instance_uuid': fake_instance_uuid,
    'migration_context': jsonutils.dumps(
        fake_migration_context_obj.obj_to_primitive()),
    }


def get_fake_migration_context_obj(ctxt):
    obj = fake_migration_context_obj.obj_clone()
    obj._context = ctxt
    return obj


def get_fake_migration_context_with_pci_devs(ctxt=None):
    obj = get_fake_migration_context_obj(ctxt)
    obj.old_pci_devices = objects.PciDeviceList(
        objects=[objects.PciDevice(vendor_id='1377',
                                   product_id='0047',
                                   address='0000:0a:00.1',
                                   compute_node_id=1,
                                   request_id=uuids.pcidev)])
    obj.new_pci_devices = objects.PciDeviceList(
        objects=[objects.PciDevice(vendor_id='1377',
                                   product_id='0047',
                                   address='0000:0b:00.1',
                                   compute_node_id=2,
                                   request_id=uuids.pcidev)])
    return obj


class _TestMigrationContext(object):

    def _test_get_by_instance_uuid(self, db_data):
        mig_context = objects.MigrationContext.get_by_instance_uuid(
            self.context, fake_db_context['instance_uuid'])
        if mig_context:
            self.assertEqual(fake_db_context['instance_uuid'],
                            mig_context.instance_uuid)
            expected_mig_context = db_data and db_data.get('migration_context')
            expected_mig_context = objects.MigrationContext.obj_from_db_obj(
                expected_mig_context)
            self.assertEqual(expected_mig_context.instance_uuid,
                             mig_context.instance_uuid)
            self.assertEqual(expected_mig_context.migration_id,
                             mig_context.migration_id)
            self.assertIsInstance(expected_mig_context.new_numa_topology,
                                  mig_context.new_numa_topology.__class__)
            self.assertIsInstance(expected_mig_context.old_numa_topology,
                                  mig_context.old_numa_topology.__class__)
            self.assertIsInstance(expected_mig_context.new_pci_devices,
                                  mig_context.new_pci_devices.__class__)
            self.assertIsInstance(expected_mig_context.old_pci_devices,
                                  mig_context.old_pci_devices.__class__)
            self.assertIsInstance(expected_mig_context.new_pci_requests,
                                  mig_context.new_pci_requests.__class__)
            self.assertIsInstance(expected_mig_context.old_pci_requests,
                                  mig_context.old_pci_requests.__class__)
            self.assertIsInstance(expected_mig_context. new_resources,
                                  mig_context.new_resources.__class__)
            self.assertIsInstance(expected_mig_context.old_resources,
                                  mig_context.old_resources.__class__)
        else:
            self.assertIsNone(mig_context)

    @mock.patch('nova.db.main.api.instance_extra_get_by_instance_uuid')
    def test_get_by_instance_uuid(self, mock_get):
        mock_get.return_value = fake_db_context
        self._test_get_by_instance_uuid(fake_db_context)

    @mock.patch('nova.db.main.api.instance_extra_get_by_instance_uuid')
    def test_get_by_instance_uuid_none(self, mock_get):
        db_context = fake_db_context.copy()
        db_context['migration_context'] = None
        mock_get.return_value = db_context
        self._test_get_by_instance_uuid(db_context)

    @mock.patch('nova.db.main.api.instance_extra_get_by_instance_uuid')
    def test_get_by_instance_uuid_missing(self, mock_get):
        mock_get.return_value = None
        self.assertRaises(
            exception.MigrationContextNotFound,
            objects.MigrationContext.get_by_instance_uuid,
            self.context, 'fake_uuid')

    @mock.patch('nova.objects.Migration.get_by_id',
                return_value=objects.Migration(cross_cell_move=True))
    def test_is_cross_cell_move(self, mock_get_by_id):
        ctxt = context.get_admin_context()
        mig_ctx = get_fake_migration_context_obj(ctxt)
        self.assertTrue(mig_ctx.is_cross_cell_move())
        mock_get_by_id.assert_called_once_with(ctxt, mig_ctx.migration_id)


class TestMigrationContext(test_objects._LocalTest, _TestMigrationContext):

    def test_pci_mapping_for_migration(self):
        mig_ctx = get_fake_migration_context_with_pci_devs()
        pci_mapping = mig_ctx.get_pci_mapping_for_migration(False)
        self.assertDictEqual(
            {mig_ctx.old_pci_devices[0].address: mig_ctx.new_pci_devices[0]},
            pci_mapping)

    def test_pci_mapping_for_migration_revert(self):
        mig_ctx = get_fake_migration_context_with_pci_devs()
        pci_mapping = mig_ctx.get_pci_mapping_for_migration(True)
        self.assertDictEqual(
            {mig_ctx.new_pci_devices[0].address: mig_ctx.old_pci_devices[0]},
            pci_mapping)


class TestMigrationContextRemote(test_objects._RemoteTest,
                                 _TestMigrationContext):
    pass