Skip to content

Commit 54198dc

Browse files
authored
test(webhook/mutating): migrate tests to Ginkgo/Gomega (#5755)
* test(webhook/mutating): migrate tests to Ginkgo/Gomega Signed-off-by: Harsh <harshmastic@gmail.com> * fix(webhook/mutating): use GinkgoT().Setenv for env-var restoration Address Copilot and Gemini review findings: replace manual DeferCleanup(os.Unsetenv) with GinkgoT().Setenv() which automatically saves and restores the prior environment variable value after the spec. Signed-off-by: Harsh <harshmastic@gmail.com> * test(webhook/mutating): add webhook_test.go for HandlerMap coverage Signed-off-by: Harsh <harshmastic@gmail.com> * fix(webhook/mutating): remove brittle HaveLen assertion from HandlerMap test Signed-off-by: Harsh <harshmastic@gmail.com> * fix(webhook/mutating): assert handler is non-nil before type check Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent d6e3cc5 commit 54198dc

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

pkg/webhook/handler/mutating/mutating_handler_test.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/agiledragon/gomonkey/v2"
2424
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
2525
"github.com/fluid-cloudnative/fluid/pkg/common"
26-
"github.com/fluid-cloudnative/fluid/pkg/utils"
2726
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
2827
"github.com/fluid-cloudnative/fluid/pkg/webhook/plugins"
2928
. "github.com/onsi/ginkgo/v2"
@@ -1465,7 +1464,6 @@ var _ = Describe("Handle - Global Injection Disabled", func() {
14651464
var (
14661465
decoder *admission.Decoder
14671466
s *runtime.Scheme
1468-
patch *gomonkey.Patches
14691467
)
14701468

14711469
BeforeEach(func() {
@@ -1474,19 +1472,8 @@ var _ = Describe("Handle - Global Injection Disabled", func() {
14741472
Expect(corev1.AddToScheme(s)).To(Succeed())
14751473
})
14761474

1477-
AfterEach(func() {
1478-
if patch != nil {
1479-
patch.Reset()
1480-
}
1481-
})
1482-
14831475
It("should skip mutation when global injection is disabled", func() {
1484-
patch = gomonkey.ApplyFunc(utils.GetBoolValueFromEnv, func(key string, defaultValue bool) bool {
1485-
if key == common.EnvDisableInjection {
1486-
return true
1487-
}
1488-
return defaultValue
1489-
})
1476+
GinkgoT().Setenv(common.EnvDisableInjection, "true")
14901477

14911478
req := admission.Request{
14921479
AdmissionRequest: admissionv1.AdmissionRequest{

pkg/webhook/handler/mutating/mutating_suite_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
package mutating_test
1+
/*
2+
Copyright 2026 The Fluid Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mutating
218

319
import (
420
"testing"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2026 The Fluid Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mutating
18+
19+
import (
20+
"github.com/fluid-cloudnative/fluid/pkg/common"
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
var _ = Describe("HandlerMap", func() {
26+
It("should register a handler under WebhookSchedulePodPath", func() {
27+
Expect(HandlerMap).To(HaveKey(common.WebhookSchedulePodPath))
28+
})
29+
30+
It("should map WebhookSchedulePodPath to a *FluidMutatingHandler", func() {
31+
handler, ok := HandlerMap[common.WebhookSchedulePodPath]
32+
Expect(ok).To(BeTrue())
33+
Expect(handler).NotTo(BeNil())
34+
Expect(handler).To(BeAssignableToTypeOf(&FluidMutatingHandler{}))
35+
})
36+
})

0 commit comments

Comments
 (0)