blob: 95305f3cbed7ea72d75c88a1118751ae429546fd [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
13
14import time
15
Doug Hellmann583ce2c2015-03-11 14:55:46 +000016from oslo_log import log as logging
Matthew Treinish01472ff2015-02-20 17:26:52 -050017
Attila Fazekas0abbc952013-07-01 19:19:42 +020018from tempest import config
19from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050020from tempest.lib.common.utils import misc as misc_utils
21from tempest.lib import exceptions as lib_exc
Attila Fazekas0abbc952013-07-01 19:19:42 +020022
Sean Dague86bd8422013-12-20 09:56:44 -050023CONF = config.CONF
Attila Fazekas0abbc952013-07-01 19:19:42 +020024LOG = logging.getLogger(__name__)
25
26
27# NOTE(afazekas): This function needs to know a token and a subject.
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +090028def wait_for_server_status(client, server_id, status, ready_wait=True,
Zhi Kun Liue5401762013-09-11 20:45:48 +080029 extra_timeout=0, raise_on_error=True):
Attila Fazekas0abbc952013-07-01 19:19:42 +020030 """Waits for a server to reach a given status."""
31
32 def _get_task_state(body):
Ken'ichi Ohmichia58c1562014-12-15 00:39:55 +000033 return body.get('OS-EXT-STS:task_state', None)
Attila Fazekas0abbc952013-07-01 19:19:42 +020034
35 # NOTE(afazekas): UNKNOWN status possible on ERROR
36 # or in a very early stage.
ghanshyam0f825252015-08-25 16:02:50 +090037 body = client.show_server(server_id)['server']
Attila Fazekas0abbc952013-07-01 19:19:42 +020038 old_status = server_status = body['status']
39 old_task_state = task_state = _get_task_state(body)
40 start_time = int(time.time())
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +090041 timeout = client.build_timeout + extra_timeout
Attila Fazekas0abbc952013-07-01 19:19:42 +020042 while True:
43 # NOTE(afazekas): Now the BUILD status only reached
Shane Wang111f86c2014-02-20 16:40:22 +080044 # between the UNKNOWN->ACTIVE transition.
Attila Fazekas0abbc952013-07-01 19:19:42 +020045 # TODO(afazekas): enumerate and validate the stable status set
46 if status == 'BUILD' and server_status != 'UNKNOWN':
47 return
48 if server_status == status:
49 if ready_wait:
50 if status == 'BUILD':
51 return
52 # NOTE(afazekas): The instance is in "ready for action state"
53 # when no task in progress
nayna-patelfdc87a52015-08-03 10:46:33 +000054 # NOTE(afazekas): Converted to string because of the XML
Attila Fazekas0abbc952013-07-01 19:19:42 +020055 # responses
56 if str(task_state) == "None":
57 # without state api extension 3 sec usually enough
Sean Dague86bd8422013-12-20 09:56:44 -050058 time.sleep(CONF.compute.ready_wait)
Attila Fazekas0abbc952013-07-01 19:19:42 +020059 return
60 else:
61 return
62
63 time.sleep(client.build_interval)
ghanshyam0f825252015-08-25 16:02:50 +090064 body = client.show_server(server_id)['server']
Attila Fazekas0abbc952013-07-01 19:19:42 +020065 server_status = body['status']
66 task_state = _get_task_state(body)
67 if (server_status != old_status) or (task_state != old_task_state):
68 LOG.info('State transition "%s" ==> "%s" after %d second wait',
69 '/'.join((old_status, str(old_task_state))),
70 '/'.join((server_status, str(task_state))),
71 time.time() - start_time)
Zhi Kun Liue5401762013-09-11 20:45:48 +080072 if (server_status == 'ERROR') and raise_on_error:
Attila Fazekas0462a7f2014-06-20 07:38:06 +020073 if 'fault' in body:
74 raise exceptions.BuildErrorException(body['fault'],
Matthew Treinish1d14c542014-06-17 20:25:40 -040075 server_id=server_id)
Attila Fazekas0462a7f2014-06-20 07:38:06 +020076 else:
77 raise exceptions.BuildErrorException(server_id=server_id)
Attila Fazekas0abbc952013-07-01 19:19:42 +020078
Ken'ichi Ohmichi39437e22013-10-06 00:21:38 +090079 timed_out = int(time.time()) - start_time >= timeout
Attila Fazekas0abbc952013-07-01 19:19:42 +020080
81 if timed_out:
Matt Riedemann629fa7c2013-12-11 18:20:56 -080082 expected_task_state = 'None' if ready_wait else 'n/a'
83 message = ('Server %(server_id)s failed to reach %(status)s '
84 'status and task state "%(expected_task_state)s" '
85 'within the required time (%(timeout)s s).' %
86 {'server_id': server_id,
87 'status': status,
88 'expected_task_state': expected_task_state,
89 'timeout': timeout})
Attila Fazekas0abbc952013-07-01 19:19:42 +020090 message += ' Current status: %s.' % server_status
Matt Riedemann629fa7c2013-12-11 18:20:56 -080091 message += ' Current task state: %s.' % task_state
Matt Riedemann128c23e2014-05-02 13:46:39 -070092 caller = misc_utils.find_test_caller()
93 if caller:
94 message = '(%s) %s' % (caller, message)
Attila Fazekas0abbc952013-07-01 19:19:42 +020095 raise exceptions.TimeoutException(message)
96 old_status = server_status
97 old_task_state = task_state
Matt Riedemannc00f3262013-12-14 12:03:55 -080098
99
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000100def wait_for_server_termination(client, server_id, ignore_error=False):
101 """Waits for server to reach termination."""
102 start_time = int(time.time())
103 while True:
104 try:
ghanshyam0f825252015-08-25 16:02:50 +0900105 body = client.show_server(server_id)['server']
Ken'ichi Ohmichie91a0c62015-08-13 02:09:16 +0000106 except lib_exc.NotFound:
107 return
108
109 server_status = body['status']
110 if server_status == 'ERROR' and not ignore_error:
111 raise exceptions.BuildErrorException(server_id=server_id)
112
113 if int(time.time()) - start_time >= client.build_timeout:
114 raise exceptions.TimeoutException
115
116 time.sleep(client.build_interval)
117
118
Matt Riedemannc00f3262013-12-14 12:03:55 -0800119def wait_for_image_status(client, image_id, status):
120 """Waits for an image to reach a given status.
121
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000122 The client should have a show_image(image_id) method to get the image.
Matt Riedemannc00f3262013-12-14 12:03:55 -0800123 The client should also have build_interval and build_timeout attributes.
124 """
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000125 image = client.show_image(image_id)
ghanshyam1756e0b2015-08-18 19:19:05 +0900126 # Compute image client return response wrapped in 'image' element
127 # which is not case with glance image client.
128 if 'image' in image:
129 image = image['image']
Matt Riedemannc00f3262013-12-14 12:03:55 -0800130 start = int(time.time())
131
132 while image['status'] != status:
133 time.sleep(client.build_interval)
Ken'ichi Ohmichi5d410762015-05-22 01:10:03 +0000134 image = client.show_image(image_id)
ghanshyam1756e0b2015-08-18 19:19:05 +0900135 # Compute image client return response wrapped in 'image' element
136 # which is not case with glance image client.
137 if 'image' in image:
138 image = image['image']
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200139 status_curr = image['status']
140 if status_curr == 'ERROR':
Matt Riedemannc00f3262013-12-14 12:03:55 -0800141 raise exceptions.AddImageException(image_id=image_id)
142
143 # check the status again to avoid a false negative where we hit
144 # the timeout at the same time that the image reached the expected
145 # status
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200146 if status_curr == status:
Matt Riedemannc00f3262013-12-14 12:03:55 -0800147 return
148
149 if int(time.time()) - start >= client.build_timeout:
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200150 message = ('Image %(image_id)s failed to reach %(status)s state'
151 '(current state %(status_curr)s) '
152 'within the required time (%(timeout)s s).' %
Matt Riedemannc00f3262013-12-14 12:03:55 -0800153 {'image_id': image_id,
154 'status': status,
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200155 'status_curr': status_curr,
Matt Riedemannc00f3262013-12-14 12:03:55 -0800156 'timeout': client.build_timeout})
Matt Riedemann128c23e2014-05-02 13:46:39 -0700157 caller = misc_utils.find_test_caller()
158 if caller:
159 message = '(%s) %s' % (caller, message)
Matt Riedemannc00f3262013-12-14 12:03:55 -0800160 raise exceptions.TimeoutException(message)
Adam Gandelman00682612014-09-02 17:10:36 -0700161
162
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000163def wait_for_volume_status(client, volume_id, status):
164 """Waits for a Volume to reach a given status."""
ghanshyamcb131762015-08-24 18:21:08 +0900165 body = client.show_volume(volume_id)['volume']
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000166 volume_status = body['status']
167 start = int(time.time())
168
169 while volume_status != status:
170 time.sleep(client.build_interval)
ghanshyamcb131762015-08-24 18:21:08 +0900171 body = client.show_volume(volume_id)['volume']
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000172 volume_status = body['status']
173 if volume_status == 'error':
174 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
Matt Riedemannf77e7dc2015-08-10 16:39:39 -0700175 if volume_status == 'error_restoring':
176 raise exceptions.VolumeRestoreErrorException(volume_id=volume_id)
Ken'ichi Ohmichib942be62015-07-08 08:16:12 +0000177
178 if int(time.time()) - start >= client.build_timeout:
179 message = ('Volume %s failed to reach %s status (current %s) '
180 'within the required time (%s s).' %
181 (volume_id, status, volume_status,
182 client.build_timeout))
183 raise exceptions.TimeoutException(message)
184
185
Gaozexub9c9d6e2015-09-10 17:08:04 +0800186def wait_for_snapshot_status(client, snapshot_id, status):
187 """Waits for a Snapshot to reach a given status."""
188 body = client.show_snapshot(snapshot_id)['snapshot']
189 snapshot_status = body['status']
190 start = int(time.time())
191
192 while snapshot_status != status:
193 time.sleep(client.build_interval)
194 body = client.show_snapshot(snapshot_id)['snapshot']
195 snapshot_status = body['status']
196 if snapshot_status == 'error':
197 raise exceptions.SnapshotBuildErrorException(
198 snapshot_id=snapshot_id)
199 if int(time.time()) - start >= client.build_timeout:
200 message = ('Snapshot %s failed to reach %s status (current %s) '
201 'within the required time (%s s).' %
202 (snapshot_id, status, snapshot_status,
203 client.build_timeout))
204 raise exceptions.TimeoutException(message)
205
206
Adam Gandelman00682612014-09-02 17:10:36 -0700207def wait_for_bm_node_status(client, node_id, attr, status):
208 """Waits for a baremetal node attribute to reach given status.
209
210 The client should have a show_node(node_uuid) method to get the node.
211 """
212 _, node = client.show_node(node_id)
213 start = int(time.time())
214
215 while node[attr] != status:
216 time.sleep(client.build_interval)
217 _, node = client.show_node(node_id)
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200218 status_curr = node[attr]
219 if status_curr == status:
Adam Gandelman00682612014-09-02 17:10:36 -0700220 return
221
222 if int(time.time()) - start >= client.build_timeout:
223 message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s '
224 'within the required time (%(timeout)s s).' %
225 {'node_id': node_id,
226 'attr': attr,
227 'status': status,
228 'timeout': client.build_timeout})
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200229 message += ' Current state of %s: %s.' % (attr, status_curr)
Adam Gandelman00682612014-09-02 17:10:36 -0700230 caller = misc_utils.find_test_caller()
231 if caller:
232 message = '(%s) %s' % (caller, message)
233 raise exceptions.TimeoutException(message)