Skip to content

Commit c3c78cc

Browse files
committed
test(webhook): add Ginkgo/Gomega tests for MountPropagationInjector
Signed-off-by: Popie52 <popiesailor@gmail.com>
1 parent db0722d commit c3c78cc

2 files changed

Lines changed: 75 additions & 47 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package mountpropagationinjector
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestMountPropagationInjector(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "MountPropagationInjector Suite")
13+
}
Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,86 @@
11
/*
2-
32
Licensed under the Apache License, Version 2.0 (the "License");
43
you may not use this file except in compliance with the License.
54
You may obtain a copy of the License at
65
7-
http://www.apache.org/licenses/LICENSE-2.0
6+
http://www.apache.org/licenses/LICENSE-2.0
87
98
Unless required by applicable law or agreed to in writing, software
109
distributed under the License is distributed on an "AS IS" BASIS,
1110
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1211
See the License for the specific language governing permissions and
1312
limitations under the License.
1413
*/
15-
1614
package mountpropagationinjector
1715

1816
import (
19-
"testing"
20-
2117
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
18+
"github.com/fluid-cloudnative/fluid/pkg/webhook/plugins/api"
19+
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
2223
corev1 "k8s.io/api/core/v1"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2527
)
2628

27-
func TestMutate(t *testing.T) {
29+
var _ = Describe("MountPropagationInjector Plugin", func() {
2830
var (
29-
client client.Client
31+
plugin api.MutatingHandler
3032
pod *corev1.Pod
3133
)
3234

33-
plugin, err := NewPlugin(client, "")
34-
if err != nil {
35-
t.Error("new plugin occurs error", err)
36-
}
37-
if plugin.GetName() != Name {
38-
t.Errorf("GetName expect %v, got %v", Name, plugin.GetName())
39-
}
40-
41-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
42-
if err != nil {
43-
t.Errorf("fail to create the runtimeInfo with error %v", err)
44-
}
45-
46-
pod = &corev1.Pod{
47-
ObjectMeta: metav1.ObjectMeta{
48-
Name: "test",
49-
Namespace: "test",
50-
},
51-
}
52-
53-
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo})
54-
if err != nil {
55-
t.Errorf("fail to mutate pod with error %v", err)
56-
}
57-
58-
if shouldStop {
59-
t.Errorf("expect shouldStop as false, but got %v", shouldStop)
60-
}
61-
62-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
63-
if err != nil {
64-
t.Errorf("fail to mutate pod with error %v", err)
65-
}
66-
67-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
68-
if err == nil {
69-
t.Errorf("expect error is not nil")
70-
}
71-
}
35+
BeforeEach(func() {
36+
scheme := runtime.NewScheme()
37+
Expect(corev1.AddToScheme(scheme)).To(Succeed())
38+
39+
c := fake.NewClientBuilder().
40+
WithScheme(scheme).
41+
Build()
42+
43+
var err error
44+
plugin, err = NewPlugin(c, "")
45+
Expect(err).NotTo(HaveOccurred())
46+
47+
pod = &corev1.Pod{
48+
ObjectMeta: metav1.ObjectMeta{
49+
Name: "test",
50+
Namespace: "test",
51+
},
52+
}
53+
})
54+
55+
It("returns correct plugin name", func() {
56+
Expect(plugin.GetName()).To(Equal(Name))
57+
})
58+
59+
Context("when mutating a pod", func() {
60+
It("does not stop when runtimeInfo exists", func() {
61+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
62+
Expect(err).NotTo(HaveOccurred())
63+
64+
shouldStop, err := plugin.Mutate(
65+
pod,
66+
map[string]base.RuntimeInfoInterface{"test": runtimeInfo},
67+
)
68+
69+
Expect(err).NotTo(HaveOccurred())
70+
Expect(shouldStop).To(BeFalse())
71+
})
72+
73+
It("does not error when runtimeInfos is empty", func() {
74+
_, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
75+
Expect(err).NotTo(HaveOccurred())
76+
})
77+
78+
It("returns error when runtimeInfo is nil", func() {
79+
_, err := plugin.Mutate(
80+
pod,
81+
map[string]base.RuntimeInfoInterface{"test": nil},
82+
)
83+
Expect(err).To(HaveOccurred())
84+
})
85+
})
86+
})

0 commit comments

Comments
 (0)