Skip to content

Commit b20f1d2

Browse files
committed
temp
1 parent 53d3d50 commit b20f1d2

File tree

3 files changed

+63
-165
lines changed

3 files changed

+63
-165
lines changed

test/e2e/bootstrap_resources.py

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# not use this file except in compliance with the License. A copy of the
55
# License is located at
66
#
7-
# http://aws.amazon.com/apache2.0/
7+
# http://aws.amazon.com/apache2.0/
88
#
99
# or in the "license" file accompanying this file. This file is distributed
1010
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
@@ -16,79 +16,33 @@
1616
"""
1717

1818
from dataclasses import dataclass
19+
from acktest.bootstrapping import Resources
1920
from e2e import bootstrap_directory
20-
import yaml
21-
import logging
22-
from pathlib import Path
21+
2322

2423
@dataclass
25-
class TestBootstrapResources:
26-
SnsTopic1: str
27-
SnsTopic2: str
28-
SecurityGroup1: str
29-
SecurityGroup2: str
30-
UserGroup1: str
31-
UserGroup2: str
24+
class TestBootstrapResources(Resources):
25+
# For test_snapshot.py and test_replicationgroup.py
26+
Snapshot: str
3227
KmsKeyID: str
33-
SnapshotName: str
28+
29+
# For test_usergroup.py
3430
NonDefaultUser: str
35-
CWLogGroup1: str
36-
CWLogGroup2: str
37-
CPGName: str
3831

3932
def replacement_dict(self):
4033
return {
41-
"SNS_TOPIC_ARN": self.SnsTopic1,
42-
"SNS_TOPIC_ARN_2": self.SnsTopic2,
43-
"SG_ID": self.SecurityGroup1,
44-
"SG_ID_2": self.SecurityGroup2,
45-
"USERGROUP_ID": self.UserGroup1,
46-
"USERGROUP_ID_2": self.UserGroup2,
34+
"SNAPSHOT_NAME": self.Snapshot,
4735
"KMS_KEY_ID": self.KmsKeyID,
48-
"SNAPSHOT_NAME": self.SnapshotName,
4936
"NON_DEFAULT_USER": self.NonDefaultUser,
50-
"LOG_GROUP": self.CWLogGroup1,
51-
"LOG_GROUP_2": self.CWLogGroup2,
52-
"CACHE_PARAMETER_GROUP_NAME": self.CPGName
5337
}
5438

39+
5540
_bootstrap_resources = None
5641

5742

58-
def get_bootstrap_resources(bootstrap_file_name: str = "bootstrap.yaml"):
43+
def get_bootstrap_resources(bootstrap_file_name: str = "bootstrap.pkl") -> TestBootstrapResources:
5944
global _bootstrap_resources
6045
if _bootstrap_resources is None:
61-
_bootstrap_resources = TestBootstrapResources(
62-
**read_bootstrap_config(bootstrap_directory, bootstrap_file_name=bootstrap_file_name),
63-
)
46+
_bootstrap_resources = TestBootstrapResources.deserialize(
47+
bootstrap_directory, bootstrap_file_name=bootstrap_file_name)
6448
return _bootstrap_resources
65-
66-
67-
def write_bootstrap_config(bootstrap: dict, output_path: Path, bootstrap_file_name: str = "bootstrap.yaml"):
68-
""" Dumps the bootstrap object into a YAML file at a given path.
69-
70-
Args:
71-
bootstrap: The bootstrap object.
72-
output_path: The directory in which to dump the bootstrap yaml.
73-
bootstrap_file_name: The name of the created bootstrap yaml file.
74-
"""
75-
path = output_path / bootstrap_file_name
76-
logging.info(f"Wrote bootstrap to {path}")
77-
with open(path, "w") as stream:
78-
yaml.safe_dump(bootstrap, stream)
79-
80-
81-
def read_bootstrap_config(config_dir: Path, bootstrap_file_name: str = "bootstrap.yaml") -> dict:
82-
""" Reads a bootstrap dictionary from a given bootstrap file.
83-
84-
Args:
85-
config_dir: The directory in which the bootstrap yaml exists.
86-
bootstrap_file_name: The name of the created bootstrap yaml file.
87-
88-
Returns:
89-
dict: The bootstrap dictionary read from the file.
90-
"""
91-
path = config_dir / bootstrap_file_name
92-
with open(path, "r") as stream:
93-
bootstrap = yaml.safe_load(stream)
94-
return bootstrap

test/e2e/service_bootstrap.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# not use this file except in compliance with the License. A copy of the
55
# License is located at
66
#
7-
# http://aws.amazon.com/apache2.0/
7+
# http://aws.amazon.com/apache2.0/
88
#
99
# or in the "license" file accompanying this file. This file is distributed
1010
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
@@ -26,6 +26,7 @@
2626
from e2e.util import wait_usergroup_active, wait_snapshot_available
2727
from e2e.bootstrap_resources import TestBootstrapResources, write_bootstrap_config
2828

29+
2930
def create_sns_topic() -> str:
3031
topic_name = random_suffix_name("ack-sns-topic", 32)
3132

@@ -36,35 +37,42 @@ def create_sns_topic() -> str:
3637
return response['TopicArn']
3738

3839
# create an EC2 VPC security group from the default VPC (not an ElastiCache security group)
40+
41+
3942
def create_security_group() -> str:
4043
region = get_region()
4144
account_id = get_account_id()
4245

4346
ec2 = boto3.client("ec2")
44-
vpc_response = ec2.describe_vpcs(Filters=[{"Name": "isDefault", "Values": ["true"]}])
47+
vpc_response = ec2.describe_vpcs(
48+
Filters=[{"Name": "isDefault", "Values": ["true"]}])
4549
if len(vpc_response['Vpcs']) == 0:
46-
raise ValueError(f"Default VPC not found for account {account_id} in region {region}")
50+
raise ValueError(
51+
f"Default VPC not found for account {account_id} in region {region}")
4752
default_vpc_id = vpc_response['Vpcs'][0]['VpcId']
4853

4954
sg_name = random_suffix_name("ack-security-group", 32)
5055
sg_description = "Security group for ACK ElastiCache tests"
51-
sg_response = ec2.create_security_group(GroupName=sg_name, VpcId=default_vpc_id, Description=sg_description)
56+
sg_response = ec2.create_security_group(
57+
GroupName=sg_name, VpcId=default_vpc_id, Description=sg_description)
5258
logging.info(f"Created VPC Security Group {sg_response['GroupId']}")
5359

5460
return sg_response['GroupId']
5561

62+
5663
def create_user_group() -> str:
5764
ec = boto3.client("elasticache")
5865

5966
usergroup_id = random_suffix_name("ack-ec-usergroup", 32)
6067
_ = ec.create_user_group(UserGroupId=usergroup_id,
61-
Engine="Redis",
62-
UserIds=["default"])
68+
Engine="Redis",
69+
UserIds=["default"])
6370
logging.info(f"Creating ElastiCache User Group {usergroup_id}")
6471
assert wait_usergroup_active(usergroup_id)
6572

6673
return usergroup_id
6774

75+
6876
def create_kms_key() -> str:
6977
kms = boto3.client("kms")
7078

@@ -75,6 +83,8 @@ def create_kms_key() -> str:
7583
return key_id
7684

7785
# create a cache cluster, snapshot it, and return the snapshot name
86+
87+
7888
def create_cc_snapshot():
7989
ec = boto3.client("elasticache")
8090

@@ -95,6 +105,7 @@ def create_cc_snapshot():
95105
SnapshotName=snapshot_name
96106
)
97107
assert wait_snapshot_available(snapshot_name)
108+
logging.info(f"Created snapshot {snapshot_name}")
98109

99110
return snapshot_name
100111

@@ -103,13 +114,14 @@ def create_non_default_user() -> str:
103114
ec = boto3.client("elasticache")
104115
user_id = random_suffix_name("ackecuser", 32)
105116

106-
_ = ec.create_user(UserId=user_id,
107-
UserName="ACKNonDefaultUser",
108-
Engine="Redis",
109-
NoPasswordRequired=True,
110-
AccessString="on -@all")
111-
112-
logging.info(f"Creating ElastiCache non default User {user_id}")
117+
_ = ec.create_user(
118+
UserId=user_id,
119+
UserName="ACKNonDefaultUser",
120+
Engine="Redis",
121+
NoPasswordRequired=True,
122+
AccessString="on -@all"
123+
)
124+
logging.info(f"Created ElastiCache non default User {user_id}")
113125
return user_id
114126

115127

@@ -172,4 +184,4 @@ def service_bootstrap() -> dict:
172184

173185
if __name__ == "__main__":
174186
config = service_bootstrap()
175-
write_bootstrap_config(config, bootstrap_directory)
187+
write_bootstrap_config(config, bootstrap_directory)

test/e2e/service_cleanup.py

Lines changed: 24 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,26 @@
44
# not use this file except in compliance with the License. A copy of the
55
# License is located at
66
#
7-
# http://aws.amazon.com/apache2.0/
7+
# http://aws.amazon.com/apache2.0/
88
#
99
# or in the "license" file accompanying this file. This file is distributed
1010
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1111
# express or implied. See the License for the specific language governing
1212
# permissions and limitations under the License.
13+
1314
"""Cleans up the resources created by the Elasticache bootstrapping process.
1415
"""
1516

1617
import boto3
1718
import logging
18-
19-
from acktest import resources
2019
from e2e import bootstrap_directory
21-
from e2e.bootstrap_resources import TestBootstrapResources, read_bootstrap_config
22-
23-
def delete_sns_topic(topic_ARN: str):
24-
sns = boto3.client("sns")
25-
sns.delete_topic(TopicArn=topic_ARN)
26-
logging.info(f"Deleted SNS topic {topic_ARN}")
27-
28-
def delete_security_group(sg_id: str):
29-
ec2 = boto3.client("ec2")
30-
ec2.delete_security_group(GroupId=sg_id)
31-
logging.info(f"Deleted VPC Security Group {sg_id}")
32-
33-
def delete_user_group(usergroup_id: str):
34-
ec = boto3.client("elasticache")
35-
ec.delete_user_group(UserGroupId=usergroup_id)
36-
logging.info(f"Deleted ElastiCache User Group {usergroup_id}")
20+
from e2e.bootstrap_resources import TestBootstrapResources
3721

38-
# KMS does not allow immediate key deletion; 7 days is the shortest deletion window
39-
def delete_kms_key(key_id: str):
40-
kms = boto3.client("kms")
41-
kms.schedule_key_deletion(KeyId=key_id, PendingWindowInDays=7)
42-
logging.info(f"Deletion scheduled for KMS key {key_id}")
4322

44-
# delete snapshot and also associated cluster/RG
4523
def delete_snapshot(snapshot_name: str):
24+
if not snapshot_name:
25+
return
26+
4627
ec = boto3.client("elasticache")
4728

4829
# delete actual snapshot
@@ -55,97 +36,48 @@ def delete_snapshot(snapshot_name: str):
5536
if snapshot['CacheClusterId']:
5637
ec.delete_cache_cluster(CacheClusterId=snapshot['CacheClusterId'])
5738
logging.info(f"Deleted cache cluster {snapshot['CacheClusterId']}")
58-
elif snapshot['ReplicationGroupId']: # should not happen
59-
ec.delete_replication_group(ReplicationGroupId=snapshot['ReplicationGroupId'])
60-
logging.info(f"Deleted replication group {snapshot['ReplicationGroupId']}")
6139

6240

63-
def delete_non_default_user(user_id: str):
64-
ec = boto3.client("elasticache")
65-
ec.delete_user(UserId=user_id)
66-
logging.info(f"Deleted non default user {user_id}")
41+
def delete_kms_key(key_id: str):
42+
if not key_id:
43+
return
6744

45+
kms = boto3.client("kms")
46+
kms.schedule_key_deletion(KeyId=key_id, PendingWindowInDays=7)
47+
logging.info(f"Scheduled deletion for KMS key {key_id}")
6848

69-
def delete_log_group(log_group_name: str):
70-
logs = boto3.client("logs")
71-
logs.delete_log_group(logGroupName=log_group_name)
72-
logging.info(f"Deleted CW log group {log_group_name}")
7349

50+
def delete_non_default_user(user_id: str):
51+
if not user_id:
52+
return
7453

75-
def delete_cpg(cpg_name: str):
7654
ec = boto3.client("elasticache")
77-
ec.delete_cache_parameter_group(CacheParameterGroupName=cpg_name)
78-
logging.info(f"Deleted ElastiCache cache parameter group {cpg_name}")
55+
ec.delete_user(UserId=user_id)
56+
logging.info(f"Deleted non default user {user_id}")
7957

8058

8159
def service_cleanup(config: dict):
8260
logging.getLogger().setLevel(logging.INFO)
8361

84-
resources = TestBootstrapResources(
85-
**config
86-
)
87-
88-
try:
89-
delete_sns_topic(resources.SnsTopic1)
90-
except:
91-
logging.exception(f"Unable to delete SNS topic {resources.SnsTopic1}")
62+
resources = TestBootstrapResources(**config)
9263

9364
try:
94-
delete_sns_topic(resources.SnsTopic2)
65+
delete_snapshot(resources.Snapshot)
9566
except:
96-
logging.exception(f"Unable to delete SNS topic {resources.SnsTopic2}")
97-
98-
try:
99-
delete_security_group(resources.SecurityGroup1)
100-
except:
101-
logging.exception(f"Unable to delete VPC Security Group {resources.SecurityGroup1}")
102-
103-
try:
104-
delete_security_group(resources.SecurityGroup2)
105-
except:
106-
logging.exception(f"Unable to delete VPC Security Group {resources.SecurityGroup2}")
107-
108-
try:
109-
delete_user_group(resources.UserGroup1)
110-
except:
111-
logging.exception(f"Unable to delete ElastiCache User Group {resources.UserGroup1}")
112-
113-
try:
114-
delete_user_group(resources.UserGroup2)
115-
except:
116-
logging.exception(f"Unable to delete ElastiCache User Group {resources.UserGroup2}")
67+
logging.exception(f"Unable to delete snapshot {resources.Snapshot}")
11768

11869
try:
11970
delete_kms_key(resources.KmsKeyID)
12071
except:
121-
logging.exception(f"Unable to schedule deletion for KMS key {resources.KmsKeyID}")
122-
123-
try:
124-
delete_snapshot(resources.SnapshotName)
125-
except:
126-
logging.exception(f"Unable to delete snapshot {resources.SnapshotName}")
72+
logging.exception(
73+
f"Unable to schedule deletion for KMS key {resources.KmsKeyID}")
12774

12875
try:
12976
delete_non_default_user(resources.NonDefaultUser)
13077
except:
13178
logging.exception(f"Unable to delete user {resources.NonDefaultUser}")
13279

133-
try:
134-
delete_log_group(resources.CWLogGroup1)
135-
except:
136-
logging.exception(f"Unable to delete CW log group {resources.CWLogGroup1}")
137-
138-
try:
139-
delete_log_group(resources.CWLogGroup2)
140-
except:
141-
logging.exception(f"Unable to delete CW log group {resources.CWLogGroup2}")
142-
143-
try:
144-
delete_cpg(resources.CPGName)
145-
except:
146-
logging.exception(f"Unable to delete Elasticache cache parameter group {resources.CPGName}")
147-
14880

14981
if __name__ == "__main__":
150-
bootstrap_config = read_bootstrap_config(bootstrap_directory)
151-
service_cleanup(bootstrap_config)
82+
config = TestBootstrapResources.deserialize(bootstrap_directory)
83+
service_cleanup(config.__dict__)

0 commit comments

Comments
 (0)