sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/network/eips_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package network 18 19 import ( 20 "testing" 21 22 "github.com/aws/aws-sdk-go/aws" 23 "github.com/aws/aws-sdk-go/aws/awserr" 24 "github.com/aws/aws-sdk-go/service/ec2" 25 "github.com/golang/mock/gomock" 26 . "github.com/onsi/gomega" 27 "github.com/pkg/errors" 28 "k8s.io/apimachinery/pkg/runtime" 29 "sigs.k8s.io/controller-runtime/pkg/client/fake" 30 31 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 32 "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/awserrors" 33 "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope" 34 "sigs.k8s.io/cluster-api-provider-aws/test/mocks" 35 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 36 ) 37 38 func TestService_releaseAddresses(t *testing.T) { 39 mockCtrl := gomock.NewController(t) 40 defer mockCtrl.Finish() 41 42 tests := []struct { 43 name string 44 expect func(m *mocks.MockEC2APIMockRecorder) 45 wantErr bool 46 }{ 47 { 48 name: "Should return error if failed to describe IP addresses", 49 expect: func(m *mocks.MockEC2APIMockRecorder) { 50 m.DescribeAddresses(gomock.AssignableToTypeOf(&ec2.DescribeAddressesInput{})).Return(nil, awserrors.NewFailedDependency("dependency failure")) 51 }, 52 wantErr: true, 53 }, 54 { 55 name: "Should ignore releasing elastic IP addresses if not found", 56 expect: func(m *mocks.MockEC2APIMockRecorder) { 57 m.DescribeAddresses(gomock.AssignableToTypeOf(&ec2.DescribeAddressesInput{})).Return(nil, nil) 58 }, 59 }, 60 { 61 name: "Should return error if failed to disassociate IP address", 62 expect: func(m *mocks.MockEC2APIMockRecorder) { 63 m.DescribeAddresses(gomock.AssignableToTypeOf(&ec2.DescribeAddressesInput{})).Return(&ec2.DescribeAddressesOutput{ 64 Addresses: []*ec2.Address{ 65 { 66 AssociationId: aws.String("association-id-1"), 67 PublicIp: aws.String("public-ip"), 68 AllocationId: aws.String("allocation-id"), 69 }, 70 }, 71 }, nil) 72 m.DisassociateAddress(gomock.AssignableToTypeOf(&ec2.DisassociateAddressInput{})).Return(nil, awserrors.NewFailedDependency("dependency-failure")) 73 }, 74 wantErr: true, 75 }, 76 { 77 name: "Should be able to release the IP address", 78 expect: func(m *mocks.MockEC2APIMockRecorder) { 79 m.DescribeAddresses(gomock.AssignableToTypeOf(&ec2.DescribeAddressesInput{})).Return(&ec2.DescribeAddressesOutput{ 80 Addresses: []*ec2.Address{ 81 { 82 AssociationId: aws.String("association-id-1"), 83 PublicIp: aws.String("public-ip"), 84 AllocationId: aws.String("allocation-id"), 85 }, 86 }, 87 }, nil) 88 m.DisassociateAddress(gomock.AssignableToTypeOf(&ec2.DisassociateAddressInput{})).Return(nil, nil) 89 m.ReleaseAddress(gomock.AssignableToTypeOf(&ec2.ReleaseAddressInput{})).Return(nil, nil) 90 }, 91 }, 92 { 93 name: "Should retry if unable to release the IP address because of Auth Failure", 94 expect: func(m *mocks.MockEC2APIMockRecorder) { 95 m.DescribeAddresses(gomock.AssignableToTypeOf(&ec2.DescribeAddressesInput{})).Return(&ec2.DescribeAddressesOutput{ 96 Addresses: []*ec2.Address{ 97 { 98 AssociationId: aws.String("association-id-1"), 99 PublicIp: aws.String("public-ip"), 100 AllocationId: aws.String("allocation-id"), 101 }, 102 }, 103 }, nil) 104 m.DisassociateAddress(gomock.AssignableToTypeOf(&ec2.DisassociateAddressInput{})).Return(nil, nil).Times(2) 105 m.ReleaseAddress(gomock.AssignableToTypeOf(&ec2.ReleaseAddressInput{})).Return(nil, awserr.New(awserrors.AuthFailure, awserrors.AuthFailure, errors.Errorf(awserrors.AuthFailure))) 106 m.ReleaseAddress(gomock.AssignableToTypeOf(&ec2.ReleaseAddressInput{})).Return(nil, nil) 107 }, 108 }, 109 { 110 name: "Should retry if unable to release the IP address because IP is already in use", 111 expect: func(m *mocks.MockEC2APIMockRecorder) { 112 m.DescribeAddresses(gomock.AssignableToTypeOf(&ec2.DescribeAddressesInput{})).Return(&ec2.DescribeAddressesOutput{ 113 Addresses: []*ec2.Address{ 114 { 115 AssociationId: aws.String("association-id-1"), 116 PublicIp: aws.String("public-ip"), 117 AllocationId: aws.String("allocation-id"), 118 }, 119 }, 120 }, nil) 121 m.DisassociateAddress(gomock.AssignableToTypeOf(&ec2.DisassociateAddressInput{})).Return(nil, nil).Times(2) 122 m.ReleaseAddress(gomock.AssignableToTypeOf(&ec2.ReleaseAddressInput{})).Return(nil, awserr.New(awserrors.InUseIPAddress, awserrors.InUseIPAddress, errors.Errorf(awserrors.InUseIPAddress))) 123 m.ReleaseAddress(gomock.AssignableToTypeOf(&ec2.ReleaseAddressInput{})).Return(nil, nil) 124 }, 125 }, 126 { 127 name: "Should not retry if unable to release the IP address due to dependency failure", 128 expect: func(m *mocks.MockEC2APIMockRecorder) { 129 m.DescribeAddresses(gomock.AssignableToTypeOf(&ec2.DescribeAddressesInput{})).Return(&ec2.DescribeAddressesOutput{ 130 Addresses: []*ec2.Address{ 131 { 132 AssociationId: aws.String("association-id-1"), 133 PublicIp: aws.String("public-ip"), 134 AllocationId: aws.String("allocation-id"), 135 }, 136 }, 137 }, nil) 138 m.DisassociateAddress(gomock.AssignableToTypeOf(&ec2.DisassociateAddressInput{})).Return(nil, nil).Times(2) 139 m.ReleaseAddress(gomock.AssignableToTypeOf(&ec2.ReleaseAddressInput{})).Return(nil, awserr.New("dependency-failure", "dependency-failure", errors.Errorf("dependency-failure"))) 140 }, 141 wantErr: true, 142 }, 143 } 144 for _, tt := range tests { 145 t.Run(tt.name, func(t *testing.T) { 146 g := NewWithT(t) 147 148 scheme := runtime.NewScheme() 149 err := infrav1.AddToScheme(scheme) 150 g.Expect(err).NotTo(HaveOccurred()) 151 client := fake.NewClientBuilder().WithScheme(scheme).Build() 152 153 ec2Mock := mocks.NewMockEC2API(mockCtrl) 154 155 cs, err := scope.NewClusterScope(scope.ClusterScopeParams{ 156 Client: client, 157 Cluster: &clusterv1.Cluster{}, 158 AWSCluster: &infrav1.AWSCluster{}, 159 }) 160 g.Expect(err).NotTo(HaveOccurred()) 161 162 s := NewService(cs) 163 s.EC2Client = ec2Mock 164 165 if tt.expect != nil { 166 tt.expect(ec2Mock.EXPECT()) 167 } 168 169 if err := s.releaseAddresses(); (err != nil) != tt.wantErr { 170 t.Errorf("releaseAddresses() error = %v, wantErr %v", err, tt.wantErr) 171 } 172 }) 173 } 174 }