|
| 1 | +package helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + v1 "k8s.io/api/core/v1" |
| 7 | +) |
| 8 | + |
| 9 | +func TestHasLeafNodeTaint(t *testing.T) { |
| 10 | + nodeWithTaint := &v1.Node{ |
| 11 | + Spec: v1.NodeSpec{ |
| 12 | + Taints: []v1.Taint{ |
| 13 | + { |
| 14 | + Key: "kosmos.io/node", |
| 15 | + Value: "true", |
| 16 | + Effect: v1.TaintEffectNoSchedule, |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + } |
| 21 | + |
| 22 | + nodeWithoutTaint := &v1.Node{ |
| 23 | + Spec: v1.NodeSpec{ |
| 24 | + Taints: []v1.Taint{}, |
| 25 | + }, |
| 26 | + } |
| 27 | + |
| 28 | + if !HasLeafNodeTaint(nodeWithTaint) { |
| 29 | + t.Errorf("Expected node to have LeafNodeTaint") |
| 30 | + } |
| 31 | + |
| 32 | + if HasLeafNodeTaint(nodeWithoutTaint) { |
| 33 | + t.Errorf("Expected node to not have LeafNodeTaint") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestTolerationsTolerateTaint(t *testing.T) { |
| 38 | + taint := &v1.Taint{ |
| 39 | + Key: "kosmos.io/node", |
| 40 | + Value: "true", |
| 41 | + Effect: v1.TaintEffectNoSchedule, |
| 42 | + } |
| 43 | + |
| 44 | + toleration := v1.Toleration{ |
| 45 | + Key: "kosmos.io/node", |
| 46 | + Operator: v1.TolerationOpEqual, |
| 47 | + Value: "true", |
| 48 | + Effect: v1.TaintEffectNoSchedule, |
| 49 | + } |
| 50 | + |
| 51 | + tolerations := []v1.Toleration{toleration} |
| 52 | + |
| 53 | + if !TolerationsTolerateTaint(tolerations, taint) { |
| 54 | + t.Errorf("Expected tolerations to tolerate the taint") |
| 55 | + } |
| 56 | + |
| 57 | + untoleratedTaint := &v1.Taint{ |
| 58 | + Key: "kosmos.io/node-test", |
| 59 | + Value: "false", |
| 60 | + Effect: v1.TaintEffectNoSchedule, |
| 61 | + } |
| 62 | + |
| 63 | + if TolerationsTolerateTaint(tolerations, untoleratedTaint) { |
| 64 | + t.Errorf("Expected tolerations to not tolerate the untolerated taint") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestFindMatchingUntoleratedTaint(t *testing.T) { |
| 69 | + taints := []v1.Taint{ |
| 70 | + { |
| 71 | + Key: "kosmos.io/node", |
| 72 | + Value: "true", |
| 73 | + Effect: v1.TaintEffectNoSchedule, |
| 74 | + }, |
| 75 | + { |
| 76 | + Key: "example.io/another-taint", |
| 77 | + Value: "true", |
| 78 | + Effect: v1.TaintEffectNoSchedule, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + tolerations := []v1.Toleration{ |
| 83 | + { |
| 84 | + Key: "example.io/another-taint", |
| 85 | + Operator: v1.TolerationOpEqual, |
| 86 | + Value: "true", |
| 87 | + Effect: v1.TaintEffectNoSchedule, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + taint, found := FindMatchingUntoleratedTaint(taints, tolerations, nil) |
| 92 | + if found { |
| 93 | + t.Errorf("Expected to find an untolerated taint") |
| 94 | + } else if taint.Key != "" && taint.Key != "kosmos.io/node" { |
| 95 | + t.Errorf("Expected untolerated taint to be 'kosmos.io/node', got '%s'", taint.Key) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestGetFilteredTaints(t *testing.T) { |
| 100 | + taints := []v1.Taint{ |
| 101 | + { |
| 102 | + Key: "kosmos.io/node", |
| 103 | + Value: "true", |
| 104 | + Effect: v1.TaintEffectNoSchedule, |
| 105 | + }, |
| 106 | + { |
| 107 | + Key: "example.io/another-taint", |
| 108 | + Value: "true", |
| 109 | + Effect: v1.TaintEffectNoSchedule, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + filterFunc := func(t *v1.Taint) bool { |
| 114 | + return t.Key == "kosmos.io/node" |
| 115 | + } |
| 116 | + |
| 117 | + filtered := getFilteredTaints(taints, filterFunc) |
| 118 | + if len(filtered) != 1 { |
| 119 | + t.Errorf("Expected 1 filtered taint, got %d", len(filtered)) |
| 120 | + } else if filtered[0].Key != "kosmos.io/node" { |
| 121 | + t.Errorf("Expected filtered taint to be 'kosmos.io/node', got '%s'", filtered[0].Key) |
| 122 | + } |
| 123 | +} |
0 commit comments