Skip to content

Commit 41d3008

Browse files
authored
test(prefernodeswithoutcache): refactor tests to use Ginkgo and add suite test (#5574)
Signed-off-by: adity1raut <araut7798@gmail.com>
1 parent b29d82a commit 41d3008

2 files changed

Lines changed: 98 additions & 108 deletions

File tree

pkg/webhook/plugins/prefernodeswithoutcache/prefer_nodes_without_cache_test.go

Lines changed: 85 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -17,120 +17,97 @@ limitations under the License.
1717
package prefernodeswithoutcache
1818

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

30-
func TestGetPreferredSchedulingTermForPodWithoutCacheWithGlobalMode(t *testing.T) {
31-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
32-
if err != nil {
33-
t.Errorf("fail to create the runtimeInfo with error %v", err)
34-
}
35-
36-
// Test case 1: Global fuse with selector enable
37-
runtimeInfo.SetFuseNodeSelector(map[string]string{"test1": "test1"})
38-
term := getPreferredSchedulingTermForPodWithoutCache()
39-
40-
expectTerm := corev1.PreferredSchedulingTerm{
41-
Weight: 100,
42-
Preference: corev1.NodeSelectorTerm{
43-
MatchExpressions: []corev1.NodeSelectorRequirement{
44-
{
45-
Key: common.GetDatasetNumLabelName(),
46-
Operator: corev1.NodeSelectorOpDoesNotExist,
29+
var _ = Describe("PreferNodesWithoutCache Plugin", func() {
30+
Describe("getPreferredSchedulingTermForPodWithoutCache", func() {
31+
It("should return correct PreferredSchedulingTerm with selector enabled and disabled", func() {
32+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
33+
Expect(err).NotTo(HaveOccurred())
34+
35+
runtimeInfo.SetFuseNodeSelector(map[string]string{"test1": "test1"})
36+
term := getPreferredSchedulingTermForPodWithoutCache()
37+
38+
expectTerm := corev1.PreferredSchedulingTerm{
39+
Weight: 100,
40+
Preference: corev1.NodeSelectorTerm{
41+
MatchExpressions: []corev1.NodeSelectorRequirement{
42+
{
43+
Key: common.GetDatasetNumLabelName(),
44+
Operator: corev1.NodeSelectorOpDoesNotExist,
45+
},
46+
},
47+
},
48+
}
49+
Expect(term).To(Equal(expectTerm))
50+
51+
runtimeInfo.SetFuseNodeSelector(map[string]string{})
52+
term = getPreferredSchedulingTermForPodWithoutCache()
53+
Expect(term).To(Equal(expectTerm))
54+
})
55+
56+
It("should return correct PreferredSchedulingTerm with default mode", func() {
57+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
58+
Expect(err).NotTo(HaveOccurred())
59+
60+
runtimeInfo.SetFuseNodeSelector(map[string]string{})
61+
term := getPreferredSchedulingTermForPodWithoutCache()
62+
63+
expectTerm := corev1.PreferredSchedulingTerm{
64+
Weight: 100,
65+
Preference: corev1.NodeSelectorTerm{
66+
MatchExpressions: []corev1.NodeSelectorRequirement{
67+
{
68+
Key: common.GetDatasetNumLabelName(),
69+
Operator: corev1.NodeSelectorOpDoesNotExist,
70+
},
71+
},
4772
},
48-
},
49-
},
50-
}
51-
52-
if !reflect.DeepEqual(term, expectTerm) {
53-
t.Errorf("getPreferredSchedulingTermForPodWithoutCache failure, want:%v, got:%v", expectTerm, term)
54-
}
55-
56-
// Test case 2: Global fuse with selector disable
57-
runtimeInfo.SetFuseNodeSelector(map[string]string{})
58-
term = getPreferredSchedulingTermForPodWithoutCache()
59-
60-
if !reflect.DeepEqual(term, expectTerm) {
61-
t.Errorf("getPreferredSchedulingTermForPodWithoutCache failure, want:%v, got:%v", expectTerm, term)
62-
}
63-
}
64-
65-
func TestGetPreferredSchedulingTermForPodWithoutCacheWithDefaultMode(t *testing.T) {
66-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
67-
if err != nil {
68-
t.Errorf("fail to create the runtimeInfo with error %v", err)
69-
}
70-
71-
runtimeInfo.SetFuseNodeSelector(map[string]string{})
72-
term := getPreferredSchedulingTermForPodWithoutCache()
73-
74-
expectTerm := corev1.PreferredSchedulingTerm{
75-
Weight: 100,
76-
Preference: corev1.NodeSelectorTerm{
77-
MatchExpressions: []corev1.NodeSelectorRequirement{
78-
{
79-
Key: common.GetDatasetNumLabelName(),
80-
Operator: corev1.NodeSelectorOpDoesNotExist,
73+
}
74+
Expect(term).To(Equal(expectTerm))
75+
})
76+
})
77+
78+
Describe("Mutate", func() {
79+
var (
80+
cl client.Client
81+
pod *corev1.Pod
82+
)
83+
84+
BeforeEach(func() {
85+
cl = nil
86+
pod = &corev1.Pod{
87+
ObjectMeta: metav1.ObjectMeta{
88+
Name: "test",
89+
Namespace: "test",
8190
},
82-
},
83-
},
84-
}
85-
86-
if !reflect.DeepEqual(term, expectTerm) {
87-
t.Errorf("getPreferredSchedulingTermForPodWithoutCache failure, want:%v, got:%v", expectTerm, term)
88-
}
89-
}
90-
91-
func TestMutate(t *testing.T) {
92-
var (
93-
client client.Client
94-
pod *corev1.Pod
95-
)
96-
97-
plugin, err := NewPlugin(client, "")
98-
if err != nil {
99-
t.Error("new plugin occurs error", err)
100-
}
101-
if plugin.GetName() != Name {
102-
t.Errorf("GetName expect %v, got %v", Name, plugin.GetName())
103-
}
104-
105-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
106-
if err != nil {
107-
t.Errorf("fail to create the runtimeInfo with error %v", err)
108-
}
109-
110-
pod = &corev1.Pod{
111-
ObjectMeta: metav1.ObjectMeta{
112-
Name: "test",
113-
Namespace: "test",
114-
},
115-
}
116-
117-
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo})
118-
if err != nil {
119-
t.Errorf("fail to mutate pod with error %v", err)
120-
}
121-
122-
if !shouldStop {
123-
t.Errorf("expect shouldStop as true, but got %v", shouldStop)
124-
}
125-
126-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
127-
if err != nil {
128-
t.Errorf("fail to mutate pod with error %v", err)
129-
}
130-
131-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
132-
if err != nil {
133-
t.Errorf("fail to mutate pod with error %v", err)
134-
}
135-
136-
}
91+
}
92+
})
93+
94+
It("should create plugin and mutate pod correctly", func() {
95+
plugin, err := NewPlugin(cl, "")
96+
Expect(err).NotTo(HaveOccurred())
97+
Expect(plugin.GetName()).To(Equal(Name))
98+
99+
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
100+
Expect(err).NotTo(HaveOccurred())
101+
102+
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo})
103+
Expect(err).NotTo(HaveOccurred())
104+
Expect(shouldStop).To(BeTrue())
105+
106+
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
107+
Expect(err).NotTo(HaveOccurred())
108+
109+
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
110+
Expect(err).NotTo(HaveOccurred())
111+
})
112+
})
113+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package prefernodeswithoutcache
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestPrefernodeswithoutcache(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Prefernodeswithoutcache Suite")
13+
}

0 commit comments

Comments
 (0)