Skip to content

Commit d4164a1

Browse files
committed
Add unit tests for blueprint, current state, & desired state.
1 parent 022ccf1 commit d4164a1

File tree

4 files changed

+964
-12
lines changed

4 files changed

+964
-12
lines changed

internal/controllers/topology/cluster/blueprint_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func TestGetBlueprint(t *testing.T) {
5656

5757
workerInfrastructureMachineTemplate := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "workerinframachinetemplate1").
5858
Build()
59+
workerInfrastructureMachinePoolTemplate := builder.InfrastructureMachinePoolTemplate(metav1.NamespaceDefault, "workerinframachinepooltemplate1").
60+
Build()
5961
workerBootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "workerbootstraptemplate1").
6062
Build()
6163
machineHealthCheck := &clusterv1.MachineHealthCheckClass{
@@ -73,6 +75,14 @@ func TestGetBlueprint(t *testing.T) {
7375

7476
mds := []clusterv1.MachineDeploymentClass{*machineDeployment}
7577

78+
machinePools := builder.MachinePoolClass("workerclass2").
79+
WithLabels(map[string]string{"foo": "bar"}).
80+
WithAnnotations(map[string]string{"a": "b"}).
81+
WithInfrastructureTemplate(workerInfrastructureMachinePoolTemplate).
82+
WithBootstrapTemplate(workerBootstrapTemplate).
83+
Build()
84+
mps := []clusterv1.MachinePoolClass{*machinePools}
85+
7686
// Define test cases.
7787
tests := []struct {
7888
name string
@@ -141,6 +151,7 @@ func TestGetBlueprint(t *testing.T) {
141151
Template: controlPlaneTemplate,
142152
},
143153
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
154+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
144155
},
145156
},
146157
{
@@ -167,6 +178,7 @@ func TestGetBlueprint(t *testing.T) {
167178
InfrastructureMachineTemplate: controlPlaneInfrastructureMachineTemplate,
168179
},
169180
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
181+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
170182
},
171183
},
172184
{
@@ -217,6 +229,7 @@ func TestGetBlueprint(t *testing.T) {
217229
MachineHealthCheck: machineHealthCheck,
218230
},
219231
},
232+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
220233
},
221234
},
222235
{
@@ -276,8 +289,60 @@ func TestGetBlueprint(t *testing.T) {
276289
MachineHealthCheck: machineHealthCheck,
277290
},
278291
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
292+
MachinePools: map[string]*scope.MachinePoolBlueprint{},
279293
},
280294
},
295+
{
296+
name: "Should read a ClusterClass with a MachinePoolClass",
297+
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
298+
WithInfrastructureClusterTemplate(infraClusterTemplate).
299+
WithControlPlaneTemplate(controlPlaneTemplate).
300+
WithWorkerMachinePoolClasses(mps...).
301+
Build(),
302+
objects: []client.Object{
303+
infraClusterTemplate,
304+
controlPlaneTemplate,
305+
workerInfrastructureMachinePoolTemplate,
306+
workerBootstrapTemplate,
307+
},
308+
want: &scope.ClusterBlueprint{
309+
ClusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
310+
WithInfrastructureClusterTemplate(infraClusterTemplate).
311+
WithControlPlaneTemplate(controlPlaneTemplate).
312+
WithWorkerMachinePoolClasses(mps...).
313+
Build(),
314+
InfrastructureClusterTemplate: infraClusterTemplate,
315+
ControlPlane: &scope.ControlPlaneBlueprint{
316+
Template: controlPlaneTemplate,
317+
},
318+
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
319+
MachinePools: map[string]*scope.MachinePoolBlueprint{
320+
"workerclass2": {
321+
Metadata: clusterv1.ObjectMeta{
322+
Labels: map[string]string{"foo": "bar"},
323+
Annotations: map[string]string{"a": "b"},
324+
},
325+
InfrastructureMachinePoolTemplate: workerInfrastructureMachinePoolTemplate,
326+
BootstrapTemplate: workerBootstrapTemplate,
327+
},
328+
},
329+
},
330+
},
331+
{
332+
name: "Fails if ClusterClass has a MachinePoolClass referencing a BootstrapConfig that does not exist",
333+
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
334+
WithInfrastructureClusterTemplate(infraClusterTemplate).
335+
WithControlPlaneTemplate(controlPlaneTemplate).
336+
WithWorkerMachinePoolClasses(mps...).
337+
Build(),
338+
objects: []client.Object{
339+
infraClusterTemplate,
340+
controlPlaneTemplate,
341+
workerInfrastructureMachinePoolTemplate,
342+
// workerInfrastructureMachinePoolTemplate is missing!
343+
},
344+
wantErr: true,
345+
},
281346
}
282347
for _, tt := range tests {
283348
t.Run(tt.name, func(t *testing.T) {
@@ -334,6 +399,7 @@ func TestGetBlueprint(t *testing.T) {
334399
g.Expect(tt.want.InfrastructureClusterTemplate).To(EqualObject(got.InfrastructureClusterTemplate), cmp.Diff(got.InfrastructureClusterTemplate, tt.want.InfrastructureClusterTemplate))
335400
g.Expect(got.ControlPlane).To(BeComparableTo(tt.want.ControlPlane), cmp.Diff(got.ControlPlane, tt.want.ControlPlane))
336401
g.Expect(tt.want.MachineDeployments).To(BeComparableTo(got.MachineDeployments), got.MachineDeployments, tt.want.MachineDeployments)
402+
g.Expect(tt.want.MachinePools).To(BeComparableTo(got.MachinePools), got.MachinePools, tt.want.MachinePools)
337403
})
338404
}
339405
}

0 commit comments

Comments
 (0)