2016-06-23 02:28:45 +08:00
|
|
|
/*
|
2018-08-25 03:03:55 +08:00
|
|
|
Copyright The Helm Authors.
|
2016-06-23 02:28:45 +08:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-05-12 06:21:14 +08:00
|
|
|
package lint
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
2019-02-08 02:40:18 +08:00
|
|
|
"testing"
|
2016-05-12 06:21:14 +08:00
|
|
|
|
2019-03-13 07:06:06 +08:00
|
|
|
"helm.sh/helm/pkg/lint/support"
|
2016-05-12 06:21:14 +08:00
|
|
|
)
|
|
|
|
|
|
2018-12-05 07:57:24 +08:00
|
|
|
var values map[string]interface{}
|
2017-12-14 06:43:12 +08:00
|
|
|
|
|
|
|
|
const namespace = "testNamespace"
|
|
|
|
|
const strict = false
|
|
|
|
|
|
2016-06-10 07:51:00 +08:00
|
|
|
const badChartDir = "rules/testdata/badchartfile"
|
|
|
|
|
const badValuesFileDir = "rules/testdata/badvaluesfile"
|
|
|
|
|
const badYamlFileDir = "rules/testdata/albatross"
|
|
|
|
|
const goodChartDir = "rules/testdata/goodone"
|
2016-05-12 06:21:14 +08:00
|
|
|
|
|
|
|
|
func TestBadChart(t *testing.T) {
|
2017-12-14 06:43:12 +08:00
|
|
|
m := All(badChartDir, values, namespace, strict).Messages
|
2017-01-12 22:15:01 +08:00
|
|
|
if len(m) != 5 {
|
2016-06-10 07:51:00 +08:00
|
|
|
t.Errorf("Number of errors %v", len(m))
|
2016-05-12 06:21:14 +08:00
|
|
|
t.Errorf("All didn't fail with expected errors, got %#v", m)
|
|
|
|
|
}
|
2017-01-12 22:15:01 +08:00
|
|
|
// There should be one INFO, 2 WARNINGs and one ERROR messages, check for them
|
|
|
|
|
var i, w, e, e2, e3 bool
|
2016-05-12 06:21:14 +08:00
|
|
|
for _, msg := range m {
|
2017-01-12 22:15:01 +08:00
|
|
|
if msg.Severity == support.InfoSev {
|
|
|
|
|
if strings.Contains(msg.Err.Error(), "icon is recommended") {
|
|
|
|
|
i = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-10 07:51:00 +08:00
|
|
|
if msg.Severity == support.WarningSev {
|
2016-07-01 10:07:56 +08:00
|
|
|
if strings.Contains(msg.Err.Error(), "directory not found") {
|
2016-05-12 06:21:14 +08:00
|
|
|
w = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-10 07:51:00 +08:00
|
|
|
if msg.Severity == support.ErrorSev {
|
2016-07-01 10:07:56 +08:00
|
|
|
if strings.Contains(msg.Err.Error(), "version 0.0.0 is less than or equal to 0") {
|
2016-05-12 06:21:14 +08:00
|
|
|
e = true
|
|
|
|
|
}
|
2016-07-01 10:07:56 +08:00
|
|
|
if strings.Contains(msg.Err.Error(), "name is required") {
|
2016-05-12 06:58:41 +08:00
|
|
|
e2 = true
|
|
|
|
|
}
|
2016-07-01 10:07:56 +08:00
|
|
|
if strings.Contains(msg.Err.Error(), "directory name (badchartfile) and chart name () must be the same") {
|
2016-06-10 07:51:00 +08:00
|
|
|
e3 = true
|
|
|
|
|
}
|
2016-05-12 06:21:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-12 22:15:01 +08:00
|
|
|
if !e || !e2 || !e3 || !w || !i {
|
2016-05-12 06:21:14 +08:00
|
|
|
t.Errorf("Didn't find all the expected errors, got %#v", m)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInvalidYaml(t *testing.T) {
|
2017-12-14 06:43:12 +08:00
|
|
|
m := All(badYamlFileDir, values, namespace, strict).Messages
|
2016-05-12 06:21:14 +08:00
|
|
|
if len(m) != 1 {
|
2017-09-28 20:54:40 +08:00
|
|
|
t.Fatalf("All didn't fail with expected errors, got %#v", m)
|
2016-05-12 06:21:14 +08:00
|
|
|
}
|
2016-07-01 10:07:56 +08:00
|
|
|
if !strings.Contains(m[0].Err.Error(), "deliberateSyntaxError") {
|
2016-05-12 06:21:14 +08:00
|
|
|
t.Errorf("All didn't have the error for deliberateSyntaxError")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 07:20:09 +08:00
|
|
|
func TestBadValues(t *testing.T) {
|
2017-12-14 06:43:12 +08:00
|
|
|
m := All(badValuesFileDir, values, namespace, strict).Messages
|
2018-08-30 00:56:19 +08:00
|
|
|
if len(m) < 1 {
|
2017-09-28 20:54:40 +08:00
|
|
|
t.Fatalf("All didn't fail with expected errors, got %#v", m)
|
2016-05-12 07:20:09 +08:00
|
|
|
}
|
2016-07-01 10:07:56 +08:00
|
|
|
if !strings.Contains(m[0].Err.Error(), "cannot unmarshal") {
|
|
|
|
|
t.Errorf("All didn't have the error for invalid key format: %s", m[0].Err)
|
2016-05-12 07:20:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-12 06:21:14 +08:00
|
|
|
func TestGoodChart(t *testing.T) {
|
2017-12-14 06:43:12 +08:00
|
|
|
m := All(goodChartDir, values, namespace, strict).Messages
|
2016-05-12 06:21:14 +08:00
|
|
|
if len(m) != 0 {
|
|
|
|
|
t.Errorf("All failed but shouldn't have: %#v", m)
|
|
|
|
|
}
|
|
|
|
|
}
|