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 committed Sep 18, 2023
1 parent 1cd6b2a commit c4c58e5
Show file tree
Hide file tree
Showing 3 changed files with 977 additions and 12 deletions.
81 changes: 81 additions & 0 deletions internal/controllers/topology/cluster/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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 +75,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 +151,7 @@ func TestGetBlueprint(t *testing.T) {
Template: controlPlaneTemplate,
},
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
MachinePools: map[string]*scope.MachinePoolBlueprint{},
},
},
{
Expand All @@ -167,6 +178,7 @@ func TestGetBlueprint(t *testing.T) {
InfrastructureMachineTemplate: controlPlaneInfrastructureMachineTemplate,
},
MachineDeployments: map[string]*scope.MachineDeploymentBlueprint{},
MachinePools: map[string]*scope.MachinePoolBlueprint{},
},
},
{
Expand Down Expand Up @@ -217,6 +229,7 @@ func TestGetBlueprint(t *testing.T) {
MachineHealthCheck: machineHealthCheck,
},
},
MachinePools: map[string]*scope.MachinePoolBlueprint{},
},
},
{
Expand Down Expand Up @@ -276,8 +289,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 BootstrapConfig 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 @@ -334,6 +414,7 @@ func TestGetBlueprint(t *testing.T) {
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.MachinePools).To(BeComparableTo(got.MachinePools), got.MachinePools, tt.want.MachinePools)
})
}
}
Loading

0 comments on commit c4c58e5

Please sign in to comment.