Skip to content

Commit d8e3c27

Browse files
authored
refactor NATGateway test to use fixture (#82)
Issue #, if available: N/A Description of changes: Refactoring e2e tests to use `pytest.fixture` for the **resource under test**. Otherwise, assertion failures within the test may not lead to successful resource cleanup. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent a438e52 commit d8e3c27

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

test/e2e/tests/test_nat_gateway.py

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from e2e.replacement_values import REPLACEMENT_VALUES
2525
from e2e.bootstrap_resources import get_bootstrap_resources
2626
from e2e.tests.helper import EC2Validator
27-
from .test_elastic_ip_address import RESOURCE_PLURAL as ELASTIC_IP_PLURAL
2827

2928
RESOURCE_PLURAL = "natgateways"
3029

@@ -47,7 +46,7 @@ def standard_elastic_address():
4746

4847
# Create the k8s resource
4948
ref = k8s.CustomResourceReference(
50-
CRD_GROUP, CRD_VERSION, ELASTIC_IP_PLURAL,
49+
CRD_GROUP, CRD_VERSION, "elasticipaddresses",
5150
cluster_name, namespace="default",
5251
)
5352
k8s.create_custom_resource(ref, resource_data)
@@ -69,41 +68,56 @@ def standard_elastic_address():
6968
except:
7069
pass
7170

72-
@service_marker
73-
@pytest.mark.canary
74-
class TestNATGateway:
75-
def test_create_delete(self, standard_elastic_address, ec2_client):
76-
test_resource_values = REPLACEMENT_VALUES.copy()
77-
resource_name = random_suffix_name("nat-gateway-test", 24)
78-
test_vpc = get_bootstrap_resources().SharedTestVPC
79-
subnet_id = test_vpc.public_subnets.subnet_ids[0]
71+
@pytest.fixture
72+
def simple_nat_gateway(standard_elastic_address):
73+
test_resource_values = REPLACEMENT_VALUES.copy()
74+
resource_name = random_suffix_name("nat-gateway-test", 24)
75+
test_vpc = get_bootstrap_resources().SharedTestVPC
76+
subnet_id = test_vpc.public_subnets.subnet_ids[0]
8077

81-
(_, eip) = standard_elastic_address
78+
(_, eip) = standard_elastic_address
8279

83-
test_resource_values["NAT_GATEWAY_NAME"] = resource_name
84-
test_resource_values["SUBNET_ID"] = subnet_id
85-
test_resource_values["ALLOCATION_ID"] = eip["status"]["allocationID"]
80+
test_resource_values["NAT_GATEWAY_NAME"] = resource_name
81+
test_resource_values["SUBNET_ID"] = subnet_id
82+
test_resource_values["ALLOCATION_ID"] = eip["status"]["allocationID"]
8683

87-
# Load NAT Gateway CR
88-
resource_data = load_ec2_resource(
89-
"nat_gateway",
90-
additional_replacements=test_resource_values,
91-
)
92-
logging.debug(resource_data)
84+
# Load NAT Gateway CR
85+
resource_data = load_ec2_resource(
86+
"nat_gateway",
87+
additional_replacements=test_resource_values,
88+
)
89+
logging.debug(resource_data)
9390

94-
# Create k8s resource
95-
ref = k8s.CustomResourceReference(
96-
CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL,
97-
resource_name, namespace="default",
98-
)
99-
k8s.create_custom_resource(ref, resource_data)
100-
cr = k8s.wait_resource_consumed_by_controller(ref)
91+
# Create k8s resource
92+
ref = k8s.CustomResourceReference(
93+
CRD_GROUP, CRD_VERSION, RESOURCE_PLURAL,
94+
resource_name, namespace="default",
95+
)
96+
k8s.create_custom_resource(ref, resource_data)
97+
cr = k8s.wait_resource_consumed_by_controller(ref)
10198

102-
assert cr is not None
103-
assert k8s.get_resource_exists(ref)
99+
# ElasticIP are not usable immediately after they are created, so this will
100+
# buy us some time in case we try to mount it too early.
101+
time.sleep(CREATE_WAIT_AFTER_SECONDS)
102+
103+
assert cr is not None
104+
assert k8s.get_resource_exists(ref)
104105

105-
resource = k8s.get_resource(ref)
106-
resource_id = resource["status"]["natGatewayID"]
106+
yield (ref, cr)
107+
108+
# Try to delete, if doesn't already exist
109+
try:
110+
_, deleted = k8s.delete_custom_resource(ref, 3, 10)
111+
assert deleted
112+
except:
113+
pass
114+
115+
@service_marker
116+
@pytest.mark.canary
117+
class TestNATGateway:
118+
def test_create_delete(self, simple_nat_gateway, ec2_client):
119+
(ref, cr) = simple_nat_gateway
120+
resource_id = cr["status"]["natGatewayID"]
107121

108122
time.sleep(CREATE_WAIT_AFTER_SECONDS)
109123

0 commit comments

Comments
 (0)