sigs.k8s.io/cluster-api@v1.7.1/api/v1beta1/index/machine_test.go (about) 1 /* 2 Copyright 2021 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 index 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/utils/ptr" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 27 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 28 ) 29 30 func TestIndexMachineByNodeName(t *testing.T) { 31 testCases := []struct { 32 name string 33 object client.Object 34 expected []string 35 }{ 36 { 37 name: "when the machine has no NodeRef", 38 object: &clusterv1.Machine{}, 39 expected: []string{}, 40 }, 41 { 42 name: "when the machine has valid a NodeRef", 43 object: &clusterv1.Machine{ 44 Status: clusterv1.MachineStatus{ 45 NodeRef: &corev1.ObjectReference{ 46 Name: "node1", 47 }, 48 }, 49 }, 50 expected: []string{"node1"}, 51 }, 52 } 53 54 for _, tc := range testCases { 55 t.Run(tc.name, func(t *testing.T) { 56 g := NewWithT(t) 57 got := MachineByNodeName(tc.object) 58 g.Expect(got).To(ConsistOf(tc.expected)) 59 }) 60 } 61 } 62 63 func TestIndexMachineByProviderID(t *testing.T) { 64 validProviderID := "aws://region/zone/id" 65 66 testCases := []struct { 67 name string 68 object client.Object 69 expected []string 70 }{ 71 { 72 name: "Machine has no providerID", 73 object: &clusterv1.Machine{}, 74 expected: nil, 75 }, 76 { 77 name: "Machine has invalid providerID", 78 object: &clusterv1.Machine{ 79 Spec: clusterv1.MachineSpec{ 80 ProviderID: ptr.To(""), 81 }, 82 }, 83 expected: nil, 84 }, 85 { 86 name: "Machine has valid providerID", 87 object: &clusterv1.Machine{ 88 Spec: clusterv1.MachineSpec{ 89 ProviderID: ptr.To(validProviderID), 90 }, 91 }, 92 expected: []string{validProviderID}, 93 }, 94 } 95 96 for _, tc := range testCases { 97 t.Run(tc.name, func(t *testing.T) { 98 g := NewWithT(t) 99 got := machineByProviderID(tc.object) 100 g.Expect(got).To(BeEquivalentTo(tc.expected)) 101 }) 102 } 103 }