blob: 6598fcc3e98957c6c37a27dc2d7f60a0ac2848fc [file] [log] [blame]
Jude Cross986e3f52017-07-24 14:57:20 -07001# Copyright 2017 GoDaddy
2# Copyright 2018 Rackspace US Inc. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16import time
17
18from oslo_log import log as logging
19from tempest import config
20from tempest.lib.common.utils import test_utils
21from tempest.lib import exceptions
22
23from octavia_tempest_plugin.common import constants as const
24
25CONF = config.CONF
26LOG = logging.getLogger(__name__)
27
28
29def wait_for_status(show_client, id, status_key, status,
30 check_interval, check_timeout, root_tag=None):
31 """Waits for an object to reach a specific status.
32
33 :param show_client: The tempest service client show method.
34 Ex. cls.os_primary.servers_client.show_server
35 :param id: The id of the object to query.
36 :param status_key: The key of the status field in the response.
37 Ex. provisioning_status
38 :param status: The status to wait for. Ex. "ACTIVE"
39 :check_interval: How often to check the status, in seconds.
40 :check_timeout: The maximum time, in seconds, to check the status.
41 :root_tag: The root tag on the response to remove, if any.
42 :raises CommandFailed: Raised if the object goes into ERROR and ERROR was
43 not the desired status.
44 :raises TimeoutException: The object did not achieve the status or ERROR in
45 the check_timeout period.
46 :returns: The object details from the show client.
47 """
48 start = int(time.time())
49 LOG.info('Waiting for {name} status to update to {status}'.format(
50 name=show_client.__name__, status=status))
51 while True:
Adam Harwella795ae62018-04-23 12:11:19 -070052 if status == const.DELETED:
53 try:
54 response = show_client(id)
55 except exceptions.NotFound:
56 return
57 else:
58 response = show_client(id)
59
Jude Cross986e3f52017-07-24 14:57:20 -070060 if root_tag:
61 object_details = response[root_tag]
62 else:
63 object_details = response
64
65 if object_details[status_key] == status:
66 LOG.info('{name}\'s status updated to {status}.'.format(
67 name=show_client.__name__, status=status))
68 return object_details
69 elif object_details[status_key] == 'ERROR':
70 message = ('{name} {field} updated to an invalid state of '
71 'ERROR'.format(name=show_client.__name__,
72 field=status_key))
73 caller = test_utils.find_test_caller()
74 if caller:
75 message = '({caller}) {message}'.format(caller=caller,
76 message=message)
77 raise exceptions.UnexpectedResponseCode(message)
78 elif int(time.time()) - start >= check_timeout:
79 message = (
80 '{name} {field} failed to update to {expected_status} within '
81 'the required time {timeout}. Current status of {name}: '
82 '{status}'.format(
83 name=show_client.__name__,
84 timeout=check_timeout,
85 status=object_details[status_key],
86 expected_status=status,
87 field=status_key
88 ))
89 caller = test_utils.find_test_caller()
90 if caller:
91 message = '({caller}) {message}'.format(caller=caller,
92 message=message)
93 raise exceptions.TimeoutException(message)
94
95 time.sleep(check_interval)
96
97
98def wait_for_not_found(delete_func, show_func, *args, **kwargs):
99 """Call the delete function, then wait for it to be 'NotFound'
100
101 :param delete_func: The delete function to call.
102 :param show_func: The show function to call looking for 'NotFound'.
103 :param ID: The ID of the object to delete/show.
104 :raises TimeoutException: The object did not achieve the status or ERROR in
105 the check_timeout period.
106 :returns: None
107 """
108 try:
Jude Crossfbbd2b42017-08-09 15:21:04 -0700109 delete_func(*args, **kwargs)
Jude Cross986e3f52017-07-24 14:57:20 -0700110 except exceptions.NotFound:
111 return
112 start = int(time.time())
113 LOG.info('Waiting for object to be NotFound')
114 while True:
115 try:
116 show_func(*args, **kwargs)
117 except exceptions.NotFound:
118 return
119 if int(time.time()) - start >= CONF.load_balancer.check_timeout:
120 message = ('{name} did not raise NotFound in {timeout} '
121 'seconds.'.format(
122 name=show_func.__name__,
123 timeout=CONF.load_balancer.check_timeout))
124 raise exceptions.TimeoutException(message)
125 time.sleep(CONF.load_balancer.check_interval)
126
127
128def wait_for_deleted_status_or_not_found(
129 show_client, id, status_key, check_interval, check_timeout,
130 root_tag=None):
131 """Waits for an object to reach a DELETED status or be not found (404).
132
133 :param show_client: The tempest service client show method.
134 Ex. cls.os_primary.servers_client.show_server
135 :param id: The id of the object to query.
136 :param status_key: The key of the status field in the response.
137 Ex. provisioning_status
138 :check_interval: How often to check the status, in seconds.
139 :check_timeout: The maximum time, in seconds, to check the status.
140 :root_tag: The root tag on the response to remove, if any.
141 :raises CommandFailed: Raised if the object goes into ERROR and ERROR was
142 not the desired status.
143 :raises TimeoutException: The object did not achieve the status or ERROR in
144 the check_timeout period.
145 :returns: None
146 """
147 start = int(time.time())
148 LOG.info('Waiting for {name} status to update to DELETED or be not '
149 'found(404)'.format(name=show_client.__name__))
150 while True:
151 try:
152 response = show_client(id)
153 except exceptions.NotFound:
154 return
155
156 if root_tag:
157 object_details = response[root_tag]
158 else:
159 object_details = response
160
161 if object_details[status_key] == const.DELETED:
162 LOG.info('{name}\'s status updated to DELETED.'.format(
163 name=show_client.__name__))
164 return
165 elif int(time.time()) - start >= check_timeout:
166 message = (
167 '{name} {field} failed to update to DELETED or become not '
168 'found (404) within the required time {timeout}. Current '
169 'status of {name}: {status}'.format(
170 name=show_client.__name__,
171 timeout=check_timeout,
172 status=object_details[status_key],
173 field=status_key
174 ))
175 caller = test_utils.find_test_caller()
176 if caller:
177 message = '({caller}) {message}'.format(caller=caller,
178 message=message)
179 raise exceptions.TimeoutException(message)
180
181 time.sleep(check_interval)