k8s.io/client-go@v0.31.1/tools/cache/object-names_test.go (about) 1 /* 2 Copyright 2023 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 cache 18 19 import ( 20 "math/rand" 21 "strings" 22 "testing" 23 ) 24 25 func TestObjectNames(t *testing.T) { 26 chars := "abcdefghi/" 27 for count := 1; count <= 100; count++ { 28 var encodedB strings.Builder 29 for index := 0; index < 10; index++ { 30 encodedB.WriteByte(chars[rand.Intn(len(chars))]) 31 } 32 encodedS := encodedB.String() 33 parts := strings.Split(encodedS, "/") 34 on, err := ParseObjectName(encodedS) 35 expectError := len(parts) > 2 36 if expectError != (err != nil) { 37 t.Errorf("Wrong error; expected=%v, got=%v", expectError, err) 38 } 39 if expectError || err != nil { 40 continue 41 } 42 var expectedObjectName ObjectName 43 if len(parts) == 2 { 44 expectedObjectName = ObjectName{Namespace: parts[0], Name: parts[1]} 45 } else { 46 expectedObjectName = ObjectName{Name: encodedS} 47 } 48 if on != expectedObjectName { 49 t.Errorf("Parse failed, expected=%+v, got=%+v", expectedObjectName, on) 50 } 51 recoded := on.String() 52 if encodedS[0] == '/' { 53 recoded = "/" + recoded 54 } 55 if encodedS != recoded { 56 t.Errorf("Parse().String() was not identity, original=%q, final=%q", encodedS, recoded) 57 } 58 } 59 }