knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kmeta/names_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 kmeta
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"k8s.io/apimachinery/pkg/util/validation"
    25  )
    26  
    27  func TestChildName(t *testing.T) {
    28  	tests := []struct {
    29  		parent string
    30  		suffix string
    31  		want   string
    32  	}{
    33  		{
    34  			parent: "asdf",
    35  			suffix: "-deployment",
    36  			want:   "asdf-deployment",
    37  		}, {
    38  			parent: strings.Repeat("f", 63),
    39  			suffix: "-deployment",
    40  			want:   "ffffffffffffffffffff105d7597f637e83cc711605ac3ea4957-deployment",
    41  		}, {
    42  			parent: strings.Repeat("f", 63),
    43  			suffix: "-deploy",
    44  			want:   "ffffffffffffffffffffffff105d7597f637e83cc711605ac3ea4957-deploy",
    45  		}, {
    46  			parent: strings.Repeat("f", 63),
    47  			suffix: strings.Repeat("f", 63),
    48  			want:   "fffffffffffffffffffffffffffffff0502661254f13c89973cb3a83e0cbec0",
    49  		}, {
    50  			parent: "a",
    51  			suffix: strings.Repeat("f", 63),
    52  			want:   "ab5cfd486935decbc0d305799f4ce4414ffffffffffffffffffffffffffffff",
    53  		}, {
    54  			parent: strings.Repeat("b", 32),
    55  			suffix: strings.Repeat("f", 32),
    56  			want:   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb329c7c81b9ab3ba71aa139066aa5625d",
    57  		}, {
    58  			parent: "aaaa",
    59  			suffix: strings.Repeat("b---a", 20),
    60  			want:   "aaaa7a3f7966594e3f0849720eced8212c18b---ab---ab---ab---ab---ab",
    61  		}, {
    62  			parent: strings.Repeat("a", 17),
    63  			suffix: "a.-.-.-.-.-.-.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    64  			want:   "aaaaaaaaaaaaaaaaaa1eb10dc911444f8434af83b7225442da",
    65  		},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		t.Run(test.parent+"-"+test.suffix, func(t *testing.T) {
    70  			got, want := ChildName(test.parent, test.suffix), test.want
    71  			if errs := validation.IsDNS1123Subdomain(got); len(errs) != 0 {
    72  				t.Errorf("Invalid DNS1123 Subdomain %63s\n\n Errors: %v", got, errs)
    73  			}
    74  			if got != want {
    75  				t.Errorf("%s-%s: got: %63s want: %63s\ndiff:%s", test.parent, test.suffix, got, want, cmp.Diff(want, got))
    76  			}
    77  		})
    78  	}
    79  }