Skip to content

Commit ef35058

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

File tree

7 files changed

+1289
-0
lines changed

7 files changed

+1289
-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,139 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package helpers
18+
19+
import (
20+
"testing"
21+
22+
v1 "k8s.io/api/core/v1"
23+
)
24+
25+
func TestHasLeafNodeTaint(t *testing.T) {
26+
nodeWithTaint := &v1.Node{
27+
Spec: v1.NodeSpec{
28+
Taints: []v1.Taint{
29+
{
30+
Key: "kosmos.io/node",
31+
Value: "true",
32+
Effect: v1.TaintEffectNoSchedule,
33+
},
34+
},
35+
},
36+
}
37+
38+
nodeWithoutTaint := &v1.Node{
39+
Spec: v1.NodeSpec{
40+
Taints: []v1.Taint{},
41+
},
42+
}
43+
44+
if !HasLeafNodeTaint(nodeWithTaint) {
45+
t.Errorf("Expected node to have LeafNodeTaint")
46+
}
47+
48+
if HasLeafNodeTaint(nodeWithoutTaint) {
49+
t.Errorf("Expected node to not have LeafNodeTaint")
50+
}
51+
}
52+
53+
func TestTolerationsTolerateTaint(t *testing.T) {
54+
taint := &v1.Taint{
55+
Key: "kosmos.io/node",
56+
Value: "true",
57+
Effect: v1.TaintEffectNoSchedule,
58+
}
59+
60+
toleration := v1.Toleration{
61+
Key: "kosmos.io/node",
62+
Operator: v1.TolerationOpEqual,
63+
Value: "true",
64+
Effect: v1.TaintEffectNoSchedule,
65+
}
66+
67+
tolerations := []v1.Toleration{toleration}
68+
69+
if !TolerationsTolerateTaint(tolerations, taint) {
70+
t.Errorf("Expected tolerations to tolerate the taint")
71+
}
72+
73+
untoleratedTaint := &v1.Taint{
74+
Key: "kosmos.io/node-test",
75+
Value: "false",
76+
Effect: v1.TaintEffectNoSchedule,
77+
}
78+
79+
if TolerationsTolerateTaint(tolerations, untoleratedTaint) {
80+
t.Errorf("Expected tolerations to not tolerate the untolerated taint")
81+
}
82+
}
83+
84+
func TestFindMatchingUntoleratedTaint(t *testing.T) {
85+
taints := []v1.Taint{
86+
{
87+
Key: "kosmos.io/node",
88+
Value: "true",
89+
Effect: v1.TaintEffectNoSchedule,
90+
},
91+
{
92+
Key: "example.io/another-taint",
93+
Value: "true",
94+
Effect: v1.TaintEffectNoSchedule,
95+
},
96+
}
97+
98+
tolerations := []v1.Toleration{
99+
{
100+
Key: "example.io/another-taint",
101+
Operator: v1.TolerationOpEqual,
102+
Value: "true",
103+
Effect: v1.TaintEffectNoSchedule,
104+
},
105+
}
106+
107+
taint, found := FindMatchingUntoleratedTaint(taints, tolerations, nil)
108+
if found {
109+
t.Errorf("Expected to find an untolerated taint")
110+
} else if taint.Key != "" && taint.Key != "kosmos.io/node" {
111+
t.Errorf("Expected untolerated taint to be 'kosmos.io/node', got '%s'", taint.Key)
112+
}
113+
}
114+
115+
func TestGetFilteredTaints(t *testing.T) {
116+
taints := []v1.Taint{
117+
{
118+
Key: "kosmos.io/node",
119+
Value: "true",
120+
Effect: v1.TaintEffectNoSchedule,
121+
},
122+
{
123+
Key: "example.io/another-taint",
124+
Value: "true",
125+
Effect: v1.TaintEffectNoSchedule,
126+
},
127+
}
128+
129+
filterFunc := func(t *v1.Taint) bool {
130+
return t.Key == "kosmos.io/node"
131+
}
132+
133+
filtered := getFilteredTaints(taints, filterFunc)
134+
if len(filtered) != 1 {
135+
t.Errorf("Expected 1 filtered taint, got %d", len(filtered))
136+
} else if filtered[0].Key != "kosmos.io/node" {
137+
t.Errorf("Expected filtered taint to be 'kosmos.io/node', got '%s'", filtered[0].Key)
138+
}
139+
}
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)