blob: 049d5ca925f707f6307a92cb9d7f756735999ba2 [file] [log] [blame]
Maru Newbyb096d9f2015-03-09 18:54:54 +00001# 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
Ihar Hrachyshkac695f9f2015-02-26 23:26:41 +010016from oslo_log import log as logging
17
Maru Newbyb096d9f2015-03-09 18:54:54 +000018from neutron.tests.tempest.common.utils import misc as misc_utils
19from neutron.tests.tempest import config
20from neutron.tests.tempest import exceptions
Maru Newbyb096d9f2015-03-09 18:54:54 +000021
22CONF = config.CONF
23LOG = logging.getLogger(__name__)
24
25
26# NOTE(afazekas): This function needs to know a token and a subject.
27def wait_for_server_status(client, server_id, status, ready_wait=True,
28 extra_timeout=0, raise_on_error=True):
29 """Waits for a server to reach a given status."""
30
31 def _get_task_state(body):
32 return body.get('OS-EXT-STS:task_state', None)
33
34 # NOTE(afazekas): UNKNOWN status possible on ERROR
35 # or in a very early stage.
36 body = client.get_server(server_id)
37 old_status = server_status = body['status']
38 old_task_state = task_state = _get_task_state(body)
39 start_time = int(time.time())
40 timeout = client.build_timeout + extra_timeout
41 while True:
42 # NOTE(afazekas): Now the BUILD status only reached
43 # between the UNKNOWN->ACTIVE transition.
44 # TODO(afazekas): enumerate and validate the stable status set
45 if status == 'BUILD' and server_status != 'UNKNOWN':
46 return
47 if server_status == status:
48 if ready_wait:
49 if status == 'BUILD':
50 return
51 # NOTE(afazekas): The instance is in "ready for action state"
52 # when no task in progress
53 # NOTE(afazekas): Converted to string bacuse of the XML
54 # responses
55 if str(task_state) == "None":
56 # without state api extension 3 sec usually enough
57 time.sleep(CONF.compute.ready_wait)
58 return
59 else:
60 return
61
62 time.sleep(client.build_interval)
63 body = client.get_server(server_id)
64 server_status = body['status']
65 task_state = _get_task_state(body)
66 if (server_status != old_status) or (task_state != old_task_state):
67 LOG.info('State transition "%s" ==> "%s" after %d second wait',
68 '/'.join((old_status, str(old_task_state))),
69 '/'.join((server_status, str(task_state))),
70 time.time() - start_time)
71 if (server_status == 'ERROR') and raise_on_error:
72 if 'fault' in body:
73 raise exceptions.BuildErrorException(body['fault'],
74 server_id=server_id)
75 else:
76 raise exceptions.BuildErrorException(server_id=server_id)
77
78 timed_out = int(time.time()) - start_time >= timeout
79
80 if timed_out:
81 expected_task_state = 'None' if ready_wait else 'n/a'
82 message = ('Server %(server_id)s failed to reach %(status)s '
83 'status and task state "%(expected_task_state)s" '
84 'within the required time (%(timeout)s s).' %
85 {'server_id': server_id,
86 'status': status,
87 'expected_task_state': expected_task_state,
88 'timeout': timeout})
89 message += ' Current status: %s.' % server_status
90 message += ' Current task state: %s.' % task_state
91 caller = misc_utils.find_test_caller()
92 if caller:
93 message = '(%s) %s' % (caller, message)
94 raise exceptions.TimeoutException(message)
95 old_status = server_status
96 old_task_state = task_state
97
98
99def wait_for_image_status(client, image_id, status):
100 """Waits for an image to reach a given status.
101
102 The client should have a get_image(image_id) method to get the image.
103 The client should also have build_interval and build_timeout attributes.
104 """
105 image = client.get_image(image_id)
106 start = int(time.time())
107
108 while image['status'] != status:
109 time.sleep(client.build_interval)
110 image = client.get_image(image_id)
111 status_curr = image['status']
112 if status_curr == 'ERROR':
113 raise exceptions.AddImageException(image_id=image_id)
114
115 # check the status again to avoid a false negative where we hit
116 # the timeout at the same time that the image reached the expected
117 # status
118 if status_curr == status:
119 return
120
121 if int(time.time()) - start >= client.build_timeout:
122 message = ('Image %(image_id)s failed to reach %(status)s state'
123 '(current state %(status_curr)s) '
124 'within the required time (%(timeout)s s).' %
125 {'image_id': image_id,
126 'status': status,
127 'status_curr': status_curr,
128 'timeout': client.build_timeout})
129 caller = misc_utils.find_test_caller()
130 if caller:
131 message = '(%s) %s' % (caller, message)
132 raise exceptions.TimeoutException(message)
133
134
135def wait_for_bm_node_status(client, node_id, attr, status):
136 """Waits for a baremetal node attribute to reach given status.
137
138 The client should have a show_node(node_uuid) method to get the node.
139 """
140 _, node = client.show_node(node_id)
141 start = int(time.time())
142
143 while node[attr] != status:
144 time.sleep(client.build_interval)
145 _, node = client.show_node(node_id)
146 status_curr = node[attr]
147 if status_curr == status:
148 return
149
150 if int(time.time()) - start >= client.build_timeout:
151 message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s '
152 'within the required time (%(timeout)s s).' %
153 {'node_id': node_id,
154 'attr': attr,
155 'status': status,
156 'timeout': client.build_timeout})
157 message += ' Current state of %s: %s.' % (attr, status_curr)
158 caller = misc_utils.find_test_caller()
159 if caller:
160 message = '(%s) %s' % (caller, message)
161 raise exceptions.TimeoutException(message)