@@ -22,6 +22,7 @@ import (
22
22
"github.com/aws/aws-sdk-go/aws"
23
23
"github.com/aws/aws-sdk-go/service/ec2"
24
24
"github.com/golang/mock/gomock"
25
+ . "github.com/onsi/gomega"
25
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
27
"k8s.io/apimachinery/pkg/runtime"
27
28
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -149,3 +150,117 @@ func TestReconcileInternetGateways(t *testing.T) {
149
150
})
150
151
}
151
152
}
153
+
154
+ func TestDeleteInternetGateways (t * testing.T ) {
155
+ mockCtrl := gomock .NewController (t )
156
+ defer mockCtrl .Finish ()
157
+
158
+ testCases := []struct {
159
+ name string
160
+ input * infrav1.NetworkSpec
161
+ expect func (m * mock_ec2iface.MockEC2APIMockRecorder )
162
+ wantErr bool
163
+ }{
164
+ {
165
+ name : "Should ignore deletion if vpc is unmanaged" ,
166
+ input : & infrav1.NetworkSpec {
167
+ VPC : infrav1.VPCSpec {
168
+ ID : "vpc-gateways" ,
169
+ },
170
+ },
171
+ expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {},
172
+ },
173
+ {
174
+ name : "Should ignore deletion if internet gateway is not found" ,
175
+ input : & infrav1.NetworkSpec {
176
+ VPC : infrav1.VPCSpec {
177
+ ID : "vpc-gateways" ,
178
+ Tags : infrav1.Tags {
179
+ infrav1 .ClusterTagKey ("test-cluster" ): "owned" ,
180
+ },
181
+ },
182
+ },
183
+ expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
184
+ m .DescribeInternetGateways (gomock .Eq (& ec2.DescribeInternetGatewaysInput {
185
+ Filters : []* ec2.Filter {
186
+ {
187
+ Name : aws .String ("attachment.vpc-id" ),
188
+ Values : aws .StringSlice ([]string {"vpc-gateways" }),
189
+ },
190
+ },
191
+ })).Return (& ec2.DescribeInternetGatewaysOutput {}, nil )
192
+ },
193
+ },
194
+ {
195
+ name : "Should successfully delete the internet gateway" ,
196
+ input : & infrav1.NetworkSpec {
197
+ VPC : infrav1.VPCSpec {
198
+ ID : "vpc-gateways" ,
199
+ Tags : infrav1.Tags {
200
+ infrav1 .ClusterTagKey ("test-cluster" ): "owned" ,
201
+ },
202
+ },
203
+ },
204
+ expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
205
+ m .DescribeInternetGateways (gomock .AssignableToTypeOf (& ec2.DescribeInternetGatewaysInput {})).
206
+ Return (& ec2.DescribeInternetGatewaysOutput {
207
+ InternetGateways : []* ec2.InternetGateway {
208
+ {
209
+ InternetGatewayId : aws .String ("igw-0" ),
210
+ Attachments : []* ec2.InternetGatewayAttachment {
211
+ {
212
+ State : aws .String (ec2 .AttachmentStatusAttached ),
213
+ VpcId : aws .String ("vpc-gateways" ),
214
+ },
215
+ },
216
+ },
217
+ },
218
+ }, nil )
219
+ m .DetachInternetGateway (& ec2.DetachInternetGatewayInput {
220
+ InternetGatewayId : aws .String ("igw-0" ),
221
+ VpcId : aws .String ("vpc-gateways" ),
222
+ }).Return (& ec2.DetachInternetGatewayOutput {}, nil )
223
+ m .DeleteInternetGateway (& ec2.DeleteInternetGatewayInput {
224
+ InternetGatewayId : aws .String ("igw-0" ),
225
+ }).Return (& ec2.DeleteInternetGatewayOutput {}, nil )
226
+ },
227
+ },
228
+ }
229
+ for _ , tc := range testCases {
230
+ t .Run (tc .name , func (t * testing.T ) {
231
+ g := NewWithT (t )
232
+ ec2Mock := mock_ec2iface .NewMockEC2API (mockCtrl )
233
+
234
+ scheme := runtime .NewScheme ()
235
+ err := infrav1 .AddToScheme (scheme )
236
+ g .Expect (err ).NotTo (HaveOccurred ())
237
+ client := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
238
+
239
+ scope , err := scope .NewClusterScope (scope.ClusterScopeParams {
240
+ Client : client ,
241
+ Cluster : & clusterv1.Cluster {
242
+ ObjectMeta : metav1.ObjectMeta {Name : "test-cluster" },
243
+ },
244
+ AWSCluster : & infrav1.AWSCluster {
245
+ ObjectMeta : metav1.ObjectMeta {Name : "test" },
246
+ Spec : infrav1.AWSClusterSpec {
247
+ NetworkSpec : * tc .input ,
248
+ },
249
+ },
250
+ })
251
+ g .Expect (err ).NotTo (HaveOccurred ())
252
+
253
+ tc .expect (ec2Mock .EXPECT ())
254
+
255
+ s := NewService (scope )
256
+ s .EC2Client = ec2Mock
257
+
258
+ err = s .deleteInternetGateways ()
259
+ if tc .wantErr {
260
+ g .Expect (err ).To (HaveOccurred ())
261
+ return
262
+ }
263
+ g .Expect (err ).NotTo (HaveOccurred ())
264
+ })
265
+ }
266
+ }
0 commit comments