|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import mock |
| 14 | + |
| 15 | +from nova.compute import api as compute_api |
| 16 | +from nova import context as nova_context |
| 17 | +from nova import objects |
| 18 | +from nova import test |
| 19 | +from nova.tests.functional import integrated_helpers |
| 20 | +from nova.tests.unit import fake_notifier |
| 21 | + |
| 22 | + |
| 23 | +class ColdMigrationDisallowSameHost( |
| 24 | + integrated_helpers.ProviderUsageBaseTestCase): |
| 25 | + """Tests cold migrate where the source host does not have the |
| 26 | + COMPUTE_SAME_HOST_COLD_MIGRATE trait. |
| 27 | + """ |
| 28 | + compute_driver = 'fake.MediumFakeDriver' |
| 29 | + |
| 30 | + def setUp(self): |
| 31 | + super(ColdMigrationDisallowSameHost, self).setUp() |
| 32 | + # Start one compute service which will use the fake virt driver |
| 33 | + # which disallows cold migration to the same host. |
| 34 | + self._start_compute('host1') |
| 35 | + |
| 36 | + def _wait_for_migrate_no_valid_host(self, error='NoValidHost'): |
| 37 | + event = fake_notifier.wait_for_versioned_notifications( |
| 38 | + 'compute_task.migrate_server.error')[0] |
| 39 | + self.assertEqual(error, |
| 40 | + event['payload']['nova_object.data']['reason'][ |
| 41 | + 'nova_object.data']['exception']) |
| 42 | + |
| 43 | + def test_cold_migrate_same_host_not_supported(self): |
| 44 | + """Simple test to show that you cannot cold-migrate to the same host |
| 45 | + when the resource provider does not expose the |
| 46 | + COMPUTE_SAME_HOST_COLD_MIGRATE trait. |
| 47 | + """ |
| 48 | + server = self._create_server(networks='none') |
| 49 | + # The fake driver does not report COMPUTE_SAME_HOST_COLD_MIGRATE |
| 50 | + # so cold migration should fail since we only have one host. |
| 51 | + self.api.post_server_action(server['id'], {'migrate': None}) |
| 52 | + self._wait_for_migrate_no_valid_host() |
| 53 | + |
| 54 | + def test_cold_migrate_same_host_old_compute_disallow(self): |
| 55 | + """Upgrade compat test where the resource provider does not report |
| 56 | + the COMPUTE_SAME_HOST_COLD_MIGRATE trait but the compute service is |
| 57 | + old so the API falls back to the allow_resize_to_same_host config which |
| 58 | + defaults to False. |
| 59 | + """ |
| 60 | + server = self._create_server(networks='none') |
| 61 | + # Stub the compute service version check to make the compute service |
| 62 | + # appear old. |
| 63 | + fake_service = objects.Service() |
| 64 | + fake_service.version = ( |
| 65 | + compute_api.MIN_COMPUTE_SAME_HOST_COLD_MIGRATE - 1) |
| 66 | + with mock.patch('nova.objects.Service.get_by_compute_host', |
| 67 | + return_value=fake_service) as mock_get_service: |
| 68 | + self.api.post_server_action(server['id'], {'migrate': None}) |
| 69 | + mock_get_service.assert_called_once_with( |
| 70 | + test.MatchType(nova_context.RequestContext), 'host1') |
| 71 | + # Since allow_resize_to_same_host defaults to False scheduling failed |
| 72 | + # since there are no other hosts. |
| 73 | + self._wait_for_migrate_no_valid_host() |
| 74 | + |
| 75 | + def test_cold_migrate_same_host_old_compute_allow(self): |
| 76 | + """Upgrade compat test where the resource provider does not report |
| 77 | + the COMPUTE_SAME_HOST_COLD_MIGRATE trait but the compute service is |
| 78 | + old so the API falls back to the allow_resize_to_same_host config which |
| 79 | + in this test is set to True. |
| 80 | + """ |
| 81 | + self.flags(allow_resize_to_same_host=True) |
| 82 | + server = self._create_server(networks='none') |
| 83 | + # Stub the compute service version check to make the compute service |
| 84 | + # appear old. |
| 85 | + fake_service = objects.Service() |
| 86 | + fake_service.version = ( |
| 87 | + compute_api.MIN_COMPUTE_SAME_HOST_COLD_MIGRATE - 1) |
| 88 | + with mock.patch('nova.objects.Service.get_by_compute_host', |
| 89 | + return_value=fake_service) as mock_get_service: |
| 90 | + self.api.post_server_action(server['id'], {'migrate': None}) |
| 91 | + mock_get_service.assert_called_once_with( |
| 92 | + test.MatchType(nova_context.RequestContext), 'host1') |
| 93 | + # In this case the compute is old so the API falls back to checking |
| 94 | + # allow_resize_to_same_host which says same-host cold migrate is |
| 95 | + # allowed so the scheduler sends the request to the only compute |
| 96 | + # available but the virt driver says same-host cold migrate is not |
| 97 | + # supported and raises UnableToMigrateToSelf. A reschedule is sent |
| 98 | + # to conductor which results in MaxRetriesExceeded since there are no |
| 99 | + # alternative hosts. |
| 100 | + self._wait_for_migrate_no_valid_host(error='MaxRetriesExceeded') |
| 101 | + |
| 102 | + |
| 103 | +class ColdMigrationAllowSameHost( |
| 104 | + integrated_helpers.ProviderUsageBaseTestCase): |
| 105 | + """Tests cold migrate where the source host has the |
| 106 | + COMPUTE_SAME_HOST_COLD_MIGRATE trait. |
| 107 | + """ |
| 108 | + compute_driver = 'fake.SameHostColdMigrateDriver' |
| 109 | + |
| 110 | + def setUp(self): |
| 111 | + super(ColdMigrationAllowSameHost, self).setUp() |
| 112 | + self._start_compute('host1') |
| 113 | + |
| 114 | + def test_cold_migrate_same_host_supported(self): |
| 115 | + """Simple test to show that you can cold-migrate to the same host |
| 116 | + when the resource provider exposes the COMPUTE_SAME_HOST_COLD_MIGRATE |
| 117 | + trait. |
| 118 | + """ |
| 119 | + server = self._create_server(networks='none') |
| 120 | + self.api.post_server_action(server['id'], {'migrate': None}) |
| 121 | + server = self._wait_for_state_change(server, 'VERIFY_RESIZE') |
| 122 | + self.assertEqual('host1', server['OS-EXT-SRV-ATTR:host']) |
0 commit comments