23
23
from e2e import service_marker , CRD_GROUP , CRD_VERSION , load_ec2_resource
24
24
from e2e .replacement_values import REPLACEMENT_VALUES
25
25
from e2e .tests .helper import EC2Validator
26
+ from e2e .bootstrap_resources import get_bootstrap_resources
26
27
27
28
RESOURCE_PLURAL = "internetgateways"
28
29
29
30
CREATE_WAIT_AFTER_SECONDS = 10
31
+ MODIFY_WAIT_AFTER_SECONDS = 10
30
32
DELETE_WAIT_AFTER_SECONDS = 10
31
33
32
34
@service_marker
@@ -70,5 +72,75 @@ def test_create_delete(self, ec2_client):
70
72
71
73
time .sleep (DELETE_WAIT_AFTER_SECONDS )
72
74
75
+ # Check Internet Gateway no longer exists in AWS
76
+ ec2_validator .assert_internet_gateway (resource_id , exists = False )
77
+
78
+ def test_vpc_association (self , ec2_client ):
79
+ resource_name = random_suffix_name ("ig-ack-test" , 24 )
80
+
81
+ test_vpc = get_bootstrap_resources ().EmptyVPC
82
+ vpc_id = test_vpc .vpc_id
83
+
84
+ replacements = REPLACEMENT_VALUES .copy ()
85
+ replacements ["INTERNET_GATEWAY_NAME" ] = resource_name
86
+ replacements ["VPC_ID" ] = vpc_id
87
+
88
+ # Load Internet Gateway CR
89
+ resource_data = load_ec2_resource (
90
+ "internet_gateway_vpc_attachment" ,
91
+ additional_replacements = replacements ,
92
+ )
93
+ logging .debug (resource_data )
94
+
95
+ # Create k8s resource
96
+ ref = k8s .CustomResourceReference (
97
+ CRD_GROUP , CRD_VERSION , RESOURCE_PLURAL ,
98
+ resource_name , namespace = "default" ,
99
+ )
100
+ k8s .create_custom_resource (ref , resource_data )
101
+ cr = k8s .wait_resource_consumed_by_controller (ref )
102
+
103
+ assert cr is not None
104
+ assert k8s .get_resource_exists (ref )
105
+
106
+ resource = k8s .get_resource (ref )
107
+ resource_id = resource ["status" ]["internetGatewayID" ]
108
+
109
+ time .sleep (CREATE_WAIT_AFTER_SECONDS )
110
+
111
+ # Check Internet Gateway exists in AWS
112
+ ec2_validator = EC2Validator (ec2_client )
113
+ ec2_validator .assert_internet_gateway (resource_id )
114
+
115
+ # Check attachments appear on Internet Gateway
116
+ igw = ec2_validator .get_internet_gateway (resource_id )
117
+ assert len (igw ["Attachments" ]) == 1
118
+ assert igw ["Attachments" ][0 ]["VpcId" ] == vpc_id
119
+ assert igw ["Attachments" ][0 ]["State" ] == "available"
120
+
121
+ # Patch the IGW, removing the attachment
122
+ updates = {
123
+ "spec" : {"vpc" : None },
124
+ }
125
+ k8s .patch_custom_resource (ref , updates )
126
+ time .sleep (MODIFY_WAIT_AFTER_SECONDS )
127
+
128
+ # Check there are no attachments on Internet Gateway
129
+ igw = ec2_validator .get_internet_gateway (resource_id )
130
+
131
+ # In the case where it shows the attachment as being in detached state
132
+ if len (igw ["Attachments" ]) == 1 :
133
+ assert igw ["Attachments" ][0 ]["VpcId" ] == vpc_id
134
+ assert igw ["Attachments" ][0 ]["State" ] == "detached"
135
+ else :
136
+ # Otherwise there should be no attachment on the IGW
137
+ assert len (igw ["Attachments" ]) == 0
138
+
139
+ # Delete k8s resource
140
+ _ , deleted = k8s .delete_custom_resource (ref , 2 , 5 )
141
+ assert deleted is True
142
+
143
+ time .sleep (DELETE_WAIT_AFTER_SECONDS )
144
+
73
145
# Check Internet Gateway no longer exists in AWS
74
146
ec2_validator .assert_internet_gateway (resource_id , exists = False )
0 commit comments