summaryrefslogtreecommitdiff
path: root/nova/tests/functional/test_cold_migrate.py
blob: e07820ba2a60abe25d914765209014d9c673273d (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
# 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 nova.compute import api as compute_api
from nova import context as nova_context
from nova import objects
from nova import test
from nova.tests.functional import integrated_helpers


class ColdMigrationDisallowSameHost(
        integrated_helpers.ProviderUsageBaseTestCase):
    """Tests cold migrate where the source host does not have the
    COMPUTE_SAME_HOST_COLD_MIGRATE trait.
    """
    compute_driver = 'fake.MediumFakeDriver'

    def setUp(self):
        super(ColdMigrationDisallowSameHost, self).setUp()
        # Start one compute service which will use the fake virt driver
        # which disallows cold migration to the same host.
        self._start_compute('host1')

    def _wait_for_migrate_no_valid_host(self, error='NoValidHost'):
        event = self.notifier.wait_for_versioned_notifications(
            'compute_task.migrate_server.error')[0]
        self.assertEqual(error,
                         event['payload']['nova_object.data']['reason'][
                             'nova_object.data']['exception'])

    def test_cold_migrate_same_host_not_supported(self):
        """Simple test to show that you cannot cold-migrate to the same host
        when the resource provider does not expose the
        COMPUTE_SAME_HOST_COLD_MIGRATE trait.
        """
        server = self._create_server(networks='none')
        # The fake driver does not report COMPUTE_SAME_HOST_COLD_MIGRATE
        # so cold migration should fail since we only have one host.
        self.api.post_server_action(server['id'], {'migrate': None})
        self._wait_for_migrate_no_valid_host()

    def test_cold_migrate_same_host_old_compute_disallow(self):
        """Upgrade compat test where the resource provider does not report
        the COMPUTE_SAME_HOST_COLD_MIGRATE trait but the compute service is
        old so the API falls back to the allow_resize_to_same_host config which
        defaults to False.
        """
        server = self._create_server(networks='none')
        # Stub the compute service version check to make the compute service
        # appear old.
        fake_service = objects.Service()
        fake_service.version = (
            compute_api.MIN_COMPUTE_SAME_HOST_COLD_MIGRATE - 1)
        with mock.patch('nova.objects.Service.get_by_compute_host',
                        return_value=fake_service) as mock_get_service:
            self.api.post_server_action(server['id'], {'migrate': None})
        mock_get_service.assert_called_once_with(
            test.MatchType(nova_context.RequestContext), 'host1')
        # Since allow_resize_to_same_host defaults to False scheduling failed
        # since there are no other hosts.
        self._wait_for_migrate_no_valid_host()

    def test_cold_migrate_same_host_old_compute_allow(self):
        """Upgrade compat test where the resource provider does not report
        the COMPUTE_SAME_HOST_COLD_MIGRATE trait but the compute service is
        old so the API falls back to the allow_resize_to_same_host config which
        in this test is set to True.
        """
        self.flags(allow_resize_to_same_host=True)
        server = self._create_server(networks='none')
        # Stub the compute service version check to make the compute service
        # appear old.
        fake_service = objects.Service()
        fake_service.version = (
            compute_api.MIN_COMPUTE_SAME_HOST_COLD_MIGRATE - 1)
        with mock.patch('nova.objects.Service.get_by_compute_host',
                        return_value=fake_service) as mock_get_service:
            self.api.post_server_action(server['id'], {'migrate': None})
        mock_get_service.assert_called_once_with(
            test.MatchType(nova_context.RequestContext), 'host1')
        # In this case the compute is old so the API falls back to checking
        # allow_resize_to_same_host which says same-host cold migrate is
        # allowed so the scheduler sends the request to the only compute
        # available but the virt driver says same-host cold migrate is not
        # supported and raises UnableToMigrateToSelf. A reschedule is sent
        # to conductor which results in MaxRetriesExceeded since there are no
        # alternative hosts.
        self._wait_for_migrate_no_valid_host(error='MaxRetriesExceeded')


class ColdMigrationAllowSameHost(
        integrated_helpers.ProviderUsageBaseTestCase):
    """Tests cold migrate where the source host has the
    COMPUTE_SAME_HOST_COLD_MIGRATE trait.
    """
    compute_driver = 'fake.SameHostColdMigrateDriver'

    def setUp(self):
        super(ColdMigrationAllowSameHost, self).setUp()
        self._start_compute('host1')

    def test_cold_migrate_same_host_supported(self):
        """Simple test to show that you can cold-migrate to the same host
        when the resource provider exposes the COMPUTE_SAME_HOST_COLD_MIGRATE
        trait.
        """
        server = self._create_server(networks='none')
        self.api.post_server_action(server['id'], {'migrate': None})
        server = self._wait_for_state_change(server, 'VERIFY_RESIZE')
        self.assertEqual('host1', server['OS-EXT-SRV-ATTR:host'])