Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add unit tests for CC MP blueprint, current_state, & desired_state #9348

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading