summaryrefslogtreecommitdiff
path: root/nova/tests/functional/regressions/test_bug_1713783.py
blob: 9a6a79d7a21817cc89e0dc16e0f3a7ef603dcab1 (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
# Copyright 2017 Ericsson
#
# 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 time

from oslo_log import log as logging

from nova import test
from nova.tests import fixtures as nova_fixtures
from nova.tests.functional import fixtures as func_fixtures
from nova.tests.functional import integrated_helpers
from nova.tests.unit import fake_network


LOG = logging.getLogger(__name__)


class FailedEvacuateStateTests(test.TestCase,
                               integrated_helpers.InstanceHelperMixin):
    """Regression Tests for bug #1713783

    When evacuation fails with NoValidHost, the migration status remains
    'accepted' instead of 'error'. This causes problem in case the compute
    service starts up again and looks for migrations with status 'accepted',
    as it then removes the local instances for those migrations even though
    the instance never actually migrated to another host.
    """

    microversion = 'latest'

    def setUp(self):
        super(FailedEvacuateStateTests, self).setUp()

        self.useFixture(nova_fixtures.RealPolicyFixture())
        self.useFixture(nova_fixtures.NeutronFixture(self))
        self.useFixture(nova_fixtures.GlanceFixture(self))
        self.useFixture(func_fixtures.PlacementFixture())

        api_fixture = self.useFixture(nova_fixtures.OSAPIFixture(
            api_version='v2.1'))

        self.api = api_fixture.admin_api
        self.api.microversion = self.microversion

        self.start_service('conductor')
        self.start_service('scheduler')

        self.hostname = 'host1'
        self.compute1 = self.start_service('compute', host=self.hostname)
        fake_network.set_stub_network_methods(self)

    def _wait_for_notification_event_type(self, event_type, max_retries=10):
        retry_counter = 0
        while True:
            if len(self.notifier.notifications) > 0:
                for notification in self.notifier.notifications:
                    if notification.event_type == event_type:
                        return
            if retry_counter == max_retries:
                self.fail('Wait for notification event type (%s) failed'
                          % event_type)
            retry_counter += 1
            time.sleep(0.5)

    def _boot_a_server(self):
        server_req = self._build_server(
            image_uuid='155d900f-4e14-4e4c-a73d-069cbf4541e6',
            networks='none')
        LOG.info('booting on %s', self.hostname)
        created_server = self.api.post_server({'server': server_req})
        return self._wait_for_state_change(created_server, 'ACTIVE')

    def test_evacuate_no_valid_host(self):
        # Boot a server
        server = self._boot_a_server()

        # Force source compute down
        compute_id = self.api.get_services(
            host=self.hostname, binary='nova-compute')[0]['id']
        self.api.put_service(compute_id, {'forced_down': 'true'})

        self.notifier = self.useFixture(
            nova_fixtures.NotificationFixture(self))

        # Initiate evacuation
        self._evacuate_server(
            server, expected_state='ERROR', expected_host=self.hostname,
            expected_migration_status='error')
        self._wait_for_notification_event_type('compute_task.rebuild_server')

        # Check migrations
        migrations = self.api.get_migrations()
        self.assertEqual(1, len(migrations))
        self.assertEqual('evacuation', migrations[0]['migration_type'])
        self.assertEqual(server['id'], migrations[0]['instance_uuid'])
        self.assertEqual(self.hostname, migrations[0]['source_compute'])
        self.assertEqual('error', migrations[0]['status'])