Skip to content

Commit b66b10a

Browse files
authored
test(requirenodewithfuse): refactor tests to use Ginkgo and add test suite (#5575)
Signed-off-by: adity1raut <araut7798@gmail.com>
1 parent 8d3ae2b commit b66b10a

2 files changed

Lines changed: 85 additions & 89 deletions

File tree

pkg/webhook/plugins/requirenodewithfuse/require_node_with_fuse_test.go

Lines changed: 72 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -17,98 +17,81 @@ limitations under the License.
1717
package requirenodewithfuse
1818

1919
import (
20-
"reflect"
21-
"testing"
22-
2320
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
21+
. "github.com/onsi/ginkgo/v2"
22+
. "github.com/onsi/gomega"
2423
corev1 "k8s.io/api/core/v1"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
"sigs.k8s.io/controller-runtime/pkg/client"
2726
)
2827

29-
func TestGetRequiredSchedulingTermWithGlobalMode(t *testing.T) {
30-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
31-
if err != nil {
32-
t.Errorf("fail to create the runtimeInfo with error %v", err)
33-
}
34-
35-
// Test case 1: Global fuse with selector enable
36-
runtimeInfo.SetFuseNodeSelector(map[string]string{"test1": "test1"})
37-
terms, _ := getRequiredSchedulingTerm(runtimeInfo)
38-
39-
expectTerms := corev1.NodeSelectorTerm{
40-
MatchExpressions: []corev1.NodeSelectorRequirement{
41-
{
42-
Key: "test1",
43-
Operator: corev1.NodeSelectorOpIn,
44-
Values: []string{"test1"},
45-
},
46-
},
47-
}
48-
49-
if !reflect.DeepEqual(terms, expectTerms) {
50-
t.Errorf("getRequiredSchedulingTerm failure, want:%v, got:%v", expectTerms, terms)
51-
}
52-
53-
// Test case 2: Global fuse with selector disable
54-
runtimeInfo.SetFuseNodeSelector(map[string]string{})
55-
terms, _ = getRequiredSchedulingTerm(runtimeInfo)
56-
expectTerms = corev1.NodeSelectorTerm{MatchExpressions: []corev1.NodeSelectorRequirement{}}
57-
58-
if !reflect.DeepEqual(terms, expectTerms) {
59-
t.Errorf("getRequiredSchedulingTerm failure, want:%v, got:%v", expectTerms, terms)
60-
}
61-
62-
// Test case 3: runtime Info is nil to handle the error path
63-
_, err = getRequiredSchedulingTerm(nil)
64-
if err == nil {
65-
t.Errorf("getRequiredSchedulingTerm failure, want:%v, got:%v", nil, err)
66-
}
67-
}
68-
69-
func TestMutate(t *testing.T) {
70-
var (
71-
client client.Client
72-
pod *corev1.Pod
73-
)
74-
75-
plugin, err := NewPlugin(client, "")
76-
if err != nil {
77-
t.Error("new plugin occurs error", err)
78-
}
79-
if plugin.GetName() != Name {
80-
t.Errorf("GetName expect %v, got %v", Name, plugin.GetName())
81-
}
82-
83-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
84-
if err != nil {
85-
t.Errorf("fail to create the runtimeInfo with error %v", err)
86-
}
87-
88-
pod = &corev1.Pod{
89-
ObjectMeta: metav1.ObjectMeta{
90-
Name: "test",
91-
Namespace: "test",
92-
},
93-
}
94-
95-
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"pvcName": runtimeInfo})
96-
if err != nil {
97-
t.Errorf("fail to mutate pod with error %v", err)
98-
}
99-
100-
if shouldStop {
101-
t.Errorf("expect shouldStop as false, but got %v", shouldStop)
102-
}
103-
104-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
105-
if err != nil {
106-
t.Errorf("fail to mutate pod with error %v", err)
107-
}
108-
109-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"pvcName": nil})
110-
if err == nil {
111-
t.Errorf("expect error is not nil")
112-
}
113-
114-
}
28+
var _ = Describe("RequireNodeWithFuse Plugin", func() {
29+
Describe("getRequiredSchedulingTerm", func() {
30+
It("should return correct NodeSelectorTerm with selector enabled and disabled", func() {
31+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
32+
Expect(err).NotTo(HaveOccurred())
33+
34+
// Global fuse with selector enable
35+
runtimeInfo.SetFuseNodeSelector(map[string]string{"test1": "test1"})
36+
terms, err := getRequiredSchedulingTerm(runtimeInfo)
37+
Expect(err).NotTo(HaveOccurred())
38+
expectTerms := corev1.NodeSelectorTerm{
39+
MatchExpressions: []corev1.NodeSelectorRequirement{
40+
{
41+
Key: "test1",
42+
Operator: corev1.NodeSelectorOpIn,
43+
Values: []string{"test1"},
44+
},
45+
},
46+
}
47+
Expect(terms).To(Equal(expectTerms))
48+
49+
// Global fuse with selector disable
50+
runtimeInfo.SetFuseNodeSelector(map[string]string{})
51+
terms, err = getRequiredSchedulingTerm(runtimeInfo)
52+
Expect(err).NotTo(HaveOccurred())
53+
expectTerms = corev1.NodeSelectorTerm{MatchExpressions: []corev1.NodeSelectorRequirement{}}
54+
Expect(terms).To(Equal(expectTerms))
55+
56+
// runtimeInfo is nil
57+
_, err = getRequiredSchedulingTerm(nil)
58+
Expect(err).To(HaveOccurred())
59+
})
60+
})
61+
62+
Describe("Mutate", func() {
63+
var (
64+
cl client.Client
65+
pod *corev1.Pod
66+
)
67+
68+
BeforeEach(func() {
69+
cl = nil
70+
pod = &corev1.Pod{
71+
ObjectMeta: metav1.ObjectMeta{
72+
Name: "test",
73+
Namespace: "test",
74+
},
75+
}
76+
})
77+
78+
It("should create plugin and mutate pod correctly", func() {
79+
plugin, err := NewPlugin(cl, "")
80+
Expect(err).NotTo(HaveOccurred())
81+
Expect(plugin.GetName()).To(Equal(Name))
82+
83+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
84+
Expect(err).NotTo(HaveOccurred())
85+
86+
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"pvcName": runtimeInfo})
87+
Expect(err).NotTo(HaveOccurred())
88+
Expect(shouldStop).To(BeFalse())
89+
90+
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
91+
Expect(err).NotTo(HaveOccurred())
92+
93+
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"pvcName": nil})
94+
Expect(err).To(HaveOccurred())
95+
})
96+
})
97+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package requirenodewithfuse
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestRequirenodewithfuse(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Requirenodewithfuse Suite")
13+
}

0 commit comments

Comments
 (0)