Skip to content

test(webhook-controller): add Ginkgo/Gomega unit tests#5742

Open
hxrshxz wants to merge 1 commit intofluid-cloudnative:masterfrom
hxrshxz:fluid-pr/webhook-v1alpha1
Open

test(webhook-controller): add Ginkgo/Gomega unit tests#5742
hxrshxz wants to merge 1 commit intofluid-cloudnative:masterfrom
hxrshxz:fluid-pr/webhook-v1alpha1

Conversation

@hxrshxz
Copy link
Copy Markdown
Contributor

@hxrshxz hxrshxz commented Mar 29, 2026

Ⅰ. Describe what this PR does

Add Ginkgo/Gomega unit tests for WebhookReconciler.Reconcile covering the success and error requeue paths.

Ⅱ. Does this pull request fix one issue?

#5676

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

  • Add pkg/controllers/v1alpha1/webhook/suite_test.go for package-level Ginkgo bootstrap.
  • Add pkg/controllers/v1alpha1/webhook/webhook_controller_test.go with success and error-path specs for Reconcile.
  • Measure package coverage at 100.0% for pkg/controllers/v1alpha1/webhook.

Ⅳ. Describe how to verify it

go test -v -coverprofile=/tmp/fluid-webhook.cover ./pkg/controllers/v1alpha1/webhook/... -count=1
go tool cover -func=/tmp/fluid-webhook.cover

Ⅴ. Special notes for reviews

N/A

Signed-off-by: Harsh <harshmastic@gmail.com>
Copilot AI review requested due to automatic review settings March 29, 2026 10:13
@fluid-e2e-bot
Copy link
Copy Markdown

fluid-e2e-bot bot commented Mar 29, 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 yangyuliufeng for approval by writing /assign @yangyuliufeng 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

@fluid-e2e-bot
Copy link
Copy Markdown

fluid-e2e-bot bot commented Mar 29, 2026

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.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
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

This pull request introduces unit tests for the WebhookReconciler using the Ginkgo and Gomega frameworks, including a test suite setup and specific test cases for the Reconcile method. The tests cover both successful and failed CA bundle patching scenarios using gomonkey for mocking. Feedback suggests refactoring the WebhookReconciler to use an interface for dependency injection instead of runtime patching to improve test robustness and safety.

Comment on lines +58 to +66
patch := gomonkey.ApplyMethod(
certBuilder,
"PatchCABundle",
func(_ *webhook.CertificateBuilder, name string, ca []byte) error {
Expect(name).To(Equal(webhookName))
Expect(ca).To(Equal(caCert))
return 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

While using gomonkey works for mocking, it's generally better to use interfaces for dependency injection to make testing easier and safer. This avoids runtime patching, which can be brittle and is based on unsafe operations.

For future improvements, consider defining an interface for the methods on CertificateBuilder that WebhookReconciler uses:

// In an appropriate package, e.g., where WebhookReconciler is defined.
type CertificatePatcher interface {
    PatchCABundle(webhookName string, ca []byte) error
}

Then, WebhookReconciler could be changed to hold a CertificatePatcher interface instead of a concrete *webhook.CertificateBuilder.

// In pkg/controllers/v1alpha1/webhook/webhook_controller.go
type WebhookReconciler struct {
	CertBuilder CertificatePatcher // Changed from *webhook.CertificateBuilder
	WebhookName string
	CaCert      []byte
}

In your tests, you could then provide a simple mock implementation of this interface without needing gomonkey. This would make the tests more robust and easier to understand.

@hxrshxz
Copy link
Copy Markdown
Contributor Author

hxrshxz commented Mar 29, 2026

/gemini review

1 similar comment
@hxrshxz
Copy link
Copy Markdown
Contributor Author

hxrshxz commented Mar 29, 2026

/gemini review

Copy link
Copy Markdown
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 Ginkgo/Gomega unit tests for the pkg/controllers/v1alpha1/webhook controller package, specifically validating WebhookReconciler.Reconcile behavior for both success and requeue-on-error paths.

Changes:

  • Added a Ginkgo suite bootstrap for the webhook controller package.
  • Added WebhookReconciler.Reconcile specs covering PatchCABundle success and failure (requeue-after) scenarios.

Reviewed changes

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

File Description
pkg/controllers/v1alpha1/webhook/suite_test.go Adds Ginkgo suite bootstrap for the webhook controller package.
pkg/controllers/v1alpha1/webhook/webhook_controller_test.go Adds unit specs for Reconcile using gomonkey to stub PatchCABundle.

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

Copy link
Copy Markdown
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

This pull request introduces unit tests for the WebhookReconciler using the Ginkgo and Gomega frameworks. It includes a test suite setup and specific test cases for the Reconcile method, utilizing gomonkey to mock the PatchCABundle functionality. Feedback was provided to use the webhook.NewCertificateBuilder constructor instead of an empty struct literal to ensure proper initialization and improve test robustness.

Copy link
Copy Markdown
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

This pull request introduces a new test suite and unit tests for the WebhookReconciler to verify the CA bundle patching logic. Feedback was provided to improve the robustness of the test setup by initializing the CertificateBuilder with a fake Kubernetes client instead of an empty struct to avoid potential nil pointer panics if mocks are modified in the future.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.22%. Comparing base (bdc4ab0) to head (042cf52).
⚠️ Report is 18 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5742   +/-   ##
=======================================
  Coverage   61.22%   61.22%           
=======================================
  Files         444      444           
  Lines       30557    30557           
=======================================
  Hits        18710    18710           
  Misses      10307    10307           
  Partials     1540     1540           

☔ View full report in Codecov by Sentry.
📢 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.

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.

2 participants