@@ -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+
466582func TestCreateCluster (t * testing.T ) {
467583 clusterName := "cluster.default"
468584 version := aws .String ("1.24" )
0 commit comments