Skip to content

Commit 52d0e7c

Browse files
adammwjoshfrench
authored andcommitted
add unit tests
1 parent 32efafb commit 52d0e7c

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

pkg/cloud/services/eks/cluster.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,17 @@ func (s *Service) reconcileAccessConfig(accessConfig *ekstypes.AccessConfigRespo
585585
}
586586
return false, err
587587
}
588+
589+
// Wait until status transitions to UPDATING because there's a short
590+
// window after UpdateClusterVersion returns where the cluster
591+
// status is ACTIVE and the update would be tried again
592+
if err := s.EKSClient.WaitUntilClusterUpdating(
593+
&eks.DescribeClusterInput{Name: aws.String(s.scope.KubernetesClusterName())},
594+
request.WithWaiterLogger(&awslog{s.GetLogger()}),
595+
); err != nil {
596+
return false, err
597+
}
598+
588599
conditions.MarkTrue(s.scope.ControlPlane, ekscontrolplanev1.EKSControlPlaneUpdatingCondition)
589600
record.Eventf(s.scope.ControlPlane, "InitiatedUpdateEKSControlPlane", "Initiated auth config update for EKS control plane %s", s.scope.KubernetesClusterName())
590601
return true, nil

pkg/cloud/services/eks/cluster_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/aws/aws-sdk-go-v2/aws"
24+
"github.com/aws/aws-sdk-go-v2/aws/awserr"
2425
"github.com/aws/aws-sdk-go-v2/service/eks"
2526
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
2627
"github.com/aws/aws-sdk-go-v2/service/iam"
@@ -474,6 +475,123 @@ func TestReconcileClusterVersion(t *testing.T) {
474475
}
475476
}
476477

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

0 commit comments

Comments
 (0)