Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 1 | # Copyright 2014 IBM Corp. |
| 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 time |
Sean McGinnis | eed8074 | 2020-04-18 12:01:03 -0500 | [diff] [blame] | 16 | from unittest import mock |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 17 | |
Lee Yarwood | c1b2a4a | 2020-01-08 17:02:49 +0000 | [diff] [blame] | 18 | from oslo_utils.fixture import uuidsentinel as uuids |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 19 | |
| 20 | from tempest.common import waiters |
| 21 | from tempest import exceptions |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
lkuchlan | f53947e | 2016-09-15 10:37:57 +0300 | [diff] [blame] | 23 | from tempest.lib.services.volume.v2 import volumes_client |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 24 | from tempest.tests import base |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 25 | import tempest.tests.utils as utils |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class TestImageWaiters(base.TestCase): |
| 29 | def setUp(self): |
| 30 | super(TestImageWaiters, self).setUp() |
| 31 | self.client = mock.MagicMock() |
| 32 | self.client.build_timeout = 1 |
| 33 | self.client.build_interval = 1 |
| 34 | |
| 35 | def test_wait_for_image_status(self): |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 36 | self.client.show_image.return_value = ({'status': 'active'}) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 37 | start_time = int(time.time()) |
| 38 | waiters.wait_for_image_status(self.client, 'fake_image_id', 'active') |
| 39 | end_time = int(time.time()) |
| 40 | # Ensure waiter returns before build_timeout |
Béla Vancsics | 64862f7 | 2016-11-08 09:12:31 +0100 | [diff] [blame] | 41 | self.assertLess((end_time - start_time), 10) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 42 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 43 | def test_wait_for_image_status_timeout(self): |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 44 | time_mock = self.patch('time.time') |
| 45 | time_mock.side_effect = utils.generate_timeout_series(1) |
| 46 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 47 | self.client.show_image.return_value = ({'status': 'saving'}) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 48 | self.assertRaises(lib_exc.TimeoutException, |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 49 | waiters.wait_for_image_status, |
| 50 | self.client, 'fake_image_id', 'active') |
| 51 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 52 | def test_wait_for_image_status_error_on_image_create(self): |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 53 | self.client.show_image.return_value = ({'status': 'ERROR'}) |
Matthew Treinish | 7b01582 | 2014-01-21 18:15:39 +0000 | [diff] [blame] | 54 | self.assertRaises(exceptions.AddImageException, |
| 55 | waiters.wait_for_image_status, |
| 56 | self.client, 'fake_image_id', 'active') |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 57 | |
Artom Lifshitz | df0d6d7 | 2018-05-11 11:31:11 -0400 | [diff] [blame] | 58 | |
| 59 | class TestInterfaceWaiters(base.TestCase): |
Artom Lifshitz | df0d6d7 | 2018-05-11 11:31:11 -0400 | [diff] [blame] | 60 | |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 61 | build_timeout = 1. |
| 62 | build_interval = 1 |
| 63 | port_down = {'interfaceAttachment': {'port_state': 'DOWN'}} |
| 64 | port_active = {'interfaceAttachment': {'port_state': 'ACTIVE'}} |
Artom Lifshitz | df0d6d7 | 2018-05-11 11:31:11 -0400 | [diff] [blame] | 65 | |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 66 | def mock_client(self, **kwargs): |
| 67 | return mock.MagicMock( |
| 68 | build_timeout=self.build_timeout, |
| 69 | build_interval=self.build_interval, |
| 70 | **kwargs) |
Artom Lifshitz | df0d6d7 | 2018-05-11 11:31:11 -0400 | [diff] [blame] | 71 | |
| 72 | def test_wait_for_interface_status(self): |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 73 | show_interface = mock.Mock( |
| 74 | side_effect=[self.port_down, self.port_active]) |
| 75 | client = self.mock_client(show_interface=show_interface) |
| 76 | self.patch('time.time', return_value=0.) |
| 77 | sleep = self.patch('time.sleep') |
| 78 | |
| 79 | result = waiters.wait_for_interface_status( |
| 80 | client, 'server_id', 'port_id', 'ACTIVE') |
| 81 | |
| 82 | self.assertIs(self.port_active['interfaceAttachment'], result) |
| 83 | show_interface.assert_has_calls([mock.call('server_id', 'port_id'), |
| 84 | mock.call('server_id', 'port_id')]) |
| 85 | sleep.assert_called_once_with(client.build_interval) |
Artom Lifshitz | df0d6d7 | 2018-05-11 11:31:11 -0400 | [diff] [blame] | 86 | |
| 87 | def test_wait_for_interface_status_timeout(self): |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 88 | show_interface = mock.MagicMock(return_value=self.port_down) |
| 89 | client = self.mock_client(show_interface=show_interface) |
| 90 | self.patch('time.time', side_effect=[0., client.build_timeout + 1.]) |
| 91 | sleep = self.patch('time.sleep') |
Artom Lifshitz | df0d6d7 | 2018-05-11 11:31:11 -0400 | [diff] [blame] | 92 | |
Artom Lifshitz | df0d6d7 | 2018-05-11 11:31:11 -0400 | [diff] [blame] | 93 | self.assertRaises(lib_exc.TimeoutException, |
| 94 | waiters.wait_for_interface_status, |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 95 | client, 'server_id', 'port_id', 'ACTIVE') |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 96 | |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 97 | show_interface.assert_has_calls([mock.call('server_id', 'port_id'), |
| 98 | mock.call('server_id', 'port_id')]) |
| 99 | sleep.assert_called_once_with(client.build_interval) |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 100 | |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 101 | one_interface = {'interfaceAttachments': [{'port_id': 'port_one'}]} |
| 102 | two_interfaces = {'interfaceAttachments': [{'port_id': 'port_one'}, |
| 103 | {'port_id': 'port_two'}]} |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 104 | |
| 105 | def test_wait_for_interface_detach(self): |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 106 | list_interfaces = mock.MagicMock( |
| 107 | side_effect=[self.two_interfaces, self.one_interface]) |
| 108 | client = self.mock_client(list_interfaces=list_interfaces) |
| 109 | self.patch('time.time', return_value=0.) |
| 110 | sleep = self.patch('time.sleep') |
| 111 | |
| 112 | result = waiters.wait_for_interface_detach( |
| 113 | client, 'server_id', 'port_two') |
| 114 | |
| 115 | self.assertIs(self.one_interface['interfaceAttachments'], result) |
| 116 | list_interfaces.assert_has_calls([mock.call('server_id'), |
| 117 | mock.call('server_id')]) |
| 118 | sleep.assert_called_once_with(client.build_interval) |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 119 | |
| 120 | def test_wait_for_interface_detach_timeout(self): |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 121 | list_interfaces = mock.MagicMock(return_value=self.one_interface) |
| 122 | client = self.mock_client(list_interfaces=list_interfaces) |
| 123 | self.patch('time.time', side_effect=[0., client.build_timeout + 1.]) |
| 124 | sleep = self.patch('time.sleep') |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 125 | |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 126 | self.assertRaises(lib_exc.TimeoutException, |
| 127 | waiters.wait_for_interface_detach, |
Federico Ressi | 8827d38 | 2018-06-12 23:27:00 +0200 | [diff] [blame] | 128 | client, 'server_id', 'port_one') |
| 129 | |
| 130 | list_interfaces.assert_has_calls([mock.call('server_id'), |
| 131 | mock.call('server_id')]) |
| 132 | sleep.assert_called_once_with(client.build_interval) |
Lee Yarwood | e559740 | 2019-02-15 20:17:00 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | class TestVolumeWaiters(base.TestCase): |
| 136 | vol_migrating_src_host = { |
| 137 | 'volume': {'migration_status': 'migrating', |
| 138 | 'os-vol-host-attr:host': 'src_host@backend#type'}} |
| 139 | vol_migrating_dst_host = { |
| 140 | 'volume': {'migration_status': 'migrating', |
| 141 | 'os-vol-host-attr:host': 'dst_host@backend#type'}} |
| 142 | vol_migration_success = { |
| 143 | 'volume': {'migration_status': 'success', |
| 144 | 'os-vol-host-attr:host': 'dst_host@backend#type'}} |
| 145 | vol_migration_error = { |
| 146 | 'volume': {'migration_status': 'error', |
| 147 | 'os-vol-host-attr:host': 'src_host@backend#type'}} |
| 148 | |
| 149 | def test_wait_for_volume_migration_timeout(self): |
| 150 | show_volume = mock.MagicMock(return_value=self.vol_migrating_src_host) |
| 151 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 152 | resource_type="volume", |
| 153 | build_interval=1, |
| 154 | build_timeout=1, |
| 155 | show_volume=show_volume) |
| 156 | self.patch('time.time', side_effect=[0., client.build_timeout + 1.]) |
| 157 | self.patch('time.sleep') |
| 158 | self.assertRaises(lib_exc.TimeoutException, |
| 159 | waiters.wait_for_volume_migration, |
| 160 | client, mock.sentinel.volume_id, 'dst_host') |
| 161 | |
| 162 | def test_wait_for_volume_migration_error(self): |
| 163 | show_volume = mock.MagicMock(side_effect=[ |
| 164 | self.vol_migrating_src_host, |
| 165 | self.vol_migrating_src_host, |
| 166 | self.vol_migration_error]) |
| 167 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 168 | resource_type="volume", |
| 169 | build_interval=1, |
| 170 | build_timeout=1, |
| 171 | show_volume=show_volume) |
| 172 | self.patch('time.time', return_value=0.) |
| 173 | self.patch('time.sleep') |
| 174 | self.assertRaises(lib_exc.TempestException, |
| 175 | waiters.wait_for_volume_migration, |
| 176 | client, mock.sentinel.volume_id, 'dst_host') |
| 177 | |
| 178 | def test_wait_for_volume_migration_success_and_dst(self): |
| 179 | show_volume = mock.MagicMock(side_effect=[ |
| 180 | self.vol_migrating_src_host, |
| 181 | self.vol_migrating_dst_host, |
| 182 | self.vol_migration_success]) |
| 183 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 184 | resource_type="volume", |
| 185 | build_interval=1, |
| 186 | build_timeout=1, |
| 187 | show_volume=show_volume) |
| 188 | self.patch('time.time', return_value=0.) |
| 189 | self.patch('time.sleep') |
| 190 | waiters.wait_for_volume_migration( |
| 191 | client, mock.sentinel.volume_id, 'dst_host') |
| 192 | |
| 193 | # Assert that we wait until migration_status is success and dst_host is |
| 194 | # part of the returned os-vol-host-attr:host. |
| 195 | show_volume.assert_has_calls([mock.call(mock.sentinel.volume_id), |
| 196 | mock.call(mock.sentinel.volume_id), |
| 197 | mock.call(mock.sentinel.volume_id)]) |
Lee Yarwood | 9e202d8 | 2020-01-08 16:41:32 +0000 | [diff] [blame] | 198 | |
| 199 | @mock.patch.object(time, 'sleep') |
| 200 | def test_wait_for_volume_status_error_restoring(self, mock_sleep): |
| 201 | # Tests that the wait method raises VolumeRestoreErrorException if |
| 202 | # the volume status is 'error_restoring'. |
| 203 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 204 | resource_type="volume", |
| 205 | build_interval=1) |
| 206 | volume1 = {'volume': {'status': 'restoring-backup'}} |
| 207 | volume2 = {'volume': {'status': 'error_restoring'}} |
| 208 | mock_show = mock.Mock(side_effect=(volume1, volume2)) |
| 209 | client.show_volume = mock_show |
| 210 | volume_id = '7532b91e-aa0a-4e06-b3e5-20c0c5ee1caa' |
| 211 | self.assertRaises(exceptions.VolumeRestoreErrorException, |
| 212 | waiters.wait_for_volume_resource_status, |
| 213 | client, volume_id, 'available') |
| 214 | mock_show.assert_has_calls([mock.call(volume_id), |
| 215 | mock.call(volume_id)]) |
| 216 | mock_sleep.assert_called_once_with(1) |
| 217 | |
| 218 | @mock.patch.object(time, 'sleep') |
| 219 | def test_wait_for_volume_status_error_extending(self, mock_sleep): |
| 220 | # Tests that the wait method raises VolumeExtendErrorException if |
| 221 | # the volume status is 'error_extending'. |
| 222 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 223 | resource_type="volume", |
| 224 | build_interval=1) |
| 225 | volume1 = {'volume': {'status': 'extending'}} |
| 226 | volume2 = {'volume': {'status': 'error_extending'}} |
| 227 | mock_show = mock.Mock(side_effect=(volume1, volume2)) |
| 228 | client.show_volume = mock_show |
| 229 | volume_id = '7532b91e-aa0a-4e06-b3e5-20c0c5ee1caa' |
| 230 | self.assertRaises(exceptions.VolumeExtendErrorException, |
| 231 | waiters.wait_for_volume_resource_status, |
| 232 | client, volume_id, 'available') |
| 233 | mock_show.assert_has_calls([mock.call(volume_id), |
| 234 | mock.call(volume_id)]) |
| 235 | mock_sleep.assert_called_once_with(1) |
Lee Yarwood | c1b2a4a | 2020-01-08 17:02:49 +0000 | [diff] [blame] | 236 | |
| 237 | def test_wait_for_volume_attachment(self): |
| 238 | vol_detached = {'volume': {'attachments': []}} |
| 239 | vol_attached = {'volume': {'attachments': [ |
| 240 | {'attachment_id': uuids.attachment_id}]}} |
| 241 | show_volume = mock.MagicMock(side_effect=[ |
| 242 | vol_attached, vol_attached, vol_detached]) |
| 243 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 244 | build_interval=1, |
| 245 | build_timeout=5, |
| 246 | show_volume=show_volume) |
| 247 | self.patch('time.time') |
| 248 | self.patch('time.sleep') |
| 249 | waiters.wait_for_volume_attachment_remove(client, uuids.volume_id, |
| 250 | uuids.attachment_id) |
| 251 | # Assert that show volume is called until the attachment is removed. |
Peter Penchev | dc4ceae | 2020-09-09 00:47:50 +0300 | [diff] [blame^] | 252 | show_volume.assert_has_calls([mock.call(uuids.volume_id), |
| 253 | mock.call(uuids.volume_id), |
| 254 | mock.call(uuids.volume_id)]) |
Lee Yarwood | c1b2a4a | 2020-01-08 17:02:49 +0000 | [diff] [blame] | 255 | |
| 256 | def test_wait_for_volume_attachment_timeout(self): |
| 257 | show_volume = mock.MagicMock(return_value={ |
| 258 | 'volume': {'attachments': [ |
| 259 | {'attachment_id': uuids.attachment_id}]}}) |
| 260 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 261 | build_interval=1, |
| 262 | build_timeout=1, |
| 263 | show_volume=show_volume) |
| 264 | self.patch('time.time', side_effect=[0., client.build_timeout + 1.]) |
| 265 | self.patch('time.sleep') |
| 266 | # Assert that a timeout is raised if the attachment remains. |
| 267 | self.assertRaises(lib_exc.TimeoutException, |
| 268 | waiters.wait_for_volume_attachment_remove, |
| 269 | client, uuids.volume_id, uuids.attachment_id) |
| 270 | |
| 271 | def test_wait_for_volume_attachment_not_present(self): |
| 272 | show_volume = mock.MagicMock(return_value={ |
| 273 | 'volume': {'attachments': []}}) |
| 274 | client = mock.Mock(spec=volumes_client.VolumesClient, |
| 275 | build_interval=1, |
| 276 | build_timeout=1, |
| 277 | show_volume=show_volume) |
| 278 | self.patch('time.time', side_effect=[0., client.build_timeout + 1.]) |
| 279 | self.patch('time.sleep') |
| 280 | waiters.wait_for_volume_attachment_remove(client, uuids.volume_id, |
| 281 | uuids.attachment_id) |
| 282 | # Assert that show volume is only called once before we return |
| 283 | show_volume.assert_called_once_with(uuids.volume_id) |