Skip to content

Commit

Permalink
Add unit tests for blueprint, current state, & desired state.
Browse files Browse the repository at this point in the history
  • Loading branch information
willie-yao authored and sbueringer committed Sep 21, 2023
1 parent 1cd6b2a commit b5d2e25
Show file tree
Hide file tree
Showing 4 changed files with 1,002 additions and 18 deletions.
87 changes: 85 additions & 2 deletions internal/controllers/topology/cluster/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func TestGetBlueprint(t *testing.T) {
builder.GenericInfrastructureClusterTemplateCRD,
builder.GenericInfrastructureMachineTemplateCRD,
builder.GenericInfrastructureMachineCRD,
builder.GenericInfrastructureMachinePoolTemplateCRD,
builder.GenericInfrastructureMachinePoolCRD,
builder.GenericControlPlaneTemplateCRD,
builder.GenericBootstrapConfigTemplateCRD,
}
Expand All @@ -56,6 +58,8 @@ func TestGetBlueprint(t *testing.T) {

workerInfrastructureMachineTemplate := builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "workerinframachinetemplate1").
Build()
workerInfrastructureMachinePoolTemplate := builder.InfrastructureMachinePoolTemplate(metav1.NamespaceDefault, "workerinframachinepooltemplate1").
Build()
workerBootstrapTemplate := builder.BootstrapTemplate(metav1.NamespaceDefault, "workerbootstraptemplate1").
Build()
machineHealthCheck := &clusterv1.MachineHealthCheckClass{
Expand All @@ -73,6 +77,14 @@ func TestGetBlueprint(t *testing.T) {

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

machinePools := builder.MachinePoolClass("workerclass2").
WithLabels(map[string]string{"foo": "bar"}).
WithAnnotations(map[string]string{"a": "b"}).
WithInfrastructureTemplate(workerInfrastructureMachinePoolTemplate).
WithBootstrapTemplate(workerBootstrapTemplate).
Build()
mps := []clusterv1.MachinePoolClass{*machinePools}

// Define test cases.
tests := []struct {
name string
Expand Down Expand Up @@ -141,6 +153,7 @@ func TestGetBlueprint(t *testing.T) {
Template: controlPlaneTemplate,
},
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
MachinePools: map[string]*scope.MachinePoolBlueprint{},
},
},
{
Expand All @@ -167,6 +180,7 @@ func TestGetBlueprint(t *testing.T) {
InfrastructureMachineTemplate: controlPlaneInfrastructureMachineTemplate,
},
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
MachinePools: map[string]*scope.MachinePoolBlueprint{},
},
},
{
Expand Down Expand Up @@ -217,10 +231,11 @@ func TestGetBlueprint(t *testing.T) {
MachineHealthCheck: machineHealthCheck,
},
},
MachinePools: map[string]*scope.MachinePoolBlueprint{},
},
},
{
name: "Fails if ClusterClass has a MachineDeploymentClass referencing a BootstrapTemplate that does not exist",
name: "Fails if ClusterClass has a MachineDeploymentClass referencing a BootstrapConfigTemplate that does not exist",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(infraClusterTemplate).
WithControlPlaneTemplate(controlPlaneTemplate).
Expand Down Expand Up @@ -276,8 +291,75 @@ func TestGetBlueprint(t *testing.T) {
MachineHealthCheck: machineHealthCheck,
},
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
MachinePools: map[string]*scope.MachinePoolBlueprint{},
},
},
{
name: "Should read a ClusterClass with a MachinePoolClass",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(infraClusterTemplate).
WithControlPlaneTemplate(controlPlaneTemplate).
WithWorkerMachinePoolClasses(mps...).
Build(),
objects: []client.Object{
infraClusterTemplate,
controlPlaneTemplate,
workerInfrastructureMachinePoolTemplate,
workerBootstrapTemplate,
},
want: &scope.ClusterBlueprint{
ClusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(infraClusterTemplate).
WithControlPlaneTemplate(controlPlaneTemplate).
WithWorkerMachinePoolClasses(mps...).
Build(),
InfrastructureClusterTemplate: infraClusterTemplate,
ControlPlane: &scope.ControlPlaneBlueprint{
Template: controlPlaneTemplate,
},
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
MachinePools: map[string]*scope.MachinePoolBlueprint{
"workerclass2": {
Metadata: clusterv1.ObjectMeta{
Labels: map[string]string{"foo": "bar"},
Annotations: map[string]string{"a": "b"},
},
InfrastructureMachinePoolTemplate: workerInfrastructureMachinePoolTemplate,
BootstrapTemplate: workerBootstrapTemplate,
},
},
},
},
{
name: "Fails if ClusterClass has a MachinePoolClass referencing a BootstrapConfigTemplate that does not exist",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(infraClusterTemplate).
WithControlPlaneTemplate(controlPlaneTemplate).
WithWorkerMachinePoolClasses(mps...).
Build(),
objects: []client.Object{
infraClusterTemplate,
controlPlaneTemplate,
workerInfrastructureMachinePoolTemplate,
// workerBootstrapTemplate is missing!
},
wantErr: true,
},
{
name: "Fails if ClusterClass has a MachinePoolClass referencing a InfrastructureMachinePoolTemplate that does not exist",
clusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(infraClusterTemplate).
WithControlPlaneTemplate(controlPlaneTemplate).
WithWorkerMachinePoolClasses(mps...).
Build(),
objects: []client.Object{
infraClusterTemplate,
controlPlaneTemplate,
workerBootstrapTemplate,
// workerInfrastructureMachinePoolTemplate is missing!
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -333,7 +415,8 @@ func TestGetBlueprint(t *testing.T) {
g.Expect(tt.want.ClusterClass).To(EqualObject(got.ClusterClass, IgnoreAutogeneratedMetadata))
g.Expect(tt.want.InfrastructureClusterTemplate).To(EqualObject(got.InfrastructureClusterTemplate), cmp.Diff(got.InfrastructureClusterTemplate, tt.want.InfrastructureClusterTemplate))
g.Expect(got.ControlPlane).To(BeComparableTo(tt.want.ControlPlane), cmp.Diff(got.ControlPlane, tt.want.ControlPlane))
g.Expect(tt.want.MachineDeployments).To(BeComparableTo(got.MachineDeployments), got.MachineDeployments, tt.want.MachineDeployments)
g.Expect(tt.want.MachineDeployments).To(BeComparableTo(got.MachineDeployments), cmp.Diff(got.MachineDeployments, tt.want.MachineDeployments))
g.Expect(tt.want.MachinePools).To(BeComparableTo(got.MachinePools), cmp.Diff(got.MachinePools, tt.want.MachinePools))
})
}
}
Loading

0 comments on commit b5d2e25

Please sign in to comment.