Skip to content

Commit 5e189bb

Browse files
committed
Add unit tests for EKS Provisioned Control Plane
Add comprehensive unit tests for: - ControlPlaneScalingConfigToSDK converter function - ControlPlaneScalingConfigFromSDK converter function - reconcileScalingConfig reconciliation logic Tests cover: - Nil input handling - All tier values (standard, tier-xl, tier-2xl, tier-4xl) - Update detection logic when tier changes - No update when tier is the same
1 parent 8868cc4 commit 5e189bb

File tree

2 files changed

+242
-0
lines changed

2 files changed

+242
-0
lines changed

pkg/cloud/converters/eks_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
2424
"github.com/google/go-cmp/cmp"
2525
"github.com/google/go-cmp/cmp/cmpopts"
26+
"k8s.io/utils/ptr"
2627

28+
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
2729
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
2830
)
2931

@@ -63,3 +65,128 @@ func TestNodeRepairConfigToSDK(t *testing.T) {
6365
})
6466
}
6567
}
68+
69+
func TestControlPlaneScalingConfigToSDK(t *testing.T) {
70+
tests := []struct {
71+
name string
72+
input *ekscontrolplanev1.ControlPlaneScalingConfig
73+
expected *ekstypes.ControlPlaneScalingConfig
74+
}{
75+
{
76+
name: "nil input returns nil",
77+
input: nil,
78+
expected: nil,
79+
},
80+
{
81+
name: "nil tier returns nil",
82+
input: &ekscontrolplanev1.ControlPlaneScalingConfig{
83+
Tier: nil,
84+
},
85+
expected: nil,
86+
},
87+
{
88+
name: "standard tier",
89+
input: &ekscontrolplanev1.ControlPlaneScalingConfig{
90+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTierStandard),
91+
},
92+
expected: &ekstypes.ControlPlaneScalingConfig{
93+
Tier: ekstypes.ProvisionedControlPlaneTierStandard,
94+
},
95+
},
96+
{
97+
name: "tier-xl",
98+
input: &ekscontrolplanev1.ControlPlaneScalingConfig{
99+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTierXL),
100+
},
101+
expected: &ekstypes.ControlPlaneScalingConfig{
102+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-xl"),
103+
},
104+
},
105+
{
106+
name: "tier-2xl",
107+
input: &ekscontrolplanev1.ControlPlaneScalingConfig{
108+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTier2XL),
109+
},
110+
expected: &ekstypes.ControlPlaneScalingConfig{
111+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-2xl"),
112+
},
113+
},
114+
{
115+
name: "tier-4xl",
116+
input: &ekscontrolplanev1.ControlPlaneScalingConfig{
117+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTier4XL),
118+
},
119+
expected: &ekstypes.ControlPlaneScalingConfig{
120+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-4xl"),
121+
},
122+
},
123+
}
124+
125+
for _, tt := range tests {
126+
t.Run(tt.name, func(t *testing.T) {
127+
result := ControlPlaneScalingConfigToSDK(tt.input)
128+
if !cmp.Equal(result, tt.expected, cmpopts.IgnoreUnexported(ekstypes.ControlPlaneScalingConfig{})) {
129+
t.Errorf("ControlPlaneScalingConfigToSDK() diff (-want +got):\n%s", cmp.Diff(tt.expected, result, cmpopts.IgnoreUnexported(ekstypes.ControlPlaneScalingConfig{})))
130+
}
131+
})
132+
}
133+
}
134+
135+
func TestControlPlaneScalingConfigFromSDK(t *testing.T) {
136+
tests := []struct {
137+
name string
138+
input *ekstypes.ControlPlaneScalingConfig
139+
expected *ekscontrolplanev1.ControlPlaneScalingConfig
140+
}{
141+
{
142+
name: "nil input returns nil",
143+
input: nil,
144+
expected: nil,
145+
},
146+
{
147+
name: "standard tier",
148+
input: &ekstypes.ControlPlaneScalingConfig{
149+
Tier: ekstypes.ProvisionedControlPlaneTierStandard,
150+
},
151+
expected: &ekscontrolplanev1.ControlPlaneScalingConfig{
152+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTierStandard),
153+
},
154+
},
155+
{
156+
name: "tier-xl",
157+
input: &ekstypes.ControlPlaneScalingConfig{
158+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-xl"),
159+
},
160+
expected: &ekscontrolplanev1.ControlPlaneScalingConfig{
161+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTierXL),
162+
},
163+
},
164+
{
165+
name: "tier-2xl",
166+
input: &ekstypes.ControlPlaneScalingConfig{
167+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-2xl"),
168+
},
169+
expected: &ekscontrolplanev1.ControlPlaneScalingConfig{
170+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTier2XL),
171+
},
172+
},
173+
{
174+
name: "tier-4xl",
175+
input: &ekstypes.ControlPlaneScalingConfig{
176+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-4xl"),
177+
},
178+
expected: &ekscontrolplanev1.ControlPlaneScalingConfig{
179+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTier4XL),
180+
},
181+
},
182+
}
183+
184+
for _, tt := range tests {
185+
t.Run(tt.name, func(t *testing.T) {
186+
result := ControlPlaneScalingConfigFromSDK(tt.input)
187+
if !cmp.Equal(result, tt.expected) {
188+
t.Errorf("ControlPlaneScalingConfigFromSDK() diff (-want +got):\n%s", cmp.Diff(tt.expected, result))
189+
}
190+
})
191+
}
192+
}

