| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from oslo_log import log as logging |
| 17 |
from oslo_serialization import jsonutils |
| 18 |
from tempest import config |
| 19 |
from tempest.lib.common import rest_client |
| 20 |
from tempest.lib.common.utils import test_utils |
| 21 |
from tempest.lib import exceptions |
| 22 |
|
| 23 |
from octavia_tempest_plugin.common import constants as const |
| 24 |
from octavia_tempest_plugin.tests import waiters |
| 25 |
|
| 26 |
CONF = config.CONF |
| 27 |
LOG = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
class Unset(object): |
| 31 |
def __bool__(self): |
| 32 |
return False |
| 33 |
|
| 34 |
__nonzero__ = __bool__ |
| 35 |
|
| 36 |
def __repr__(self): |
| 37 |
return 'Unset' |
| 38 |
|
| 39 |
|
| 40 |
class BaseLBaaSClient(rest_client.RestClient): |
| 41 |
|
| 42 |
root_tag = None |
| 43 |
list_root_tag = None |
| 44 |
base_uri = '/v2.0/lbaas/{object}' |
| 45 |
|
| 46 |
def __init__(self, auth_provider, service, region, **kwargs): |
| 47 |
super(BaseLBaaSClient, self).__init__(auth_provider, service, |
| 48 |
region, **kwargs) |
| 49 |
self.timeout = CONF.load_balancer.build_timeout |
| 50 |
self.build_interval = CONF.load_balancer.build_interval |
| 51 |
self.uri = self.base_uri.format(object=self.list_root_tag) |
| 52 |
|
| 53 |
|
| 54 |
cleanup_func_name = 'cleanup_{}'.format(self.root_tag) |
| 55 |
if not hasattr(self, cleanup_func_name): |
| 56 |
setattr(self, cleanup_func_name, self._cleanup_obj) |
| 57 |
|
| 58 |
def _create_object(self, parent_id=None, return_object_only=True, |
| 59 |
**kwargs): |
| 60 |
"""Create an object. |
| 61 |
|
| 62 |
:param return_object_only: If True, the response returns the object |
| 63 |
inside the root tag. False returns the full |
| 64 |
response from the API. |
| 65 |
:param **kwargs: All attributes of the object should be passed as |
| 66 |
keyword arguments to this function. |
| 67 |
:raises AssertionError: if the expected_code isn't a valid http success |
| 68 |
response code |
| 69 |
:raises BadRequest: If a 400 response code is received |
| 70 |
:raises Conflict: If a 409 response code is received |
| 71 |
:raises Forbidden: If a 403 response code is received |
| 72 |
:raises Gone: If a 410 response code is received |
| 73 |
:raises InvalidContentType: If a 415 response code is received |
| 74 |
:raises InvalidHTTPResponseBody: The response body wasn't valid JSON |
| 75 |
:raises InvalidHttpSuccessCode: if the read code isn't an expected |
| 76 |
http success code |
| 77 |
:raises NotFound: If a 404 response code is received |
| 78 |
:raises NotImplemented: If a 501 response code is received |
| 79 |
:raises OverLimit: If a 413 response code is received and over_limit is |
| 80 |
not in the response body |
| 81 |
:raises RateLimitExceeded: If a 413 response code is received and |
| 82 |
over_limit is in the response body |
| 83 |
:raises ServerFault: If a 500 response code is received |
| 84 |
:raises Unauthorized: If a 401 response code is received |
| 85 |
:raises UnexpectedContentType: If the content-type of the response |
| 86 |
isn't an expect type |
| 87 |
:raises UnexpectedResponseCode: If a response code above 400 is |
| 88 |
received and it doesn't fall into any |
| 89 |
of the handled checks |
| 90 |
:raises UnprocessableEntity: If a 422 response code is received and |
| 91 |
couldn't be parsed |
| 92 |
:returns: An appropriate object. |
| 93 |
""" |
| 94 |
obj_dict = {self.root_tag: kwargs} |
| 95 |
|
| 96 |
if parent_id: |
| 97 |
request_uri = self.uri.format(parent=parent_id) |
| 98 |
else: |
| 99 |
request_uri = self.uri |
| 100 |
|
| 101 |
response, body = self.post(request_uri, jsonutils.dumps(obj_dict)) |
| 102 |
self.expected_success(201, response.status) |
| 103 |
if return_object_only: |
| 104 |
return jsonutils.loads(body.decode('utf-8'))[self.root_tag] |
| 105 |
else: |
| 106 |
return jsonutils.loads(body.decode('utf-8')) |
| 107 |
|
| 108 |
def _show_object(self, obj_id, parent_id=None, query_params=None, |
| 109 |
return_object_only=True): |
| 110 |
"""Get object details. |
| 111 |
|
| 112 |
:param obj_id: The object ID to query. |
| 113 |
:param query_params: The optional query parameters to append to the |
| 114 |
request. Ex. fields=id&fields=name |
| 115 |
:param return_object_only: If True, the response returns the object |
| 116 |
inside the root tag. False returns the full |
| 117 |
response from the API. |
| 118 |
:raises AssertionError: if the expected_code isn't a valid http success |
| 119 |
response code |
| 120 |
:raises BadRequest: If a 400 response code is received |
| 121 |
:raises Conflict: If a 409 response code is received |
| 122 |
:raises Forbidden: If a 403 response code is received |
| 123 |
:raises Gone: If a 410 response code is received |
| 124 |
:raises InvalidContentType: If a 415 response code is received |
| 125 |
:raises InvalidHTTPResponseBody: The response body wasn't valid JSON |
| 126 |
:raises InvalidHttpSuccessCode: if the read code isn't an expected |
| 127 |
http success code |
| 128 |
:raises NotFound: If a 404 response code is received |
| 129 |
:raises NotImplemented: If a 501 response code is received |
| 130 |
:raises OverLimit: If a 413 response code is received and over_limit is |
| 131 |
not in the response body |
| 132 |
:raises RateLimitExceeded: If a 413 response code is received and |
| 133 |
over_limit is in the response body |
| 134 |
:raises ServerFault: If a 500 response code is received |
| 135 |
:raises Unauthorized: If a 401 response code is received |
| 136 |
:raises UnexpectedContentType: If the content-type of the response |
| 137 |
isn't an expect type |
| 138 |
:raises UnexpectedResponseCode: If a response code above 400 is |
| 139 |
received and it doesn't fall into any |
| 140 |
of the handled checks |
| 141 |
:raises UnprocessableEntity: If a 422 response code is received and |
| 142 |
couldn't be parsed |
| 143 |
:returns: An appropriate object. |
| 144 |
""" |
| 145 |
if parent_id: |
| 146 |
uri = self.uri.format(parent=parent_id) |
| 147 |
else: |
| 148 |
uri = self.uri |
| 149 |
|
| 150 |
if query_params: |
| 151 |
request_uri = '{0}/{1}?{2}'.format(uri, obj_id, query_params) |
| 152 |
else: |
| 153 |
request_uri = '{0}/{1}'.format(uri, obj_id) |
| 154 |
|
| 155 |
response, body = self.get(request_uri) |
| 156 |
self.expected_success(200, response.status) |
| 157 |
if return_object_only: |
| 158 |
return jsonutils.loads(body.decode('utf-8'))[self.root_tag] |
| 159 |
else: |
| 160 |
return jsonutils.loads(body.decode('utf-8')) |
| 161 |
|
| 162 |
def _list_objects(self, parent_id=None, query_params=None, |
| 163 |
return_object_only=True): |
| 164 |
"""Get a list of the appropriate objects. |
| 165 |
|
| 166 |
:param query_params: The optional query parameters to append to the |
| 167 |
request. Ex. fields=id&fields=name |
| 168 |
:param return_object_only: If True, the response returns the object |
| 169 |
inside the root tag. False returns the full |
| 170 |
response from the API. |
| 171 |
:raises AssertionError: if the expected_code isn't a valid http success |
| 172 |
response code |
| 173 |
:raises BadRequest: If a 400 response code is received |
| 174 |
:raises Conflict: If a 409 response code is received |
| 175 |
:raises Forbidden: If a 403 response code is received |
| 176 |
:raises Gone: If a 410 response code is received |
| 177 |
:raises InvalidContentType: If a 415 response code is received |
| 178 |
:raises InvalidHTTPResponseBody: The response body wasn't valid JSON |
| 179 |
:raises InvalidHttpSuccessCode: if the read code isn't an expected |
| 180 |
http success code |
| 181 |
:raises NotFound: If a 404 response code is received |
| 182 |
:raises NotImplemented: If a 501 response code is received |
| 183 |
:raises OverLimit: If a 413 response code is received and over_limit is |
| 184 |
not in the response body |
| 185 |
:raises RateLimitExceeded: If a 413 response code is received and |
| 186 |
over_limit is in the response body |
| 187 |
:raises ServerFault: If a 500 response code is received |
| 188 |
:raises Unauthorized: If a 401 response code is received |
| 189 |
:raises UnexpectedContentType: If the content-type of the response |
| 190 |
isn't an expect type |
| 191 |
:raises UnexpectedResponseCode: If a response code above 400 is |
| 192 |
received and it doesn't fall into any |
| 193 |
of the handled checks |
| 194 |
:raises UnprocessableEntity: If a 422 response code is received and |
| 195 |
couldn't be parsed |
| 196 |
:returns: A list of appropriate objects. |
| 197 |
""" |
| 198 |
if parent_id: |
| 199 |
uri = self.uri.format(parent=parent_id) |
| 200 |
else: |
| 201 |
uri = self.uri |
| 202 |
|
| 203 |
if query_params: |
| 204 |
request_uri = '{0}?{1}'.format(uri, query_params) |
| 205 |
else: |
| 206 |
request_uri = uri |
| 207 |
response, body = self.get(request_uri) |
| 208 |
self.expected_success(200, response.status) |
| 209 |
if return_object_only: |
| 210 |
return jsonutils.loads(body.decode('utf-8'))[self.list_root_tag] |
| 211 |
else: |
| 212 |
return jsonutils.loads(body.decode('utf-8')) |
| 213 |
|
| 214 |
def _update_object(self, obj_id, parent_id=None, return_object_only=True, |
| 215 |
**kwargs): |
| 216 |
"""Update an object. |
| 217 |
|
| 218 |
:param obj_id: The object ID to update. |
| 219 |
:param parent_id: The parent object ID, if applicable. |
| 220 |
:param return_object_only: If True, the response returns the object |
| 221 |
inside the root tag. False returns the full |
| 222 |
response from the API. |
| 223 |
:param **kwargs: All attributes of the object should be passed as |
| 224 |
keyword arguments to this function. |
| 225 |
:raises AssertionError: if the expected_code isn't a valid http success |
| 226 |
response code |
| 227 |
:raises BadRequest: If a 400 response code is received |
| 228 |
:raises Conflict: If a 409 response code is received |
| 229 |
:raises Forbidden: If a 403 response code is received |
| 230 |
:raises Gone: If a 410 response code is received |
| 231 |
:raises InvalidContentType: If a 415 response code is received |
| 232 |
:raises InvalidHTTPResponseBody: The response body wasn't valid JSON |
| 233 |
:raises InvalidHttpSuccessCode: if the read code isn't an expected |
| 234 |
http success code |
| 235 |
:raises NotFound: If a 404 response code is received |
| 236 |
:raises NotImplemented: If a 501 response code is received |
| 237 |
:raises OverLimit: If a 413 response code is received and over_limit is |
| 238 |
not in the response body |
| 239 |
:raises RateLimitExceeded: If a 413 response code is received and |
| 240 |
over_limit is in the response body |
| 241 |
:raises ServerFault: If a 500 response code is received |
| 242 |
:raises Unauthorized: If a 401 response code is received |
| 243 |
:raises UnexpectedContentType: If the content-type of the response |
| 244 |
isn't an expect type |
| 245 |
:raises UnexpectedResponseCode: If a response code above 400 is |
| 246 |
received and it doesn't fall into any |
| 247 |
of the handled checks |
| 248 |
:raises UnprocessableEntity: If a 422 response code is received and |
| 249 |
couldn't be parsed |
| 250 |
:returns: An appropriate object. |
| 251 |
""" |
| 252 |
obj_dict = {self.root_tag: kwargs} |
| 253 |
|
| 254 |
if parent_id: |
| 255 |
uri = self.uri.format(parent=parent_id) |
| 256 |
else: |
| 257 |
uri = self.uri |
| 258 |
|
| 259 |
request_uri = '{0}/{1}'.format(uri, obj_id) |
| 260 |
response, body = self.put(request_uri, jsonutils.dumps(obj_dict)) |
| 261 |
self.expected_success(200, response.status) |
| 262 |
if return_object_only: |
| 263 |
return jsonutils.loads(body.decode('utf-8'))[self.root_tag] |
| 264 |
else: |
| 265 |
return jsonutils.loads(body.decode('utf-8')) |
| 266 |
|
| 267 |
def _delete_obj(self, obj_id, parent_id=None, ignore_errors=False, |
| 268 |
cascade=False): |
| 269 |
"""Delete an object. |
| 270 |
|
| 271 |
:param obj_id: The object ID to delete. |
| 272 |
:param ignore_errors: True if errors should be ignored. |
| 273 |
:param cascade: If true will delete all child objects of an |
| 274 |
object, if that object supports it. |
| 275 |
:raises AssertionError: if the expected_code isn't a valid http success |
| 276 |
response code |
| 277 |
:raises BadRequest: If a 400 response code is received |
| 278 |
:raises Conflict: If a 409 response code is received |
| 279 |
:raises Forbidden: If a 403 response code is received |
| 280 |
:raises Gone: If a 410 response code is received |
| 281 |
:raises InvalidContentType: If a 415 response code is received |
| 282 |
:raises InvalidHTTPResponseBody: The response body wasn't valid JSON |
| 283 |
:raises InvalidHttpSuccessCode: if the read code isn't an expected |
| 284 |
http success code |
| 285 |
:raises NotFound: If a 404 response code is received |
| 286 |
:raises NotImplemented: If a 501 response code is received |
| 287 |
:raises OverLimit: If a 413 response code is received and over_limit is |
| 288 |
not in the response body |
| 289 |
:raises RateLimitExceeded: If a 413 response code is received and |
| 290 |
over_limit is in the response body |
| 291 |
:raises ServerFault: If a 500 response code is received |
| 292 |
:raises Unauthorized: If a 401 response code is received |
| 293 |
:raises UnexpectedContentType: If the content-type of the response |
| 294 |
isn't an expect type |
| 295 |
:raises UnexpectedResponseCode: If a response code above 400 is |
| 296 |
received and it doesn't fall into any |
| 297 |
of the handled checks |
| 298 |
:raises UnprocessableEntity: If a 422 response code is received and |
| 299 |
couldn't be parsed |
| 300 |
:returns: None if ignore_errors is True, the response status code |
| 301 |
if not. |
| 302 |
""" |
| 303 |
if parent_id: |
| 304 |
uri = self.uri.format(parent=parent_id) |
| 305 |
else: |
| 306 |
uri = self.uri |
| 307 |
|
| 308 |
if cascade: |
| 309 |
request_uri = '{0}/{1}?cascade=true'.format(uri, obj_id) |
| 310 |
else: |
| 311 |
request_uri = '{0}/{1}'.format(uri, obj_id) |
| 312 |
if ignore_errors: |
| 313 |
try: |
| 314 |
response, body = self.delete(request_uri) |
| 315 |
except Exception: |
| 316 |
return |
| 317 |
else: |
| 318 |
response, body = self.delete(request_uri) |
| 319 |
|
| 320 |
self.expected_success(204, response.status) |
| 321 |
return response.status |
| 322 |
|
| 323 |
def _cleanup_obj(self, obj_id, lb_client=None, lb_id=None, parent_id=None, |
| 324 |
cascade=False): |
| 325 |
"""Clean up an object (for use in tempest addClassResourceCleanup). |
| 326 |
|
| 327 |
We always need to wait for the parent LB to be in a mutable state |
| 328 |
before deleting the child object, and the cleanups will not guarantee |
| 329 |
this if we just pass the delete function to tempest cleanup. |
| 330 |
For example, if we add multiple listeners on the same LB to cleanup, |
| 331 |
tempest will delete the first one and then immediately try to delete |
| 332 |
the second one, which will fail because the LB will be immutable. |
| 333 |
|
| 334 |
We also need to wait to return until the parent LB is back in a mutable |
| 335 |
state so future tests don't break right at the start. |
| 336 |
|
| 337 |
This function: |
| 338 |
* Waits until the parent LB is ACTIVE |
| 339 |
* Deletes the object |
| 340 |
* Waits until the parent LB is ACTIVE |
| 341 |
|
| 342 |
:param obj_id: The object ID to clean up. |
| 343 |
:param lb_client: (Optional) The loadbalancer client, if this isn't the |
| 344 |
loadbalancer client already. |
| 345 |
:param lb_id: (Optional) The ID of the parent loadbalancer, if the main |
| 346 |
obj_id is for a sub-object and not a loadbalancer. |
| 347 |
:param cascade: If true will delete all child objects of an |
| 348 |
object, if that object supports it. |
| 349 |
:return: |
| 350 |
""" |
| 351 |
if parent_id: |
| 352 |
uri = self.uri.format(parent=parent_id) |
| 353 |
else: |
| 354 |
uri = self.uri |
| 355 |
|
| 356 |
if lb_client and lb_id: |
| 357 |
wait_id = lb_id |
| 358 |
wait_client = lb_client |
| 359 |
wait_func = lb_client.show_loadbalancer |
| 360 |
else: |
| 361 |
wait_id = obj_id |
| 362 |
wait_client = self |
| 363 |
wait_func = self._show_object |
| 364 |
|
| 365 |
LOG.info("Starting cleanup for %s %s...", self.root_tag, obj_id) |
| 366 |
|
| 367 |
try: |
| 368 |
request_uri = '{0}/{1}'.format(uri, obj_id) |
| 369 |
response, body = self.get(request_uri) |
| 370 |
resp_obj = jsonutils.loads(body.decode('utf-8'))[self.root_tag] |
| 371 |
if (response.status == 404 or |
| 372 |
resp_obj['provisioning_status'] == const.DELETED): |
| 373 |
raise exceptions.NotFound() |
| 374 |
except exceptions.NotFound: |
| 375 |
|
| 376 |
LOG.info("%s %s is already gone. Cleanup considered complete.", |
| 377 |
self.root_tag, obj_id) |
| 378 |
return |
| 379 |
|
| 380 |
LOG.info("Waiting for %s %s to be ACTIVE...", |
| 381 |
wait_client.root_tag, wait_id) |
| 382 |
try: |
| 383 |
waiters.wait_for_status(wait_func, wait_id, |
| 384 |
const.PROVISIONING_STATUS, |
| 385 |
const.ACTIVE, |
| 386 |
CONF.load_balancer.check_interval, |
| 387 |
CONF.load_balancer.check_timeout) |
| 388 |
except exceptions.UnexpectedResponseCode: |
| 389 |
|
| 390 |
LOG.debug("Found %s %s in ERROR status, proceeding with cleanup.", |
| 391 |
wait_client.root_tag, wait_id) |
| 392 |
except exceptions.TimeoutException: |
| 393 |
|
| 394 |
LOG.error("Timeout exceeded waiting to clean up %s %s.", |
| 395 |
self.root_tag, obj_id) |
| 396 |
except exceptions.NotFound: |
| 397 |
|
| 398 |
LOG.info("%s %s is already gone. Cleanup considered complete.", |
| 399 |
wait_client.root_tag.capitalize(), wait_id) |
| 400 |
return |
| 401 |
except Exception as e: |
| 402 |
|
| 403 |
LOG.error("Cleanup encountered an unknown exception while waiting " |
| 404 |
"for %s %s: %s", wait_client.root_tag, wait_id, e) |
| 405 |
|
| 406 |
if cascade: |
| 407 |
uri = '{0}/{1}?cascade=true'.format(uri, obj_id) |
| 408 |
else: |
| 409 |
uri = '{0}/{1}'.format(uri, obj_id) |
| 410 |
LOG.info("Cleaning up %s %s...", self.root_tag, obj_id) |
| 411 |
return_status = test_utils.call_and_ignore_notfound_exc( |
| 412 |
self.delete, uri) |
| 413 |
|
| 414 |
if lb_id and lb_client: |
| 415 |
LOG.info("Waiting for %s %s to be ACTIVE...", |
| 416 |
wait_client.root_tag, wait_id) |
| 417 |
waiters.wait_for_status(wait_func, wait_id, |
| 418 |
const.PROVISIONING_STATUS, |
| 419 |
const.ACTIVE, |
| 420 |
CONF.load_balancer.check_interval, |
| 421 |
CONF.load_balancer.check_timeout) |
| 422 |
else: |
| 423 |
LOG.info("Waiting for %s %s to be DELETED...", |
| 424 |
wait_client.root_tag, wait_id) |
| 425 |
waiters.wait_for_deleted_status_or_not_found( |
| 426 |
wait_func, wait_id, const.PROVISIONING_STATUS, |
| 427 |
CONF.load_balancer.check_interval, |
| 428 |
CONF.load_balancer.check_timeout) |
| 429 |
|
| 430 |
LOG.info("Cleanup complete for %s %s...", self.root_tag, obj_id) |
| 431 |
return return_status |
| 432 |
|
| 433 |
def is_resource_deleted(self, id): |
| 434 |
"""Check if the object is deleted. |
| 435 |
|
| 436 |
:param id: The object ID to check. |
| 437 |
:return: boolean state representing the object's deleted state |
| 438 |
""" |
| 439 |
try: |
| 440 |
obj = self._show_object(id) |
| 441 |
if obj.get(const.PROVISIONING_STATUS) == const.DELETED: |
| 442 |
return True |
| 443 |
except exceptions.NotFound: |
| 444 |
return True |
| 445 |
return False |
| 446 |
|
| 447 |
def get_max_api_version(self): |
| 448 |
"""Get the maximum version available on the API endpoint. |
| 449 |
|
| 450 |
:return: Maximum version string available on the endpoint. |
| 451 |
""" |
| 452 |
response, body = self.get('/') |
| 453 |
self.expected_success(200, response.status) |
| 454 |
|
| 455 |
versions_list = jsonutils.loads(body.decode('utf-8'))['versions'] |
| 456 |
current_versions = (version for version in versions_list if |
| 457 |
version['status'] == 'CURRENT') |
| 458 |
max_version = '0.0' |
| 459 |
for version in current_versions: |
| 460 |
|
| 461 |
ver_string = version['id'] |
| 462 |
if ver_string.startswith("v"): |
| 463 |
ver_string = ver_string[1:] |
| 464 |
|
| 465 |
ver_split = list(map(int, ver_string.split('.'))) |
| 466 |
max_split = list(map(int, max_version.split('.'))) |
| 467 |
|
| 468 |
if len(ver_split) > 2: |
| 469 |
raise exceptions.InvalidAPIVersionString(version=ver_string) |
| 470 |
|
| 471 |
if ver_split[0] > max_split[0] or ( |
| 472 |
ver_split[0] == max_split[0] and |
| 473 |
ver_split[1] >= max_split[1]): |
| 474 |
max_version = ver_string |
| 475 |
|
| 476 |
if max_version == '0.0': |
| 477 |
raise exceptions.InvalidAPIVersionString(version=max_version) |
| 478 |
|
| 479 |
return max_version |
| 480 |
|
| 481 |
def is_version_supported(self, api_version, version): |
| 482 |
"""Check if a version is supported by the API. |
| 483 |
|
| 484 |
:param api_version: Reference endpoint API version. |
| 485 |
:param version: Version to check against API version. |
| 486 |
:return: boolean if the version is supported. |
| 487 |
""" |
| 488 |
|
| 489 |
api_split = list(map(int, api_version.split('.'))) |
| 490 |
ver_split = list(map(int, version.split('.'))) |
| 491 |
|
| 492 |
if api_split[0] > ver_split[0] or ( |
| 493 |
api_split[0] == ver_split[0] and api_split[1] >= ver_split[1]): |
| 494 |
return True |
| 495 |
return False |