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 tests for k0scontrolplane controller #806

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

apedriza
Copy link
Contributor

@apedriza apedriza commented Nov 11, 2024

These changes add unit and integration tests on the k0scontrolplane controller. They use envtest to spin-up to kube-apiserver+etcd without using a cluster. In some of these tests it is enough to use fakeclient for the creation of objects and subsequent state check but since for others it is necessary to use envtest (an apiserver is needed) and in order to reduce the different ways of creation of the environment I have chosen to use envtest for any test. For the test implementation I have taken inspiration from how upstream projects do this job.

The idea is, after agreeing on how we want this type of testing for the project, to add the rest of the tests for the other controllers.

More detailed testing of the machine reconciliation logic can be added but since it is undergoing changes I have decided to add only basic testing for the time being.

@apedriza apedriza requested a review from a team as a code owner November 11, 2024 17:03
@apedriza apedriza marked this pull request as draft November 11, 2024 17:04
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch from a402a53 to 2201865 Compare November 11, 2024 17:06
@apedriza apedriza changed the title WIP: Add envtest for testing controllers WIP: Add tests for k0scontrolplane controller Nov 12, 2024
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch 8 times, most recently from 8da9d1f to 6f6bee1 Compare November 18, 2024 19:19
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch 18 times, most recently from 2ecdcb8 to 6a21e3f Compare November 20, 2024 17:14
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch 11 times, most recently from 2ede89b to 53c8370 Compare November 21, 2024 11:22
},
Client: client.Options{
Cache: &client.CacheOptions{
DisableFor: []client.Object{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If caching is not disabled in these cases, testing might encounter errors due to delays in cache synchronization, as the cache may not immediately reflect the current state in the API server

@apedriza apedriza marked this pull request as ready for review November 21, 2024 11:49
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch 8 times, most recently from d8b18d3 to 717b8d8 Compare November 28, 2024 10:36
@apedriza apedriza requested a review from makhov November 28, 2024 18:05
@apedriza apedriza marked this pull request as draft November 29, 2024 14:23
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch from 717b8d8 to 51d1e3c Compare November 29, 2024 15:35
@apedriza apedriza marked this pull request as ready for review November 29, 2024 16:00
@apedriza
Copy link
Contributor Author

@makhov sorry for the noise. I didnt realize that unit/integration testing was failing due to new updates in machines reconciliation. Ready for review :)

)

func (c *K0sController) createMachine(ctx context.Context, name string, cluster *clusterv1.Cluster, kcp *cpv1beta1.K0sControlPlane, infraRef corev1.ObjectReference, failureDomain *string) (*clusterv1.Machine, error) {
machine, err := c.generateMachine(ctx, name, cluster, kcp, infraRef, failureDomain)
if err != nil {
return nil, fmt.Errorf("error generating machine: %w", err)
}
_ = ctrl.SetControllerReference(kcp, machine, c.Scheme)
_ = ctrl.SetControllerReference(kcp, machine, c.Client.Scheme())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why do we change it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writing tests for the controller I saw that we access the schema at this point and I thought about refactoring it using the client schema directly and remove K0scontroller.Scheme field. Both are obtained from manager.Manager so I thought it was safe to make this change

@@ -210,7 +211,11 @@ func (c *K0sController) generateMachineFromTemplate(ctx context.Context, name st
return nil, err
}

_ = ctrl.SetControllerReference(kcp, unstructuredMachineTemplate, c.Scheme)
_ = ctrl.SetControllerReference(cluster, unstructuredMachineTemplate, c.Client.Scheme())
err = c.Client.Patch(ctx, unstructuredMachineTemplate, client.Merge, &client.PatchOptions{FieldManager: "k0smotron"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you patch it here? We apply patches in createMachineFromTemplate func

Copy link
Contributor Author

@apedriza apedriza Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently we patch InfraMachine but not InfraMachineTemplate. The current SetControllerReference in (Infra)MachineTemplate is not patched. If I dont patch this ownership, test will fail when checking ownership:

g.Expect(gmt.GetOwnerReferences()).To(ContainElement(metav1.OwnerReference{
		APIVersion:         clusterv1.GroupVersion.String(),
		Kind:               "Cluster",
		Name:               cluster.Name,
		Controller:         ptr.To(true),
		BlockOwnerDeletion: ptr.To(true),
		UID:                cluster.UID,
	}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants