github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/chart/metadata_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  package chart
    17  
    18  import (
    19  	"testing"
    20  )
    21  
    22  func TestValidate(t *testing.T) {
    23  	tests := []struct {
    24  		md  *Metadata
    25  		err error
    26  	}{
    27  		{
    28  			nil,
    29  			ValidationError("chart.metadata is required"),
    30  		},
    31  		{
    32  			&Metadata{Name: "test", Version: "1.0"},
    33  			ValidationError("chart.metadata.apiVersion is required"),
    34  		},
    35  		{
    36  			&Metadata{APIVersion: "v2", Version: "1.0"},
    37  			ValidationError("chart.metadata.name is required"),
    38  		},
    39  		{
    40  			&Metadata{Name: "test", APIVersion: "v2"},
    41  			ValidationError("chart.metadata.version is required"),
    42  		},
    43  		{
    44  			&Metadata{Name: "test", APIVersion: "v2", Version: "1.0", Type: "test"},
    45  			ValidationError("chart.metadata.type must be application or library"),
    46  		},
    47  		{
    48  			&Metadata{Name: "test", APIVersion: "v2", Version: "1.0", Type: "application"},
    49  			nil,
    50  		},
    51  		{
    52  			&Metadata{
    53  				Name:       "test",
    54  				APIVersion: "v2",
    55  				Version:    "1.0",
    56  				Type:       "application",
    57  				Dependencies: []*Dependency{
    58  					{Name: "dependency", Alias: "legal-alias"},
    59  				},
    60  			},
    61  			nil,
    62  		},
    63  		{
    64  			&Metadata{
    65  				Name:       "test",
    66  				APIVersion: "v2",
    67  				Version:    "1.0",
    68  				Type:       "application",
    69  				Dependencies: []*Dependency{
    70  					{Name: "bad", Alias: "illegal alias"},
    71  				},
    72  			},
    73  			ValidationError("dependency \"bad\" has disallowed characters in the alias"),
    74  		},
    75  		{
    76  			&Metadata{
    77  				Name:       "test",
    78  				APIVersion: "v2",
    79  				Version:    "1.0",
    80  				Type:       "application",
    81  				Dependencies: []*Dependency{
    82  					nil,
    83  				},
    84  			},
    85  			ValidationError("dependency cannot be an empty list"),
    86  		},
    87  		{
    88  			&Metadata{
    89  				Name:       "test",
    90  				APIVersion: "v2",
    91  				Version:    "1.0",
    92  				Type:       "application",
    93  				Maintainers: []*Maintainer{
    94  					nil,
    95  				},
    96  			},
    97  			ValidationError("maintainer cannot be an empty list"),
    98  		},
    99  		{
   100  			&Metadata{APIVersion: "v2", Name: "test", Version: "1.2.3.4"},
   101  			ValidationError("chart.metadata.version \"1.2.3.4\" is invalid"),
   102  		},
   103  	}
   104  
   105  	for _, tt := range tests {
   106  		result := tt.md.Validate()
   107  		if result != tt.err {
   108  			t.Errorf("expected '%s', got '%s'", tt.err, result)
   109  		}
   110  	}
   111  }
   112  
   113  func TestValidate_sanitize(t *testing.T) {
   114  	md := &Metadata{APIVersion: "v2", Name: "test", Version: "1.0", Description: "\adescr\u0081iption\rtest", Maintainers: []*Maintainer{{Name: "\r"}}}
   115  	if err := md.Validate(); err != nil {
   116  		t.Fatalf("unexpected error: %s", err)
   117  	}
   118  	if md.Description != "description test" {
   119  		t.Fatalf("description was not sanitized: %q", md.Description)
   120  	}
   121  	if md.Maintainers[0].Name != " " {
   122  		t.Fatal("maintainer name was not sanitized")
   123  	}
   124  }