pkg/cloud/services/eks/cluster_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,3 +1064,118 @@ func TestCreateClusterWithBootstrapClusterCreatorAdminPermissions(t *testing.T)
10641064
_, err = s.createCluster(context.TODO(), clusterName)
10651065
g.Expect(err).To(BeNil())
10661066
}
1067+
1068+
func TestReconcileScalingConfig(t *testing.T) {
1069+
testCases := []struct {
1070+
name string
1071+
specConfig *ekscontrolplanev1.ControlPlaneScalingConfig
1072+
clusterConfig *ekstypes.ControlPlaneScalingConfig
1073+
expectUpdate bool
1074+
expectedConfig *ekstypes.ControlPlaneScalingConfig
1075+
}{
1076+
{
1077+
name: "spec has no scaling config - no update",
1078+
specConfig: nil,
1079+
clusterConfig: nil,
1080+
expectUpdate: false,
1081+
expectedConfig: nil,
1082+
},
1083+
{
1084+
name: "spec has nil tier - no update",
1085+
specConfig: &ekscontrolplanev1.ControlPlaneScalingConfig{
1086+
Tier: nil,
1087+
},
1088+
clusterConfig: nil,
1089+
expectUpdate: false,
1090+
expectedConfig: nil,
1091+
},
1092+
{
1093+
name: "cluster has no scaling config but spec does - update needed",
1094+
specConfig: &ekscontrolplanev1.ControlPlaneScalingConfig{
1095+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTier2XL),
1096+
},
1097+
clusterConfig: nil,
1098+
expectUpdate: true,
1099+
expectedConfig: &ekstypes.ControlPlaneScalingConfig{
1100+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-2xl"),
1101+
},
1102+
},
1103+
{
1104+
name: "tier differs - update needed",
1105+
specConfig: &ekscontrolplanev1.ControlPlaneScalingConfig{
1106+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTier4XL),
1107+
},
1108+
clusterConfig: &ekstypes.ControlPlaneScalingConfig{
1109+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-2xl"),
1110+
},
1111+
expectUpdate: true,
1112+
expectedConfig: &ekstypes.ControlPlaneScalingConfig{
1113+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-4xl"),
1114+
},
1115+
},
1116+
{
1117+
name: "tier is same - no update",
1118+
specConfig: &ekscontrolplanev1.ControlPlaneScalingConfig{
1119+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTier2XL),
1120+
},
1121+
clusterConfig: &ekstypes.ControlPlaneScalingConfig{
1122+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-2xl"),
1123+
},
1124+
expectUpdate: false,
1125+
expectedConfig: nil,
1126+
},
1127+
{
1128+
name: "changing from standard to xl - update needed",
1129+
specConfig: &ekscontrolplanev1.ControlPlaneScalingConfig{
1130+
Tier: ptr.To(ekscontrolplanev1.ControlPlaneScalingTierXL),
1131+
},
1132+
clusterConfig: &ekstypes.ControlPlaneScalingConfig{
1133+
Tier: ekstypes.ProvisionedControlPlaneTierStandard,
1134+
},
1135+
expectUpdate: true,
1136+
expectedConfig: &ekstypes.ControlPlaneScalingConfig{
1137+
Tier: ekstypes.ProvisionedControlPlaneTier("tier-xl"),
1138+
},
1139+
},
1140+
}
1141+
1142+
for _, tc := range testCases {
1143+
t.Run(tc.name, func(t *testing.T) {
1144+
g := NewWithT(t)
1145+
mockControl := gomock.NewController(t)
1146+
defer mockControl.Finish()
1147+
1148+
scheme := runtime.NewScheme()
1149+
_ = infrav1.AddToScheme(scheme)
1150+
_ = ekscontrolplanev1.AddToScheme(scheme)
1151+
client := fake.NewClientBuilder().WithScheme(scheme).Build()
1152+
1153+
scope, err := scope.NewManagedControlPlaneScope(scope.ManagedControlPlaneScopeParams{
1154+
Client: client,
1155+
Cluster: &clusterv1.Cluster{
1156+
ObjectMeta: metav1.ObjectMeta{
1157+
Namespace: "ns",
1158+
Name: "capi-name",
1159+
},
1160+
},
1161+
ControlPlane: &ekscontrolplanev1.AWSManagedControlPlane{
1162+
Spec: ekscontrolplanev1.AWSManagedControlPlaneSpec{
1163+
EKSClusterName: "test-cluster",
1164+
ControlPlaneScalingConfig: tc.specConfig,
1165+
},
1166+
},
1167+
})
1168+
g.Expect(err).To(BeNil())
1169+
1170+
s := NewService(scope)
1171+
result := s.reconcileScalingConfig(tc.clusterConfig)
1172+
1173+
if tc.expectUpdate {
1174+
g.Expect(result).ToNot(BeNil(), "expected update config to be returned")
1175+
g.Expect(result.Tier).To(Equal(tc.expectedConfig.Tier))
1176+
} else {
1177+
g.Expect(result).To(BeNil(), "expected no update")
1178+
}
1179+
})
1180+
}
1181+
}

0 commit comments

Comments
 (0)