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

Merged

Conversation

apedriza
Copy link
Contributor

@apedriza apedriza commented Nov 11, 2024

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.

  • updates docs about testing and moving some line to the proper place

@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 from 51d1e3c to e845357 Compare January 13, 2025 16:22
@apedriza apedriza requested a review from makhov January 13, 2025 17:07
@apedriza apedriza marked this pull request as draft January 24, 2025 11:52
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch 18 times, most recently from df4b917 to a7e37a6 Compare January 27, 2025 11:52
Signed-off-by: Adrian Pedriza <adripedriza@gmail.com>
@apedriza apedriza force-pushed the add-envtest-for-controllers-testing branch from a7e37a6 to f9d3211 Compare January 27, 2025 12:01
@apedriza apedriza marked this pull request as ready for review January 27, 2025 12:48
@apedriza
Copy link
Contributor Author

Ready for review, also I added some docs changes about testing


3. Check documentation.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think all this should be in contribute workflow instead testing so I moved it there


// if it is necessary to reduce the number of replicas even counting the replicas to be eliminated
// because they are outdated, we choose the oldest among the valid ones.
if activeMachines.Len() > int(kcp.Spec.Replicas)+len(machineNamesToDelete) && len(desiredMachineNamesSlice) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you elaborate a bit on why the logic was changed? The result looks more or less the same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, so basically I found a bug in the logic when running the proposed test TestReconcileMachinesSyncOldMachines. Bug comes from two places: logic calculating machinesTodDelete var + new machine name calculation.

Reproducible when:

  • desired kcp replicas = 3
  • current desired replicas: <machine-name>-1
  • outdated replicas: <machine-name>-0 <machine-name>-2

👉 You can repro it by running new test TestReconcileMachinesSyncOldMachines with the current reconcileMachines func.

In this case, desired machine (<machine-name>-1) is added to machineToDelete variable even others machines which doesn't has same version should be delated first. In this case, <machine-name>-1 is added becauseactiveMachines.Len() > int(kcp.Spec.Replicas)+len(machineNamesToDelete) is true but others machines not updated should be removed first so condition activeMachines.Len() > int(kcp.Spec.Replicas)+len(machineNamesToDelete) has been moved out of the loop to be sure a desired machine is not added until all others machine that could be added to machineToDelete are checked.

In this case described above, machineName func was calculating a bad machine name, facing an infinite loop an returning a machine name which was already created:

  • machines to delete: <machine-name>-1, <machine-name>-2
  • desired machines: <machine-name>-4, <machine-name>-5
  • output: <machine-name>-5 ❌ (is already desired)

but I think all this comes from the error adding the wrong machine into machinesToDelete so the fix is there and not in the naming logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great catch!

@apedriza apedriza merged commit 3643037 into k0sproject:main Jan 28, 2025
42 checks passed
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