Skip to content

Commit dc892e8

Browse files
authored
Refactor e2e tests to use assertion helper class (#41)
Issue #, if available: N/A Description of changes: * Adds a helper class, `Ec2Validator`, to assert resource data/state against aws apis more easily * This refactor will also make implementing resource reference e2e test easier (follow-up PR) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c90d50e commit dc892e8

9 files changed

+172
-251
lines changed

test/e2e/tests/helper.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
"""Helper functions for ec2 tests
15+
"""
16+
17+
class EC2Validator:
18+
def __init__(self, ec2_client):
19+
self.ec2_client = ec2_client
20+
21+
def assert_dhcp_options(self, dhcp_options_id: str, exists=True):
22+
res_found = False
23+
try:
24+
aws_res = self.ec2_client.describe_dhcp_options(DhcpOptionsIds=[dhcp_options_id])
25+
res_found = len(aws_res["DhcpOptions"]) > 0
26+
except self.ec2_client.exceptions.ClientError:
27+
pass
28+
assert res_found is exists
29+
30+
def assert_internet_gateway(self, ig_id: str, exists=True):
31+
res_found = False
32+
try:
33+
aws_res = self.ec2_client.describe_internet_gateways(InternetGatewayIds=[ig_id])
34+
res_found = len(aws_res["InternetGateways"]) > 0
35+
except self.ec2_client.exceptions.ClientError:
36+
pass
37+
assert res_found is exists
38+
39+
def assert_route(self, route_table_id: str, gateway_id: str, origin: str, exists=True):
40+
res_found = False
41+
try:
42+
aws_res = self.ec2_client.describe_route_tables(RouteTableIds=[route_table_id])
43+
routes = aws_res["RouteTables"][0]["Routes"]
44+
for route in routes:
45+
if route["Origin"] == origin and route["GatewayId"] == gateway_id:
46+
res_found = True
47+
except self.ec2_client.exceptions.ClientError:
48+
pass
49+
assert res_found is exists
50+
51+
def assert_route_table(self, route_table_id: str, exists=True):
52+
res_found = False
53+
try:
54+
aws_res = self.ec2_client.describe_route_tables(RouteTableIds=[route_table_id])
55+
res_found = len(aws_res["RouteTables"]) > 0
56+
except self.ec2_client.exceptions.ClientError:
57+
pass
58+
assert res_found is exists
59+
60+
def assert_security_group(self, sg_id: str, exists=True):
61+
res_found = False
62+
try:
63+
aws_res = self.ec2_client.describe_security_groups(GroupIds=[sg_id])
64+
res_found = len(aws_res["SecurityGroups"]) > 0
65+
except self.ec2_client.exceptions.ClientError:
66+
pass
67+
assert res_found is exists
68+
69+
def assert_subnet(self, subnet_id: str, exists=True):
70+
res_found = False
71+
try:
72+
aws_res = self.ec2_client.describe_subnets(SubnetIds=[subnet_id])
73+
res_found = len(aws_res["Subnets"]) > 0
74+
except self.ec2_client.exceptions.ClientError:
75+
pass
76+
assert res_found is exists
77+
78+
def assert_transit_gateway(self, tgw_id: str, exists=True):
79+
res_found = False
80+
try:
81+
aws_res = self.ec2_client.describe_transit_gateways(TransitGatewayIds=[tgw_id])
82+
tgw = aws_res["TransitGateways"][0]
83+
# TransitGateway may take awhile to be removed server-side, so
84+
# treat 'deleting' and 'deleted' states as resource no longer existing
85+
res_found = tgw is not None and tgw['State'] != "deleting" and tgw['State'] != "deleted"
86+
except self.ec2_client.exceptions.ClientError:
87+
pass
88+
assert res_found is exists
89+
90+
def assert_vpc(self, vpc_id: str, exists=True):
91+
res_found = False
92+
try:
93+
aws_res = self.ec2_client.describe_vpcs(VpcIds=[vpc_id])
94+
res_found = len(aws_res["Vpcs"]) > 0
95+
except self.ec2_client.exceptions.ClientError:
96+
pass
97+
assert res_found is exists
98+
99+
def assert_vpc_endpoint(self, vpc_endpoint_id: str, exists=True):
100+
res_found = False
101+
try:
102+
aws_res = self.ec2_client.describe_vpc_endpoints(VpcEndpointIds=[vpc_endpoint_id])
103+
res_found = len(aws_res["VpcEndpoints"]) > 0
104+
except self.ec2_client.exceptions.ClientError:
105+
pass
106+
assert res_found is exists

test/e2e/tests/test_dhcp_options.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,14 @@
2222
from acktest.k8s import resource as k8s
2323
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource
2424
from e2e.replacement_values import REPLACEMENT_VALUES
25+
from e2e.tests.helper import EC2Validator
2526

2627
RESOURCE_PLURAL = "dhcpoptions"
2728

2829
DEFAULT_WAIT_AFTER_SECONDS = 5
2930
CREATE_WAIT_AFTER_SECONDS = 10
3031
DELETE_WAIT_AFTER_SECONDS = 10
3132

32-
33-
def get_dhcp_options(ec2_client, dhcp_options_id: str) -> dict:
34-
try:
35-
resp = ec2_client.describe_dhcp_options(
36-
Filters=[{"Name": "dhcp-options-id", "Values": [dhcp_options_id]}]
37-
)
38-
except Exception as e:
39-
logging.debug(e)
40-
return None
41-
42-
if len(resp["DhcpOptions"]) == 0:
43-
return None
44-
return resp["DhcpOptions"][0]
45-
46-
47-
def dhcp_options_exist(ec2_client, dhcp_options_id: str) -> bool:
48-
return get_dhcp_options(ec2_client, dhcp_options_id) is not None
49-
5033
@service_marker
5134
@pytest.mark.canary
5235
class TestDhcpOptions:
@@ -84,14 +67,15 @@ def test_create_delete(self, ec2_client):
8467

8568
time.sleep(CREATE_WAIT_AFTER_SECONDS)
8669

87-
# Check DHCP Options exists
88-
assert dhcp_options_exist(ec2_client, resource_id)
70+
# Check DHCP Options exists in AWS
71+
ec2_validator = EC2Validator(ec2_client)
72+
ec2_validator.assert_dhcp_options(resource_id)
8973

9074
# Delete k8s resource
9175
_, deleted = k8s.delete_custom_resource(ref)
9276
assert deleted is True
9377

9478
time.sleep(DELETE_WAIT_AFTER_SECONDS)
9579

96-
# Check DHCP Options doesn't exist
97-
assert not dhcp_options_exist(ec2_client, resource_id)
80+
# Check DHCP Options no longer exists in AWS
81+
ec2_validator.assert_dhcp_options(resource_id, exists=False)

test/e2e/tests/test_internet_gateway.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,13 @@
2222
from acktest.k8s import resource as k8s
2323
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource
2424
from e2e.replacement_values import REPLACEMENT_VALUES
25+
from e2e.tests.helper import EC2Validator
2526

2627
RESOURCE_PLURAL = "internetgateways"
2728

2829
CREATE_WAIT_AFTER_SECONDS = 10
2930
DELETE_WAIT_AFTER_SECONDS = 10
3031

31-
32-
def get_internet_gateway(ec2_client, ig_id: str) -> dict:
33-
try:
34-
resp = ec2_client.describe_internet_gateways(
35-
Filters=[{"Name": "internet-gateway-id", "Values": [ig_id]}]
36-
)
37-
except Exception as e:
38-
logging.debug(e)
39-
return None
40-
41-
if len(resp["InternetGateways"]) == 0:
42-
return None
43-
return resp["InternetGateways"][0]
44-
45-
46-
def internet_gateway_exists(ec2_client, ig_id: str) -> bool:
47-
return get_internet_gateway(ec2_client, ig_id) is not None
48-
4932
@service_marker
5033
@pytest.mark.canary
5134
class TestInternetGateway:
@@ -77,16 +60,15 @@ def test_create_delete(self, ec2_client):
7760

7861
time.sleep(CREATE_WAIT_AFTER_SECONDS)
7962

80-
# Check Internet Gateway exists
81-
exists = internet_gateway_exists(ec2_client, resource_id)
82-
assert exists
63+
# Check Internet Gateway exists in AWS
64+
ec2_validator = EC2Validator(ec2_client)
65+
ec2_validator.assert_internet_gateway(resource_id)
8366

8467
# Delete k8s resource
8568
_, deleted = k8s.delete_custom_resource(ref, 2, 5)
8669
assert deleted is True
8770

8871
time.sleep(DELETE_WAIT_AFTER_SECONDS)
8972

90-
# Check Internet Gateway doesn't exist
91-
exists = internet_gateway_exists(ec2_client, resource_id)
92-
assert not exists
73+
# Check Internet Gateway no longer exists in AWS
74+
ec2_validator.assert_internet_gateway(resource_id, exists=False)

test/e2e/tests/test_route_table.py

Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,14 @@
2323
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource
2424
from e2e.replacement_values import REPLACEMENT_VALUES
2525
from e2e.bootstrap_resources import get_bootstrap_resources
26+
from e2e.tests.helper import EC2Validator
2627

2728
RESOURCE_PLURAL = "routetables"
2829

2930
DEFAULT_WAIT_AFTER_SECONDS = 5
3031
CREATE_WAIT_AFTER_SECONDS = 10
3132
DELETE_WAIT_AFTER_SECONDS = 10
3233

33-
34-
def get_route_table(ec2_client, route_table_id: str) -> dict:
35-
try:
36-
resp = ec2_client.describe_route_tables(
37-
Filters=[{"Name": "route-table-id", "Values": [route_table_id]}]
38-
)
39-
except Exception as e:
40-
logging.debug(e)
41-
return None
42-
43-
if len(resp["RouteTables"]) == 0:
44-
return None
45-
return resp["RouteTables"][0]
46-
47-
48-
def route_table_exists(ec2_client, route_table_id: str) -> bool:
49-
return get_route_table(ec2_client, route_table_id) is not None
50-
51-
def get_routes(ec2_client, route_table_id: str) -> list:
52-
try:
53-
resp = ec2_client.describe_route_tables(
54-
Filters=[{"Name": "route-table-id", "Values": [route_table_id]}]
55-
)
56-
except Exception as e:
57-
logging.debug(e)
58-
return None
59-
60-
if len(resp["RouteTables"]) == 0:
61-
return None
62-
return resp["RouteTables"][0]["Routes"]
63-
64-
def route_exists(ec2_client, route_table_id: str, gateway_id: str, origin: str) -> bool:
65-
routes = get_routes(ec2_client, route_table_id)
66-
for route in routes:
67-
if route["Origin"] == origin and route["GatewayId"] == gateway_id:
68-
return True
69-
return False
70-
7134
@service_marker
7235
@pytest.mark.canary
7336
class TestRouteTable:
@@ -107,18 +70,18 @@ def test_create_delete(self, ec2_client):
10770

10871
time.sleep(CREATE_WAIT_AFTER_SECONDS)
10972

110-
# Check Route Table exists
111-
assert route_table_exists(ec2_client, resource_id)
73+
# Check Route Table exists in AWS
74+
ec2_validator = EC2Validator(ec2_client)
75+
ec2_validator.assert_route_table(resource_id)
11276

11377
# Delete k8s resource
11478
_, deleted = k8s.delete_custom_resource(ref)
11579
assert deleted is True
11680

11781
time.sleep(DELETE_WAIT_AFTER_SECONDS)
11882

119-
# Check Route Table doesn't exist
120-
exists = route_table_exists(ec2_client, resource_id)
121-
assert not exists
83+
# Check Route Table no longer exists in AWS
84+
ec2_validator.assert_route_table(resource_id, exists=False)
12285

12386

12487
def test_terminal_condition(self):
@@ -189,21 +152,16 @@ def test_crud_route(self, ec2_client):
189152

190153
time.sleep(CREATE_WAIT_AFTER_SECONDS)
191154

192-
# Check Route Table exists
193-
assert route_table_exists(ec2_client, resource_id)
194-
195-
# Check Routes exist (default and desired)
196-
routes = get_routes(ec2_client, resource_id)
197-
for route in routes:
198-
if route["GatewayId"] == "local":
199-
default_cidr = route["DestinationCidrBlock"]
200-
assert route["Origin"] == "CreateRouteTable"
201-
elif route["GatewayId"] == igw_id:
202-
assert route["Origin"] == "CreateRoute"
203-
else:
204-
assert False
155+
# Check Route Table exists in AWS
156+
ec2_validator = EC2Validator(ec2_client)
157+
ec2_validator.assert_route_table(resource_id)
158+
159+
# Check Routes exist (default and desired) in AWS
160+
ec2_validator.assert_route(resource_id, "local", "CreateRouteTable")
161+
ec2_validator.assert_route(resource_id, igw_id, "CreateRoute")
205162

206163
# Update Route
164+
default_cidr = "10.0.0.0/16"
207165
updated_cidr = "192.168.1.0/24"
208166
patch = {"spec": {"routes": [
209167
{
@@ -224,15 +182,6 @@ def test_crud_route(self, ec2_client):
224182
# assert patched state
225183
resource = k8s.get_resource(ref)
226184
assert len(resource['status']['routeStatuses']) == 2
227-
for route in resource['status']['routeStatuses']:
228-
if route["gatewayID"] == "local":
229-
assert route_exists(ec2_client, resource_id, "local", "CreateRouteTable")
230-
elif route["gatewayID"] == igw_id:
231-
# origin and state are set server-side
232-
assert route_exists(ec2_client, resource_id, igw_id, "CreateRoute")
233-
assert route["state"] == "active"
234-
else:
235-
assert False
236185

237186
# Delete Route
238187
patch = {"spec": {"routes": [
@@ -248,12 +197,10 @@ def test_crud_route(self, ec2_client):
248197

249198
resource = k8s.get_resource(ref)
250199
assert len(resource['spec']['routes']) == 1
251-
for route in resource['spec']['routes']:
252-
if route["gatewayID"] == "local":
253-
assert route_exists(ec2_client, resource_id, "local", "CreateRouteTable")
254-
else:
255-
assert False
256200

201+
# Route should no longer exist in AWS (default will remain)
202+
ec2_validator.assert_route(resource_id, "local", "CreateRouteTable")
203+
ec2_validator.assert_route(resource_id, igw_id, "CreateRoute", exists=False)
257204

258205
# Should not be able to delete default route
259206
patch = {"spec": {"routes": [
@@ -273,6 +220,5 @@ def test_crud_route(self, ec2_client):
273220

274221
time.sleep(DELETE_WAIT_AFTER_SECONDS)
275222

276-
# Check Route Table doesn't exist
277-
exists = route_table_exists(ec2_client, resource_id)
278-
assert not exists
223+
# Check Route Table no longer exists in AWS
224+
ec2_validator.assert_route_table(resource_id, exists=False)

0 commit comments

Comments
 (0)