Skip to content

Refactor e2e tests to use acktest.tags helper #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apis/v1alpha1/ack-generate-metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
ack_generate_info:
build_date: "2022-09-21T13:36:34Z"
build_date: "2022-09-27T22:29:46Z"
build_hash: 6e2ffbc3b16a30ac59be6719918c601c2c864064
go_version: go1.18.1
version: v0.20.1-3-g6e2ffbc
api_directory_checksum: 127aa0f51fbcdde596b8f42f93bcdf3b6dee8488
api_version: v1alpha1
aws_sdk_go_version: v1.44.93
generator_config_info:
file_checksum: 8573a89b8220023263610579bfb84b762d99f5fa
file_checksum: a31177552ce6ea5ce3b624520711382f113cf05b
original_file_name: generator.yaml
last_modification:
reason: API generation
4 changes: 4 additions & 0 deletions apis/v1alpha1/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ resources:
hooks:
sdk_create_post_build_request:
template_path: hooks/instance/sdk_create_post_build_request.go.tpl
sdk_create_post_set_output:
template_path: hooks/instance/sdk_create_post_set_output.go.tpl
sdk_read_many_post_set_output:
template_path: hooks/instance/sdk_read_many_post_set_output.go.tpl
sdk_delete_post_build_request:
template_path: hooks/instance/sdk_delete_post_build_request.go.tpl
sdk_file_end:
Expand Down
4 changes: 4 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ resources:
hooks:
sdk_create_post_build_request:
template_path: hooks/instance/sdk_create_post_build_request.go.tpl
sdk_create_post_set_output:
template_path: hooks/instance/sdk_create_post_set_output.go.tpl
sdk_read_many_post_set_output:
template_path: hooks/instance/sdk_read_many_post_set_output.go.tpl
sdk_delete_post_build_request:
template_path: hooks/instance/sdk_delete_post_build_request.go.tpl
sdk_file_end:
Expand Down
14 changes: 14 additions & 0 deletions pkg/resource/instance/sdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pkg/resource/route_table/sdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions templates/hooks/instance/sdk_create_post_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
toAdd, toDelete := computeTagsDelta(desired.ko.Spec.Tags, ko.Spec.Tags)
if len(toAdd) == 0 && len(toDelete) == 0 {
// if desired tags and response tags are equal,
// then assign desired tags to maintain tag order
ko.Spec.Tags = desired.ko.Spec.Tags
}
8 changes: 8 additions & 0 deletions templates/hooks/instance/sdk_read_many_post_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

toAdd, toDelete := computeTagsDelta(r.ko.Spec.Tags, ko.Spec.Tags)
if len(toAdd) == 0 && len(toDelete) == 0 {
// if resource's initial tags and response tags are equal,
// then assign resource's tags to maintain tag order
ko.Spec.Tags = r.ko.Spec.Tags
}

7 changes: 7 additions & 0 deletions templates/hooks/route_table/sdk_create_post_set_output.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@
if err := rm.createRoutes(ctx, &resource{ko}); err != nil {
return nil, err
}
}

