github.com/vtuson/helm@v2.8.2+incompatible/pkg/lint/lint_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  
    22  	"k8s.io/helm/pkg/lint/support"
    23  
    24  	"testing"
    25  )
    26  
    27  var values = []byte{}
    28  
    29  const namespace = "testNamespace"
    30  const strict = false
    31  
    32  const badChartDir = "rules/testdata/badchartfile"
    33  const badValuesFileDir = "rules/testdata/badvaluesfile"
    34  const badYamlFileDir = "rules/testdata/albatross"
    35  const goodChartDir = "rules/testdata/goodone"
    36  
    37  func TestBadChart(t *testing.T) {
    38  	m := All(badChartDir, values, namespace, strict).Messages
    39  	if len(m) != 5 {
    40  		t.Errorf("Number of errors %v", len(m))
    41  		t.Errorf("All didn't fail with expected errors, got %#v", m)
    42  	}
    43  	// There should be one INFO, 2 WARNINGs and one ERROR messages, check for them
    44  	var i, w, e, e2, e3 bool
    45  	for _, msg := range m {
    46  		if msg.Severity == support.InfoSev {
    47  			if strings.Contains(msg.Err.Error(), "icon is recommended") {
    48  				i = true
    49  			}
    50  		}
    51  		if msg.Severity == support.WarningSev {
    52  			if strings.Contains(msg.Err.Error(), "directory not found") {
    53  				w = true
    54  			}
    55  		}
    56  		if msg.Severity == support.ErrorSev {
    57  			if strings.Contains(msg.Err.Error(), "version 0.0.0 is less than or equal to 0") {
    58  				e = true
    59  			}
    60  			if strings.Contains(msg.Err.Error(), "name is required") {
    61  				e2 = true
    62  			}
    63  			if strings.Contains(msg.Err.Error(), "directory name (badchartfile) and chart name () must be the same") {
    64  				e3 = true
    65  			}
    66  		}
    67  	}
    68  	if !e || !e2 || !e3 || !w || !i {
    69  		t.Errorf("Didn't find all the expected errors, got %#v", m)
    70  	}
    71  }
    72  
    73  func TestInvalidYaml(t *testing.T) {
    74  	m := All(badYamlFileDir, values, namespace, strict).Messages
    75  	if len(m) != 1 {
    76  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
    77  	}
    78  	if !strings.Contains(m[0].Err.Error(), "deliberateSyntaxError") {
    79  		t.Errorf("All didn't have the error for deliberateSyntaxError")
    80  	}
    81  }
    82  
    83  func TestBadValues(t *testing.T) {
    84  	m := All(badValuesFileDir, values, namespace, strict).Messages
    85  	if len(m) != 1 {
    86  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
    87  	}
    88  	if !strings.Contains(m[0].Err.Error(), "cannot unmarshal") {
    89  		t.Errorf("All didn't have the error for invalid key format: %s", m[0].Err)
    90  	}
    91  }
    92  
    93  func TestGoodChart(t *testing.T) {
    94  	m := All(goodChartDir, values, namespace, strict).Messages
    95  	if len(m) != 0 {
    96  		t.Errorf("All failed but shouldn't have: %#v", m)
    97  	}
    98  }