Skip to content

test(dataset): add Ginkgo unit coverage for dataset controller - #5827

Merged
cheyang merged 3 commits into
fluid-cloudnative:masterfrom
hxrshxz:test/dataset-ut-week10-ginkgo
Jul 20, 2026
Merged

test(dataset): add Ginkgo unit coverage for dataset controller#5827
cheyang merged 3 commits into
fluid-cloudnative:masterfrom
hxrshxz:test/dataset-ut-week10-ginkgo

Conversation

@hxrshxz

@hxrshxz hxrshxz commented May 3, 2026

Copy link
Copy Markdown
Contributor

I. Describe what this PR does

Add package-scoped Ginkgo v2 + Gomega unit coverage for the dataset controller and replace the dataset package’s legacy envtest-heavy suite with a minimal package bootstrap.

II. Does this pull request fix one issue?

#5676

III. List the added test cases (unit test/integration test) if any, please explain if no tests are needed.

Adds dataset controller unit tests covering not-found reconcile, invalid name validation, finalizer add/requeue, phase initialization, scaleout requeue behavior, deletion/finalizer removal, datasetRef pruning, and reference dataset runtime creation.

IV. Describe how to verify it

Run the dataset controller package tests and confirm the package coverage stays above 75 percent.

V. Special notes for reviews

N/A

Copilot AI review requested due to automatic review settings May 3, 2026 10:33
@fluid-e2e-bot

fluid-e2e-bot Bot commented May 3, 2026

Copy link
Copy Markdown

Hi @hxrshxz. 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.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a comprehensive unit test suite for the Dataset controller, replacing the previous integration tests. The new tests cover core reconciliation logic, including finalizer handling and dataset deletion. Feedback focuses on improving test reliability and maintainability by suggesting dependency injection over monkey patching to avoid race conditions, using context.Background() instead of context.TODO(), strengthening error assertions for DNS validation, and ensuring deterministic checks for finalizer removal in the fake client.

