-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(webhook-controller): add Ginkgo/Gomega unit tests #5742
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| Copyright 2026 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package webhook | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| // These tests use Ginkgo (BDD-style Go testing framework). Refer to | ||
| // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. | ||
|
|
||
| func TestWebhook(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Webhook Controller Suite") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| Copyright 2026 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package webhook | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "github.com/agiledragon/gomonkey/v2" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
|
|
||
| "github.com/fluid-cloudnative/fluid/pkg/webhook" | ||
| ) | ||
|
|
||
| var _ = Describe("WebhookReconciler", func() { | ||
| const ( | ||
| webhookName = "fluid-webhook" | ||
| ) | ||
|
|
||
| var ( | ||
| caCert = []byte("test-ca-cert") | ||
| ) | ||
|
|
||
| Describe("Reconcile", func() { | ||
| var ( | ||
| reconciler *WebhookReconciler | ||
| certBuilder *webhook.CertificateBuilder | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| certBuilder = &webhook.CertificateBuilder{} | ||
| reconciler = &WebhookReconciler{ | ||
| CertBuilder: certBuilder, | ||
| WebhookName: webhookName, | ||
| CaCert: caCert, | ||
| } | ||
hxrshxz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
|
|
||
| Context("when PatchCABundle succeeds", func() { | ||
| It("should return no requeue and no error", func() { | ||
| 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 | ||
| }, | ||
| ) | ||
|
Comment on lines
+58
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While using For future improvements, consider defining an interface for the methods on // In an appropriate package, e.g., where WebhookReconciler is defined.
type CertificatePatcher interface {
PatchCABundle(webhookName string, ca []byte) error
}Then, // 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 |
||
| defer patch.Reset() | ||
|
|
||
| result, err := reconciler.Reconcile(context.Background(), ctrl.Request{}) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(result.Requeue).To(BeFalse()) | ||
| Expect(result.RequeueAfter).To(BeZero()) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when PatchCABundle fails", func() { | ||
| It("should requeue after 10 seconds and return no error", func() { | ||
| patch := gomonkey.ApplyMethod( | ||
| certBuilder, | ||
| "PatchCABundle", | ||
| func(_ *webhook.CertificateBuilder, _ string, _ []byte) error { | ||
| return errors.New("patch failed") | ||
| }, | ||
| ) | ||
| defer patch.Reset() | ||
|
|
||
| result, err := reconciler.Reconcile(context.Background(), ctrl.Request{}) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(result.RequeueAfter).To(Equal(10 * time.Second)) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.