Skip to content

Commit 8594fb5

Browse files
authored
Refactor e2e tests to use acktest.tags helper (#106)
Issue #, if available: aws-controllers-k8s/community#1235 Description of changes: * Use `acktest.tags` assertions in e2e tests to accurately test for ACK tags and user-defined tags * Enhance existing tag tests by checking both server-side values as well as `Spec.Tags` values * Add hook code for `Instance` and `RouteTable` so ACK tags are not populated in `Spec` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 423e97d commit 8594fb5

22 files changed

+631
-133
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-09-21T13:36:34Z"
2+
build_date: "2022-09-27T22:29:46Z"
33
build_hash: 6e2ffbc3b16a30ac59be6719918c601c2c864064
44
go_version: go1.18.1
55
version: v0.20.1-3-g6e2ffbc
66
api_directory_checksum: 127aa0f51fbcdde596b8f42f93bcdf3b6dee8488
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: 8573a89b8220023263610579bfb84b762d99f5fa
10+
file_checksum: a31177552ce6ea5ce3b624520711382f113cf05b
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ resources:
205205
hooks:
206206
sdk_create_post_build_request:
207207
template_path: hooks/instance/sdk_create_post_build_request.go.tpl
208+
sdk_create_post_set_output:
209+
template_path: hooks/instance/sdk_create_post_set_output.go.tpl
210+
sdk_read_many_post_set_output:
211+
template_path: hooks/instance/sdk_read_many_post_set_output.go.tpl
208212
sdk_delete_post_build_request:
209213
template_path: hooks/instance/sdk_delete_post_build_request.go.tpl
210214
sdk_file_end:

generator.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ resources:
205205
hooks:
206206
sdk_create_post_build_request:
207207
template_path: hooks/instance/sdk_create_post_build_request.go.tpl
208+
sdk_create_post_set_output:
209+
template_path: hooks/instance/sdk_create_post_set_output.go.tpl
210+
sdk_read_many_post_set_output:
211+
template_path: hooks/instance/sdk_read_many_post_set_output.go.tpl
208212
sdk_delete_post_build_request:
209213
template_path: hooks/instance/sdk_delete_post_build_request.go.tpl
210214
sdk_file_end:

pkg/resource/instance/sdk.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/route_table/sdk.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
toAdd, toDelete := computeTagsDelta(desired.ko.Spec.Tags, ko.Spec.Tags)
2+
if len(toAdd) == 0 && len(toDelete) == 0 {
3+
// if desired tags and response tags are equal,
4+
// then assign desired tags to maintain tag order
5+
ko.Spec.Tags = desired.ko.Spec.Tags
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
toAdd, toDelete := computeTagsDelta(r.ko.Spec.Tags, ko.Spec.Tags)
3+
if len(toAdd) == 0 && len(toDelete) == 0 {
4+
// if resource's initial tags and response tags are equal,
5+
// then assign resource's tags to maintain tag order
6+
ko.Spec.Tags = r.ko.Spec.Tags
7+
}
8+

templates/hooks/route_table/sdk_create_post_set_output.go.tpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010
if err := rm.createRoutes(ctx, &resource{ko}); err != nil {
1111
return nil, err
1212
}
13+
}
14+
15+
toAdd, toDelete := computeTagsDelta(desired.ko.Spec.Tags, ko.Spec.Tags)
16+
if len(toAdd) == 0 && len(toDelete) == 0 {
17+
// if desired tags and response tags are equal,
18+
// then assign desired tags to maintain tag order
19+
ko.Spec.Tags = desired.ko.Spec.Tags
1320
}

templates/hooks/route_table/sdk_read_many_post_set_output.go.tpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
if found {
33
rm.addRoutesToStatus(ko, resp.RouteTables[0])
44
}
5+
toAdd, toDelete := computeTagsDelta(r.ko.Spec.Tags, ko.Spec.Tags)
6+
if len(toAdd) == 0 && len(toDelete) == 0 {
7+
// if resource's initial tags and response tags are equal,
8+
// then assign resource's tags to maintain tag order
9+
ko.Spec.Tags = r.ko.Spec.Tags
10+
}
511

test/e2e/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@5220331d003e3a23de4470e68d02c99b16c81989
1+
acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@73eb0d5af8ac19d17fad8787d6462d56f4e3e37d

test/e2e/tests/helper.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ class EC2Validator:
2121
def __init__(self, ec2_client):
2222
self.ec2_client = ec2_client
2323

24+
def get_dhcp_options(self, dhcp_options_id: str):
25+
try:
26+
aws_res = self.ec2_client.describe_dhcp_options(DhcpOptionsIds=[dhcp_options_id])
27+
if len(aws_res["DhcpOptions"]) > 0:
28+
return aws_res["DhcpOptions"][0]
29+
return None
30+
except self.ec2_client.exceptions.ClientError:
31+
return None
32+
2433
def assert_dhcp_options(self, dhcp_options_id: str, exists=True):
2534
res_found = False
2635
try:
@@ -42,6 +51,15 @@ def get_internet_gateway(self, igw_id: str):
4251
def assert_internet_gateway(self, igw_id: str, exists=True):
4352
assert (self.get_internet_gateway(igw_id) is not None) == exists
4453

54+
def get_nat_gateway(self, ngw_id: str):
55+
try:
56+
aws_res = self.ec2_client.describe_nat_gateways(NatGatewayIds=[ngw_id])
57+
if len(aws_res["NatGateways"]) > 0:
58+
return aws_res["NatGateways"][0]
59+
return None
60+
except self.ec2_client.exceptions.ClientError:
61+
return None
62+
4563
def assert_nat_gateway(self, ngw_id: str, exists=True):
4664
res_found = False
4765
try:
@@ -116,6 +134,15 @@ def get_subnet(self, subnet_id: str) -> Union[None, Dict]:
116134
def assert_subnet(self, subnet_id: str, exists=True):
117135
assert (self.get_subnet(subnet_id) is not None) == exists
118136

137+
def get_transit_gateway(self, tgw_id: str) -> Union[None, Dict]:
138+
try:
139+
aws_res = self.ec2_client.describe_transit_gateways(TransitGatewayIds=[tgw_id])
140+
if len(aws_res["TransitGateways"]) > 0:
141+
return aws_res["TransitGateways"][0]
142+
return None
143+
except self.ec2_client.exceptions.ClientError:
144+
return None
145+
119146
def assert_transit_gateway(self, tgw_id: str, exists=True):
120147
res_found = False
121148
try:
@@ -146,6 +173,15 @@ def assert_vpc(self, vpc_id: str, exists=True):
146173
pass
147174
assert res_found is exists
148175

176+
def get_vpc_endpoint(self, vpc_endpoint_id: str) -> Union[None, Dict]:
177+
try:
178+
aws_res = self.ec2_client.describe_vpc_endpoints(VpcEndpointIds=[vpc_endpoint_id])
179+
if len(aws_res["VpcEndpoints"]) > 0:
180+
return aws_res["VpcEndpoints"][0]
181+
return None
182+
except self.ec2_client.exceptions.ClientError:
183+
return None
184+
149185
def assert_vpc_endpoint(self, vpc_endpoint_id: str, exists=True):
150186
res_found = False
151187
try:

test/e2e/tests/test_dhcp_options.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import time
1919
import logging
2020

21+
from acktest import tags
2122
from acktest.resources import random_suffix_name
2223
from acktest.k8s import resource as k8s
2324
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource
@@ -131,23 +132,36 @@ def test_crud_tags(self, ec2_client, simple_dhcp_options):
131132
# Check dhcpOptions exists in AWS
132133
ec2_validator = EC2Validator(ec2_client)
133134
ec2_validator.assert_dhcp_options(resource_id)
135+
136+
# Check system and user tags exist for dhcpOptions resource
137+
dhcp_options = ec2_validator.get_dhcp_options(resource_id)
138+
user_tags = {
139+
"initialtagkey": "initialtagvalue"
140+
}
141+
tags.assert_ack_system_tags(
142+
tags=dhcp_options["Tags"],
143+
)
144+
tags.assert_equal_without_ack_tags(
145+
expected=user_tags,
146+
actual=dhcp_options["Tags"],
147+
)
134148

135-
# Check tags exist for dhcpOptions resource
149+
# Only user tags should be present in Spec
150+
assert len(resource["spec"]["tags"]) == 1
136151
assert resource["spec"]["tags"][0]["key"] == "initialtagkey"
137152
assert resource["spec"]["tags"][0]["value"] == "initialtagvalue"
138153

139-
# New pair of tags
140-
new_tags = [
154+
# Update tags
155+
update_tags = [
141156
{
142157
"key": "updatedtagkey",
143158
"value": "updatedtagvalue",
144159
}
145-
146160
]
147161

148162
# Patch the dhcpOptions, updating the tags with new pair
149163
updates = {
150-
"spec": {"tags": new_tags},
164+
"spec": {"tags": update_tags},
151165
}
152166

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

159-
# Assert tags are updated for dhcpOptions resource
173+
# Check for updated user tags; system tags should persist
174+
dhcp_options = ec2_validator.get_dhcp_options(resource_id)
175+
updated_tags = {
176+
"updatedtagkey": "updatedtagvalue"
177+
}
178+
tags.assert_ack_system_tags(
179+
tags=dhcp_options["Tags"],
180+
)
181+
tags.assert_equal_without_ack_tags(
182+
expected=updated_tags,
183+
actual=dhcp_options["Tags"],
184+
)
185+
186+
# Only user tags should be present in Spec
160187
resource = k8s.get_resource(ref)
188+
assert len(resource["spec"]["tags"]) == 1
161189
assert resource["spec"]["tags"][0]["key"] == "updatedtagkey"
162190
assert resource["spec"]["tags"][0]["value"] == "updatedtagvalue"
163191

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

175-
# Assert tags are deleted
203+
# Check for removed user tags; system tags should persist
204+
dhcp_options = ec2_validator.get_dhcp_options(resource_id)
205+
tags.assert_ack_system_tags(
206+
tags=dhcp_options["Tags"],
207+
)
208+
tags.assert_equal_without_ack_tags(
209+
expected=[],
210+
actual=dhcp_options["Tags"],
211+
)
212+
213+
# Check user tags are removed from Spec
176214
resource = k8s.get_resource(ref)
177-
assert len(resource['spec']['tags']) == 0
215+
assert len(resource["spec"]["tags"]) == 0
178216

179217
# Delete k8s resource
180218
_, deleted = k8s.delete_custom_resource(ref)

test/e2e/tests/test_elastic_ip_address.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import time
1919
import logging
2020

21+
from acktest import tags
2122
from acktest.resources import random_suffix_name
2223
from acktest.k8s import resource as k8s
2324
from e2e import service_marker, CRD_GROUP, CRD_VERSION, load_ec2_resource
@@ -153,13 +154,27 @@ def test_crud_tags(self, ec2_client, simple_elastic_ip_address):
153154
# Check Address exists
154155
exists = address_exists(ec2_client, resource_id)
155156
assert exists
156-
157-
# Check tags exist for elasticipaddress resource
157+
158+
# Check system and user tags exist for elasticIP resource
159+
elastic_ip = get_address(ec2_client, resource_id)
160+
user_tags = {
161+
"initialtagkey": "initialtagvalue"
162+
}
163+
tags.assert_ack_system_tags(
164+
tags=elastic_ip["Tags"],
165+
)
166+
tags.assert_equal_without_ack_tags(
167+
expected=user_tags,
168+
actual=elastic_ip["Tags"],
169+
)
170+
171+
# Only user tags should be present in Spec
172+
assert len(resource["spec"]["tags"]) == 1
158173
assert resource["spec"]["tags"][0]["key"] == "initialtagkey"
159174
assert resource["spec"]["tags"][0]["value"] == "initialtagvalue"
160175

161-
# New pair of tags
162-
new_tags = [
176+
# Update tags
177+
update_tags = [
163178
{
164179
"key": "updatedtagkey",
165180
"value": "updatedtagvalue",
@@ -169,17 +184,31 @@ def test_crud_tags(self, ec2_client, simple_elastic_ip_address):
169184

170185
# Patch the elasticipaddress, updating the tags with new pair
171186
updates = {
172-
"spec": {"tags": new_tags},
187+
"spec": {"tags": update_tags},
173188
}
174189

175190
k8s.patch_custom_resource(ref, updates)
176191
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
177192

178193
# Check resource synced successfully
179194
assert k8s.wait_on_condition(ref, "ACK.ResourceSynced", "True", wait_periods=5)
180-
181-
# Assert tags are updated for elasticipaddress resource
195+
196+
# Check for updated user tags; system tags should persist
197+
elastic_ip = get_address(ec2_client, resource_id)
198+
updated_tags = {
199+
"updatedtagkey": "updatedtagvalue"
200+
}
201+
tags.assert_ack_system_tags(
202+
tags=elastic_ip["Tags"],
203+
)
204+
tags.assert_equal_without_ack_tags(
205+
expected=updated_tags,
206+
actual=elastic_ip["Tags"],
207+
)
208+
209+
# Only user tags should be present in Spec
182210
resource = k8s.get_resource(ref)
211+
assert len(resource["spec"]["tags"]) == 1
183212
assert resource["spec"]["tags"][0]["key"] == "updatedtagkey"
184213
assert resource["spec"]["tags"][0]["value"] == "updatedtagvalue"
185214

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

197-
# Assert tags are deleted
226+
# Check for removed user tags; system tags should persist
227+
elastic_ip = get_address(ec2_client, resource_id)
228+
tags.assert_ack_system_tags(
229+
tags=elastic_ip["Tags"],
230+
)
231+
tags.assert_equal_without_ack_tags(
232+
expected=[],
233+
actual=elastic_ip["Tags"],
234+
)
235+
236+
# Check user tags are removed from Spec
198237
resource = k8s.get_resource(ref)
199-
assert len(resource['spec']['tags']) == 0
238+
assert len(resource["spec"]["tags"]) == 0
200239

201240
# Delete k8s resource
202241
_, deleted = k8s.delete_custom_resource(ref)

0 commit comments

Comments
 (0)