Skip to content

Commit 332882b

Browse files
authored
Merge pull request kubernetes-sigs#9348 from willie-yao/cc-mp-unit-tests
🌱 Add unit tests for CC MP blueprint, current_state, & desired_state
2 parents 2f9c7bd + b5d2e25 commit 332882b

File tree

4 files changed

+1002
-18
lines changed

4 files changed

+1002
-18
lines changed

internal/controllers/topology/cluster/blueprint_test.go

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func TestGetBlueprint(t *testing.T) {
3737
builder.GenericInfrastructureClusterTemplateCRD,
3838
builder.GenericInfrastructureMachineTemplateCRD,
3939
builder.GenericInfrastructureMachineCRD,
40+
builder.GenericInfrastructureMachinePoolTemplateCRD,
41+
builder.GenericInfrastructureMachinePoolCRD,
4042
builder.GenericControlPlaneTemplateCRD,
4143
builder.GenericBootstrapConfigTemplateCRD,
4244
}
@@ -56,6 +58,8 @@ func TestGetBlueprint(t *testing.T) {
5658

5759
workerInfrastructureMachineTemplate := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "workerinframachinetemplate1").
5860
Build()
61+
workerInfrastructureMachinePoolTemplate := builder.InfrastructureMachinePoolTemplate(metav1.NamespaceDefault, "workerinframachinepooltemplate1").
62+
Build()
5963
workerBootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "workerbootstraptemplate1").
6064
Build()
6165
machineHealthCheck := &clusterv1.MachineHealthCheckClass{
@@ -73,6 +77,14 @@ func TestGetBlueprint(t *testing.T) {
7377

7478
mds := []clusterv1.MachineDeploymentClass{*machineDeployment}
7579

80+
machinePools := builder.MachinePoolClass("workerclass2").
81+
WithLabels(map[string]string{"foo": "bar"}).
82+
WithAnnotations(map[string]string{"a": "b"}).
83+
WithInfrastructureTemplate(workerInfrastructureMachinePoolTemplate).
84+
WithBootstrapTemplate(workerBootstrapTemplate).
85+
Build()
86+
mps := []clusterv1.MachinePoolClass{*machinePools}
87+
7688
// Define test cases.
7789
tests := []struct {
7890
name string
@@ -141,6 +153,7 @@ func TestGetBlueprint(t *testing.T) {
141153
Template: controlPlaneTemplate,
142154
},
143155
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
156+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
144157
},
145158
},
146159
{
@@ -167,6 +180,7 @@ func TestGetBlueprint(t *testing.T) {
167180
InfrastructureMachineTemplate: controlPlaneInfrastructureMachineTemplate,
168181
},
169182
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
183+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
170184
},
171185
},
172186
{
@@ -217,10 +231,11 @@ func TestGetBlueprint(t *testing.T) {
217231
MachineHealthCheck: machineHealthCheck,
218232
},
219233
},
234+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
220235
},
221236
},
222237
{
223-
name: "Fails if ClusterClass has a MachineDeploymentClass referencing a BootstrapTemplate that does not exist",
238+
name: "Fails if ClusterClass has a MachineDeploymentClass referencing a BootstrapConfigTemplate that does not exist",
224239
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
225240
WithInfrastructureClusterTemplate(infraClusterTemplate).
226241
WithControlPlaneTemplate(controlPlaneTemplate).
@@ -276,8 +291,75 @@ func TestGetBlueprint(t *testing.T) {
276291
MachineHealthCheck: machineHealthCheck,
277292
},
278293
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
294+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
295+
},
296+
},
297+
{
298+
name: "Should read a ClusterClass with a MachinePoolClass",
299+
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
300+
WithInfrastructureClusterTemplate(infraClusterTemplate).
301+
WithControlPlaneTemplate(controlPlaneTemplate).
302+
WithWorkerMachinePoolClasses(mps...).
303+
Build(),
304+
objects: []client.Object{
305+
infraClusterTemplate,
306+
controlPlaneTemplate,
307+
workerInfrastructureMachinePoolTemplate,
308+
workerBootstrapTemplate,
309+
},
310+
want: &scope.ClusterBlueprint{
311+
ClusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
312+
WithInfrastructureClusterTemplate(infraClusterTemplate).
313+
WithControlPlaneTemplate(controlPlaneTemplate).
314+
WithWorkerMachinePoolClasses(mps...).
315+
Build(),
316+
InfrastructureClusterTemplate: infraClusterTemplate,
317+
ControlPlane: &scope.ControlPlaneBlueprint{
318+
Template: controlPlaneTemplate,
319+
},
320+
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
321+
MachinePools: map[string]*scope.MachinePoolBlueprint{
322+
"workerclass2": {
323+
Metadata: clusterv1.ObjectMeta{
324+
Labels: map[string]string{"foo": "bar"},
325+
Annotations: map[string]string{"a": "b"},
326+
},
327+
InfrastructureMachinePoolTemplate: workerInfrastructureMachinePoolTemplate,
328+
BootstrapTemplate: workerBootstrapTemplate,
329+
},
330+
},
279331
},
280332
},
333+
{
334+
name: "Fails if ClusterClass has a MachinePoolClass referencing a BootstrapConfigTemplate that does not exist",
335+
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
336+
WithInfrastructureClusterTemplate(infraClusterTemplate).
337+
WithControlPlaneTemplate(controlPlaneTemplate).
338+
WithWorkerMachinePoolClasses(mps...).
339+
Build(),
340+
objects: []client.Object{
341+
infraClusterTemplate,
342+
controlPlaneTemplate,
343+
workerInfrastructureMachinePoolTemplate,
344+
// workerBootstrapTemplate is missing!
345+
},
346+
wantErr: true,
347+
},
348+
{
349+
name: "Fails if ClusterClass has a MachinePoolClass referencing a InfrastructureMachinePoolTemplate that does not exist",
350+
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
351+
WithInfrastructureClusterTemplate(infraClusterTemplate).
352+
WithControlPlaneTemplate(controlPlaneTemplate).
353+
WithWorkerMachinePoolClasses(mps...).
354+
Build(),
355+
objects: []client.Object{
356+
infraClusterTemplate,
357+
controlPlaneTemplate,
358+
workerBootstrapTemplate,
359+
// workerInfrastructureMachinePoolTemplate is missing!
360+
},
361+
wantErr: true,
362+
},
281363
}
282364
for _, tt := range tests {
283365
t.Run(tt.name, func(t *testing.T) {
@@ -333,7 +415,8 @@ func TestGetBlueprint(t *testing.T) {
333415
g.Expect(tt.want.ClusterClass).To(EqualObject(got.ClusterClass, IgnoreAutogeneratedMetadata))
334416
g.Expect(tt.want.InfrastructureClusterTemplate).To(EqualObject(got.InfrastructureClusterTemplate), cmp.Diff(got.InfrastructureClusterTemplate, tt.want.InfrastructureClusterTemplate))
335417
g.Expect(got.ControlPlane).To(BeComparableTo(tt.want.ControlPlane), cmp.Diff(got.ControlPlane, tt.want.ControlPlane))
336-
g.Expect(tt.want.MachineDeployments).To(BeComparableTo(got.MachineDeployments), got.MachineDeployments, tt.want.MachineDeployments)
418+
g.Expect(tt.want.MachineDeployments).To(BeComparableTo(got.MachineDeployments), cmp.Diff(got.MachineDeployments, tt.want.MachineDeployments))
419+
g.Expect(tt.want.MachinePools).To(BeComparableTo(got.MachinePools), cmp.Diff(got.MachinePools, tt.want.MachinePools))
337420
})
338421
}
339422
}

0 commit comments

Comments
 (0)