github.com/koderover/helm@v2.17.0+incompatible/pkg/lint/rules/chartfile_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 rules
    18  
    19  import (
    20  	"errors"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"k8s.io/helm/pkg/chartutil"
    27  	"k8s.io/helm/pkg/lint/support"
    28  	"k8s.io/helm/pkg/proto/hapi/chart"
    29  )
    30  
    31  const (
    32  	badChartDir     = "testdata/badchartfile"
    33  	badNameChartDir = "testdata/badnamechart"
    34  	goodChartDir    = "testdata/goodone"
    35  )
    36  
    37  var (
    38  	badChartFilePath         = filepath.Join(badChartDir, "Chart.yaml")
    39  	badNameChartFilePath     = filepath.Join(badNameChartDir, "Chart.yaml")
    40  	goodChartFilePath        = filepath.Join(goodChartDir, "Chart.yaml")
    41  	nonExistingChartFilePath = filepath.Join(os.TempDir(), "Chart.yaml")
    42  )
    43  
    44  var badChart, chatLoadRrr = chartutil.LoadChartfile(badChartFilePath)
    45  var badNameChart, _ = chartutil.LoadChartfile(badNameChartFilePath)
    46  var goodChart, _ = chartutil.LoadChartfile(goodChartFilePath)
    47  
    48  // Validation functions Test
    49  func TestValidateChartYamlNotDirectory(t *testing.T) {
    50  	_ = os.Mkdir(nonExistingChartFilePath, os.ModePerm)
    51  	defer os.Remove(nonExistingChartFilePath)
    52  
    53  	err := validateChartYamlNotDirectory(nonExistingChartFilePath)
    54  	if err == nil {
    55  		t.Errorf("validateChartYamlNotDirectory to return a linter error, got no error")
    56  	}
    57  }
    58  
    59  func TestValidateChartYamlFormat(t *testing.T) {
    60  	err := validateChartYamlFormat(errors.New("Read error"))
    61  	if err == nil {
    62  		t.Errorf("validateChartYamlFormat to return a linter error, got no error")
    63  	}
    64  
    65  	err = validateChartYamlFormat(nil)
    66  	if err != nil {
    67  		t.Errorf("validateChartYamlFormat to return no error, got a linter error")
    68  	}
    69  }
    70  
    71  func TestValidateChartName(t *testing.T) {
    72  	err := validateChartNamePresence(badChart)
    73  	if err == nil {
    74  		t.Errorf("validateChartName to return a linter error, got no error")
    75  	}
    76  }
    77  
    78  func TestValidateChartNameFormat(t *testing.T) {
    79  	err := validateChartNameFormat(badNameChart)
    80  	if err == nil {
    81  		t.Errorf("validateChartNameFormat to return a linter error, got no error")
    82  	}
    83  }
    84  
    85  func TestValidateChartNameDirMatch(t *testing.T) {
    86  	err := validateChartNameDirMatch(goodChartDir, goodChart)
    87  	if err != nil {
    88  		t.Errorf("validateChartNameDirMatch to return no error, gor a linter error")
    89  	}
    90  	// It has not name
    91  	err = validateChartNameDirMatch(badChartDir, badChart)
    92  	if err == nil {
    93  		t.Errorf("validatechartnamedirmatch to return a linter error, got no error")
    94  	}
    95  
    96  	// Wrong path
    97  	err = validateChartNameDirMatch(badChartDir, goodChart)
    98  	if err == nil {
    99  		t.Errorf("validatechartnamedirmatch to return a linter error, got no error")
   100  	}
   101  }
   102  
   103  func TestValidateChartVersion(t *testing.T) {
   104  	var failTest = []struct {
   105  		Version  string
   106  		ErrorMsg string
   107  	}{
   108  		{"", "version is required"},
   109  		{"1.2.3.4", "version '1.2.3.4' is not a valid SemVer"},
   110  		{"waps", "'waps' is not a valid SemVer"},
   111  		{"-3", "'-3' is not a valid SemVer"},
   112  	}
   113  
   114  	var successTest = []string{"0.0.1", "0.0.1+build", "0.0.1-beta"}
   115  
   116  	for _, test := range failTest {
   117  		badChart.Version = test.Version
   118  		err := validateChartVersion(badChart)
   119  		if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) {
   120  			t.Errorf("validateChartVersion(%s) to return \"%s\", got no error", test.Version, test.ErrorMsg)
   121  		}
   122  	}
   123  
   124  	for _, version := range successTest {
   125  		badChart.Version = version
   126  		err := validateChartVersion(badChart)
   127  		if err != nil {
   128  			t.Errorf("validateChartVersion(%s) to return no error, got a linter error", version)
   129  		}
   130  	}
   131  }
   132  
   133  func TestValidateChartEngine(t *testing.T) {
   134  	var successTest = []string{"", "gotpl"}
   135  
   136  	for _, engine := range successTest {
   137  		badChart.Engine = engine
   138  		err := validateChartEngine(badChart)
   139  		if err != nil {
   140  			t.Errorf("validateChartEngine(%s) to return no error, got a linter error %s", engine, err.Error())
   141  		}
   142  	}
   143  
   144  	badChart.Engine = "foobar"
   145  	err := validateChartEngine(badChart)
   146  	if err == nil || !strings.Contains(err.Error(), "not valid. Valid options are [gotpl") {
   147  		t.Errorf("validateChartEngine(%s) to return an error, got no error", badChart.Engine)
   148  	}
   149  }
   150  
   151  func TestValidateChartMaintainer(t *testing.T) {
   152  	var failTest = []struct {
   153  		Name     string
   154  		Email    string
   155  		ErrorMsg string
   156  	}{
   157  		{"", "", "each maintainer requires a name"},
   158  		{"", "test@test.com", "each maintainer requires a name"},
   159  		{"John Snow", "wrongFormatEmail.com", "invalid email"},
   160  	}
   161  
   162  	var successTest = []struct {
   163  		Name  string
   164  		Email string
   165  	}{
   166  		{"John Snow", ""},
   167  		{"John Snow", "john@winterfell.com"},
   168  	}
   169  
   170  	for _, test := range failTest {
   171  		badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}}
   172  		err := validateChartMaintainer(badChart)
   173  		if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) {
   174  			t.Errorf("validateChartMaintainer(%s, %s) to return \"%s\", got no error", test.Name, test.Email, test.ErrorMsg)
   175  		}
   176  	}
   177  
   178  	for _, test := range successTest {
   179  		badChart.Maintainers = []*chart.Maintainer{{Name: test.Name, Email: test.Email}}
   180  		err := validateChartMaintainer(badChart)
   181  		if err != nil {
   182  			t.Errorf("validateChartMaintainer(%s, %s) to return no error, got %s", test.Name, test.Email, err.Error())
   183  		}
   184  	}
   185  }
   186  
   187  func TestValidateChartSources(t *testing.T) {
   188  	var failTest = []string{"", "RiverRun", "john@winterfell", "riverrun.io"}
   189  	var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish"}
   190  	for _, test := range failTest {
   191  		badChart.Sources = []string{test}
   192  		err := validateChartSources(badChart)
   193  		if err == nil || !strings.Contains(err.Error(), "invalid source URL") {
   194  			t.Errorf("validateChartSources(%s) to return \"invalid source URL\", got no error", test)
   195  		}
   196  	}
   197  
   198  	for _, test := range successTest {
   199  		badChart.Sources = []string{test}
   200  		err := validateChartSources(badChart)
   201  		if err != nil {
   202  			t.Errorf("validateChartSources(%s) to return no error, got %s", test, err.Error())
   203  		}
   204  	}
   205  }
   206  
   207  func TestValidateChartIconPresence(t *testing.T) {
   208  	err := validateChartIconPresence(badChart)
   209  	if err == nil {
   210  		t.Errorf("validateChartIconPresence to return a linter error, got no error")
   211  	}
   212  }
   213  
   214  func TestValidateChartIconURL(t *testing.T) {
   215  	var failTest = []string{"RiverRun", "john@winterfell", "riverrun.io"}
   216  	var successTest = []string{"http://riverrun.io", "https://riverrun.io", "https://riverrun.io/blackfish.png"}
   217  	for _, test := range failTest {
   218  		badChart.Icon = test
   219  		err := validateChartIconURL(badChart)
   220  		if err == nil || !strings.Contains(err.Error(), "invalid icon URL") {
   221  			t.Errorf("validateChartIconURL(%s) to return \"invalid icon URL\", got no error", test)
   222  		}
   223  	}
   224  
   225  	for _, test := range successTest {
   226  		badChart.Icon = test
   227  		err := validateChartSources(badChart)
   228  		if err != nil {
   229  			t.Errorf("validateChartIconURL(%s) to return no error, got %s", test, err.Error())
   230  		}
   231  	}
   232  }
   233  
   234  func TestChartfile(t *testing.T) {
   235  	linter := support.Linter{ChartDir: badChartDir}
   236  	Chartfile(&linter)
   237  	msgs := linter.Messages
   238  
   239  	if len(msgs) != 5 {
   240  		t.Errorf("Expected 4 errors, got %d", len(msgs))
   241  	}
   242  
   243  	if !strings.Contains(msgs[0].Err.Error(), "name is required") {
   244  		t.Errorf("Unexpected message 0: %s", msgs[0].Err)
   245  	}
   246  
   247  	if !strings.Contains(msgs[1].Err.Error(), "directory name (badchartfile) and chart name () must be the same") {
   248  		t.Errorf("Unexpected message 1: %s", msgs[1].Err)
   249  	}
   250  
   251  	if !strings.Contains(msgs[2].Err.Error(), "apiVersion is required") {
   252  		t.Errorf("Unexpected message 2: %s", msgs[2].Err)
   253  	}
   254  
   255  	if !strings.Contains(msgs[3].Err.Error(), "version '0.0.0.0' is not a valid SemVer") {
   256  		t.Errorf("Unexpected message 3: %s", msgs[2].Err)
   257  	}
   258  
   259  	if !strings.Contains(msgs[4].Err.Error(), "icon is recommended") {
   260  		t.Errorf("Unexpected message 4: %s", msgs[3].Err)
   261  	}
   262  
   263  }