|
| 1 | +# Copyright 2020, Red Hat, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import mock |
| 16 | +import time |
| 17 | + |
| 18 | +from nova import context |
| 19 | +from nova import objects |
| 20 | +from nova.tests.functional import integrated_helpers |
| 21 | + |
| 22 | + |
| 23 | +class TestDuplicateVolAttachRace(integrated_helpers._IntegratedTestBase): |
| 24 | + """Regression test for bug #1937375 |
| 25 | +
|
| 26 | + A regression test to assert the behaviour of bug #1937375 where calls |
| 27 | + to reserve_block_device_name can race and create duplicate bdm records. |
| 28 | +
|
| 29 | + As we can't recreate the race with pure API driven requests in our |
| 30 | + functional tests this instead makes duplicate calls to |
| 31 | + reserve_block_device_name during an attach to mimic the behaviour. |
| 32 | + """ |
| 33 | + |
| 34 | + microversion = 'latest' |
| 35 | + |
| 36 | + # TODO(lyarwood): Copied from test_bug_1675570.py, move both into |
| 37 | + # _IntegratedTestBase. |
| 38 | + def _wait_for_volume_attach(self, server_id, volume_id): |
| 39 | + timeout = 0.0 |
| 40 | + server = self.api.get_server(server_id) |
| 41 | + attached_vols = [vol['id'] for vol in |
| 42 | + server['os-extended-volumes:volumes_attached']] |
| 43 | + |
| 44 | + while volume_id not in attached_vols and timeout < 10.0: |
| 45 | + time.sleep(.1) |
| 46 | + timeout += .1 |
| 47 | + server = self.api.get_server(server_id) |
| 48 | + attached_vols = [vol['id'] for vol in |
| 49 | + server['os-extended-volumes:volumes_attached']] |
| 50 | + |
| 51 | + if volume_id not in attached_vols: |
| 52 | + self.fail('Timed out waiting for volume %s to be attached to ' |
| 53 | + 'server %s. Currently attached volumes: %s' % |
| 54 | + (volume_id, server_id, attached_vols)) |
| 55 | + |
| 56 | + def test_duplicate_volume_attach_race(self): |
| 57 | + |
| 58 | + ctxt = context.get_admin_context() |
| 59 | + volume_id = self.cinder.IMAGE_BACKED_VOL |
| 60 | + server_id = self._create_server(networks='none')['id'] |
| 61 | + original_reserve_name = self.compute.manager.reserve_block_device_name |
| 62 | + |
| 63 | + def wrap_reserve_block_device_name(*args, **kwargs): |
| 64 | + # We can't cause a race with duplicate API requests as functional |
| 65 | + # tests are single threaded and the first would always complete |
| 66 | + # before the second was serviced. Instead we can wrap |
| 67 | + # reserve_block_device_name on the compute manager and call it |
| 68 | + # twice to mimic two callers racing each other after the checks on |
| 69 | + # the api. |
| 70 | + original_bdm = original_reserve_name(*args, **kwargs) |
| 71 | + original_reserve_name(*args, **kwargs) |
| 72 | + return original_bdm |
| 73 | + |
| 74 | + with mock.patch.object( |
| 75 | + self.compute.manager, |
| 76 | + 'reserve_block_device_name', |
| 77 | + wrap_reserve_block_device_name |
| 78 | + ): |
| 79 | + self.api.post_server_volume( |
| 80 | + server_id, {'volumeAttachment': {'volumeId': volume_id}}) |
| 81 | + |
| 82 | + # Wait for a volume to be attached |
| 83 | + self._wait_for_volume_attach(server_id, volume_id) |
| 84 | + |
| 85 | + # Fetch all bdms for the instance to assert what we have |
| 86 | + bdms = objects.BlockDeviceMappingList.get_by_instance_uuid( |
| 87 | + ctxt, server_id) |
| 88 | + |
| 89 | + # FIXME(lyarwood): This is bug #1937375, we now have 3 bdms for the |
| 90 | + # instance, the original root disk and two duplicate volume bdms for |
| 91 | + # the same volume attachment. |
| 92 | + self.assertEqual(3, len(bdms)) |
| 93 | + self.assertEqual(volume_id, bdms[2].volume_id) |
| 94 | + self.assertEqual(volume_id, bdms[1].volume_id) |
| 95 | + self.assertEqual('local', bdms[0].destination_type) |
0 commit comments