sigs.k8s.io/cluster-api@v1.7.1/util/labels/helpers_test.go (about) 1 /* 2 Copyright 2020 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 labels 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 corev1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 26 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 27 ) 28 29 func TestHasWatchLabel(t *testing.T) { 30 g := NewWithT(t) 31 32 var testcases = []struct { 33 name string 34 obj metav1.Object 35 input string 36 expected bool 37 }{ 38 { 39 name: "should handle empty input", 40 obj: &corev1.Node{ 41 ObjectMeta: metav1.ObjectMeta{ 42 Labels: map[string]string{ 43 "foo": "bar", 44 }, 45 }, 46 Spec: corev1.NodeSpec{}, 47 Status: corev1.NodeStatus{}, 48 }, 49 input: "", 50 expected: false, 51 }, 52 { 53 name: "should return false if no input string is give", 54 obj: &corev1.Node{ 55 ObjectMeta: metav1.ObjectMeta{ 56 Labels: map[string]string{ 57 clusterv1.WatchLabel: "bar", 58 }, 59 }, 60 }, 61 input: "", 62 expected: false, 63 }, 64 { 65 name: "should return true if label matches", 66 obj: &corev1.Node{ 67 ObjectMeta: metav1.ObjectMeta{ 68 Labels: map[string]string{ 69 clusterv1.WatchLabel: "bar", 70 }, 71 }, 72 Spec: corev1.NodeSpec{}, 73 Status: corev1.NodeStatus{}, 74 }, 75 input: "bar", 76 expected: true, 77 }, 78 } 79 80 for _, tc := range testcases { 81 t.Run(tc.name, func(*testing.T) { 82 res := HasWatchLabel(tc.obj, tc.input) 83 g.Expect(res).To(Equal(tc.expected)) 84 }) 85 } 86 } 87 88 func TestIsMachinePoolOwned(t *testing.T) { 89 tests := []struct { 90 name string 91 object metav1.Object 92 expected bool 93 }{ 94 { 95 name: "machine is a MachinePoolMachine", 96 object: &clusterv1.Machine{ 97 ObjectMeta: metav1.ObjectMeta{ 98 Labels: map[string]string{ 99 clusterv1.MachinePoolNameLabel: "foo", 100 }, 101 }, 102 }, 103 expected: true, 104 }, 105 { 106 name: "random type is machinepool owned", 107 object: &corev1.Node{ 108 ObjectMeta: metav1.ObjectMeta{ 109 Labels: map[string]string{ 110 clusterv1.MachinePoolNameLabel: "foo", 111 }, 112 }, 113 }, 114 expected: true, 115 }, 116 { 117 name: "machine not a MachinePoolMachine with random labels", 118 object: &clusterv1.Machine{ 119 ObjectMeta: metav1.ObjectMeta{ 120 Labels: map[string]string{ 121 "foo": "bar", 122 }, 123 }, 124 }, 125 expected: false, 126 }, 127 { 128 name: "machine is not a MachinePoolMachine with no labels", 129 object: &clusterv1.Machine{}, 130 expected: false, 131 }, 132 } 133 134 for i := range tests { 135 tt := tests[i] 136 t.Run(tt.name, func(t *testing.T) { 137 g := NewWithT(t) 138 139 result := IsMachinePoolOwned(tt.object) 140 g.Expect(result).To(Equal(tt.expected)) 141 }) 142 } 143 }