blob: 32d6498c15967a012a48487b98ccb3ac02b5c1b8 [file] [log] [blame]
Matthew Treinish7b015822014-01-21 18:15:39 +00001# 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
15import time
Sean McGinniseed80742020-04-18 12:01:03 -050016from unittest import mock
Matthew Treinish7b015822014-01-21 18:15:39 +000017
Lee Yarwoodc1b2a4a2020-01-08 17:02:49 +000018from oslo_utils.fixture import uuidsentinel as uuids
Matthew Treinish7b015822014-01-21 18:15:39 +000019
20from tempest.common import waiters
21from tempest import exceptions
guo yunxianebb15f22016-11-01 21:03:35 +080022from tempest.lib import exceptions as lib_exc
lkuchlanf53947e2016-09-15 10:37:57 +030023from tempest.lib.services.volume.v2 import volumes_client
Matthew Treinishffad78a2016-04-16 14:39:52 -040024from tempest.tests import base
Jordan Pittier0e53b612016-03-03 14:23:17 +010025import tempest.tests.utils as utils
Matthew Treinish7b015822014-01-21 18:15:39 +000026
27
28class 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 Ohmichi5d410762015-05-22 01:10:03 +000036 self.client.show_image.return_value = ({'status': 'active'})
Matthew Treinish7b015822014-01-21 18:15:39 +000037 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 Vancsics64862f72016-11-08 09:12:31 +010041 self.assertLess((end_time - start_time), 10)
Matthew Treinish7b015822014-01-21 18:15:39 +000042
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +030043 def test_wait_for_image_status_timeout(self):
Jordan Pittier0e53b612016-03-03 14:23:17 +010044 time_mock = self.patch('time.time')
45 time_mock.side_effect = utils.generate_timeout_series(1)
46
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000047 self.client.show_image.return_value = ({'status': 'saving'})
guo yunxianebb15f22016-11-01 21:03:35 +080048 self.assertRaises(lib_exc.TimeoutException,
Matthew Treinish7b015822014-01-21 18:15:39 +000049 waiters.wait_for_image_status,
50 self.client, 'fake_image_id', 'active')
51
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +030052 def test_wait_for_image_status_error_on_image_create(self):
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +000053 self.client.show_image.return_value = ({'status': 'ERROR'})
Matthew Treinish7b015822014-01-21 18:15:39 +000054 self.assertRaises(exceptions.AddImageException,
55 waiters.wait_for_image_status,
56 self.client, 'fake_image_id', 'active')
Matt Riedemannf77e7dc2015-08-10 16:39:39 -070057
Artom Lifshitzdf0d6d72018-05-11 11:31:11 -040058
59class TestInterfaceWaiters(base.TestCase):
Artom Lifshitzdf0d6d72018-05-11 11:31:11 -040060
Federico Ressi8827d382018-06-12 23:27:00 +020061 build_timeout = 1.
62 build_interval = 1
63 port_down = {'interfaceAttachment': {'port_state': 'DOWN'}}
64 port_active = {'interfaceAttachment': {'port_state': 'ACTIVE'}}
Artom Lifshitzdf0d6d72018-05-11 11:31:11 -040065
Federico Ressi8827d382018-06-12 23:27:00 +020066 def mock_client(self, **kwargs):
67 return mock.MagicMock(
68 build_timeout=self.build_timeout,
69 build_interval=self.build_interval,
70 **kwargs)
Artom Lifshitzdf0d6d72018-05-11 11:31:11 -040071
72 def test_wait_for_interface_status(self):
Federico Ressi8827d382018-06-12 23:27:00 +020073 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 Lifshitzdf0d6d72018-05-11 11:31:11 -040086
87 def test_wait_for_interface_status_timeout(self):
Federico Ressi8827d382018-06-12 23:27:00 +020088 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 Lifshitzdf0d6d72018-05-11 11:31:11 -040092
Artom Lifshitzdf0d6d72018-05-11 11:31:11 -040093 self.assertRaises(lib_exc.TimeoutException,
94 waiters.wait_for_interface_status,
Federico Ressi8827d382018-06-12 23:27:00 +020095 client, 'server_id', 'port_id', 'ACTIVE')
Artom Lifshitz3306d422018-03-22 12:20:54 -040096
Federico Ressi8827d382018-06-12 23:27:00 +020097 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 Lifshitz3306d422018-03-22 12:20:54 -0400100
Federico Ressi8827d382018-06-12 23:27:00 +0200101 one_interface = {'interfaceAttachments': [{'port_id': 'port_one'}]}
102 two_interfaces = {'interfaceAttachments': [{'port_id': 'port_one'},
103 {'port_id': 'port_two'}]}
Artom Lifshitz3306d422018-03-22 12:20:54 -0400104
105 def test_wait_for_interface_detach(self):
Federico Ressi8827d382018-06-12 23:27:00 +0200106 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 Lifshitz3306d422018-03-22 12:20:54 -0400119
120 def test_wait_for_interface_detach_timeout(self):
Federico Ressi8827d382018-06-12 23:27:00 +0200121 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 Lifshitz3306d422018-03-22 12:20:54 -0400125
Artom Lifshitz3306d422018-03-22 12:20:54 -0400126 self.assertRaises(lib_exc.TimeoutException,
127 waiters.wait_for_interface_detach,
Federico Ressi8827d382018-06-12 23:27:00 +0200128 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 Yarwoode5597402019-02-15 20:17:00 +0000133
134
135class 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 Yarwood9e202d82020-01-08 16:41:32 +0000198
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 Yarwoodc1b2a4a2020-01-08 17:02:49 +0000236
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 Penchevdc4ceae2020-09-09 00:47:50 +0300252 show_volume.assert_has_calls([mock.call(uuids.volume_id),
253 mock.call(uuids.volume_id),
254 mock.call(uuids.volume_id)])
Lee Yarwoodc1b2a4a2020-01-08 17:02:49 +0000255
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)