github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/chartutil/validate_name_test.go (about)

     1  /*
     2  Copyright The Helm 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 chartutil
    18  
    19  import "testing"
    20  
    21  // TestValidateName is a regression test for ValidateName
    22  //
    23  // Kubernetes has strict naming conventions for resource names. This test represents
    24  // those conventions.
    25  //
    26  // See https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
    27  //
    28  // NOTE: At the time of this writing, the docs above say that names cannot begin with
    29  // digits. However, `kubectl`'s regular expression explicit allows this, and
    30  // Kubernetes (at least as of 1.18) also accepts resources whose names begin with digits.
    31  func TestValidateReleaseName(t *testing.T) {
    32  	names := map[string]bool{
    33  		"":                          false,
    34  		"foo":                       true,
    35  		"foo.bar1234baz.seventyone": true,
    36  		"FOO":                       false,
    37  		"123baz":                    true,
    38  		"foo.BAR.baz":               false,
    39  		"one-two":                   true,
    40  		"-two":                      false,
    41  		"one_two":                   false,
    42  		"a..b":                      false,
    43  		"%^&#$%*@^*@&#^":            false,
    44  		"example:com":               false,
    45  		"example%%com":              false,
    46  		"a1111111111111111111111111111111111111111111111111111111111z": false,
    47  	}
    48  	for input, expectPass := range names {
    49  		if err := ValidateReleaseName(input); (err == nil) != expectPass {
    50  			st := "fail"
    51  			if expectPass {
    52  				st = "succeed"
    53  			}
    54  			t.Errorf("Expected %q to %s", input, st)
    55  		}
    56  	}
    57  }
    58  
    59  func TestValidateMetadataName(t *testing.T) {
    60  	names := map[string]bool{
    61  		"":                          false,
    62  		"foo":                       true,
    63  		"foo.bar1234baz.seventyone": true,
    64  		"FOO":                       false,
    65  		"123baz":                    true,
    66  		"foo.BAR.baz":               false,
    67  		"one-two":                   true,
    68  		"-two":                      false,
    69  		"one_two":                   false,
    70  		"a..b":                      false,
    71  		"%^&#$%*@^*@&#^":            false,
    72  		"example:com":               false,
    73  		"example%%com":              false,
    74  		"a1111111111111111111111111111111111111111111111111111111111z": true,
    75  		"a1111111111111111111111111111111111111111111111111111111111z" +
    76  			"a1111111111111111111111111111111111111111111111111111111111z" +
    77  			"a1111111111111111111111111111111111111111111111111111111111z" +
    78  			"a1111111111111111111111111111111111111111111111111111111111z" +
    79  			"a1111111111111111111111111111111111111111111111111111111111z" +
    80  			"a1111111111111111111111111111111111111111111111111111111111z": false,
    81  	}
    82  	for input, expectPass := range names {
    83  		if err := ValidateMetadataName(input); (err == nil) != expectPass {
    84  			st := "fail"
    85  			if expectPass {
    86  				st = "succeed"
    87  			}
    88  			t.Errorf("Expected %q to %s", input, st)
    89  		}
    90  	}
    91  }