Skip to content

Add unit tests for RuntimeReconciler lifecycle logic#5658

Open
mrhapile wants to merge 1 commit intofluid-cloudnative:masterfrom
mrhapile:UnitTest-for-criticalRuntime
Open

Add unit tests for RuntimeReconciler lifecycle logic#5658
mrhapile wants to merge 1 commit intofluid-cloudnative:masterfrom
mrhapile:UnitTest-for-criticalRuntime

Conversation

@mrhapile
Copy link

@mrhapile mrhapile commented Feb 3, 2026

Fixes #5657

This PR adds comprehensive unit test coverage for the core RuntimeReconciler
logic in pkg/controllers/runtime_controller.go.

What this PR does

  • Introduces pkg/controllers/runtime_controller_test.go
  • Adds controller-level unit tests using controller-runtime fake client
  • Validates critical runtime lifecycle behavior without modifying production logic

Coverage added

  • OwnerReference creation linking Runtime → Dataset
  • Finalizer addition during normal reconciliation
  • Finalizer removal and cleanup during deletion
  • Happy-path reconciliation (Setup / Sync)
  • Error propagation when runtime engine initialization fails

Implementation details

  • Uses controller-runtime/pkg/client/fake with status subresource enabled
  • Tests the abstract RuntimeReconciler base logic in isolation via a mock engine
  • Avoids gomonkey in favor of interfaces and dependency injection
  • Tests are deterministic and CI-safe

Verification

go test -v ./pkg/controllers/

Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
Copilot AI review requested due to automatic review settings February 3, 2026 11:15
@fluid-e2e-bot
Copy link

fluid-e2e-bot bot commented Feb 3, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign ronggu for approval by writing /assign @ronggu in a comment. For more information see:The Kubernetes Code Review Process.

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

Details 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

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @mrhapile, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness of the RuntimeReconciler by introducing a new suite of unit tests. These tests cover critical lifecycle operations, ensuring correct behavior during the creation, reconciliation, and deletion of runtime resources. The focus is on validating the core logic of the reconciler in isolation, using mock implementations to achieve deterministic and reliable testing.

Highlights

  • New Unit Test File: A new unit test file, pkg/controllers/runtime_controller_test.go, has been added to provide comprehensive test coverage for the RuntimeReconciler.
  • Comprehensive Lifecycle Testing: The new tests validate critical aspects of the RuntimeReconciler's lifecycle, including OwnerReference creation, Finalizer addition and removal during deletion, happy-path reconciliation (Setup/Sync), and error propagation during runtime engine initialization.
  • Isolated Testing Approach: The tests utilize controller-runtime/pkg/client/fake with status subresource enabled and employ mock engine implementations to test the abstract RuntimeReconciler base logic in isolation, avoiding external dependencies like gomonkey.
  • Deterministic and CI-Safe: The implementation ensures that the tests are deterministic and suitable for continuous integration environments.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • pkg/controllers/runtime_controller_test.go
    • Added new unit tests for the RuntimeReconciler's lifecycle logic.
    • Introduced mockEngine and mockRuntimeReconciler for isolated testing of the reconciler's core functionalities.
    • Implemented tests for OwnerReference management, Finalizer handling, happy-path reconciliation, engine creation error handling, and deletion cleanup.
Activity
  • The pull request was created by mrhapile with a detailed description outlining the purpose, scope, and implementation of the new unit tests. No further activity (comments, reviews) is provided in the context.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@fluid-e2e-bot
Copy link

fluid-e2e-bot bot commented Feb 3, 2026

Hi @mrhapile. Thanks for your PR.

I'm waiting for a fluid-cloudnative 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.

Details

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/test-infra repository.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 3, 2026

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces comprehensive unit tests for the RuntimeReconciler's lifecycle logic, covering owner reference creation, finalizer management, happy-path reconciliation, and error propagation. The tests are well-structured using mock engines and controller-runtime's fake client, which is a good approach for isolating the logic under test. Overall, this is a valuable addition to the codebase, significantly improving test coverage for critical controller functionality. I've identified a couple of areas where the test assertions could be made more robust to ensure they accurately reflect the expected behavior.

Comment on lines +375 to +377
if err.Error() != "Failed to create: induced engine creation failure" && err.Error() != "induced engine creation failure" {
t.Logf("Got expected error: %v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The error message assertion logic here is inverted and could lead to false positives. The if condition currently logs a message if the error is not one of the expected strings, implying it's an expected error. Instead, the test should explicitly t.Fatalf if the error message does not match the expected value. Given that errors.Wrap is used in the actual code, the error message will be prefixed. A precise check for the full expected error string would make this test more robust.

Suggested change
if err.Error() != "Failed to create: induced engine creation failure" && err.Error() != "induced engine creation failure" {
t.Logf("Got expected error: %v", err)
}
if err == nil {
t.Fatalf("Expected error from ReconcileInternal due to engine failure, got nil")
}
expectedError := "Failed to create: induced engine creation failure"
if err.Error() != expectedError {
t.Fatalf("Expected error message \"%s\", got \"%v\"", expectedError, err)
}

Comment on lines +326 to +328
if result.Requeue && result.RequeueAfter == 0 {
t.Errorf("Did not expect immediate Requeue for successful reconcile")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The current check if result.Requeue && result.RequeueAfter == 0 only catches a specific case of immediate requeue. To fully verify the utils.NoRequeue() semantics, which implies no requeue at all, it's better to explicitly check that both Requeue is false and RequeueAfter is zero. This ensures that the test fails if any form of requeue (immediate or delayed) is unexpectedly requested.

Suggested change
if result.Requeue && result.RequeueAfter == 0 {
t.Errorf("Did not expect immediate Requeue for successful reconcile")
}
if result.Requeue || result.RequeueAfter != 0 {
t.Errorf("Expected no requeue for successful reconcile, got %v", result)
}

Copy link
Contributor

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

Adds controller-level unit tests to cover core RuntimeReconciler lifecycle behavior (owner refs, finalizers, deletion cleanup, and engine errors) using a controller-runtime fake client and a mock engine.

Changes:

  • Introduces pkg/controllers/runtime_controller_test.go with a mock base.Engine and a harness around RuntimeReconciler.
  • Adds tests for OwnerReference creation, finalizer addition/removal, happy-path reconcile, and engine creation error propagation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +375 to +377
if err.Error() != "Failed to create: induced engine creation failure" && err.Error() != "induced engine creation failure" {
t.Logf("Got expected error: %v", err)
}
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

In TestReconcileInternal_EngineError the assertion for the returned error is inverted and never fails on unexpected errors. The current condition logs "Got expected error" only when the error string does NOT match either expected value, and it doesn't fail the test in that case. Please change this to positively assert the error message (preferably using a contains check for the induced message to avoid brittle full-string comparisons) and fail when it doesn't match.

Copilot uses AI. Check for mistakes.
Comment on lines +326 to +328
if result.Requeue && result.RequeueAfter == 0 {
t.Errorf("Did not expect immediate Requeue for successful reconcile")
}
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

TestReconcileInternal_ReconcileRuntime is currently non-deterministic / under-asserted because RuntimeReconciler.ReconcileRuntime uses utils.GenerateRandomRequeueDurationFromEnv(), which by default forces a requeue-after (90s) unless FLUID_RUNTIME_RECONCILE_DURATION is set to -1. The test only checks that there is no immediate requeue, so it won't catch regressions. Consider setting utils.RuntimeReconcileDurationEnvVal (and restoring it with t.Cleanup) so the reconciler returns NoRequeue deterministically, and assert the full ctrl.Result you expect.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing unit tests for RuntimeReconciler lifecycle logic

1 participant