24
24
from e2e .replacement_values import REPLACEMENT_VALUES
25
25
from e2e .bootstrap_resources import get_bootstrap_resources
26
26
from e2e .tests .helper import EC2Validator
27
- from .test_elastic_ip_address import RESOURCE_PLURAL as ELASTIC_IP_PLURAL
28
27
29
28
RESOURCE_PLURAL = "natgateways"
30
29
@@ -47,7 +46,7 @@ def standard_elastic_address():
47
46
48
47
# Create the k8s resource
49
48
ref = k8s .CustomResourceReference (
50
- CRD_GROUP , CRD_VERSION , ELASTIC_IP_PLURAL ,
49
+ CRD_GROUP , CRD_VERSION , "elasticipaddresses" ,
51
50
cluster_name , namespace = "default" ,
52
51
)
53
52
k8s .create_custom_resource (ref , resource_data )
@@ -69,41 +68,56 @@ def standard_elastic_address():
69
68
except :
70
69
pass
71
70
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 ]
80
77
81
- (_ , eip ) = standard_elastic_address
78
+ (_ , eip ) = standard_elastic_address
82
79
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" ]
86
83
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 )
93
90
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 )
101
98
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 )
104
105
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" ]
107
121
108
122
time .sleep (CREATE_WAIT_AFTER_SECONDS )
109
123
0 commit comments