Skip to content

Commit 299e8f8

Browse files
authored
test(mountpropagationinjector): enhance unit tests for plugin functionality (#5572)
Signed-off-by: adity1raut <araut7798@gmail.com>
1 parent 7147fa2 commit 299e8f8

2 files changed

Lines changed: 57 additions & 37 deletions

File tree

pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_test.go

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,63 @@ limitations under the License.
1616
package mountpropagationinjector
1717

1818
import (
19-
"testing"
20-
2119
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
20+
"github.com/fluid-cloudnative/fluid/pkg/webhook/plugins/api"
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
2223
corev1 "k8s.io/api/core/v1"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"sigs.k8s.io/controller-runtime/pkg/client"
2526
)
2627

27-
func TestMutate(t *testing.T) {
28+
var _ = Describe("MountPropagationInjector Plugin", func() {
2829
var (
29-
client client.Client
30+
cl client.Client
3031
pod *corev1.Pod
32+
plugin api.MutatingHandler
33+
err error
3134
)
3235

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-
}
36+
BeforeEach(func() {
37+
cl = nil
38+
plugin, err = NewPlugin(cl, "")
39+
})
4040

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-
}
41+
It("should create plugin without error", func() {
42+
Expect(err).NotTo(HaveOccurred())
43+
Expect(plugin.GetName()).To(Equal(Name))
44+
})
4545

46-
pod = &corev1.Pod{
47-
ObjectMeta: metav1.ObjectMeta{
48-
Name: "test",
49-
Namespace: "test",
50-
},
51-
}
46+
Context("Mutate method", func() {
47+
var runtimeInfo base.RuntimeInfoInterface
5248

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-
}
49+
BeforeEach(func() {
50+
runtimeInfo, err = base.BuildRuntimeInfo("test", "fluid", "alluxio")
51+
Expect(err).NotTo(HaveOccurred())
52+
pod = &corev1.Pod{
53+
ObjectMeta: metav1.ObjectMeta{
54+
Name: "test",
55+
Namespace: "test",
56+
},
57+
}
58+
})
5759

58-
if shouldStop {
59-
t.Errorf("expect shouldStop as false, but got %v", shouldStop)
60-
}
60+
It("should mutate pod with valid runtimeInfo and not stop", func() {
61+
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo})
62+
Expect(err).NotTo(HaveOccurred())
63+
Expect(shouldStop).To(BeFalse())
64+
})
6165

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+
It("should mutate pod with empty runtimeInfos", func() {
67+
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
68+
Expect(err).NotTo(HaveOccurred())
69+
Expect(shouldStop).To(BeFalse())
70+
})
6671

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-
}
72+
It("should return error when runtimeInfo is nil", func() {
73+
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
74+
Expect(err).To(HaveOccurred())
75+
Expect(shouldStop).To(BeTrue())
76+
})
77+
})
78+
})
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+
}

0 commit comments

Comments
 (0)