toAdd, toDelete := computeTagsDelta(desired.ko.Spec.Tags, ko.Spec.Tags)
if len(toAdd) == 0 && len(toDelete) == 0 {
// if desired tags and response tags are equal,
// then assign desired tags to maintain tag order
ko.Spec.Tags = desired.ko.Spec.Tags
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
if found {
rm.addRoutesToStatus(ko, resp.RouteTables[0])
}
toAdd, toDelete := computeTagsDelta(r.ko.Spec.Tags, ko.Spec.Tags)
if len(toAdd) == 0 && len(toDelete) == 0 {
// if resource's initial tags and response tags are equal,
// then assign resource's tags to maintain tag order
ko.Spec.Tags = r.ko.Spec.Tags
}

2 changes: 1 addition & 1 deletion test/e2e/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@5220331d003e3a23de4470e68d02c99b16c81989
acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@73eb0d5af8ac19d17fad8787d6462d56f4e3e37d
36 changes: 36 additions & 0 deletions test/e2e/tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class EC2Validator:
def __init__(self, ec2_client):
self.ec2_client = ec2_client

def get_dhcp_options(self, dhcp_options_id: str):
try:
aws_res = self.ec2_client.describe_dhcp_options(DhcpOptionsIds=[dhcp_options_id])
if len(aws_res["DhcpOptions"]) > 0:
return aws_res["DhcpOptions"][0]
return None
except self.ec2_client.exceptions.ClientError:
return None

def assert_dhcp_options(self, dhcp_options_id: str, exists=True):
res_found = False
try:
Expand All @@ -42,6 +51,15 @@ def get_internet_gateway(self, igw_id: str):
def assert_internet_gateway(self, igw_id: str, exists=True):
assert (self.get_internet_gateway(igw_id) is not None) == exists

def get_nat_gateway(self, ngw_id: str):
try:
aws_res = self.ec2_client.describe_nat_gateways(NatGatewayIds=[ngw_id])
if len(aws_res["NatGateways"]) > 0:
return aws_res["NatGateways"][0]
return None
except self.ec2_client.exceptions.ClientError:
return None

def assert_nat_gateway(self, ngw_id: str, exists=True):
res_found = False
try:
Expand Down Expand Up @@ -116,6 +134,15 @@ def get_subnet(self, subnet_id: str) -> Union[None, Dict]:
def assert_subnet(self, subnet_id: str, exists=True):
assert (self.get_subnet(subnet_id) is not None) == exists

def get_transit_gateway(self, tgw_id: str) -> Union[None, Dict]:
try:
aws_res = self.ec2_client.describe_transit_gateways(TransitGatewayIds=[tgw_id])
if len(aws_res["TransitGateways"]) > 0:
return aws_res["TransitGateways"][0]
return None
except self.ec2_client.exceptions.ClientError:
return None

def assert_transit_gateway(self, tgw_id: str, exists=True):
res_found = False
try:
Expand Down Expand Up @@ -146,6 +173,15 @@ def assert_vpc(self, vpc_id: str, exists=True):
pass
assert res_found is exists

def get_vpc_endpoint(self, vpc_endpoint_id: str) -> Union[None, Dict]:
try:
aws_res = self.ec2_client.describe_vpc_endpoints(VpcEndpointIds=[vpc_endpoint_id])
if len(aws_res["VpcEndpoints"]) > 0:
return aws_res["VpcEndpoints"][0]
return None
except self.ec2_client.exceptions.ClientError:
return None

def assert_vpc_endpoint(self, vpc_endpoint_id: str, exists=True):
res_found = False
try:
Expand Down
54 changes: 46 additions & 8 deletions test/e2e/tests/test_dhcp_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import time
import logging

from acktest import tags
from acktest.resources import random_suffix_name
from acktest.k8s import resource as k8s
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource
Expand Down Expand Up @@ -131,23 +132,36 @@ def test_crud_tags(self, ec2_client, simple_dhcp_options):
# Check dhcpOptions exists in AWS
ec2_validator = EC2Validator(ec2_client)
ec2_validator.assert_dhcp_options(resource_id)

# Check system and user tags exist for dhcpOptions resource
dhcp_options = ec2_validator.get_dhcp_options(resource_id)
user_tags = {
"initialtagkey": "initialtagvalue"
}
tags.assert_ack_system_tags(
tags=dhcp_options["Tags"],
)
tags.assert_equal_without_ack_tags(
expected=user_tags,
actual=dhcp_options["Tags"],
)

# Check tags exist for dhcpOptions resource
# Only user tags should be present in Spec
assert len(resource["spec"]["tags"]) == 1
assert resource["spec"]["tags"][0]["key"] == "initialtagkey"
assert resource["spec"]["tags"][0]["value"] == "initialtagvalue"

# New pair of tags
new_tags = [
# Update tags
update_tags = [
{
"key": "updatedtagkey",
"value": "updatedtagvalue",
}

]

# Patch the dhcpOptions, updating the tags with new pair
updates = {
"spec": {"tags": new_tags},
"spec": {"tags": update_tags},
}

k8s.patch_custom_resource(ref, updates)
Expand All @@ -156,8 +170,22 @@ def test_crud_tags(self, ec2_client, simple_dhcp_options):
# Check resource synced successfully
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)

# Assert tags are updated for dhcpOptions resource
# Check for updated user tags; system tags should persist
dhcp_options = ec2_validator.get_dhcp_options(resource_id)
updated_tags = {
"updatedtagkey": "updatedtagvalue"
}
tags.assert_ack_system_tags(
tags=dhcp_options["Tags"],
)
tags.assert_equal_without_ack_tags(
expected=updated_tags,
actual=dhcp_options["Tags"],
)

# Only user tags should be present in Spec
resource = k8s.get_resource(ref)
assert len(resource["spec"]["tags"]) == 1
assert resource["spec"]["tags"][0]["key"] == "updatedtagkey"
assert resource["spec"]["tags"][0]["value"] == "updatedtagvalue"

Expand All @@ -172,9 +200,19 @@ def test_crud_tags(self, ec2_client, simple_dhcp_options):
# Check resource synced successfully
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)

