-
Couldn't load subscription status.
- Fork 1.6k
🌱 Refactor CronJob controller tests: utils extraction, env isolation for easier test scenario extension #5141
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
base: master
Are you sure you want to change the base?
🌱 Refactor CronJob controller tests: utils extraction, env isolation for easier test scenario extension #5141
Conversation
…structure optimization
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: rayowang 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 |
|
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 Once the patch is verified, the new status will be reflected by the 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. |
docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go
Show resolved
Hide resolved
docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go
Show resolved
Hide resolved
There was a problem hiding this 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.
docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go
Outdated
Show resolved
Hide resolved
docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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:
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:
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
…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.