istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/resource/name_test.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package resource 16 17 import ( 18 "testing" 19 ) 20 21 func TestFullNameString(t *testing.T) { 22 n := NewFullName("ns1", "l1") 23 if n.String() != "ns1/l1" { 24 t.Fatalf("unexpected name string: %v", n.String()) 25 } 26 } 27 28 func TestFullNameString_NoName(t *testing.T) { 29 n := NewFullName("ns1", "") 30 if n.String() != "ns1/" { 31 t.Fatalf("unexpected name string: %v", n.String()) 32 } 33 } 34 35 func TestFullNameString_NoNamespace(t *testing.T) { 36 n := NewFullName("", "l1") 37 if n.String() != "l1" { 38 t.Fatalf("unexpected name string: %v", n.String()) 39 } 40 } 41 42 func TestParseFullName_Segments(t *testing.T) { 43 steps := []struct { 44 description string 45 name string 46 want string 47 err string 48 valid bool 49 }{ 50 { 51 description: "namespace with emapty name", 52 name: "testNamespace/", 53 want: "", 54 err: "failed parsing name 'testNamespace/': invalid name 'testNamespace/': name must not be empty", 55 valid: false, 56 }, 57 { 58 description: "name with empty namespace", 59 name: "/testName", 60 want: "testName", 61 err: "", 62 valid: true, 63 }, 64 { 65 description: "all empty", 66 name: "/", 67 want: "", 68 err: "failed parsing name '/': invalid name '': name must not be empty", 69 valid: false, 70 }, 71 { 72 description: "multiple segments", // Only the first segment is treated specially 73 name: "testName//someotherStuff", 74 want: "testName//someotherStuff", 75 err: "", 76 valid: true, 77 }, 78 { 79 description: "valid name with namespace", 80 name: "testNamespace/testName", 81 want: "testNamespace/testName", 82 valid: true, 83 }, 84 } 85 for _, s := range steps { 86 t.Run(s.description, func(tt *testing.T) { 87 n, err := ParseFullName(s.name) 88 if err == nil && n.String() != s.want { 89 tt.Fatalf("unexpected name: %v", n) 90 } 91 if s.valid { 92 if err != nil { 93 tt.Fatalf("expected no err but got: %v", err) 94 } 95 return 96 } 97 if err == nil { 98 tt.Fatalf("expected err but got: %v", err) 99 } 100 if err.Error() != s.err { 101 tt.Fatalf("expected err \"%s\" but got: %v", s.err, err.Error()) 102 } 103 }) 104 } 105 } 106 107 func TestNewShortOrFullName_HasNamespace(t *testing.T) { 108 actual := NewShortOrFullName("ns1", "ns2/l1") 109 expected := NewFullName("ns2", "l1") 110 if actual != expected { 111 t.Fatalf("Expected %v, got %v", expected, actual) 112 } 113 } 114 115 func TestNewShortOrFullName_NoNamespace(t *testing.T) { 116 actual := NewShortOrFullName("ns1", "l1") 117 expected := NewFullName("ns1", "l1") 118 if actual != expected { 119 t.Fatalf("Expected %v, got %v", expected, actual) 120 } 121 }