Skip to content

Commit

Permalink
Merge pull request #10055 from willie-yao/cc-webhook-tests
Browse files Browse the repository at this point in the history
🌱 Add unit tests for MachinePools for webhooks and cc controller
  • Loading branch information
k8s-ci-robot authored Feb 15, 2024
2 parents d63021c + 5c0758a commit 22bf43a
Show file tree
Hide file tree
Showing 4 changed files with 1,176 additions and 13 deletions.
64 changes: 64 additions & 0 deletions internal/controllers/clusterclass/clusterclass_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestClusterClassReconciler_reconcile(t *testing.T) {
// InfraMachineTemplates for the workers and the control plane.
infraMachineTemplateControlPlane := builder.InfrastructureMachineTemplate(ns.Name, "inframachinetemplate-control-plane").Build()
infraMachineTemplateWorker := builder.InfrastructureMachineTemplate(ns.Name, "inframachinetemplate-worker").Build()
infraMachinePoolTemplateWorker := builder.InfrastructureMachinePoolTemplate(ns.Name, "inframachinepooltemplate-worker").Build()

// Control plane template.
controlPlaneTemplate := builder.ControlPlaneTemplate(ns.Name, "controlplanetemplate").Build()
Expand All @@ -82,12 +83,23 @@ func TestClusterClassReconciler_reconcile(t *testing.T) {
WithInfrastructureTemplate(infraMachineTemplateWorker).
Build()

// MachinePoolClasses that will be part of the ClusterClass.
machinePoolClass1 := builder.MachinePoolClass(workerClassName1).
WithBootstrapTemplate(bootstrapTemplate).
WithInfrastructureTemplate(infraMachinePoolTemplateWorker).
Build()
machinePoolClass2 := builder.MachinePoolClass(workerClassName2).
WithBootstrapTemplate(bootstrapTemplate).
WithInfrastructureTemplate(infraMachinePoolTemplateWorker).
Build()

// ClusterClass.
clusterClass := builder.ClusterClass(ns.Name, clusterClassName).
WithInfrastructureClusterTemplate(infraClusterTemplate).
WithControlPlaneTemplate(controlPlaneTemplate).
WithControlPlaneInfrastructureMachineTemplate(infraMachineTemplateControlPlane).
WithWorkerMachineDeploymentClasses(*machineDeploymentClass1, *machineDeploymentClass2).
WithWorkerMachinePoolClasses(*machinePoolClass1, *machinePoolClass2).
WithVariables(
clusterv1.ClusterClassVariable{
Name: "hdd",
Expand All @@ -112,6 +124,7 @@ func TestClusterClassReconciler_reconcile(t *testing.T) {
initObjs := []client.Object{
bootstrapTemplate,
infraMachineTemplateWorker,
infraMachinePoolTemplateWorker,
infraMachineTemplateControlPlane,
controlPlaneTemplate,
infraClusterTemplate,
Expand All @@ -137,6 +150,8 @@ func TestClusterClassReconciler_reconcile(t *testing.T) {

g.Expect(assertMachineDeploymentClasses(ctx, actualClusterClass, ns)).Should(Succeed())

g.Expect(assertMachinePoolClasses(ctx, actualClusterClass, ns)).Should(Succeed())

g.Expect(assertStatusVariables(actualClusterClass)).Should(Succeed())
return nil
}, timeout).Should(Succeed())
Expand Down Expand Up @@ -290,6 +305,55 @@ func assertMachineDeploymentClass(ctx context.Context, actualClusterClass *clust
builder.BootstrapGroupVersion)
}

func assertMachinePoolClasses(ctx context.Context, actualClusterClass *clusterv1.ClusterClass, ns *corev1.Namespace) error {
for _, mpClass := range actualClusterClass.Spec.Workers.MachinePools {
if err := assertMachinePoolClass(ctx, actualClusterClass, mpClass, ns); err != nil {
return err
}
}
return nil
}

func assertMachinePoolClass(ctx context.Context, actualClusterClass *clusterv1.ClusterClass, mpClass clusterv1.MachinePoolClass, ns *corev1.Namespace) error {
// Assert the infrastructure machinepool template in the MachinePoolClass has an owner reference to the ClusterClass.
actualInfrastructureMachinePoolTemplate := builder.InfrastructureMachinePoolTemplate("", "").Build()
actualInfrastructureMachinePoolTemplateKey := client.ObjectKey{
Namespace: ns.Name,
Name: mpClass.Template.Infrastructure.Ref.Name,
}
if err := env.Get(ctx, actualInfrastructureMachinePoolTemplateKey, actualInfrastructureMachinePoolTemplate); err != nil {
return err
}
if err := assertHasOwnerReference(actualInfrastructureMachinePoolTemplate, *ownerReferenceTo(actualClusterClass, clusterv1.GroupVersion.WithKind("ClusterClass"))); err != nil {
return err
}

// Assert the MachinePoolClass has the expected APIVersion and Kind to the infrastructure machinepool template
if err := referenceExistsWithCorrectKindAndAPIVersion(mpClass.Template.Infrastructure.Ref,
builder.GenericInfrastructureMachinePoolTemplateKind,
builder.InfrastructureGroupVersion); err != nil {
return err
}

// Assert the bootstrap template in the MachinePoolClass has an owner reference to the ClusterClass.
actualBootstrapTemplate := builder.BootstrapTemplate("", "").Build()
actualBootstrapTemplateKey := client.ObjectKey{
Namespace: ns.Name,
Name: mpClass.Template.Bootstrap.Ref.Name,
}
if err := env.Get(ctx, actualBootstrapTemplateKey, actualBootstrapTemplate); err != nil {
return err
}
if err := assertHasOwnerReference(actualBootstrapTemplate, *ownerReferenceTo(actualClusterClass, clusterv1.GroupVersion.WithKind("ClusterClass"))); err != nil {
return err
}

// Assert the MachinePoolClass has the expected APIVersion and Kind to the bootstrap template
return referenceExistsWithCorrectKindAndAPIVersion(mpClass.Template.Bootstrap.Ref,
builder.GenericBootstrapConfigTemplateKind,
builder.BootstrapGroupVersion)
}

func assertHasOwnerReference(obj client.Object, ownerRef metav1.OwnerReference) error {
found := false
for _, ref := range obj.GetOwnerReferences() {
Expand Down
Loading

0 comments on commit 22bf43a

Please sign in to comment.