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+ import boto3
15+ import pytest
16+ import time
17+ import logging
18+ from typing import Dict , Tuple
19+
20+ from acktest .resources import random_suffix_name
21+ from acktest .k8s import resource as k8s
22+ from acktest .aws .identity import get_region
23+ from e2e import (
24+ service_marker ,
25+ CRD_GROUP ,
26+ CRD_VERSION ,
27+ load_dynamodb_resource ,
28+ wait_for_cr_status ,
29+ )
30+ from e2e .replacement_values import REPLACEMENT_VALUES
31+
32+ RESOURCE_PLURAL = "backups"
33+
34+ DELETE_WAIT_AFTER_SECONDS = 10
35+
36+ @pytest .fixture (scope = "module" )
37+ def dynamodb_client ():
38+ return boto3 .client ("dynamodb" )
39+
40+ @pytest .fixture (scope = "module" )
41+ def dynamodb_table ():
42+ resource_name = random_suffix_name ("table" , 32 )
43+
44+ replacements = REPLACEMENT_VALUES .copy ()
45+ replacements ["TABLE_NAME" ] = resource_name
46+
47+ # load resource
48+ resource_data = load_dynamodb_resource (
49+ "table_forums" ,
50+ additional_replacements = replacements ,
51+ )
52+
53+ table_reference = k8s .CustomResourceReference (
54+ CRD_GROUP , CRD_VERSION , "tables" ,
55+ resource_name , namespace = "default" ,
56+ )
57+
58+ # Create table
59+ k8s .create_custom_resource (table_reference , resource_data )
60+ table_resource = k8s .wait_resource_consumed_by_controller (table_reference )
61+
62+ assert table_resource is not None
63+ assert k8s .get_resource_exists (table_reference )
64+
65+ wait_for_cr_status (
66+ table_reference ,
67+ "tableStatus" ,
68+ "ACTIVE" ,
69+ 10 ,
70+ 30 ,
71+ )
72+
73+ yield (table_reference , table_resource )
74+
75+ _ , deleted = k8s .delete_custom_resource (table_reference )
76+ assert deleted
77+
78+ @service_marker
79+ @pytest .mark .canary
80+ class TestBackup :
81+ def get_backup (self , dynamodb_client , backup_arn : str ) -> dict :
82+ try :
83+ resp = dynamodb_client .describe_backup (
84+ BackupArn = backup_arn ,
85+ )
86+ return resp ["BackupDescription" ]
87+
88+ except Exception as e :
89+ logging .debug (e )
90+ return None
91+
92+ def backup_exists (self , dynamodb_client , backup_arn : str ) -> bool :
93+ return self .get_backup (dynamodb_client , backup_arn ) is not None
94+
95+ def test_smoke (self , dynamodb_client , dynamodb_table ):
96+ (_ , table_resource ) = dynamodb_table
97+ resource_name = random_suffix_name ("backup" , 32 )
98+ table_name = table_resource ["spec" ]["tableName" ]
99+
100+ replacements = REPLACEMENT_VALUES .copy ()
101+ replacements ["TABLE_NAME" ] = table_name
102+ replacements ["BACKUP_NAME" ] = resource_name
103+
104+ # Load Backup CR
105+ resource_data = load_dynamodb_resource (
106+ "backup" ,
107+ additional_replacements = replacements ,
108+ )
109+ logging .debug (resource_data )
110+
111+ # Create k8s resource
112+ ref = k8s .CustomResourceReference (
113+ CRD_GROUP , CRD_VERSION , RESOURCE_PLURAL ,
114+ resource_name , namespace = "default" ,
115+ )
116+ k8s .create_custom_resource (ref , resource_data )
117+ cr = k8s .wait_resource_consumed_by_controller (ref )
118+
119+ assert cr is not None
120+ assert k8s .get_resource_exists (ref )
121+
122+ wait_for_cr_status (
123+ ref ,
124+ "backupStatus" ,
125+ "AVAILABLE" ,
126+ 10 ,
127+ 5 ,
128+ )
129+
130+ backupArn = k8s .get_resource_arn (cr )
131+ # Check DynamoDB Backup exists
132+ exists = self .backup_exists (dynamodb_client , backupArn )
133+ assert exists
134+
135+ # Delete k8s resource
136+ _ , deleted = k8s .delete_custom_resource (ref )
137+ assert deleted is True
138+
139+ time .sleep (DELETE_WAIT_AFTER_SECONDS )
140+
141+ # Check DynamoDB Backup doesn't exists
142+ exists = self .backup_exists (dynamodb_client , backupArn )
143+ assert not exists
0 commit comments