Skip to content

Commit

Permalink
Add tests to ensure resource reference is not replaced by controller (#…
Browse files Browse the repository at this point in the history
…220)

Issue [#1880](aws-controllers-k8s/community#1880)

Description of changes:
These tests ensure that after an update, 
the controller won't replace the resource 
reference with the ID itself.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
michaelhtm authored Sep 18, 2024
1 parent ddb3682 commit ffc48b2
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/resource/route_table/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ func (rm *resourceManager) customUpdateRouteTable(
}
}

return updated, nil
newDesired := rm.concreteResource(desired.DeepCopy())
newDesired.ko.Status = updated.ko.Status
return newDesired, nil
}

func (rm *resourceManager) requiredFieldsMissingForCreateRoute(
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/resources/internet_gateway_ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: ec2.services.k8s.aws/v1alpha1
kind: InternetGateway
metadata:
name: $INTERNET_GATEWAY_NAME
spec:
vpcRef:
from:
name: $VPC_NAME
16 changes: 16 additions & 0 deletions test/e2e/resources/route_table_ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: ec2.services.k8s.aws/v1alpha1
kind: RouteTable
metadata:
name: $ROUTE_TABLE_NAME
spec:
routes:
- destinationCIDRBlock: $DEST_CIDR_BLOCK
gatewayRef:
from:
name: $INTERNET_GATEWAY_NAME
vpcRef:
from:
name: $VPC_NAME
tags:
- key: $TAG_KEY
value: $TAG_VALUE
118 changes: 117 additions & 1 deletion test/e2e/tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from e2e.tests.helper import EC2Validator

CREATE_WAIT_AFTER_SECONDS = 20
MODIFY_WAIT_AFTER_SECONDS = 30
DELETE_WAIT_AFTER_SECONDS = 10
DELETE_TIMEOUT_SECONDS = 300

Expand Down Expand Up @@ -167,4 +168,119 @@ def test_references(self, ec2_client):
ec2_validator.assert_vpc_endpoint(vpc_endpoint_id, exists=False)
ec2_validator.assert_subnet(subnet_id, exists=False)
ec2_validator.assert_security_group(sg_id, exists=False)
ec2_validator.assert_vpc(vpc_id, exists=False)
ec2_validator.assert_vpc(vpc_id, exists=False)

def test_array_references(self, ec2_client):
route_table_name = random_suffix_name("route-table-test", 24)
vpc_name = random_suffix_name("vpc-ref-test", 24)
gateway_name = random_suffix_name("gateway-ref-test", 24)

test_values = REPLACEMENT_VALUES.copy()
test_values["ROUTE_TABLE_NAME"] = route_table_name
test_values["DEST_CIDR_BLOCK"] = "0.0.0.0/0"
test_values["INTERNET_GATEWAY_NAME"] = gateway_name
test_values["VPC_NAME"] = vpc_name
test_values["CIDR_BLOCK"] = "10.0.0.0/16"
test_values["ENABLE_DNS_SUPPORT"] = "False"
test_values["ENABLE_DNS_HOSTNAMES"] = "False"
test_values["DISALLOW_DEFAULT_SECURITY_GROUP_RULE"] = "False"

# Load CRs
route_table_resource_data = load_ec2_resource(
"route_table_ref",
additional_replacements=test_values
)
vpc_resource_data = load_ec2_resource(
"vpc",
additional_replacements=test_values
)
gateway_resource_data = load_ec2_resource(
"internet_gateway_ref",
additional_replacements=test_values
)

# This test creates resources in order,

# Create VPC
vpc_ref = k8s.CustomResourceReference(
CRD_GROUP, CRD_VERSION, 'vpcs',
vpc_name, namespace="default",
)
k8s.create_custom_resource(vpc_ref, vpc_resource_data)

# Create Internet Gateway
gateway_ref = k8s.CustomResourceReference(
CRD_GROUP, CRD_VERSION, 'internetgateways',
gateway_name, namespace="default",
)
k8s.create_custom_resource(gateway_ref, gateway_resource_data)

# Create route table
route_table_ref = k8s.CustomResourceReference(
CRD_GROUP, CRD_VERSION, 'routetables',
route_table_name, namespace="default",
)
k8s.create_custom_resource(route_table_ref, route_table_resource_data)

# Wait a few seconds so resources are synced
time.sleep(CREATE_WAIT_AFTER_SECONDS)
assert k8s.wait_on_condition(vpc_ref, "ACK.ResourceSynced", "True", wait_periods=5)
assert k8s.wait_on_condition(gateway_ref, "ACK.ResourceSynced", "True", wait_periods=5)
assert k8s.wait_on_condition(route_table_ref, "ACK.ResourceSynced", "True", wait_periods=10)

assert k8s.wait_on_condition(gateway_ref, "ACK.ReferencesResolved", "True", wait_periods=5)
assert k8s.wait_on_condition(route_table_ref, "ACK.ReferencesResolved", "True", wait_periods=10)

# Acquire Internet Gateway ID
gateway_cr = k8s.get_resource(gateway_ref)
assert 'status' in gateway_cr
gateway_id = gateway_cr["status"]["internetGatewayID"]

# Ensure routetable contains reference in spec
route_table_cr = k8s.get_resource(route_table_ref)
assert 'spec' in route_table_cr
assert 'vpcRef' in route_table_cr['spec']
assert route_table_cr['spec']['vpcRef']['from']['name'] == vpc_name
assert 'routes' in route_table_cr['spec']
assert len(route_table_cr['spec']['routes']) == 1
assert 'gatewayID' not in route_table_cr['spec']['routes'][0]
assert 'gatewayRef' in route_table_cr['spec']['routes'][0]
assert route_table_cr['spec']['routes'][0]['gatewayRef']['from']['name'] == gateway_name
assert 'status' in route_table_cr
assert 'routeStatuses' in route_table_cr['status']
found_gateway_id = False
for rs in route_table_cr['status']['routeStatuses']:
if 'gatewayID' in rs and rs['gatewayID'] == gateway_id:
found_gateway_id = True
assert found_gateway_id

user_tag = {
"tag": "my_tag",
"value": "my_val"
}
route_table_update = {
'spec': {
'tags': [user_tag]
}
}
k8s.patch_custom_resource(route_table_ref, route_table_update)
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
assert k8s.wait_on_condition(route_table_ref, "ACK.ResourceSynced", "True", wait_periods=5)
assert k8s.wait_on_condition(route_table_ref, "ACK.ReferencesResolved", "True", wait_periods=5)

# Ensure that the reference has not changed
route_table_cr = k8s.get_resource(route_table_ref)
assert 'spec' in route_table_cr
assert 'routes' in route_table_cr['spec']
assert len(route_table_cr['spec']['routes']) == 1
assert 'gatewayID' not in route_table_cr['spec']['routes'][0]
assert 'gatewayRef' in route_table_cr['spec']['routes'][0]
assert route_table_cr['spec']['routes'][0]['gatewayRef']['from']['name'] == gateway_name

# Delete All
_, deleted = k8s.delete_custom_resource(route_table_ref)
assert deleted
_, deleted = k8s.delete_custom_resource(gateway_ref)
assert deleted
_, deleted = k8s.delete_custom_resource(vpc_ref)
assert deleted

0 comments on commit ffc48b2

Please sign in to comment.