...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s/name_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s

     1  // Copyright 2022 Google LLC
     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 k8s_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    21  )
    22  
    23  func TestValueToDNSSubdomainName(t *testing.T) {
    24  	testCases := []struct {
    25  		Name           string
    26  		Value          string
    27  		ExpectedResult string
    28  	}{
    29  		{
    30  			Name:           "empty string",
    31  			Value:          "",
    32  			ExpectedResult: "a",
    33  		},
    34  		{
    35  			Name:           "Strip non-alphanumeric",
    36  			Value:          "this-name-contains-#chars;_that#shouldbe-replaced",
    37  			ExpectedResult: "this-name-contains--chars--that-shouldbe-replaced",
    38  		},
    39  		{
    40  			Name:           "Periods and hyphens",
    41  			Value:          "this.is-legal",
    42  			ExpectedResult: "this.is-legal",
    43  		},
    44  		{
    45  			Name:           "Number",
    46  			Value:          "0123456789",
    47  			ExpectedResult: "0123456789",
    48  		},
    49  		{
    50  			Name:           "hyphen prefix",
    51  			Value:          "-value",
    52  			ExpectedResult: "a-value",
    53  		},
    54  		{
    55  			Name:           "hyphen prefix period suffix",
    56  			Value:          "-value.",
    57  			ExpectedResult: "a-value.a",
    58  		},
    59  	}
    60  	for _, tc := range testCases {
    61  		t.Run(tc.Name, func(t *testing.T) {
    62  			result := k8s.ValueToDNSSubdomainName(tc.Value)
    63  			if result != tc.ExpectedResult {
    64  				t.Fatalf("result mismatch: got '%v', want '%v'", result, tc.ExpectedResult)
    65  			}
    66  		})
    67  	}
    68  }
    69  

View as plain text