1
16
17 package v1
18
19 import (
20 "testing"
21 )
22
23 func TestTolerationToleratesTaint(t *testing.T) {
24
25 testCases := []struct {
26 description string
27 toleration Toleration
28 taint Taint
29 expectTolerated bool
30 }{
31 {
32 description: "toleration and taint have the same key and effect, and operator is Exists, and taint has no value, expect tolerated",
33 toleration: Toleration{
34 Key: "foo",
35 Operator: TolerationOpExists,
36 Effect: TaintEffectNoSchedule,
37 },
38 taint: Taint{
39 Key: "foo",
40 Effect: TaintEffectNoSchedule,
41 },
42 expectTolerated: true,
43 },
44 {
45 description: "toleration and taint have the same key and effect, and operator is Exists, and taint has some value, expect tolerated",
46 toleration: Toleration{
47 Key: "foo",
48 Operator: TolerationOpExists,
49 Effect: TaintEffectNoSchedule,
50 },
51 taint: Taint{
52 Key: "foo",
53 Value: "bar",
54 Effect: TaintEffectNoSchedule,
55 },
56 expectTolerated: true,
57 },
58 {
59 description: "toleration and taint have the same effect, toleration has empty key and operator is Exists, means match all taints, expect tolerated",
60 toleration: Toleration{
61 Key: "",
62 Operator: TolerationOpExists,
63 Effect: TaintEffectNoSchedule,
64 },
65 taint: Taint{
66 Key: "foo",
67 Value: "bar",
68 Effect: TaintEffectNoSchedule,
69 },
70 expectTolerated: true,
71 },
72 {
73 description: "toleration and taint have the same key, effect and value, and operator is Equal, expect tolerated",
74 toleration: Toleration{
75 Key: "foo",
76 Operator: TolerationOpEqual,
77 Value: "bar",
78 Effect: TaintEffectNoSchedule,
79 },
80 taint: Taint{
81 Key: "foo",
82 Value: "bar",
83 Effect: TaintEffectNoSchedule,
84 },
85 expectTolerated: true,
86 },
87 {
88 description: "toleration and taint have the same key and effect, but different values, and operator is Equal, expect not tolerated",
89 toleration: Toleration{
90 Key: "foo",
91 Operator: TolerationOpEqual,
92 Value: "value1",
93 Effect: TaintEffectNoSchedule,
94 },
95 taint: Taint{
96 Key: "foo",
97 Value: "value2",
98 Effect: TaintEffectNoSchedule,
99 },
100 expectTolerated: false,
101 },
102 {
103 description: "toleration and taint have the same key and value, but different effects, and operator is Equal, expect not tolerated",
104 toleration: Toleration{
105 Key: "foo",
106 Operator: TolerationOpEqual,
107 Value: "bar",
108 Effect: TaintEffectNoSchedule,
109 },
110 taint: Taint{
111 Key: "foo",
112 Value: "bar",
113 Effect: TaintEffectNoExecute,
114 },
115 expectTolerated: false,
116 },
117 }
118 for _, tc := range testCases {
119 if tolerated := tc.toleration.ToleratesTaint(&tc.taint); tc.expectTolerated != tolerated {
120 t.Errorf("[%s] expect %v, got %v: toleration %+v, taint %s", tc.description, tc.expectTolerated, tolerated, tc.toleration, tc.taint.ToString())
121 }
122 }
123 }
124
View as plain text