# Assert tags are deleted
# Check for removed user tags; system tags should persist
dhcp_options = ec2_validator.get_dhcp_options(resource_id)
tags.assert_ack_system_tags(
tags=dhcp_options["Tags"],
)
tags.assert_equal_without_ack_tags(
expected=[],
actual=dhcp_options["Tags"],
)

# Check user tags are removed from Spec
resource = k8s.get_resource(ref)
assert len(resource['spec']['tags']) == 0
assert len(resource["spec"]["tags"]) == 0

# Delete k8s resource
_, deleted = k8s.delete_custom_resource(ref)
Expand Down
57 changes: 48 additions & 9 deletions test/e2e/tests/test_elastic_ip_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import time
import logging

from acktest import tags
from acktest.resources import random_suffix_name
from acktest.k8s import resource as k8s
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource
Expand Down Expand Up @@ -153,13 +154,27 @@ def test_crud_tags(self, ec2_client, simple_elastic_ip_address):
# Check Address exists
exists = address_exists(ec2_client, resource_id)
assert exists

# Check tags exist for elasticipaddress resource

# Check system and user tags exist for elasticIP resource
elastic_ip = get_address(ec2_client, resource_id)
user_tags = {
"initialtagkey": "initialtagvalue"
}
tags.assert_ack_system_tags(
tags=elastic_ip["Tags"],
)
tags.assert_equal_without_ack_tags(
expected=user_tags,
actual=elastic_ip["Tags"],
)

# Only user tags should be present in Spec
assert len(resource["spec"]["tags"]) == 1
assert resource["spec"]["tags"][0]["key"] == "initialtagkey"
assert resource["spec"]["tags"][0]["value"] == "initialtagvalue"

# New pair of tags
new_tags = [
# Update tags
update_tags = [
{
"key": "updatedtagkey",
"value": "updatedtagvalue",
Expand All @@ -169,17 +184,31 @@ def test_crud_tags(self, ec2_client, simple_elastic_ip_address):

# Patch the elasticipaddress, updating the tags with new pair
updates = {
"spec": {"tags": new_tags},
"spec": {"tags": update_tags},
}

k8s.patch_custom_resource(ref, updates)
time.sleep(MODIFY_WAIT_AFTER_SECONDS)

# Check resource synced successfully
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)

# Assert tags are updated for elasticipaddress resource

# Check for updated user tags; system tags should persist
elastic_ip = get_address(ec2_client, resource_id)
updated_tags = {
"updatedtagkey": "updatedtagvalue"
}
tags.assert_ack_system_tags(
tags=elastic_ip["Tags"],
)
tags.assert_equal_without_ack_tags(
expected=updated_tags,
actual=elastic_ip["Tags"],
)

# Only user tags should be present in Spec
resource = k8s.get_resource(ref)
assert len(resource["spec"]["tags"]) == 1
assert resource["spec"]["tags"][0]["key"] == "updatedtagkey"
assert resource["spec"]["tags"][0]["value"] == "updatedtagvalue"

Expand All @@ -194,9 +223,19 @@ def test_crud_tags(self, ec2_client, simple_elastic_ip_address):
# Check resource synced successfully
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)

# Assert tags are deleted
# Check for removed user tags; system tags should persist
elastic_ip = get_address(ec2_client, resource_id)
tags.assert_ack_system_tags(
tags=elastic_ip["Tags"],
)
tags.assert_equal_without_ack_tags(
expected=[],
actual=elastic_ip["Tags"],
)

# Check user tags are removed from Spec
resource = k8s.get_resource(ref)
assert len(resource['spec']['tags']) == 0
assert len(resource["spec"]["tags"]) == 0

# Delete k8s resource
_, deleted = k8s.delete_custom_resource(ref)
Expand Down
Loading