Skip to content

Conversation

@rayowang
Copy link
Contributor

…structure optimization

Fixes #4140

Refactor CronJob controller tests by extracting reusable utility functions, isolating test environments via unique namespaces and lifecycle hooks, and optimizing test structure (splitting monolithic cases, unifying timeout/interval params, aligning with Kubebuilder best practices). These improvements streamline test maintenance and lay a clear, scalable foundation for easily adding more test scenarios (e.g., edge cases, feature-specific tests) in subsequent iterations, while fully preserving the original core test logic and functionality.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: rayowang
Once this PR has been reviewed and has the lgtm label, please assign kavinjsir for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 23, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @rayowang. Thanks for your PR.

I'm waiting for a github.com member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the CronJob controller tests to improve maintainability and support easier extension of test scenarios. It extracts reusable utility functions for common test operations, implements per-test namespace isolation to prevent test interference, and restructures the monolithic test case into focused, independently executable tests.

Key changes include:

  • Extraction of helper functions (assertCondition, randomString, createTestCronJob, createOwnedJob) to reduce code duplication
  • Implementation of unique namespace generation and BeforeEach/AfterEach lifecycle hooks for test isolation
  • Restructuring from a single large test case into smaller, focused Context blocks

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

The code under book/src contains sample files used in the tutorials.
All content within /* ... */ blocks represents text used for the documentation. Cannot be removed, you can see the preview of the docs with the ci:

Screenshot 2025-10-23 at 11 52 31

The goal here of those tutorials are to ensure the test structure remains as similar as possible to what we scaffold for the deploy image.
You can refer to this example for reference:

https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-plugins/internal/controller/busybox_controller_test.go

However, in this case, we’ll need to adapt it to use a CronJob instead of BusyBox.

Additionally, note that the files under this path are auto-generated via make generate-docs.

Therefore, any required modifications should be made in the hack/generate scripts.
For more context, see this related discussion: #5024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review and Improve Test Coverage and Alignment for Controller Tests in CronJob Tutorial

3 participants