sigs.k8s.io/cluster-api@v1.7.1/util/labels/format/helpers_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 format 18 19 import ( 20 "testing" 21 22 "github.com/onsi/gomega" 23 ) 24 25 func TestNameLabelValue(t *testing.T) { 26 g := gomega.NewWithT(t) 27 tests := []struct { 28 name string 29 machineSetName string 30 want string 31 }{ 32 { 33 name: "return the name if it's less than 63 characters", 34 machineSetName: "machineSetName", 35 want: "machineSetName", 36 }, 37 { 38 name: "return for a name with more than 63 characters", 39 machineSetName: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName", 40 want: "hash_FR_ghQ_z", 41 }, 42 } 43 for _, tt := range tests { 44 t.Run(tt.name, func(*testing.T) { 45 got := MustFormatValue(tt.machineSetName) 46 g.Expect(got).To(gomega.Equal(tt.want)) 47 }) 48 } 49 } 50 51 func TestMustMatchLabelValueForName(t *testing.T) { 52 g := gomega.NewWithT(t) 53 tests := []struct { 54 name string 55 machineSetName string 56 labelValue string 57 want bool 58 }{ 59 { 60 name: "match labels when MachineSet name is short", 61 machineSetName: "ms1", 62 labelValue: "ms1", 63 want: true, 64 }, 65 { 66 name: "don't match different labels when MachineSet name is short", 67 machineSetName: "ms1", 68 labelValue: "notMS1", 69 want: false, 70 }, 71 { 72 name: "don't match labels when MachineSet name is long", 73 machineSetName: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName", 74 labelValue: "hash_Nx4RdE_z", 75 want: false, 76 }, 77 { 78 name: "match labels when MachineSet name is long", 79 machineSetName: "machineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetNamemachineSetName", 80 labelValue: "hash_FR_ghQ_z", 81 want: true, 82 }, 83 } 84 for _, tt := range tests { 85 t.Run(tt.name, func(*testing.T) { 86 got := MustEqualValue(tt.machineSetName, tt.labelValue) 87 g.Expect(got).To(gomega.Equal(tt.want)) 88 }) 89 } 90 }