...
1
16
17 package nodetaint
18
19 import (
20 "context"
21 "reflect"
22
23 "testing"
24
25 v1 "k8s.io/api/core/v1"
26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/runtime"
28 "k8s.io/apiserver/pkg/admission"
29 "k8s.io/apiserver/pkg/authentication/user"
30 api "k8s.io/kubernetes/pkg/apis/core"
31 )
32
33 func Test_nodeTaints(t *testing.T) {
34 var (
35 mynode = &user.DefaultInfo{Name: "system:node:mynode", Groups: []string{"system:nodes"}}
36 resource = api.Resource("nodes").WithVersion("v1")
37 notReadyTaint = api.Taint{Key: v1.TaintNodeNotReady, Effect: api.TaintEffectNoSchedule}
38 notReadyCondition = api.NodeCondition{Type: api.NodeReady, Status: api.ConditionFalse}
39 myNodeObjMeta = metav1.ObjectMeta{Name: "mynode"}
40 myNodeObj = api.Node{ObjectMeta: myNodeObjMeta}
41 myTaintedNodeObj = api.Node{ObjectMeta: myNodeObjMeta,
42 Spec: api.NodeSpec{Taints: []api.Taint{notReadyTaint}}}
43 myUnreadyNodeObj = api.Node{ObjectMeta: myNodeObjMeta,
44 Status: api.NodeStatus{Conditions: []api.NodeCondition{notReadyCondition}}}
45 nodeKind = api.Kind("Node").WithVersion("v1")
46 )
47 tests := []struct {
48 name string
49 node api.Node
50 oldNode api.Node
51 operation admission.Operation
52 options runtime.Object
53 expectedTaints []api.Taint
54 }{
55 {
56 name: "notReady taint is added on creation",
57 node: myNodeObj,
58 operation: admission.Create,
59 options: &metav1.CreateOptions{},
60 expectedTaints: []api.Taint{notReadyTaint},
61 },
62 {
63 name: "already tainted node is not tainted again",
64 node: myTaintedNodeObj,
65 operation: admission.Create,
66 options: &metav1.CreateOptions{},
67 expectedTaints: []api.Taint{notReadyTaint},
68 },
69 {
70 name: "NotReady taint is added to an unready node as well",
71 node: myUnreadyNodeObj,
72 operation: admission.Create,
73 options: &metav1.CreateOptions{},
74 expectedTaints: []api.Taint{notReadyTaint},
75 },
76 }
77 for _, tt := range tests {
78 t.Run(tt.name, func(t *testing.T) {
79 attributes := admission.NewAttributesRecord(&tt.node, &tt.oldNode, nodeKind, myNodeObj.Namespace, myNodeObj.Name, resource, "", tt.operation, tt.options, false, mynode)
80 c := NewPlugin()
81 err := c.Admit(context.TODO(), attributes, nil)
82 if err != nil {
83 t.Errorf("nodePlugin.Admit() error = %v", err)
84 }
85 node, _ := attributes.GetObject().(*api.Node)
86 if !reflect.DeepEqual(node.Spec.Taints, tt.expectedTaints) {
87 t.Errorf("Unexpected Node taints. Got %v\nExpected: %v", node.Spec.Taints, tt.expectedTaints)
88 }
89 })
90 }
91 }
92
View as plain text