Skip to content

Commit debab66

Browse files
author
Nicholas Thomson
committed
Add empty VPC and e2e tests
1 parent 0b56951 commit debab66

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed

test/e2e/bootstrap_resources.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
@dataclass
2222
class BootstrapResources(Resources):
2323
SharedTestVPC: VPC
24+
EmptyVPC: VPC
2425

2526
_bootstrap_resources = None
2627

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: ec2.services.k8s.aws/v1alpha1
2+
kind: InternetGateway
3+
metadata:
4+
name: $INTERNET_GATEWAY_NAME
5+
spec:
6+
vpc: $VPC_ID

test/e2e/service_bootstrap.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def service_bootstrap() -> Resources:
2424
logging.getLogger().setLevel(logging.INFO)
2525

2626
resources = BootstrapResources(
27-
SharedTestVPC=VPC(name_prefix="e2e-test-vpc", num_public_subnet=1, num_private_subnet=0)
27+
SharedTestVPC=VPC(name_prefix="e2e-test-vpc", num_public_subnet=1, num_private_subnet=0),
28+
EmptyVPC=VPC(name_prefix="e2e-empty-vpc", num_public_subnet=0, num_private_subnet=0)
2829
)
2930

3031
try:

test/e2e/tests/helper.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ def assert_dhcp_options(self, dhcp_options_id: str, exists=True):
3030
pass
3131
assert res_found is exists
3232

33-
def assert_internet_gateway(self, ig_id: str, exists=True):
34-
res_found = False
33+
def get_internet_gateway(self, igw_id: str):
3534
try:
36-
aws_res = self.ec2_client.describe_internet_gateways(InternetGatewayIds=[ig_id])
37-
res_found = len(aws_res["InternetGateways"]) > 0
35+
aws_res = self.ec2_client.describe_internet_gateways(InternetGatewayIds=[igw_id])
36+
if len(aws_res["InternetGateways"]) > 0:
37+
return aws_res["InternetGateways"][0]
38+
return None
3839
except self.ec2_client.exceptions.ClientError:
39-
pass
40-
assert res_found is exists
40+
return None
41+
42+
def assert_internet_gateway(self, igw_id: str, exists=True):
43+
assert (self.get_internet_gateway(igw_id) is not None) == exists
4144

4245
def assert_nat_gateway(self, ngw_id: str, exists=True):
4346
res_found = False

test/e2e/tests/test_internet_gateway.py

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

2728
RESOURCE_PLURAL = "internetgateways"
2829

2930
CREATE_WAIT_AFTER_SECONDS = 10
31+
MODIFY_WAIT_AFTER_SECONDS = 10
3032
DELETE_WAIT_AFTER_SECONDS = 10
3133

3234
@service_marker
@@ -70,5 +72,75 @@ def test_create_delete(self, ec2_client):
7072

7173
time.sleep(DELETE_WAIT_AFTER_SECONDS)
7274

75+
# Check Internet Gateway no longer exists in AWS
76+
ec2_validator.assert_internet_gateway(resource_id, exists=False)
77+
78+
def test_vpc_association(self, ec2_client):
79+
resource_name = random_suffix_name("ig-ack-test", 24)
80+
81+
test_vpc = get_bootstrap_resources().EmptyVPC
82+
vpc_id = test_vpc.vpc_id
83+
84+
replacements = REPLACEMENT_VALUES.copy()
85+
replacements["INTERNET_GATEWAY_NAME"] = resource_name
86+
replacements["VPC_ID"] = vpc_id
87+
88+
# Load Internet Gateway CR
89+
resource_data = load_ec2_resource(
90+
"internet_gateway_vpc_attachment",
91+
additional_replacements=replacements,
92+
)
93+
logging.debug(resource_data)
94+
95+
# Create k8s resource
96+
ref = k8s.CustomResourceReference(
97+
CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL,
98+
resource_name, namespace="default",
99+
)
100+
k8s.create_custom_resource(ref, resource_data)
101+
cr = k8s.wait_resource_consumed_by_controller(ref)
102+
103+
assert cr is not None
104+
assert k8s.get_resource_exists(ref)
105+
106+
resource = k8s.get_resource(ref)
107+
resource_id = resource["status"]["internetGatewayID"]
108+
109+
time.sleep(CREATE_WAIT_AFTER_SECONDS)
110+
111+
# Check Internet Gateway exists in AWS
112+
ec2_validator = EC2Validator(ec2_client)
113+
ec2_validator.assert_internet_gateway(resource_id)
114+
115+
# Check attachments appear on Internet Gateway
116+
igw = ec2_validator.get_internet_gateway(resource_id)
117+
assert len(igw["Attachments"]) == 1
118+
assert igw["Attachments"][0]["VpcId"] == vpc_id
119+
assert igw["Attachments"][0]["State"] == "available"
120+
121+
# Patch the IGW, removing the attachment
122+
updates = {
123+
"spec": {"vpc": None},
124+
}
125+
k8s.patch_custom_resource(ref, updates)
126+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
127+
128+
# Check there are no attachments on Internet Gateway
129+
igw = ec2_validator.get_internet_gateway(resource_id)
130+
131+
# In the case where it shows the attachment as being in detached state
132+
if len(igw["Attachments"]) == 1:
133+
assert igw["Attachments"][0]["VpcId"] == vpc_id
134+
assert igw["Attachments"][0]["State"] == "detached"
135+
else:
136+
# Otherwise there should be no attachment on the IGW
137+
assert len(igw["Attachments"]) == 0
138+
139+
# Delete k8s resource
140+
_, deleted = k8s.delete_custom_resource(ref, 2, 5)
141+
assert deleted is True
142+
143+
time.sleep(DELETE_WAIT_AFTER_SECONDS)
144+
73145
# Check Internet Gateway no longer exists in AWS
74146
ec2_validator.assert_internet_gateway(resource_id, exists=False)

0 commit comments

Comments
 (0)