|
| 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 | +} |
0 commit comments