|
| 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 Table 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 | + |
| 26 | +TableMatchFunc = typing.NewType( |
| 27 | + 'TableMatchFunc', |
| 28 | + typing.Callable[[dict], bool], |
| 29 | +) |
| 30 | + |
| 31 | +class StatusMatcher: |
| 32 | + def __init__(self, status): |
| 33 | + self.match_on = status |
| 34 | + |
| 35 | + def __call__(self, record: dict) -> bool: |
| 36 | + return ('TableStatus' in record |
| 37 | + and record['TableStatus'] == self.match_on) |
| 38 | + |
| 39 | + |
| 40 | +def status_matches(status: str) -> TableMatchFunc: |
| 41 | + return StatusMatcher(status) |
| 42 | + |
| 43 | + |
| 44 | +class TTLAttributeMatcher: |
| 45 | + def __init__(self, attr_name): |
| 46 | + self.attr_name = attr_name |
| 47 | + |
| 48 | + def __call__(self, record: dict) -> bool: |
| 49 | + if 'TableStatus' in record and record['TableStatus'] != 'ACTIVE': |
| 50 | + return False |
| 51 | + table_name = record['TableName'] |
| 52 | + # NOTE(jaypipes): The reason we have to do this craziness is because |
| 53 | + # DynamoDB's DescribeTimeToLive API is straight up bonkers. If you |
| 54 | + # update the TTL on a Table, the Table needs to transition to ACTIVE |
| 55 | + # status before DescribeTimeToLive will return a 200 and even after it |
| 56 | + # does, you need to wait additional time until the |
| 57 | + # TimeToLiveDescription response shape contains an AttributeName field |
| 58 | + # that matches what you set in your update call. The |
| 59 | + # TimeToLiveDescription field can be empty or can be a blank struct |
| 60 | + # with no fields in it for a long time after updating TTL on a Table... |
| 61 | + ttl = get_time_to_live(table_name) |
| 62 | + if ttl is not None: |
| 63 | + if 'AttributeName' in ttl: |
| 64 | + if ttl['AttributeName'] == self.attr_name: |
| 65 | + return True |
| 66 | + return False |
| 67 | + |
| 68 | + |
| 69 | +def ttl_on_attribute_matches(attr_name: str) -> TableMatchFunc: |
| 70 | + return TTLAttributeMatcher(attr_name) |
| 71 | + |
| 72 | + |
| 73 | +def wait_until( |
| 74 | + table_name: str, |
| 75 | + match_fn: TableMatchFunc, |
| 76 | + timeout_seconds: int = DEFAULT_WAIT_UNTIL_TIMEOUT_SECONDS, |
| 77 | + interval_seconds: int = DEFAULT_WAIT_UNTIL_INTERVAL_SECONDS, |
| 78 | + ) -> None: |
| 79 | + """Waits until a Table with a supplied name is returned from the DynamoDB |
| 80 | + API and the matching functor returns True. |
| 81 | +
|
| 82 | + Usage: |
| 83 | + from e2e.table import wait_until, status_matches |
| 84 | +
|
| 85 | + wait_until( |
| 86 | + table_name, |
| 87 | + status_matches("ACTIVE"), |
| 88 | + ) |
| 89 | +
|
| 90 | + Raises: |
| 91 | + pytest.fail upon timeout |
| 92 | + """ |
| 93 | + now = datetime.datetime.now() |
| 94 | + timeout = now + datetime.timedelta(seconds=timeout_seconds) |
| 95 | + |
| 96 | + while not match_fn(get(table_name)): |
| 97 | + if datetime.datetime.now() >= timeout: |
| 98 | + pytest.fail("failed to match DBInstance before timeout") |
| 99 | + time.sleep(interval_seconds) |
| 100 | + |
| 101 | + |
| 102 | +def get(table_name): |
| 103 | + """Returns a dict containing the Role record from the IAM API. |
| 104 | +
|
| 105 | + If no such Table exists, returns None. |
| 106 | + """ |
| 107 | + c = boto3.client('dynamodb') |
| 108 | + try: |
| 109 | + resp = c.describe_table(TableName=table_name) |
| 110 | + return resp['Table'] |
| 111 | + except c.exceptions.ResourceNotFoundException: |
| 112 | + return None |
| 113 | + |
| 114 | + |
| 115 | +def get_time_to_live(table_name): |
| 116 | + """Returns the TTL specification for the table with a supplied name. |
| 117 | +
|
| 118 | + If no such Table exists, returns None. |
| 119 | + """ |
| 120 | + c = boto3.client('dynamodb') |
| 121 | + try: |
| 122 | + resp = c.describe_time_to_live(TableName=table_name) |
| 123 | + return resp['TimeToLiveDescription'] |
| 124 | + except c.exceptions.ResourceNotFoundException: |
| 125 | + return None |
0 commit comments