github.com/doitroot/helm@v3.0.0-beta.3+incompatible/pkg/lint/lint_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 lint
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"helm.sh/helm/pkg/lint/support"
    24  )
    25  
    26  var values map[string]interface{}
    27  
    28  const namespace = "testNamespace"
    29  const strict = false
    30  
    31  const badChartDir = "rules/testdata/badchartfile"
    32  const badValuesFileDir = "rules/testdata/badvaluesfile"
    33  const badYamlFileDir = "rules/testdata/albatross"
    34  const goodChartDir = "rules/testdata/goodone"
    35  
    36  func TestBadChart(t *testing.T) {
    37  	m := All(badChartDir, values, namespace, strict).Messages
    38  	if len(m) != 8 {
    39  		t.Errorf("Number of errors %v", len(m))
    40  		t.Errorf("All didn't fail with expected errors, got %#v", m)
    41  	}
    42  	// There should be one INFO, 2 WARNINGs and one ERROR messages, check for them
    43  	var i, w, e, e2, e3, e4, e5, e6 bool
    44  	for _, msg := range m {
    45  		if msg.Severity == support.InfoSev {
    46  			if strings.Contains(msg.Err.Error(), "icon is recommended") {
    47  				i = true
    48  			}
    49  		}
    50  		if msg.Severity == support.WarningSev {
    51  			if strings.Contains(msg.Err.Error(), "directory not found") {
    52  				w = true
    53  			}
    54  		}
    55  		if msg.Severity == support.ErrorSev {
    56  			if strings.Contains(msg.Err.Error(), "version 0.0.0 is less than or equal to 0") {
    57  				e = true
    58  			}
    59  			if strings.Contains(msg.Err.Error(), "name is required") {
    60  				e2 = true
    61  			}
    62  			if strings.Contains(msg.Err.Error(), "directory name (badchartfile) and chart name () must be the same") {
    63  				e3 = true
    64  			}
    65  
    66  			if strings.Contains(msg.Err.Error(), "apiVersion is required. The value must be either \"v1\" or \"v2\"") {
    67  				e4 = true
    68  			}
    69  
    70  			if strings.Contains(msg.Err.Error(), "chart type is not valid in apiVersion") {
    71  				e5 = true
    72  			}
    73  
    74  			if strings.Contains(msg.Err.Error(), "dependencies are not valid in the Chart file with apiVersion") {
    75  				e6 = true
    76  			}
    77  		}
    78  	}
    79  	if !e || !e2 || !e3 || !e4 || !e5 || !e6 || !w || !i {
    80  		t.Errorf("Didn't find all the expected errors, got %#v", m)
    81  	}
    82  }
    83  
    84  func TestInvalidYaml(t *testing.T) {
    85  	m := All(badYamlFileDir, values, namespace, strict).Messages
    86  	if len(m) != 1 {
    87  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
    88  	}
    89  	if !strings.Contains(m[0].Err.Error(), "deliberateSyntaxError") {
    90  		t.Errorf("All didn't have the error for deliberateSyntaxError")
    91  	}
    92  }
    93  
    94  func TestBadValues(t *testing.T) {
    95  	m := All(badValuesFileDir, values, namespace, strict).Messages
    96  	if len(m) < 1 {
    97  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
    98  	}
    99  	if !strings.Contains(m[0].Err.Error(), "cannot unmarshal") {
   100  		t.Errorf("All didn't have the error for invalid key format: %s", m[0].Err)
   101  	}
   102  }
   103  
   104  func TestGoodChart(t *testing.T) {
   105  	m := All(goodChartDir, values, namespace, strict).Messages
   106  	if len(m) != 0 {
   107  		t.Errorf("All failed but shouldn't have: %#v", m)
   108  	}
   109  }