Comment on lines +50 to +53
scaleoutPatch = gomonkey.ApplyFunc(deploy.ScaleoutRuntimeControllerOnDemand,
func(client.Client, types.NamespacedName, logr.Logger) (string, bool, error) {
return "", false, nil
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using gomonkey to patch global functions can lead to race conditions and flaky tests if they are executed in parallel (e.g., using ginkgo -p). It also requires disabling compiler inlining to work reliably. A more robust approach for long-term maintainability would be to refactor DatasetReconciler to use dependency injection (e.g., an interface) for the scale-out logic.

Recorder: record.NewFakeRecorder(16),
}

result, err := r.Reconcile(context.TODO(), ctrl.Request{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In unit tests, context.Background() is generally preferred over context.TODO(). context.TODO() is intended for use when it's unclear which context to use, whereas tests should typically use a clean background context. This applies to multiple locations throughout this file.

Suggested change
result, err := r.Reconcile(context.TODO(), ctrl.Request{
result, err := r.Reconcile(context.Background(), ctrl.Request{

NamespacedName: types.NamespacedName{Name: invalidName, Namespace: "default"},
})

Expect(err).To(HaveOccurred())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

It is better to verify the specific error returned to ensure the test fails for the expected reason (DNS-1035 validation) rather than any other potential error during reconciliation.

Suggested change
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(ContainSubstring("metadata.name")))

Comment on lines +315 to +320
err = r.Get(context.TODO(), types.NamespacedName{Name: storedDataset.Name, Namespace: storedDataset.Namespace}, updatedDataset)
if err == nil {
Expect(updatedDataset.GetFinalizers()).NotTo(ContainElement(finalizer))
} else {
Expect(client.IgnoreNotFound(err)).To(BeNil())
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This assertion is non-deterministic. Since the fake client typically does not simulate the asynchronous deletion logic of the Kubernetes API server (where objects are removed after finalizers are cleared), you should explicitly assert the expected state in the fake client's cache. If the object is expected to persist without the finalizer, assert that err is nil and the finalizer is absent.

			Expect(r.Get(context.Background(), types.NamespacedName{Name: storedDataset.Name, Namespace: storedDataset.Namespace}, updatedDataset)).To(Succeed())
			Expect(updatedDataset.GetFinalizers()).NotTo(ContainElement(finalizer))

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 migrates the dataset controller package toward the repository’s newer Ginkgo/Gomega unit-testing approach as part of the broader testing modernization work in #5676. It removes the package’s old envtest-heavy suite and replaces it with focused unit coverage around DatasetReconciler behavior.

Changes:

  • Replace the dataset package’s envtest-based suite_test.go with a minimal Ginkgo suite bootstrap.
  • Add package-scoped unit tests for dataset reconcile paths such as not-found handling, name validation, finalizer behavior, phase initialization, deletion flows, datasetRef pruning, and reference-dataset runtime creation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
pkg/controllers/v1alpha1/dataset/suite_test.go Simplifies the package test bootstrap to a minimal Ginkgo suite runner.
pkg/controllers/v1alpha1/dataset/dataset_controller_ut_test.go Adds new unit tests covering key DatasetReconciler helper and reconcile scenarios.

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

DeletionTimestamp: &deletionTime,
},
Status: datav1alpha1.DatasetStatus{
DatasetRef: []string{"consumer-a"},
Comment on lines +307 to +308
Context: context.TODO(),
Dataset: currentDataset,
Comment on lines +382 to +383
Context: context.TODO(),
Dataset: currentDataset,
@codecov

codecov Bot commented May 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.29%. Comparing base (d14dcc7) to head (70a1c3a).
⚠️ Report is 141 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5827      +/-   ##
==========================================
+ Coverage   59.10%   66.29%   +7.19%     
==========================================
  Files         480      490      +10     
  Lines       32512    35812    +3300     
==========================================
+ Hits        19215    23742    +4527     
+ Misses      11747    10330    -1417     
- Partials     1550     1740     +190     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cheyang cheyang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

感谢贡献!建议:

  1. AfterEach 只重置 scaleoutPatch,但部分 It 内还创建了其他 gomonkey patches(如 kubeclient 相关)。请确认所有 patches 都在对应 It 内用 defer Reset() 清理,或在 AfterEach 中统一管理。
  2. 建议补充 Reconcile 中 dataset found 但 ScaleoutRuntimeControllerOnDemand 返回错误时的场景覆盖。

cheyang

This comment was marked as duplicate.

@cheyang

This comment has been minimized.

@cheyang

cheyang commented May 8, 2026

Copy link
Copy Markdown
Collaborator

English translation of the previous review comments:

  1. AfterEach only resets scaleoutPatch, but some It blocks also create other gomonkey patches (e.g., kubeclient-related). Please ensure all patches are cleaned up within the corresponding It using defer Reset(), or manage them uniformly in AfterEach.
  2. Consider adding coverage for the scenario where the dataset is found but ScaleoutRuntimeControllerOnDemand returns an error.

@cheyang

cheyang commented May 8, 2026

Copy link
Copy Markdown
Collaborator

This PR has merge conflicts with the base branch. Please rebase your branch onto the latest master and resolve the conflicts so we can proceed with the review.

hxrshxz added 3 commits May 8, 2026 23:38
Signed-off-by: Harsh <harshmastic@gmail.com>
Signed-off-by: Harsh <harshmastic@gmail.com>
Signed-off-by: Harsh <harshmastic@gmail.com>
@hxrshxz
hxrshxz force-pushed the test/dataset-ut-week10-ginkgo branch from 221f3b6 to 70a1c3a Compare May 8, 2026 18:12
@sonarqubecloud

sonarqubecloud Bot commented May 8, 2026

Copy link
Copy Markdown

@hxrshxz

hxrshxz commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

Tightened the patch cleanup so non-scaleoutPatch gomonkey patches are reset in their owning specs, avoiding residual state between It blocks.

I also added coverage for the Reconcile path where the dataset is found but ScaleoutRuntimeControllerOnDemand returns an error. The branch has also been rebased and the merge conflicts are resolved.

@cheyang

cheyang commented May 18, 2026

Copy link
Copy Markdown
Collaborator

/ok-to-test

@cheyang

cheyang commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

Solid controller unit test coverage. Tests cover not-found, DNS-1035 validation, finalizer lifecycle, phase initialization, scaleout requeue, deletion with finalizer removal, datasetRef pruning, and reference dataset runtime creation. Good use of gomonkey for isolating ScaleoutRuntimeControllerOnDemand. The suite_test.go simplification from envtest to fake-client is a welcome improvement for test speed. LGTM.

@fluid-e2e-bot

fluid-e2e-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

@cheyang: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test fluid-e2e
  • /test fluid-e2e-nightly
  • /test fluid-e2e-release
Details

In response to this:

/test kind-e2e-test (v1.24.17)

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.

@cheyang cheyang closed this Jul 14, 2026
@cheyang cheyang reopened this Jul 14, 2026
@sonarqubecloud

Copy link
Copy Markdown

@cheyang cheyang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

/lgtm
/approve

@fluid-e2e-bot

fluid-e2e-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cheyang

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

The pull request process is described 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

@cheyang
cheyang merged commit 8e00343 into fluid-cloudnative:master Jul 20, 2026
38 of 40 checks passed
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.

3 participants