-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoleration_test.go-31d955cc7f3029fe1dc874cbb40892a5af6836a7e74a6567748db1b96cf1cdde
252 lines (240 loc) · 6.85 KB
/
toleration_test.go-31d955cc7f3029fe1dc874cbb40892a5af6836a7e74a6567748db1b96cf1cdde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// PUBLISH-CODE
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This code is taken from github.com/kubernetes and modified
package toleration
import (
"testing"
core "go.tectonic.network/lib/k8s/imp/api/core/v1"
)
// from api/core/v1/toleration_test.go
func TestTolerationToleratesTaint(t *testing.T) {
testCases := []struct {
description string
toleration core.Toleration
taint core.Taint
expectTolerated bool
}{
{
description: "toleration and taint have the same key and effect, and operator is Exists, and taint has no value, expect tolerated",
toleration: core.Toleration{
Key: "foo",
Operator: core.TolerationOpExists,
Effect: core.TaintEffectNoSchedule,
},
taint: core.Taint{
Key: "foo",
Effect: core.TaintEffectNoSchedule,
},
expectTolerated: true,
},
{
description: "toleration and taint have the same key and effect, and operator is Exists, and taint has some value, expect tolerated",
toleration: core.Toleration{
Key: "foo",
Operator: core.TolerationOpExists,
Effect: core.TaintEffectNoSchedule,
},
taint: core.Taint{
Key: "foo",
Value: "bar",
Effect: core.TaintEffectNoSchedule,
},
expectTolerated: true,
},
{
description: "toleration and taint have the same effect, toleration has empty key and operator is Exists, means match all taints, expect tolerated",
toleration: core.Toleration{
Key: "",
Operator: core.TolerationOpExists,
Effect: core.TaintEffectNoSchedule,
},
taint: core.Taint{
Key: "foo",
Value: "bar",
Effect: core.TaintEffectNoSchedule,
},
expectTolerated: true,
},
{
description: "toleration and taint have the same key, effect and value, and operator is Equal, expect tolerated",
toleration: core.Toleration{
Key: "foo",
Operator: core.TolerationOpEqual,
Value: "bar",
Effect: core.TaintEffectNoSchedule,
},
taint: core.Taint{
Key: "foo",
Value: "bar",
Effect: core.TaintEffectNoSchedule,
},
expectTolerated: true,
},
{
description: "toleration and taint have the same key and effect, but different values, and operator is Equal, expect not tolerated",
toleration: core.Toleration{
Key: "foo",
Operator: core.TolerationOpEqual,
Value: "value1",
Effect: core.TaintEffectNoSchedule,
},
taint: core.Taint{
Key: "foo",
Value: "value2",
Effect: core.TaintEffectNoSchedule,
},
expectTolerated: false,
},
{
description: "toleration and taint have the same key and value, but different effects, and operator is Equal, expect not tolerated",
toleration: core.Toleration{
Key: "foo",
Operator: core.TolerationOpEqual,
Value: "bar",
Effect: core.TaintEffectNoSchedule,
},
taint: core.Taint{
Key: "foo",
Value: "bar",
Effect: core.TaintEffectNoExecute,
},
expectTolerated: false,
},
}
for _, tc := range testCases {
if tolerated := ToleratesTaint(tc.toleration, &tc.taint); tc.expectTolerated != tolerated {
t.Errorf("[%s] expect %v, got %v: toleration %+v, taint %s", tc.description, tc.expectTolerated, tolerated, tc.toleration, tc.taint)
}
}
}
// from component-helpers/scheduling/corev1/helpers_test.go
func TestFindMatchingUntoleratedTaint(t *testing.T) {
testCases := []struct {
description string
tolerations []core.Toleration
taints []core.Taint
applyFilter taintsFilterFunc
expectTolerated bool
}{
{
description: "empty tolerations tolerate empty taints",
tolerations: []core.Toleration{},
taints: []core.Taint{},
applyFilter: func(t *core.Taint) bool { return true },
expectTolerated: true,
},
{
description: "non-empty tolerations tolerate empty taints",
tolerations: []core.Toleration{
{
Key: "foo",
Operator: "Exists",
Effect: core.TaintEffectNoSchedule,
},
},
taints: []core.Taint{},
applyFilter: func(t *core.Taint) bool { return true },
expectTolerated: true,
},
{
description: "tolerations match all taints, expect tolerated",
tolerations: []core.Toleration{
{
Key: "foo",
Operator: "Exists",
Effect: core.TaintEffectNoSchedule,
},
},
taints: []core.Taint{
{
Key: "foo",
Effect: core.TaintEffectNoSchedule,
},
},
applyFilter: func(t *core.Taint) bool { return true },
expectTolerated: true,
},
{
description: "tolerations don't match taints, but no taints apply to the filter, expect tolerated",
tolerations: []core.Toleration{
{
Key: "foo",
Operator: "Exists",
Effect: core.TaintEffectNoSchedule,
},
},
taints: []core.Taint{
{
Key: "bar",
Effect: core.TaintEffectNoSchedule,
},
},
applyFilter: func(t *core.Taint) bool { return false },
expectTolerated: true,
},
{
description: "no filterFunc indicated, means all taints apply to the filter, tolerations don't match taints, expect untolerated",
tolerations: []core.Toleration{
{
Key: "foo",
Operator: "Exists",
Effect: core.TaintEffectNoSchedule,
},
},
taints: []core.Taint{
{
Key: "bar",
Effect: core.TaintEffectNoSchedule,
},
},
applyFilter: nil,
expectTolerated: false,
},
{
description: "tolerations match taints, expect tolerated",
tolerations: []core.Toleration{
{
Key: "foo",
Operator: "Exists",
Effect: core.TaintEffectNoExecute,
},
},
taints: []core.Taint{
{
Key: "foo",
Effect: core.TaintEffectNoExecute,
},
{
Key: "bar",
Effect: core.TaintEffectNoSchedule,
},
},
applyFilter: func(t *core.Taint) bool { return t.Effect == core.TaintEffectNoExecute },
expectTolerated: true,
},
}
for _, tc := range testCases {
_, untolerated := FindMatchingUntoleratedTaint(tc.taints, tc.tolerations, tc.applyFilter)
if tc.expectTolerated != !untolerated {
filteredTaints := []core.Taint{}
for _, taint := range tc.taints {
if tc.applyFilter != nil && !tc.applyFilter(&taint) {
continue
}
filteredTaints = append(filteredTaints, taint)
}
t.Errorf("[%s] expect tolerations %+v tolerate filtered taints %+v in taints %+v", tc.description, tc.tolerations, filteredTaints, tc.taints)
}
}
}