Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/controllers/v1alpha1/webhook/suite_test.go
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")
}
93 changes: 93 additions & 0 deletions pkg/controllers/v1alpha1/webhook/webhook_controller_test.go
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,
}
})

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
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.

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))
})
})
})
})
Loading