Skip to content

Commit 3e5754c

Browse files
committed
add unit tests
1 parent 49e7a7b commit 3e5754c

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

pkg/cloud/services/eks/cluster.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,17 @@ func (s *Service) reconcileAccessConfig(accessConfig *eks.AccessConfigResponse)
544544
}
545545
return false, err
546546
}
547+
548+
// Wait until status transitions to UPDATING because there's a short
549+
// window after UpdateClusterVersion returns where the cluster
550+
// status is ACTIVE and the update would be tried again
551+
if err := s.EKSClient.WaitUntilClusterUpdating(
552+
&eks.DescribeClusterInput{Name: aws.String(s.scope.KubernetesClusterName())},
553+
request.WithWaiterLogger(&awslog{s.GetLogger()}),
554+
); err != nil {
555+
return false, err
556+
}
557+
547558
conditions.MarkTrue(s.scope.ControlPlane, ekscontrolplanev1.EKSControlPlaneUpdatingCondition)
548559
record.Eventf(s.scope.ControlPlane, "InitiatedUpdateEKSControlPlane", "Initiated auth config update for EKS control plane %s", s.scope.KubernetesClusterName())
549560
return true, nil

pkg/cloud/services/eks/cluster_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/aws/aws-sdk-go/aws"
23+
"github.com/aws/aws-sdk-go/aws/awserr"
2324
"github.com/aws/aws-sdk-go/service/eks"
2425
"github.com/aws/aws-sdk-go/service/iam"
2526
"github.com/golang/mock/gomock"
@@ -463,6 +464,121 @@ func TestReconcileClusterVersion(t *testing.T) {
463464
}
464465
}
465466

467+
func TestReconcileAccessConfig(t *testing.T) {
468+
clusterName := "default.cluster"
469+
tests := []struct {
470+
name string
471+
expect func(m *mock_eksiface.MockEKSAPIMockRecorder)
472+
expectError bool
473+
}{
474+
{
475+
name: "no upgrade necessary",
476+
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {
477+
m.
478+
DescribeCluster(gomock.AssignableToTypeOf(&eks.DescribeClusterInput{})).
479+
Return(&eks.DescribeClusterOutput{
480+
Cluster: &eks.Cluster{
481+
Name: aws.String("default.cluster"),
482+
AccessConfig: &eks.AccessConfigResponse{
483+
AuthenticationMode: aws.String(eks.AuthenticationModeApiAndConfigMap),
484+
},
485+
},
486+
}, nil)
487+
},
488+
expectError: false,
489+
},
490+
{
491+
name: "needs upgrade",
492+
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {
493+
m.
494+
DescribeCluster(gomock.AssignableToTypeOf(&eks.DescribeClusterInput{})).
495+
Return(&eks.DescribeClusterOutput{
496+
Cluster: &eks.Cluster{
497+
Name: aws.String("default.cluster"),
498+
AccessConfig: &eks.AccessConfigResponse{
499+
AuthenticationMode: aws.String(eks.AuthenticationModeConfigMap),
500+
},
501+
},
502+
}, nil)
503+
m.WaitUntilClusterUpdating(
504+
gomock.AssignableToTypeOf(&eks.DescribeClusterInput{}), gomock.Any(),
505+
).Return(nil)
506+
m.
507+
UpdateClusterConfig(gomock.AssignableToTypeOf(&eks.UpdateClusterConfigInput{})).
508+
Return(&eks.UpdateClusterConfigOutput{}, nil)
509+
},
510+
expectError: false,
511+
},
512+
{
513+
name: "api error",
514+
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {
515+
m.
516+
DescribeCluster(gomock.AssignableToTypeOf(&eks.DescribeClusterInput{})).
517+
Return(&eks.DescribeClusterOutput{
518+
Cluster: &eks.Cluster{
519+
Name: aws.String("default.cluster"),
520+
AccessConfig: &eks.AccessConfigResponse{
521+
AuthenticationMode: aws.String(eks.AuthenticationModeApi),
522+
},
523+
},
524+
}, nil)
525+
m.
526+
UpdateClusterConfig(gomock.AssignableToTypeOf(&eks.UpdateClusterConfigInput{})).
527+
Return(&eks.UpdateClusterConfigOutput{}, awserr.New(eks.ErrCodeInvalidParameterException, "Unsupported authentication mode update", nil))
528+
},
529+
expectError: true,
530+
},
531+
}
532+
533+
for _, tc := range tests {
534+
t.Run(tc.name, func(t *testing.T) {
535+
g := NewWithT(t)
536+
537+
mockControl := gomock.NewController(t)
538+
defer mockControl.Finish()
539+
540+
eksMock := mock_eksiface.NewMockEKSAPI(mockControl)
541+
542+
scheme := runtime.NewScheme()
543+
_ = infrav1.AddToScheme(scheme)
544+
_ = ekscontrolplanev1.AddToScheme(scheme)
545+
client := fake.NewClientBuilder().WithScheme(scheme).Build()
546+
scope, err := scope.NewManagedControlPlaneScope(scope.ManagedControlPlaneScopeParams{
547+
Client: client,
548+
Cluster: &clusterv1.Cluster{
549+
ObjectMeta: metav1.ObjectMeta{
550+
Namespace: "ns",
551+
Name: clusterName,
552+
},
553+
},
554+
ControlPlane: &ekscontrolplanev1.AWSManagedControlPlane{
555+
Spec: ekscontrolplanev1.AWSManagedControlPlaneSpec{
556+
EKSClusterName: clusterName,
557+
AccessConfig: &ekscontrolplanev1.AccessConfig{
558+
AuthenticationMode: eks.AuthenticationModeApiAndConfigMap,
559+
},
560+
},
561+
},
562+
})
563+
g.Expect(err).To(BeNil())
564+
565+
tc.expect(eksMock.EXPECT())
566+
s := NewService(scope)
567+
s.EKSClient = eksMock
568+
569+
cluster, err := s.describeEKSCluster(clusterName)
570+
g.Expect(err).To(BeNil())
571+
572+
err = s.reconcileAccessConfig(cluster.AccessConfig)
573+
if tc.expectError {
574+
g.Expect(err).To(HaveOccurred())
575+
return
576+
}
577+
g.Expect(err).To(BeNil())
578+
})
579+
}
580+
}
581+
466582
func TestCreateCluster(t *testing.T) {
467583
clusterName := "cluster.default"
468584
version := aws.String("1.24")

0 commit comments

Comments
 (0)