blob: e3c33c7cabd5a2bb287768a375735861af0ebaee [file] [log] [blame]
Attila Fazekas0abbc952013-07-01 19:19:42 +02001# 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
lkuchlan52d7b0d2016-11-07 20:53:19 +020013import re
Attila Fazekas0abbc952013-07-01 19:19:42 +020014import time
15
Doug Hellmann583ce2c2015-03-11 14:55:46 +000016from oslo_log import log as logging
Matthew Treinish01472ff2015-02-20 17:26:52 -050017
Ken'ichi Ohmichi01151e82016-06-10 11:19:52 -070018from tempest.common import image as common_image
Attila Fazekas0abbc952013-07-01 19:19:42 +020019from tempest import config
20from tempest import exceptions
zhufl88c89b52016-07-01 18:09:05 +080021from tempest.lib.common.utils import test_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050022from tempest.lib import exceptions as lib_exc
Ken'ichi Ohmichi41721012016-06-22 10:41:26 -070023from tempest.lib.services.image.v1 import images_client as images_v1_client
Attila Fazekas0abbc952013-07-01 19:19:42 +020024
Sean Dague86bd8422013-12-20 09:56:44 -050025CONF = config.CONF
Attila Fazekas0abbc952013-07-01 19:19:42 +020026LOG = logging.getLogger(__name__)
27
28
Matt Riedemannf748c112017-02-03 11:33:11 -050029def _get_task_state(body):
30 return body.get('OS-EXT-STS:task_state', None)
31
32
Attila Fazekas0abbc952013-07-01 19:19:42 +020033# NOTE(afazekas): This function needs to know a token and a subject.
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +090034def wait_for_server_status(client, server_id, status, ready_wait=True,
Zhi Kun Liue5401762013-09-11 20:45:48 +080035 extra_timeout=0, raise_on_error=True):
Attila Fazekas0abbc952013-07-01 19:19:42 +020036 """Waits for a server to reach a given status."""
37
Attila Fazekas0abbc952013-07-01 19:19:42 +020038 # NOTE(afazekas): UNKNOWN status possible on ERROR
39 # or in a very early stage.
ghanshyam0f825252015-08-25 16:02:50 +090040 body = client.show_server(server_id)['server']
Attila Fazekas0abbc952013-07-01 19:19:42 +020041 old_status = server_status = body['status']
42 old_task_state = task_state = _get_task_state(body)
43 start_time = int(time.time())
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +090044 timeout = client.build_timeout + extra_timeout
Attila Fazekas0abbc952013-07-01 19:19:42 +020045 while True:
46 # NOTE(afazekas): Now the BUILD status only reached
Shane Wang111f86c2014-02-20 16:40:22 +080047 # between the UNKNOWN->ACTIVE transition.
Attila Fazekas0abbc952013-07-01 19:19:42 +020048 # TODO(afazekas): enumerate and validate the stable status set
49 if status == 'BUILD' and server_status != 'UNKNOWN':
50 return
51 if server_status == status:
52 if ready_wait:
53 if status == 'BUILD':
54 return
55 # NOTE(afazekas): The instance is in "ready for action state"
56 # when no task in progress
Ken'ichi Ohmichi534a05e2016-08-03 14:45:15 -070057 if task_state is None:
Attila Fazekas0abbc952013-07-01 19:19:42 +020058 # without state api extension 3 sec usually enough
Sean Dague86bd8422013-12-20 09:56:44 -050059 time.sleep(CONF.compute.ready_wait)
Attila Fazekas0abbc952013-07-01 19:19:42 +020060 return
61 else:
62 return
63
64 time.sleep(client.build_interval)
ghanshyam0f825252015-08-25 16:02:50 +090065 body = client.show_server(server_id)['server']
Attila Fazekas0abbc952013-07-01 19:19:42 +020066 server_status = body['status']
67 task_state = _get_task_state(body)
68 if (server_status != old_status) or (task_state != old_task_state):
69 LOG.info('State transition "%s" ==> "%s" after %d second wait',
70 '/'.join((old_status, str(old_task_state))),
71 '/'.join((server_status, str(task_state))),
72 time.time() - start_time)
Zhi Kun Liue5401762013-09-11 20:45:48 +080073 if (server_status == 'ERROR') and raise_on_error:
Attila Fazekas0462a7f2014-06-20 07:38:06 +020074 if 'fault' in body:
75 raise exceptions.BuildErrorException(body['fault'],
Matthew Treinish1d14c542014-06-17 20:25:40 -040076 server_id=server_id)
Attila Fazekas0462a7f2014-06-20 07:38:06 +020077 else:
78 raise exceptions.BuildErrorException(server_id=server_id)
Attila Fazekas0abbc952013-07-01 19:19:42 +020079
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +090080 timed_out = int(time.time()) - start_time >= timeout
Attila Fazekas0abbc952013-07-01 19:19:42 +020081
82 if timed_out:
Matt Riedemann629fa7c2013-12-11 18:20:56 -080083 expected_task_state = 'None' if ready_wait else 'n/a'
84 message = ('Server %(server_id)s failed to reach %(status)s '
85 'status and task state "%(expected_task_state)s" '
86 'within the required time (%(timeout)s s).' %
87 {'server_id': server_id,
88 'status': status,
89 'expected_task_state': expected_task_state,
90 'timeout': timeout})
Attila Fazekas0abbc952013-07-01 19:19:42 +020091 message += ' Current status: %s.' % server_status
Matt Riedemann629fa7c2013-12-11 18:20:56 -080092 message += ' Current task state: %s.' % task_state
zhufl88c89b52016-07-01 18:09:05 +080093 caller = test_utils.find_test_caller()
Matt Riedemann128c23e2014-05-02 13:46:39 -070094 if caller:
95 message = '(%s) %s' % (caller, message)
guo yunxianebb15f22016-11-01 21:03:35 +080096 raise lib_exc.TimeoutException(message)
Attila Fazekas0abbc952013-07-01 19:19:42 +020097 old_status = server_status
98 old_task_state = task_state
Matt Riedemannc00f3262013-12-14 12:03:55 -080099
100
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000101def wait_for_server_termination(client, server_id, ignore_error=False):
102 """Waits for server to reach termination."""
Matt Riedemannf748c112017-02-03 11:33:11 -0500103 try:
104 body = client.show_server(server_id)['server']
105 except lib_exc.NotFound:
106 return
zhuflf5cff8b2019-04-28 15:26:32 +0800107 old_status = body['status']
108 old_task_state = _get_task_state(body)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000109 start_time = int(time.time())
110 while True:
Matt Riedemannf748c112017-02-03 11:33:11 -0500111 time.sleep(client.build_interval)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000112 try:
ghanshyam0f825252015-08-25 16:02:50 +0900113 body = client.show_server(server_id)['server']
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000114 except lib_exc.NotFound:
115 return
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000116 server_status = body['status']
Matt Riedemannf748c112017-02-03 11:33:11 -0500117 task_state = _get_task_state(body)
118 if (server_status != old_status) or (task_state != old_task_state):
119 LOG.info('State transition "%s" ==> "%s" after %d second wait',
120 '/'.join((old_status, str(old_task_state))),
121 '/'.join((server_status, str(task_state))),
122 time.time() - start_time)
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000123 if server_status == 'ERROR' and not ignore_error:
zhufl0ded98f2019-06-13 11:44:24 +0800124 raise lib_exc.DeleteErrorException(
125 "Server %s failed to delete and is in ERROR status" %
126 server_id)
ericxiett5f65bf52020-09-25 08:42:26 +0000127
ericxiettd0ef93e2019-12-16 08:55:05 +0000128 if server_status == 'SOFT_DELETED':
129 # Soft-deleted instances need to be forcibly deleted to
130 # prevent some test cases from failing.
131 LOG.debug("Automatically force-deleting soft-deleted server %s",
132 server_id)
ericxiett5f65bf52020-09-25 08:42:26 +0000133 try:
134 client.force_delete_server(server_id)
135 except lib_exc.NotFound:
136 # The instance may have been deleted so ignore
137 # NotFound exception
138 return
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000139
140 if int(time.time()) - start_time >= client.build_timeout:
guo yunxianebb15f22016-11-01 21:03:35 +0800141 raise lib_exc.TimeoutException
Matt Riedemannf748c112017-02-03 11:33:11 -0500142 old_status = server_status
143 old_task_state = task_state
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000144
145
Matt Riedemannc00f3262013-12-14 12:03:55 -0800146def wait_for_image_status(client, image_id, status):
147 """Waits for an image to reach a given status.
148
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000149 The client should have a show_image(image_id) method to get the image.
Matt Riedemannc00f3262013-12-14 12:03:55 -0800150 The client should also have build_interval and build_timeout attributes.
151 """
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +0300152 if isinstance(client, images_v1_client.ImagesClient):
153 # The 'check_image' method is used here because the show_image method
154 # returns image details plus the image itself which is very expensive.
155 # The 'check_image' method returns just image details.
Ken'ichi Ohmichi01151e82016-06-10 11:19:52 -0700156 def _show_image_v1(image_id):
157 resp = client.check_image(image_id)
158 return common_image.get_image_meta_from_headers(resp)
159
160 show_image = _show_image_v1
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +0300161 else:
162 show_image = client.show_image
Matt Riedemannc00f3262013-12-14 12:03:55 -0800163
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +0300164 current_status = 'An unknown status'
165 start = int(time.time())
166 while int(time.time()) - start < client.build_timeout:
167 image = show_image(image_id)
168 # Compute image client returns response wrapped in 'image' element
Xiangfei Zhua8fd20a2016-07-26 21:28:12 -0700169 # which is not the case with Glance image client.
ghanshyam1756e0b2015-08-18 19:19:05 +0900170 if 'image' in image:
171 image = image['image']
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +0300172
173 current_status = image['status']
174 if current_status == status:
175 return
176 if current_status.lower() == 'killed':
177 raise exceptions.ImageKilledException(image_id=image_id,
178 status=status)
179 if current_status.lower() == 'error':
Matt Riedemannc00f3262013-12-14 12:03:55 -0800180 raise exceptions.AddImageException(image_id=image_id)
181
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +0300182 time.sleep(client.build_interval)
Matt Riedemannc00f3262013-12-14 12:03:55 -0800183
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +0300184 message = ('Image %(image_id)s failed to reach %(status)s state '
185 '(current state %(current_status)s) within the required '
186 'time (%(timeout)s s).' % {'image_id': image_id,
187 'status': status,
188 'current_status': current_status,
189 'timeout': client.build_timeout})
zhufl88c89b52016-07-01 18:09:05 +0800190 caller = test_utils.find_test_caller()
Yaroslav Lobankov2fea4052016-04-19 15:05:57 +0300191 if caller:
192 message = '(%s) %s' % (caller, message)
guo yunxianebb15f22016-11-01 21:03:35 +0800193 raise lib_exc.TimeoutException(message)
Adam Gandelman00682612014-09-02 17:10:36 -0700194
195
PranaliD491d63e2020-08-18 13:29:21 +0000196def wait_for_image_imported_to_stores(client, image_id, stores):
197 """Waits for an image to be imported to all requested stores.
198
199 The client should also have build_interval and build_timeout attributes.
200 """
201
202 start = int(time.time())
203 while int(time.time()) - start < client.build_timeout:
204 image = client.show_image(image_id)
205 if image['status'] == 'active' and image['stores'] == stores:
206 return
207
208 time.sleep(client.build_interval)
209
zhufl414ffba2020-11-19 16:57:06 +0800210 message = ('Image %s failed to import on stores: %s' %
211 (image_id, str(image['os_glance_failed_import'])))
PranaliD491d63e2020-08-18 13:29:21 +0000212 caller = test_utils.find_test_caller()
213 if caller:
214 message = '(%s) %s' % (caller, message)
215 raise lib_exc.TimeoutException(message)
216
217
Ghanshyam Mann4346a822020-07-29 13:45:04 -0500218def wait_for_image_copied_to_stores(client, image_id):
219 """Waits for an image to be copied on all requested stores.
220
221 The client should also have build_interval and build_timeout attributes.
222 This return the list of stores where copy is failed.
223 """
224
225 start = int(time.time())
226 store_left = []
227 while int(time.time()) - start < client.build_timeout:
228 image = client.show_image(image_id)
229 store_left = image.get('os_glance_importing_to_stores')
230 # NOTE(danms): If os_glance_importing_to_stores is None, then
231 # we've raced with the startup of the task and should continue
232 # to wait.
233 if store_left is not None and not store_left:
234 return image['os_glance_failed_import']
235 if image['status'].lower() == 'killed':
236 raise exceptions.ImageKilledException(image_id=image_id,
237 status=image['status'])
238
239 time.sleep(client.build_interval)
240
zhufl414ffba2020-11-19 16:57:06 +0800241 message = ('Image %s failed to finish the copy operation '
242 'on stores: %s' % (image_id, str(store_left)))
Ghanshyam Mann4346a822020-07-29 13:45:04 -0500243 caller = test_utils.find_test_caller()
244 if caller:
245 message = '(%s) %s' % (caller, message)
246 raise lib_exc.TimeoutException(message)
247
248
Matt Riedemannb36186b2017-12-04 17:54:08 +0000249def wait_for_volume_resource_status(client, resource_id, status):
250 """Waits for a volume resource to reach a given status.
lkuchlan52d7b0d2016-11-07 20:53:19 +0200251
252 This function is a common function for volume, snapshot and backup
253 resources. The function extracts the name of the desired resource from
254 the client class name of the resource.
255 """
xing-yang41ed7152017-05-03 06:52:56 -0400256 resource_name = re.findall(
257 r'(volume|group-snapshot|snapshot|backup|group)',
258 client.resource_type)[-1].replace('-', '_')
lkuchlan52d7b0d2016-11-07 20:53:19 +0200259 show_resource = getattr(client, 'show_' + resource_name)
260 resource_status = show_resource(resource_id)[resource_name]['status']
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000261 start = int(time.time())
262
Matt Riedemannb36186b2017-12-04 17:54:08 +0000263 while resource_status != status:
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000264 time.sleep(client.build_interval)
lkuchlan52d7b0d2016-11-07 20:53:19 +0200265 resource_status = show_resource(resource_id)[
266 '{}'.format(resource_name)]['status']
Matt Riedemannb36186b2017-12-04 17:54:08 +0000267 if resource_status == 'error' and resource_status != status:
lkuchlan52d7b0d2016-11-07 20:53:19 +0200268 raise exceptions.VolumeResourceBuildErrorException(
269 resource_name=resource_name, resource_id=resource_id)
270 if resource_name == 'volume' and resource_status == 'error_restoring':
271 raise exceptions.VolumeRestoreErrorException(volume_id=resource_id)
zhufl0ea2c012019-06-03 15:37:13 +0800272 if resource_status == 'error_extending' and resource_status != status:
273 raise exceptions.VolumeExtendErrorException(volume_id=resource_id)
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000274
275 if int(time.time()) - start >= client.build_timeout:
lkuchlan52d7b0d2016-11-07 20:53:19 +0200276 message = ('%s %s failed to reach %s status (current %s) '
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000277 'within the required time (%s s).' %
Matt Riedemannb36186b2017-12-04 17:54:08 +0000278 (resource_name, resource_id, status, resource_status,
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000279 client.build_timeout))
guo yunxianebb15f22016-11-01 21:03:35 +0800280 raise lib_exc.TimeoutException(message)
zhufl019ad732017-08-28 13:51:22 +0800281 LOG.info('%s %s reached %s after waiting for %f seconds',
Matt Riedemannb36186b2017-12-04 17:54:08 +0000282 resource_name, resource_id, status, time.time() - start)
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000283
284
Peter Penchev5c243a92020-09-06 02:26:03 +0300285def wait_for_volume_attachment_create(client, volume_id, server_id):
286 """Waits for a volume attachment to be created at a given volume."""
287 start = int(time.time())
288 while True:
289 attachments = client.show_volume(volume_id)['volume']['attachments']
290 found = [a for a in attachments if a['server_id'] == server_id]
291 if found:
292 LOG.info('Attachment %s created for volume %s to server %s after '
293 'waiting for %f seconds', found[0]['attachment_id'],
294 volume_id, server_id, time.time() - start)
295 return found[0]
296 time.sleep(client.build_interval)
297 if int(time.time()) - start >= client.build_timeout:
298 message = ('Failed to attach volume %s to server %s '
299 'within the required time (%s s).' %
300 (volume_id, server_id, client.build_timeout))
301 raise lib_exc.TimeoutException(message)
302
303
Lee Yarwoodc1b2a4a2020-01-08 17:02:49 +0000304def wait_for_volume_attachment_remove(client, volume_id, attachment_id):
305 """Waits for a volume attachment to be removed from a given volume."""
306 start = int(time.time())
307 attachments = client.show_volume(volume_id)['volume']['attachments']
308 while any(attachment_id == a['attachment_id'] for a in attachments):
309 time.sleep(client.build_interval)
310 if int(time.time()) - start >= client.build_timeout:
zhufl747300b2020-04-14 14:23:47 +0800311 message = ('Failed to remove attachment %s from volume %s '
Lee Yarwoodc1b2a4a2020-01-08 17:02:49 +0000312 'within the required time (%s s).' %
313 (attachment_id, volume_id, client.build_timeout))
314 raise lib_exc.TimeoutException(message)
315 attachments = client.show_volume(volume_id)['volume']['attachments']
316 LOG.info('Attachment %s removed from volume %s after waiting for %f '
317 'seconds', attachment_id, volume_id, time.time() - start)
318
319
Balazs Gibizer2e515fe2020-12-07 15:10:11 +0100320def wait_for_volume_attachment_remove_from_server(
321 client, server_id, volume_id):
322 """Waits for a volume to be removed from a given server.
323
324 This waiter checks the compute API if the volume attachment is removed.
325 """
326 start = int(time.time())
327 volumes = client.list_volume_attachments(server_id)['volumeAttachments']
328
329 while any(volume for volume in volumes if volume['volumeId'] == volume_id):
330 time.sleep(client.build_interval)
331
332 timed_out = int(time.time()) - start >= client.build_timeout
333 if timed_out:
334 message = ('Volume %s failed to detach from server %s within '
335 'the required time (%s s) from the compute API '
336 'perspective' %
337 (volume_id, server_id, client.build_timeout))
338 raise lib_exc.TimeoutException(message)
339
340 volumes = client.list_volume_attachments(server_id)[
341 'volumeAttachments']
342
343 return volumes
344
345
Lee Yarwoode5597402019-02-15 20:17:00 +0000346def wait_for_volume_migration(client, volume_id, new_host):
347 """Waits for a Volume to move to a new host."""
348 body = client.show_volume(volume_id)['volume']
349 host = body['os-vol-host-attr:host']
350 migration_status = body['migration_status']
351 start = int(time.time())
352
353 # new_host is hostname@backend while current_host is hostname@backend#type
354 while migration_status != 'success' or new_host not in host:
355 time.sleep(client.build_interval)
356 body = client.show_volume(volume_id)['volume']
357 host = body['os-vol-host-attr:host']
358 migration_status = body['migration_status']
359
360 if migration_status == 'error':
361 message = ('volume %s failed to migrate.' % (volume_id))
362 raise lib_exc.TempestException(message)
363
364 if int(time.time()) - start >= client.build_timeout:
365 message = ('Volume %s failed to migrate to %s (current %s) '
366 'within the required time (%s s).' %
367 (volume_id, new_host, host, client.build_timeout))
368 raise lib_exc.TimeoutException(message)
369
370
scottda61f68ac2016-06-07 12:07:55 -0600371def wait_for_volume_retype(client, volume_id, new_volume_type):
372 """Waits for a Volume to have a new volume type."""
373 body = client.show_volume(volume_id)['volume']
374 current_volume_type = body['volume_type']
375 start = int(time.time())
376
377 while current_volume_type != new_volume_type:
378 time.sleep(client.build_interval)
379 body = client.show_volume(volume_id)['volume']
380 current_volume_type = body['volume_type']
381
382 if int(time.time()) - start >= client.build_timeout:
383 message = ('Volume %s failed to reach %s volume type (current %s) '
384 'within the required time (%s s).' %
385 (volume_id, new_volume_type, current_volume_type,
386 client.build_timeout))
Matt Riedemann74eb3b52017-02-14 11:34:30 -0500387 raise lib_exc.TimeoutException(message)
scottda61f68ac2016-06-07 12:07:55 -0600388
389
lkuchlanec1ba4f2016-09-06 10:28:18 +0300390def wait_for_qos_operations(client, qos_id, operation, args=None):
391 """Waits for a qos operations to be completed.
392
393 NOTE : operation value is required for wait_for_qos_operations()
394 operation = 'qos-key' / 'disassociate' / 'disassociate-all'
395 args = keys[] when operation = 'qos-key'
396 args = volume-type-id disassociated when operation = 'disassociate'
397 args = None when operation = 'disassociate-all'
398 """
399 start_time = int(time.time())
400 while True:
401 if operation == 'qos-key-unset':
402 body = client.show_qos(qos_id)['qos_specs']
403 if not any(key in body['specs'] for key in args):
404 return
405 elif operation == 'disassociate':
406 body = client.show_association_qos(qos_id)['qos_associations']
407 if not any(args in body[i]['id'] for i in range(0, len(body))):
408 return
409 elif operation == 'disassociate-all':
410 body = client.show_association_qos(qos_id)['qos_associations']
411 if not body:
412 return
413 else:
414 msg = (" operation value is either not defined or incorrect.")
415 raise lib_exc.UnprocessableEntity(msg)
416
417 if int(time.time()) - start_time >= client.build_timeout:
guo yunxianebb15f22016-11-01 21:03:35 +0800418 raise lib_exc.TimeoutException
lkuchlanec1ba4f2016-09-06 10:28:18 +0300419 time.sleep(client.build_interval)
zhufl7b638332016-11-14 10:23:30 +0800420
421
Matt Riedemann2ea48f02017-04-03 11:36:19 -0400422def wait_for_interface_status(client, server_id, port_id, status):
zhufl7b638332016-11-14 10:23:30 +0800423 """Waits for an interface to reach a given status."""
Matt Riedemann2ea48f02017-04-03 11:36:19 -0400424 body = (client.show_interface(server_id, port_id)
zhufl7b638332016-11-14 10:23:30 +0800425 ['interfaceAttachment'])
426 interface_status = body['port_state']
427 start = int(time.time())
428
429 while(interface_status != status):
430 time.sleep(client.build_interval)
Matt Riedemann2ea48f02017-04-03 11:36:19 -0400431 body = (client.show_interface(server_id, port_id)
zhufl7b638332016-11-14 10:23:30 +0800432 ['interfaceAttachment'])
433 interface_status = body['port_state']
434
435 timed_out = int(time.time()) - start >= client.build_timeout
436
437 if interface_status != status and timed_out:
438 message = ('Interface %s failed to reach %s status '
439 '(current %s) within the required time (%s s).' %
440 (port_id, status, interface_status,
441 client.build_timeout))
442 raise lib_exc.TimeoutException(message)
443
444 return body
Artom Lifshitz3306d422018-03-22 12:20:54 -0400445
446
447def wait_for_interface_detach(client, server_id, port_id):
448 """Waits for an interface to be detached from a server."""
449 body = client.list_interfaces(server_id)['interfaceAttachments']
450 ports = [iface['port_id'] for iface in body]
451 start = int(time.time())
452
453 while port_id in ports:
454 time.sleep(client.build_interval)
455 body = client.list_interfaces(server_id)['interfaceAttachments']
456 ports = [iface['port_id'] for iface in body]
457 if port_id not in ports:
458 return body
459
460 timed_out = int(time.time()) - start >= client.build_timeout
461 if timed_out:
462 message = ('Interface %s failed to detach from server %s within '
463 'the required time (%s s)' % (port_id, server_id,
464 client.build_timeout))
465 raise lib_exc.TimeoutException(message)
Slawek Kaplonskie3405ba2020-11-09 17:24:13 +0100466
467
468def wait_for_guest_os_boot(client, server_id):
469 start_time = int(time.time())
470 while True:
471 console_output = client.get_console_output(server_id)['output']
472 for line in console_output.split('\n'):
473 if 'login:' in line.lower():
474 return
475 if int(time.time()) - start_time >= client.build_timeout:
476 LOG.info("Guest OS on server %s probably isn't ready or its "
477 "console log can't be parsed properly. If guest OS "
478 "isn't ready, that may cause problems with SSH to "
479 "the server.",
480 server_id)
481 return
482 time.sleep(client.build_interval)