Skip to content

Commit ab53e1d

Browse files
committed
Add integration tests for AWSMachine controller
1 parent c64de9a commit ab53e1d

File tree

10 files changed

+2085
-1592
lines changed

10 files changed

+2085
-1592
lines changed

controllers/awscluster_controller_test.go

Lines changed: 1 addition & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/aws/aws-sdk-go/aws"
2424
"github.com/aws/aws-sdk-go/service/ec2"
25-
"github.com/aws/aws-sdk-go/service/elb"
2625
"github.com/golang/mock/gomock"
2726
. "github.com/onsi/gomega"
2827
"github.com/pkg/errors"
@@ -73,18 +72,11 @@ func TestAWSClusterReconciler_IntegrationTests(t *testing.T) {
7372
expect := func(m *mock_ec2iface.MockEC2APIMockRecorder, e *mock_elbiface.MockELBAPIMockRecorder) {
7473
mockedCreateVPCCalls(m)
7574
mockedCreateSGCalls(m)
76-
mockedCreateLBCalls(e)
75+
mockedCreateLBCalls(t, e)
7776
mockedDescribeInstanceCall(m)
7877
}
7978
expect(ec2Mock.EXPECT(), elbMock.EXPECT())
8079

81-
capaNS := &corev1.Namespace{
82-
ObjectMeta: metav1.ObjectMeta{
83-
Name: "capa-system",
84-
},
85-
}
86-
g.Expect(testEnv.Create(ctx, capaNS)).To(Succeed())
87-
8880
controllerIdentity := createControllerIdentity(g)
8981
ns, err := testEnv.CreateNamespace(ctx, fmt.Sprintf("integ-test-%s", util.RandomString(5)))
9082
g.Expect(err).To(BeNil())
@@ -385,47 +377,6 @@ func mockedDeleteInstanceCalls(m *mock_ec2iface.MockEC2APIMockRecorder) {
385377
Return(nil)
386378
}
387379

388-
func mockedDeleteLBCalls(m *mock_elbiface.MockELBAPIMockRecorder) {
389-
m.DescribeLoadBalancers(gomock.Eq(&elb.DescribeLoadBalancersInput{
390-
LoadBalancerNames: aws.StringSlice([]string{"test-cluster-apiserver"}),
391-
})).
392-
Return(&elb.DescribeLoadBalancersOutput{
393-
LoadBalancerDescriptions: []*elb.LoadBalancerDescription{
394-
{
395-
Scheme: aws.String(string(infrav1.ClassicELBSchemeInternetFacing)),
396-
Subnets: []*string{aws.String("1")},
397-
AvailabilityZones: []*string{aws.String("us-east-1a")},
398-
VPCId: aws.String("vpc-exists"),
399-
},
400-
},
401-
}, nil)
402-
m.DescribeLoadBalancers(gomock.Eq(&elb.DescribeLoadBalancersInput{
403-
LoadBalancerNames: aws.StringSlice([]string{"test-cluster-apiserver"}),
404-
})).
405-
Return(&elb.DescribeLoadBalancersOutput{}, nil).AnyTimes()
406-
m.DescribeTags(&elb.DescribeTagsInput{LoadBalancerNames: []*string{aws.String("test-cluster-apiserver")}}).Return(
407-
&elb.DescribeTagsOutput{
408-
TagDescriptions: []*elb.TagDescription{
409-
{
410-
LoadBalancerName: aws.String("test-cluster-apiserver"),
411-
},
412-
},
413-
}, nil)
414-
m.DescribeLoadBalancerAttributes(gomock.Eq(&elb.DescribeLoadBalancerAttributesInput{
415-
LoadBalancerName: aws.String("test-cluster-apiserver"),
416-
})).
417-
Return(&elb.DescribeLoadBalancerAttributesOutput{
418-
LoadBalancerAttributes: &elb.LoadBalancerAttributes{
419-
CrossZoneLoadBalancing: &elb.CrossZoneLoadBalancing{
420-
Enabled: aws.Bool(false),
421-
},
422-
},
423-
}, nil)
424-
m.DeleteLoadBalancer(gomock.Eq(&elb.DeleteLoadBalancerInput{LoadBalancerName: aws.String("test-cluster-apiserver")})).
425-
Return(&elb.DeleteLoadBalancerOutput{}, nil)
426-
m.DescribeLoadBalancersPages(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
427-
}
428-
429380
func mockedCreateVPCCalls(m *mock_ec2iface.MockEC2APIMockRecorder) {
430381
m.CreateTags(gomock.Eq(&ec2.CreateTagsInput{
431382
Resources: aws.StringSlice([]string{"subnet-1"}),
@@ -950,111 +901,3 @@ func mockedCreateSGCalls(m *mock_ec2iface.MockEC2APIMockRecorder) {
950901
Return(&ec2.AuthorizeSecurityGroupIngressOutput{}, nil).
951902
After(securityGroupNode).Times(2)
952903
}
953-
954-
func mockedCreateLBCalls(m *mock_elbiface.MockELBAPIMockRecorder) {
955-
m.DescribeLoadBalancers(gomock.Eq(&elb.DescribeLoadBalancersInput{
956-
LoadBalancerNames: aws.StringSlice([]string{"test-cluster-apiserver"}),
957-
})).
958-
Return(&elb.DescribeLoadBalancersOutput{
959-
LoadBalancerDescriptions: []*elb.LoadBalancerDescription{
960-
{
961-
Scheme: aws.String(string(infrav1.ClassicELBSchemeInternetFacing)),
962-
Subnets: []*string{aws.String("1")},
963-
AvailabilityZones: []*string{aws.String("us-east-1a")},
964-
VPCId: aws.String("vpc-exists"),
965-
},
966-
},
967-
}, nil)
968-
m.DescribeLoadBalancerAttributes(gomock.Eq(&elb.DescribeLoadBalancerAttributesInput{
969-
LoadBalancerName: aws.String("test-cluster-apiserver"),
970-
})).
971-
Return(&elb.DescribeLoadBalancerAttributesOutput{
972-
LoadBalancerAttributes: &elb.LoadBalancerAttributes{
973-
CrossZoneLoadBalancing: &elb.CrossZoneLoadBalancing{
974-
Enabled: aws.Bool(false),
975-
},
976-
},
977-
}, nil)
978-
m.DescribeTags(&elb.DescribeTagsInput{LoadBalancerNames: []*string{aws.String("test-cluster-apiserver")}}).Return(
979-
&elb.DescribeTagsOutput{
980-
TagDescriptions: []*elb.TagDescription{
981-
{
982-
LoadBalancerName: aws.String("test-cluster-apiserver"),
983-
Tags: []*elb.Tag{{
984-
Key: aws.String(infrav1.ClusterTagKey("test-cluster-apiserver")),
985-
Value: aws.String(string(infrav1.ResourceLifecycleOwned)),
986-
}},
987-
},
988-
},
989-
}, nil)
990-
m.ModifyLoadBalancerAttributes(gomock.Eq(&elb.ModifyLoadBalancerAttributesInput{
991-
LoadBalancerAttributes: &elb.LoadBalancerAttributes{
992-
ConnectionSettings: &elb.ConnectionSettings{IdleTimeout: aws.Int64(600)},
993-
CrossZoneLoadBalancing: &elb.CrossZoneLoadBalancing{Enabled: aws.Bool(false)},
994-
},
995-
LoadBalancerName: aws.String(""),
996-
}))
997-
m.AddTags(gomock.Eq(&elb.AddTagsInput{
998-
LoadBalancerNames: aws.StringSlice([]string{""}),
999-
Tags: []*elb.Tag{
1000-
{
1001-
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
1002-
Value: aws.String("apiserver"),
1003-
},
1004-
{
1005-
Key: aws.String("Name"),
1006-
Value: aws.String("test-cluster-apiserver"),
1007-
},
1008-
{
1009-
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster"),
1010-
Value: aws.String("owned"),
1011-
},
1012-
},
1013-
})).AnyTimes()
1014-
m.AddTags(gomock.Eq(&elb.AddTagsInput{
1015-
LoadBalancerNames: aws.StringSlice([]string{""}),
1016-
Tags: []*elb.Tag{
1017-
{
1018-
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster"),
1019-
Value: aws.String("owned"),
1020-
},
1021-
{
1022-
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
1023-
Value: aws.String("apiserver"),
1024-
},
1025-
{
1026-
Key: aws.String("Name"),
1027-
Value: aws.String("test-cluster-apiserver"),
1028-
},
1029-
},
1030-
})).AnyTimes()
1031-
m.AddTags(gomock.Eq(&elb.AddTagsInput{
1032-
LoadBalancerNames: aws.StringSlice([]string{""}),
1033-
Tags: []*elb.Tag{
1034-
{
1035-
Key: aws.String("Name"),
1036-
Value: aws.String("test-cluster-apiserver"),
1037-
},
1038-
{
1039-
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster"),
1040-
Value: aws.String("owned"),
1041-
},
1042-
{
1043-
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/role"),
1044-
Value: aws.String("apiserver"),
1045-
},
1046-
},
1047-
})).AnyTimes()
1048-
m.RemoveTags(gomock.Eq(&elb.RemoveTagsInput{
1049-
LoadBalancerNames: aws.StringSlice([]string{""}),
1050-
Tags: []*elb.TagKeyOnly{
1051-
{
1052-
Key: aws.String("sigs.k8s.io/cluster-api-provider-aws/cluster/test-cluster-apiserver"),
1053-
},
1054-
},
1055-
}))
1056-
m.ApplySecurityGroupsToLoadBalancer(gomock.Eq(&elb.ApplySecurityGroupsToLoadBalancerInput{
1057-
LoadBalancerName: aws.String(""),
1058-
SecurityGroups: aws.StringSlice([]string{"sg-apiserver-lb"}),
1059-
}))
1060-
}

controllers/awscluster_controller_unit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
209209
},
210210
)
211211
g.Expect(err).To(BeNil())
212-
awsCluster.Status.Network.APIServerELB.DNSName = "www.google.com"
212+
awsCluster.Status.Network.APIServerELB.DNSName = DNSName
213213
awsCluster.Status.Network.APIServerELB.AvailabilityZones = []string{"us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d", "us-east-1e"}
214214
cs.SetSubnets(infrav1.Subnets{
215215
{

controllers/awsmachine_controller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ func (r *AWSMachineReconciler) getSecretService(machineScope *scope.MachineScope
112112
return nil, errors.New("invalid secret backend")
113113
}
114114

115-
func (r *AWSMachineReconciler) getELBService(scope scope.ELBScope) services.ELBInterface {
115+
func (r *AWSMachineReconciler) getELBService(elbScope scope.ELBScope) services.ELBInterface {
116116
if r.elbServiceFactory != nil {
117-
return r.elbServiceFactory(scope)
117+
return r.elbServiceFactory(elbScope)
118118
}
119-
120-
return elb.NewService(scope)
119+
return elb.NewService(elbScope)
121120
}
122121

123122
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmachines,verbs=get;list;watch;create;update;patch;delete
@@ -682,17 +681,17 @@ func (r *AWSMachineReconciler) resolveUserData(machineScope *scope.MachineScope,
682681
return encryptedCloudInit, nil
683682
}
684683

685-
func (r *AWSMachineReconciler) reconcileLBAttachment(machineScope *scope.MachineScope, clusterScope scope.ELBScope, i *infrav1.Instance) error {
684+
func (r *AWSMachineReconciler) reconcileLBAttachment(machineScope *scope.MachineScope, elbScope scope.ELBScope, i *infrav1.Instance) error {
686685
if !machineScope.IsControlPlane() {
687686
return nil
688687
}
689688

690-
elbsvc := r.getELBService(clusterScope)
689+
elbsvc := r.getELBService(elbScope)
691690

692691
// In order to prevent sending request to a "not-ready" control plane machines, it is required to remove the machine
693692
// from the ELB as soon as the machine gets deleted or when the machine is in a not running state.
694693
if !machineScope.AWSMachine.DeletionTimestamp.IsZero() || !machineScope.InstanceIsRunning() {
695-
registered, err := elbsvc.InstanceIsRegisteredWithAPIServerELB(i)
694+
registered, err := elbsvc.IsInstanceRegisteredWithAPIServerELB(i)
696695
if err != nil {
697696
r.Recorder.Eventf(machineScope.AWSMachine, corev1.EventTypeWarning, "FailedDetachControlPlaneELB",
698697
"Failed to deregister control plane instance %q from load balancer: failed to determine registration status: %v", i.ID, err)
@@ -714,7 +713,7 @@ func (r *AWSMachineReconciler) reconcileLBAttachment(machineScope *scope.Machine
714713
return nil
715714
}
716715

717-
registered, err := elbsvc.InstanceIsRegisteredWithAPIServerELB(i)
716+
registered, err := elbsvc.IsInstanceRegisteredWithAPIServerELB(i)
718717
if err != nil {
719718
r.Recorder.Eventf(machineScope.AWSMachine, corev1.EventTypeWarning, "FailedAttachControlPlaneELB",
720719
"Failed to register control plane instance %q with load balancer: failed to determine registration status: %v", i.ID, err)

0 commit comments

Comments
 (0)