|
| 1 | +/* |
| 2 | +Copyright 2016 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 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/api/core/v1" |
| 24 | +) |
| 25 | + |
| 26 | +func TestTaintExists(t *testing.T) { |
| 27 | + testingTaints := []v1.Taint{ |
| 28 | + { |
| 29 | + Key: "foo_1", |
| 30 | + Value: "bar_1", |
| 31 | + Effect: v1.TaintEffectNoExecute, |
| 32 | + }, |
| 33 | + { |
| 34 | + Key: "foo_2", |
| 35 | + Value: "bar_2", |
| 36 | + Effect: v1.TaintEffectNoSchedule, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + cases := []struct { |
| 41 | + name string |
| 42 | + taintToFind *v1.Taint |
| 43 | + expectedResult bool |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "taint exists", |
| 47 | + taintToFind: &v1.Taint{Key: "foo_1", Value: "bar_1", Effect: v1.TaintEffectNoExecute}, |
| 48 | + expectedResult: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "different key", |
| 52 | + taintToFind: &v1.Taint{Key: "no_such_key", Value: "bar_1", Effect: v1.TaintEffectNoExecute}, |
| 53 | + expectedResult: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "different effect", |
| 57 | + taintToFind: &v1.Taint{Key: "foo_1", Value: "bar_1", Effect: v1.TaintEffectNoSchedule}, |
| 58 | + expectedResult: false, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + for _, c := range cases { |
| 63 | + result := taintExists(testingTaints, c.taintToFind) |
| 64 | + |
| 65 | + if result != c.expectedResult { |
| 66 | + t.Errorf("[%s] unexpected results: %v", c.name, result) |
| 67 | + continue |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestRemoveTaint(t *testing.T) { |
| 73 | + cases := []struct { |
| 74 | + name string |
| 75 | + node *v1.Node |
| 76 | + taintToRemove *v1.Taint |
| 77 | + expectedTaints []v1.Taint |
| 78 | + expectedResult bool |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "remove taint unsuccessfully", |
| 82 | + node: &v1.Node{ |
| 83 | + Spec: v1.NodeSpec{ |
| 84 | + Taints: []v1.Taint{ |
| 85 | + { |
| 86 | + Key: "foo", |
| 87 | + Effect: v1.TaintEffectNoSchedule, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + taintToRemove: &v1.Taint{ |
| 93 | + Key: "foo_1", |
| 94 | + Effect: v1.TaintEffectNoSchedule, |
| 95 | + }, |
| 96 | + expectedTaints: []v1.Taint{ |
| 97 | + { |
| 98 | + Key: "foo", |
| 99 | + Effect: v1.TaintEffectNoSchedule, |
| 100 | + }, |
| 101 | + }, |
| 102 | + expectedResult: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "remove taint successfully", |
| 106 | + node: &v1.Node{ |
| 107 | + Spec: v1.NodeSpec{ |
| 108 | + Taints: []v1.Taint{ |
| 109 | + { |
| 110 | + Key: "foo", |
| 111 | + Effect: v1.TaintEffectNoSchedule, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + taintToRemove: &v1.Taint{ |
| 117 | + Key: "foo", |
| 118 | + Effect: v1.TaintEffectNoSchedule, |
| 119 | + }, |
| 120 | + expectedTaints: []v1.Taint{}, |
| 121 | + expectedResult: true, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "remove taint from node with no taint", |
| 125 | + node: &v1.Node{ |
| 126 | + Spec: v1.NodeSpec{ |
| 127 | + Taints: []v1.Taint{}, |
| 128 | + }, |
| 129 | + }, |
| 130 | + taintToRemove: &v1.Taint{ |
| 131 | + Key: "foo", |
| 132 | + Effect: v1.TaintEffectNoSchedule, |
| 133 | + }, |
| 134 | + expectedTaints: []v1.Taint{}, |
| 135 | + expectedResult: false, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, c := range cases { |
| 140 | + newNode, result, err := removeTaint(c.node, c.taintToRemove) |
| 141 | + if err != nil { |
| 142 | + t.Errorf("[%s] should not raise error but got: %v", c.name, err) |
| 143 | + } |
| 144 | + if result != c.expectedResult { |
| 145 | + t.Errorf("[%s] should return %t, but got: %t", c.name, c.expectedResult, result) |
| 146 | + } |
| 147 | + if !reflect.DeepEqual(newNode.Spec.Taints, c.expectedTaints) { |
| 148 | + t.Errorf("[%s] the new node object should have taints %v, but got: %v", c.name, c.expectedTaints, newNode.Spec.Taints) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestDeleteTaint(t *testing.T) { |
| 154 | + cases := []struct { |
| 155 | + name string |
| 156 | + taints []v1.Taint |
| 157 | + taintToDelete *v1.Taint |
| 158 | + expectedTaints []v1.Taint |
| 159 | + expectedResult bool |
| 160 | + }{ |
| 161 | + { |
| 162 | + name: "delete taint with different name", |
| 163 | + taints: []v1.Taint{ |
| 164 | + { |
| 165 | + Key: "foo", |
| 166 | + Effect: v1.TaintEffectNoSchedule, |
| 167 | + }, |
| 168 | + }, |
| 169 | + taintToDelete: &v1.Taint{Key: "foo_1", Effect: v1.TaintEffectNoSchedule}, |
| 170 | + expectedTaints: []v1.Taint{ |
| 171 | + { |
| 172 | + Key: "foo", |
| 173 | + Effect: v1.TaintEffectNoSchedule, |
| 174 | + }, |
| 175 | + }, |
| 176 | + expectedResult: false, |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "delete taint with different effect", |
| 180 | + taints: []v1.Taint{ |
| 181 | + { |
| 182 | + Key: "foo", |
| 183 | + Effect: v1.TaintEffectNoSchedule, |
| 184 | + }, |
| 185 | + }, |
| 186 | + taintToDelete: &v1.Taint{Key: "foo", Effect: v1.TaintEffectNoExecute}, |
| 187 | + expectedTaints: []v1.Taint{ |
| 188 | + { |
| 189 | + Key: "foo", |
| 190 | + Effect: v1.TaintEffectNoSchedule, |
| 191 | + }, |
| 192 | + }, |
| 193 | + expectedResult: false, |
| 194 | + }, |
| 195 | + { |
| 196 | + name: "delete taint successfully", |
| 197 | + taints: []v1.Taint{ |
| 198 | + { |
| 199 | + Key: "foo", |
| 200 | + Effect: v1.TaintEffectNoSchedule, |
| 201 | + }, |
| 202 | + }, |
| 203 | + taintToDelete: &v1.Taint{Key: "foo", Effect: v1.TaintEffectNoSchedule}, |
| 204 | + expectedTaints: []v1.Taint{}, |
| 205 | + expectedResult: true, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "delete taint from empty taint array", |
| 209 | + taints: []v1.Taint{}, |
| 210 | + taintToDelete: &v1.Taint{Key: "foo", Effect: v1.TaintEffectNoSchedule}, |
| 211 | + expectedTaints: []v1.Taint{}, |
| 212 | + expectedResult: false, |
| 213 | + }, |
| 214 | + } |
| 215 | + |
| 216 | + for _, c := range cases { |
| 217 | + taints, result := deleteTaint(c.taints, c.taintToDelete) |
| 218 | + if result != c.expectedResult { |
| 219 | + t.Errorf("[%s] should return %t, but got: %t", c.name, c.expectedResult, result) |
| 220 | + } |
| 221 | + if !reflect.DeepEqual(taints, c.expectedTaints) { |
| 222 | + t.Errorf("[%s] the result taints should be %v, but got: %v", c.name, c.expectedTaints, taints) |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments