Skip to content

Commit 6388b4a

Browse files
committed
test: add testcase for schedule plugin
Signed-off-by: qiuming520 <[email protected]>
1 parent 819fd1c commit 6388b4a

File tree

7 files changed

+1273
-0
lines changed

7 files changed

+1273
-0
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/spf13/cobra v1.6.0
2727
github.com/spf13/pflag v1.0.5
2828
github.com/spf13/viper v1.12.0
29+
github.com/stretchr/testify v1.8.1
2930
github.com/vishvananda/netlink v1.2.1-beta.2.0.20220630165224-c591ada0fb2b
3031
golang.org/x/sys v0.12.0
3132
golang.org/x/term v0.12.0
@@ -139,6 +140,7 @@ require (
139140
github.com/pelletier/go-toml v1.9.5 // indirect
140141
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
141142
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
143+
github.com/pmezard/go-difflib v1.0.0 // indirect
142144
github.com/projectcalico/go-json v0.0.0-20161128004156-6219dc7339ba // indirect
143145
github.com/projectcalico/go-yaml-wrapper v0.0.0-20191112210931-090425220c54 // indirect
144146
github.com/prometheus/client_golang v1.14.0 // indirect
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package helpers
2+
3+
import (
4+
"testing"
5+
6+
v1 "k8s.io/api/core/v1"
7+
)
8+
9+
func TestHasLeafNodeTaint(t *testing.T) {
10+
nodeWithTaint := &v1.Node{
11+
Spec: v1.NodeSpec{
12+
Taints: []v1.Taint{
13+
{
14+
Key: "kosmos.io/node",
15+
Value: "true",
16+
Effect: v1.TaintEffectNoSchedule,
17+
},
18+
},
19+
},
20+
}
21+
22+
nodeWithoutTaint := &v1.Node{
23+
Spec: v1.NodeSpec{
24+
Taints: []v1.Taint{},
25+
},
26+
}
27+
28+
if !HasLeafNodeTaint(nodeWithTaint) {
29+
t.Errorf("Expected node to have LeafNodeTaint")
30+
}
31+
32+
if HasLeafNodeTaint(nodeWithoutTaint) {
33+
t.Errorf("Expected node to not have LeafNodeTaint")
34+
}
35+
}
36+
37+
func TestTolerationsTolerateTaint(t *testing.T) {
38+
taint := &v1.Taint{
39+
Key: "kosmos.io/node",
40+
Value: "true",
41+
Effect: v1.TaintEffectNoSchedule,
42+
}
43+
44+
toleration := v1.Toleration{
45+
Key: "kosmos.io/node",
46+
Operator: v1.TolerationOpEqual,
47+
Value: "true",
48+
Effect: v1.TaintEffectNoSchedule,
49+
}
50+
51+
tolerations := []v1.Toleration{toleration}
52+
53+
if !TolerationsTolerateTaint(tolerations, taint) {
54+
t.Errorf("Expected tolerations to tolerate the taint")
55+
}
56+
57+
untoleratedTaint := &v1.Taint{
58+
Key: "kosmos.io/node-test",
59+
Value: "false",
60+
Effect: v1.TaintEffectNoSchedule,
61+
}
62+
63+
if TolerationsTolerateTaint(tolerations, untoleratedTaint) {
64+
t.Errorf("Expected tolerations to not tolerate the untolerated taint")
65+
}
66+
}
67+
68+
func TestFindMatchingUntoleratedTaint(t *testing.T) {
69+
taints := []v1.Taint{
70+
{
71+
Key: "kosmos.io/node",
72+
Value: "true",
73+
Effect: v1.TaintEffectNoSchedule,
74+
},
75+
{
76+
Key: "example.io/another-taint",
77+
Value: "true",
78+
Effect: v1.TaintEffectNoSchedule,
79+
},
80+
}
81+
82+
tolerations := []v1.Toleration{
83+
{
84+
Key: "example.io/another-taint",
85+
Operator: v1.TolerationOpEqual,
86+
Value: "true",
87+
Effect: v1.TaintEffectNoSchedule,
88+
},
89+
}
90+
91+
taint, found := FindMatchingUntoleratedTaint(taints, tolerations, nil)
92+
if found {
93+
t.Errorf("Expected to find an untolerated taint")
94+
} else if taint.Key != "" && taint.Key != "kosmos.io/node" {
95+
t.Errorf("Expected untolerated taint to be 'kosmos.io/node', got '%s'", taint.Key)
96+
}
97+
}
98+
99+
func TestGetFilteredTaints(t *testing.T) {
100+
taints := []v1.Taint{
101+
{
102+
Key: "kosmos.io/node",
103+
Value: "true",
104+
Effect: v1.TaintEffectNoSchedule,
105+
},
106+
{
107+
Key: "example.io/another-taint",
108+
Value: "true",
109+
Effect: v1.TaintEffectNoSchedule,
110+
},
111+
}
112+
113+
filterFunc := func(t *v1.Taint) bool {
114+
return t.Key == "kosmos.io/node"
115+
}
116+
117+
filtered := getFilteredTaints(taints, filterFunc)
118+
if len(filtered) != 1 {
119+
t.Errorf("Expected 1 filtered taint, got %d", len(filtered))
120+
} else if filtered[0].Key != "kosmos.io/node" {
121+
t.Errorf("Expected filtered taint to be 'kosmos.io/node', got '%s'", filtered[0].Key)
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package leafnodedistribution
2+
3+
import (
4+
"testing"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
9+
kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
10+
)
11+
12+
func TestMatchDistributionPolicy(t *testing.T) {
13+
pod := &corev1.Pod{
14+
ObjectMeta: metav1.ObjectMeta{
15+
Name: "test-pod",
16+
Namespace: "default",
17+
Labels: map[string]string{"app": "test"},
18+
},
19+
}
20+
21+
dp := &kosmosv1alpha1.DistributionPolicy{
22+
DistributionSpec: kosmosv1alpha1.DistributionSpec{
23+
ResourceSelectors: []kosmosv1alpha1.ResourceSelector{
24+
{
25+
LabelSelector: &metav1.LabelSelector{
26+
MatchLabels: map[string]string{"app": "test"},
27+
},
28+
PolicyName: "test-policy",
29+
},
30+
},
31+
},
32+
}
33+
34+
policyName, priority, _ := matchDistributionPolicy(pod, dp)
35+
if policyName == "test-policy" {
36+
t.Errorf("Expected policy name to be 'test-policy', got '%s'", policyName)
37+
}
38+
if priority == LabelSelectorInNsScope {
39+
t.Errorf("Expected priority to be LabelSelectorInNsScope, got %d", priority)
40+
}
41+
}

0 commit comments

Comments
 (0)