@@ -262,15 +262,22 @@ func TestAWSClusterReconciler_IntegrationTests(t *testing.T) {
262
262
{conditionType : infrav1 .SubnetsReadyCondition , status : corev1 .ConditionTrue , severity : "" , reason : "" },
263
263
})
264
264
})
265
+
265
266
t .Run ("Should fail on AWSCluster reconciliation if VPC limit exceeded" , func (t * testing.T ) {
266
267
// Assuming the max VPC limit is 2 and when two VPCs are created, the creation of 3rd VPC throws mocked error from EC2 API
267
268
g := NewWithT (t )
268
269
mockCtrl = gomock .NewController (t )
269
270
ec2Mock := mocks .NewMockEC2API (mockCtrl )
270
- expect := func (m * mocks.MockEC2APIMockRecorder ) {
271
+ elbv2Mock := mocks .NewMockELBV2API (mockCtrl )
272
+ elbMock := mocks .NewMockELBAPI (mockCtrl )
273
+ expect := func (m * mocks.MockEC2APIMockRecorder , ev2 * mocks.MockELBV2APIMockRecorder , e * mocks.MockELBAPIMockRecorder ) {
271
274
mockedCreateMaximumVPCCalls (m )
275
+ mockedDeleteVPCCallsForNonExistentVPC (m )
276
+ mockedDeleteLBCalls (true , ev2 , e )
277
+ mockedDescribeInstanceCall (m )
278
+ mockedDeleteInstanceCalls (m )
272
279
}
273
- expect (ec2Mock .EXPECT ())
280
+ expect (ec2Mock .EXPECT (), elbv2Mock . EXPECT (), elbMock . EXPECT () )
274
281
275
282
setup (t )
276
283
controllerIdentity := createControllerIdentity (g )
@@ -283,9 +290,6 @@ func TestAWSClusterReconciler_IntegrationTests(t *testing.T) {
283
290
},
284
291
Spec : infrav1.AWSClusterSpec {
285
292
Region : "us-east-1" ,
286
- ControlPlaneLoadBalancer : & infrav1.AWSLoadBalancerSpec {
287
- LoadBalancerType : infrav1 .LoadBalancerTypeClassic ,
288
- },
289
293
},
290
294
}
291
295
g .Expect (testEnv .Create (ctx , & awsCluster )).To (Succeed ())
@@ -305,14 +309,32 @@ func TestAWSClusterReconciler_IntegrationTests(t *testing.T) {
305
309
})
306
310
cs , err := getClusterScope (awsCluster )
307
311
g .Expect (err ).To (BeNil ())
308
- s := network .NewService (cs )
309
- s .EC2Client = ec2Mock
310
312
313
+ networkSvc := network .NewService (cs )
314
+ networkSvc .EC2Client = ec2Mock
311
315
reconciler .networkServiceFactory = func (clusterScope scope.ClusterScope ) services.NetworkInterface {
312
- return s
316
+ return networkSvc
317
+ }
318
+
319
+ elbSvc := elbService .NewService (cs )
320
+ elbSvc .EC2Client = ec2Mock
321
+ elbSvc .ELBClient = elbMock
322
+ elbSvc .ELBV2Client = elbv2Mock
323
+ reconciler .elbServiceFactory = func (elbScope scope.ELBScope ) services.ELBInterface {
324
+ return elbSvc
313
325
}
326
+
327
+ ec2Svc := ec2Service .NewService (cs )
328
+ ec2Svc .EC2Client = ec2Mock
329
+ reconciler .ec2ServiceFactory = func (ec2Scope scope.EC2Scope ) services.EC2Interface {
330
+ return ec2Svc
331
+ }
332
+
314
333
_ , err = reconciler .reconcileNormal (cs )
315
334
g .Expect (err .Error ()).To (ContainSubstring ("The maximum number of VPCs has been reached" ))
335
+
336
+ _ , err = reconciler .reconcileDelete (ctx , cs )
337
+ g .Expect (err ).To (BeNil ())
316
338
})
317
339
t .Run ("Should successfully delete AWSCluster with managed VPC" , func (t * testing.T ) {
318
340
g := NewWithT (t )
@@ -710,6 +732,60 @@ func mockedCreateMaximumVPCCalls(m *mocks.MockEC2APIMockRecorder) {
710
732
m .CreateVpc (gomock .AssignableToTypeOf (& ec2.CreateVpcInput {})).Return (nil , errors .New ("The maximum number of VPCs has been reached" ))
711
733
}
712
734
735
+ func mockedDeleteVPCCallsForNonExistentVPC (m * mocks.MockEC2APIMockRecorder ) {
736
+ m .DescribeSubnets (gomock .Eq (& ec2.DescribeSubnetsInput {
737
+ Filters : []* ec2.Filter {
738
+ {
739
+ Name : aws .String ("state" ),
740
+ Values : aws .StringSlice ([]string {ec2 .VpcStatePending , ec2 .VpcStateAvailable }),
741
+ },
742
+ {
743
+ Name : aws .String ("tag-key" ),
744
+ Values : aws .StringSlice ([]string {"sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster" }),
745
+ },
746
+ }})).Return (& ec2.DescribeSubnetsOutput {
747
+ Subnets : []* ec2.Subnet {},
748
+ }, nil ).AnyTimes ()
749
+ m .DescribeRouteTables (gomock .Eq (& ec2.DescribeRouteTablesInput {
750
+ Filters : []* ec2.Filter {{
751
+ Name : aws .String ("vpc-id" ),
752
+ Values : aws .StringSlice ([]string {"" }),
753
+ },
754
+ {
755
+ Name : aws .String ("tag-key" ),
756
+ Values : aws .StringSlice ([]string {"sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster" }),
757
+ },
758
+ }})).Return (& ec2.DescribeRouteTablesOutput {
759
+ RouteTables : []* ec2.RouteTable {}}, nil ).AnyTimes ()
760
+ m .DescribeInternetGateways (gomock .Eq (& ec2.DescribeInternetGatewaysInput {
761
+ Filters : []* ec2.Filter {
762
+ {
763
+ Name : aws .String ("attachment.vpc-id" ),
764
+ Values : aws .StringSlice ([]string {"" }),
765
+ },
766
+ },
767
+ })).Return (& ec2.DescribeInternetGatewaysOutput {
768
+ InternetGateways : []* ec2.InternetGateway {},
769
+ }, nil )
770
+ m .DescribeNatGatewaysPages (gomock .Eq (& ec2.DescribeNatGatewaysInput {
771
+ Filter : []* ec2.Filter {
772
+ {
773
+ Name : aws .String ("vpc-id" ),
774
+ Values : []* string {aws .String ("" )},
775
+ },
776
+ },
777
+ }), gomock .Any ()).Return (nil ).AnyTimes ()
778
+ m .DescribeAddresses (gomock .Eq (& ec2.DescribeAddressesInput {
779
+ Filters : []* ec2.Filter {
780
+ {
781
+ Name : aws .String ("tag-key" ),
782
+ Values : aws .StringSlice ([]string {"sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster" }),
783
+ }},
784
+ })).Return (nil , nil )
785
+ m .DeleteVpc (gomock .AssignableToTypeOf (& ec2.DeleteVpcInput {
786
+ VpcId : aws .String ("vpc-exists" )})).Return (nil , nil )
787
+ }
788
+
713
789
func mockedDeleteVPCCalls (m * mocks.MockEC2APIMockRecorder ) {
714
790
m .DescribeSubnets (gomock .Eq (& ec2.DescribeSubnetsInput {
715
791
Filters : []* ec2.Filter {
0 commit comments