|
| 1 | +/* |
| 2 | +Copyright 2026 The Cozystack 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 model |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | +) |
| 24 | + |
| 25 | +func readyNode(mutate func(*corev1.Node)) *corev1.Node { |
| 26 | + n := &corev1.Node{Status: corev1.NodeStatus{Conditions: []corev1.NodeCondition{{Type: corev1.NodeReady, Status: corev1.ConditionTrue}}}} |
| 27 | + n.Name = "gpu-node-3" |
| 28 | + if mutate != nil { |
| 29 | + mutate(n) |
| 30 | + } |
| 31 | + return n |
| 32 | +} |
| 33 | + |
| 34 | +func withTaint(key string, effect corev1.TaintEffect) func(*corev1.Node) { |
| 35 | + return func(n *corev1.Node) { |
| 36 | + n.Spec.Taints = append(n.Spec.Taints, corev1.Taint{Key: key, Effect: effect}) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +const farTaint = "medik8s.io/fence-agents-remediation" |
| 41 | + |
| 42 | +func TestCordoned(t *testing.T) { |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + node *corev1.Node |
| 46 | + want bool |
| 47 | + }{ |
| 48 | + {"unschedulable", readyNode(func(n *corev1.Node) { n.Spec.Unschedulable = true }), true}, |
| 49 | + {"schedulable", readyNode(nil), false}, |
| 50 | + {"Fence Agents Remediation taint alone", readyNode(withTaint(farTaint, corev1.TaintEffectNoSchedule)), false}, |
| 51 | + {"unschedulable taint without the field", readyNode(withTaint(corev1.TaintNodeUnschedulable, corev1.TaintEffectNoSchedule)), false}, |
| 52 | + } |
| 53 | + for _, tc := range tests { |
| 54 | + if got := Cordoned(tc.node); got != tc.want { |
| 55 | + t.Errorf("%s: Cordoned = %v, want %v", tc.name, got, tc.want) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestReady(t *testing.T) { |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + node *corev1.Node |
| 64 | + want bool |
| 65 | + }{ |
| 66 | + {"ready", readyNode(nil), true}, |
| 67 | + {"not ready", readyNode(func(n *corev1.Node) { n.Status.Conditions[0].Status = corev1.ConditionFalse }), false}, |
| 68 | + {"unknown", readyNode(func(n *corev1.Node) { n.Status.Conditions[0].Status = corev1.ConditionUnknown }), false}, |
| 69 | + {"no condition", readyNode(func(n *corev1.Node) { n.Status.Conditions = nil }), false}, |
| 70 | + } |
| 71 | + for _, tc := range tests { |
| 72 | + if got := Ready(tc.node); got != tc.want { |
| 73 | + t.Errorf("%s: Ready = %v, want %v", tc.name, got, tc.want) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestEligible(t *testing.T) { |
| 79 | + tolerateFAR := []corev1.Toleration{{Key: farTaint, Operator: corev1.TolerationOpExists}} |
| 80 | + tests := []struct { |
| 81 | + name string |
| 82 | + node *corev1.Node |
| 83 | + tolerations []corev1.Toleration |
| 84 | + want bool |
| 85 | + }{ |
| 86 | + {name: "ready, schedulable, untainted", node: readyNode(nil), want: true}, |
| 87 | + {name: "cordoned", node: readyNode(func(n *corev1.Node) { n.Spec.Unschedulable = true }), want: false}, |
| 88 | + {name: "not ready", node: readyNode(func(n *corev1.Node) { n.Status.Conditions[0].Status = corev1.ConditionFalse }), want: false}, |
| 89 | + {name: "untolerated NoSchedule", node: readyNode(withTaint(farTaint, corev1.TaintEffectNoSchedule)), want: false}, |
| 90 | + {name: "untolerated NoExecute", node: readyNode(withTaint("example.com/maintenance", corev1.TaintEffectNoExecute)), want: false}, |
| 91 | + {name: "tolerated NoSchedule", node: readyNode(withTaint(farTaint, corev1.TaintEffectNoSchedule)), tolerations: tolerateFAR, want: true}, |
| 92 | + {name: "PreferNoSchedule does not count", node: readyNode(withTaint("example.com/soft", corev1.TaintEffectPreferNoSchedule)), want: true}, |
| 93 | + // The node.kubernetes.io taints mirror cordon and readiness, which |
| 94 | + // the field and the condition already decide. |
| 95 | + {name: "node.kubernetes.io/unschedulable taint on a schedulable node", node: readyNode(withTaint(corev1.TaintNodeUnschedulable, corev1.TaintEffectNoSchedule)), want: true}, |
| 96 | + {name: "node.kubernetes.io/not-ready taint on a ready node", node: readyNode(withTaint(corev1.TaintNodeNotReady, corev1.TaintEffectNoExecute)), want: true}, |
| 97 | + { |
| 98 | + name: "Gt toleration does not count", |
| 99 | + node: readyNode(func(n *corev1.Node) { |
| 100 | + n.Spec.Taints = []corev1.Taint{{Key: "example.com/gpu-gen", Value: "5", Effect: corev1.TaintEffectNoSchedule}} |
| 101 | + }), |
| 102 | + tolerations: []corev1.Toleration{{Key: "example.com/gpu-gen", Operator: corev1.TolerationOpGt, Value: "3", Effect: corev1.TaintEffectNoSchedule}}, |
| 103 | + want: false, |
| 104 | + }, |
| 105 | + {name: "toleration of another effect", node: readyNode(withTaint(farTaint, corev1.TaintEffectNoExecute)), tolerations: []corev1.Toleration{{Key: farTaint, Operator: corev1.TolerationOpExists, Effect: corev1.TaintEffectNoSchedule}}, want: false}, |
| 106 | + } |
| 107 | + for _, tc := range tests { |
| 108 | + if got := Eligible(tc.node, tc.tolerations); got != tc.want { |
| 109 | + t.Errorf("%s: Eligible = %v, want %v", tc.name, got, tc.want) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments