knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/helpers/name_test.go (about) 1 /* 2 Copyright 2019 The Knative 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 helpers 18 19 import ( 20 "regexp" 21 "strings" 22 "testing" 23 ) 24 25 var matcher = regexp.MustCompile("abcd-[a-z]{8}") 26 27 func TestAppendRandomString(t *testing.T) { 28 const s = "abcd" 29 w := AppendRandomString(s) 30 o := AppendRandomString(s) 31 if !matcher.MatchString(w) || !matcher.MatchString(o) || o == w { 32 t.Fatalf("Generated string(s) are incorrect: %q, %q", w, o) 33 } 34 } 35 36 func TestMakeK8sNamePrefix(t *testing.T) { 37 testCases := []struct { 38 input string 39 expected string 40 }{ 41 {"abcd123", "abcd123"}, 42 {"AbCdef", "ab-cdef"}, 43 {"ABCD", "a-b-c-d"}, 44 {"aBc*ef&d", "a-bc-ef-d"}, 45 {"*aBc*ef&d", "a-bc-ef-d"}, 46 {"AutoTLS", "auto-tls"}, 47 {"GRPCLoadBalancing", "grpc-load-balancing"}, 48 {"HTTPSTermination", "https-termination"}, 49 {"HTTPIsNotHTTP2", "http-is-not-http2"}, 50 } 51 for _, tc := range testCases { 52 t.Run(tc.input, func(t *testing.T) { 53 actual := MakeK8sNamePrefix(tc.input) 54 if tc.expected != actual { 55 t.Errorf("MakeK8sNamePrefix = %q, want: %q", actual, tc.expected) 56 } 57 }) 58 } 59 } 60 61 func TestGetBaseFuncName(t *testing.T) { 62 testCases := []struct { 63 input string 64 expected string 65 }{ 66 {"test/e2e.TestMain", "TestMain"}, 67 {"e2e.TestMain", "TestMain"}, 68 {"test/TestMain", "TestMain"}, 69 {"TestMain", "TestMain"}, 70 } 71 for _, v := range testCases { 72 actual := GetBaseFuncName(v.input) 73 if v.expected != actual { 74 t.Fatalf("Expect %q but actual is %q", v.expected, actual) 75 } 76 } 77 } 78 79 func TestObjectNameForTest(t *testing.T) { 80 testCases := []struct { 81 input testNamed 82 expectedPrefix string 83 }{ 84 {testNamed{name: "TestFooBar"}, "foo-bar-"}, 85 {testNamed{name: "Foo-bar"}, "foo-bar-"}, 86 {testNamed{name: "with_underscore"}, "with-underscore-"}, 87 {testNamed{name: "WithHTTP"}, "with-http-"}, 88 {testNamed{name: "ANameExceedingTheLimitLength-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, "a-name-exceeding-the-limit-length-aaaaaaa-"}, 89 } 90 for _, v := range testCases { 91 actual := ObjectNameForTest(&v.input) 92 if !strings.HasPrefix(actual, v.expectedPrefix) { 93 t.Fatalf("Expect prefix %q but actual is %q", v.expectedPrefix, actual) 94 } 95 } 96 } 97 98 type testNamed struct { 99 name string 100 } 101 102 func (n *testNamed) Name() string { 103 return n.name 104 }