|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +# not use this file except in compliance with the License. A copy of the |
| 5 | +# License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is distributed |
| 10 | +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +# express or implied. See the License for the specific language governing |
| 12 | +# permissions and limitations under the License. |
| 13 | + |
| 14 | +"""Utilities for working with Global Cluster resources""" |
| 15 | + |
| 16 | +import datetime |
| 17 | +import time |
| 18 | +import typing |
| 19 | + |
| 20 | +import boto3 |
| 21 | +import pytest |
| 22 | + |
| 23 | +DEFAULT_WAIT_UNTIL_TIMEOUT_SECONDS = 60*10 |
| 24 | +DEFAULT_WAIT_UNTIL_INTERVAL_SECONDS = 15 |
| 25 | +DEFAULT_WAIT_UNTIL_DELETED_TIMEOUT_SECONDS = 60*10 |
| 26 | +DEFAULT_WAIT_UNTIL_DELETED_INTERVAL_SECONDS = 15 |
| 27 | + |
| 28 | +GlobalClusterMatchFunc = typing.NewType( |
| 29 | + 'GlobalClusterMatchFunc', |
| 30 | + typing.Callable[[dict], bool], |
| 31 | +) |
| 32 | + |
| 33 | +class StatusMatcher: |
| 34 | + def __init__(self, status): |
| 35 | + self.match_on = status |
| 36 | + |
| 37 | + def __call__(self, record: dict) -> bool: |
| 38 | + return 'Status' in record and record['Status'] == self.match_on |
| 39 | + |
| 40 | + |
| 41 | +def status_matches(status: str) -> GlobalClusterMatchFunc: |
| 42 | + return StatusMatcher(status) |
| 43 | + |
| 44 | + |
| 45 | +def wait_until( |
| 46 | + global_cluster_id: str, |
| 47 | + match_fn: GlobalClusterMatchFunc, |
| 48 | + timeout_seconds: int = DEFAULT_WAIT_UNTIL_TIMEOUT_SECONDS, |
| 49 | + interval_seconds: int = DEFAULT_WAIT_UNTIL_INTERVAL_SECONDS, |
| 50 | + ) -> None: |
| 51 | + """Waits until a DB global cluster with a supplied ID is returned from the RDS API |
| 52 | + and the matching functor returns True. |
| 53 | +
|
| 54 | + Usage: |
| 55 | + from e2e.db_global_cluster import wait_until, status_matches |
| 56 | +
|
| 57 | + wait_until( |
| 58 | + global_cluster_id, |
| 59 | + status_matches("available"), |
| 60 | + ) |
| 61 | +
|
| 62 | + Raises: |
| 63 | + pytest.fail upon timeout |
| 64 | + """ |
| 65 | + now = datetime.datetime.now() |
| 66 | + timeout = now + datetime.timedelta(seconds=timeout_seconds) |
| 67 | + |
| 68 | + while not match_fn(get(global_cluster_id)): |
| 69 | + if datetime.datetime.now() >= timeout: |
| 70 | + pytest.fail("failed to match Global Cluster before timeout") |
| 71 | + time.sleep(interval_seconds) |
| 72 | + |
| 73 | + |
| 74 | +def wait_until_deleted( |
| 75 | + global_cluster_id: str, |
| 76 | + timeout_seconds: int = DEFAULT_WAIT_UNTIL_DELETED_TIMEOUT_SECONDS, |
| 77 | + interval_seconds: int = DEFAULT_WAIT_UNTIL_DELETED_INTERVAL_SECONDS, |
| 78 | + ) -> None: |
| 79 | + """Waits until a Global Cluster with a supplied ID is no longer returned from |
| 80 | + the RDS API. |
| 81 | +
|
| 82 | + Usage: |
| 83 | + from e2e.global_cluster import wait_until_deleted |
| 84 | +
|
| 85 | + wait_until_deleted(global_cluster_id) |
| 86 | +
|
| 87 | + Raises: |
| 88 | + pytest.fail upon timeout or if the Global Cluster goes to any other status |
| 89 | + other than 'deleting' |
| 90 | + """ |
| 91 | + now = datetime.datetime.now() |
| 92 | + timeout = now + datetime.timedelta(seconds=timeout_seconds) |
| 93 | + |
| 94 | + while True: |
| 95 | + if datetime.datetime.now() >= timeout: |
| 96 | + pytest.fail( |
| 97 | + "Timed out waiting for Global Cluster to be " |
| 98 | + "deleted in RDS API" |
| 99 | + ) |
| 100 | + time.sleep(interval_seconds) |
| 101 | + |
| 102 | + latest = get(global_cluster_id) |
| 103 | + if latest is None: |
| 104 | + break |
| 105 | + |
| 106 | + if latest['Status'] != "deleting": |
| 107 | + pytest.fail( |
| 108 | + "Status is not 'deleting' for global cluster that was " |
| 109 | + "deleted. Status is " + latest['Status'] |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def get(global_cluster_id): |
| 114 | + """Returns a dict containing the Global Cluster record from the RDS API. |
| 115 | +
|
| 116 | + If no such Global Cluster exists, returns None. |
| 117 | + """ |
| 118 | + c = boto3.client('rds') |
| 119 | + try: |
| 120 | + resp = c.describe_global_clusters(GlobalClusterIdentifier=global_cluster_id) |
| 121 | + assert len(resp['GlobalClusters']) == 1 |
| 122 | + return resp['GlobalClusters'][0] |
| 123 | + except c.exceptions.GlobalClusterNotFoundFault: |
| 124 | + return None |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +def get_tags(global_cluster_arn): |
| 129 | + """Returns a dict containing the Global Cluster's tag records from the RDS API. |
| 130 | + Currently RDS doesn't support add tags to global cluster, so this is a noop for now |
| 131 | + If no such global cluster exists, returns None. |
| 132 | + """ |
| 133 | + c = boto3.client('rds') |
| 134 | + try: |
| 135 | + resp = c.list_tags_for_resource( |
| 136 | + ResourceName=global_cluster_arn, |
| 137 | + ) |
| 138 | + return resp['TagList'] |
| 139 | + except c.exceptions.GlobalClusterNotFoundFault: |
| 140 | + return None |
0 commit comments