github.com/verrazzano/verrazzano@v1.7.0/pkg/k8s/node/node_test.go (about) 1 // Copyright (c) 2022, 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package node 5 6 import ( 7 "context" 8 "github.com/stretchr/testify/assert" 9 v1 "k8s.io/api/core/v1" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 k8scheme "k8s.io/client-go/kubernetes/scheme" 12 "sigs.k8s.io/controller-runtime/pkg/client/fake" 13 "testing" 14 ) 15 16 // TestGetK8sNodeList tests getting a list of Kubernetes nodes 17 func TestGetK8sNodeList(t *testing.T) { 18 asserts := assert.New(t) 19 client := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects( 20 &v1.Node{ 21 TypeMeta: metav1.TypeMeta{ 22 Kind: "Node", 23 APIVersion: "v1", 24 }, 25 ObjectMeta: metav1.ObjectMeta{ 26 Name: "node1", 27 }, 28 }, &v1.Node{ 29 TypeMeta: metav1.TypeMeta{ 30 Kind: "Node", 31 APIVersion: "v1", 32 }, 33 ObjectMeta: metav1.ObjectMeta{ 34 Name: "node2", 35 }, 36 }).Build() 37 nodeList, err := GetK8sNodeList(client) 38 asserts.NoError(err) 39 asserts.Equal(2, len(nodeList.Items)) 40 asserts.Equal("node1", nodeList.Items[0].Name) 41 asserts.Equal("node2", nodeList.Items[1].Name) 42 } 43 44 func TestSetSingleNodeTaints(t *testing.T) { 45 ctx := context.TODO() 46 workerNode := createTestNode("worker") 47 masterNode := createTestNode("master", v1.Taint{ 48 Key: controlPlaneTaint, 49 Effect: "NoSchedule", 50 }, v1.Taint{ 51 Key: masterTaint, 52 Effect: "NoSchedule", 53 }, v1.Taint{ 54 Key: "abc", 55 Effect: "xyz", 56 }) 57 cli := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(workerNode, masterNode).Build() 58 59 err := SetControlPlaneScheduling(ctx, cli) 60 assert.NoError(t, err) 61 62 nodes := &v1.NodeList{} 63 err = cli.List(ctx, nodes) 64 assert.NoError(t, err) 65 assert.Len(t, nodes.Items, 2) 66 for _, node := range nodes.Items { 67 for _, taint := range node.Spec.Taints { 68 assert.NotEqual(t, controlPlaneTaint, taint.Key) 69 assert.NotEqual(t, masterTaint, taint.Key) 70 } 71 } 72 } 73 74 func createTestNode(name string, taints ...v1.Taint) *v1.Node { 75 return &v1.Node{ 76 ObjectMeta: metav1.ObjectMeta{ 77 Name: name, 78 }, 79 Spec: v1.NodeSpec{ 80 Taints: taints, 81 }, 82 } 83 }