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