diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index b686a833e..94c60956b 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -124,8 +124,10 @@ func Templates(linter *support.Linter) { // get the schema validator schema, err := f.Validator(true, kubeClient.SchemaCacheDir) - if err != nil { - panic(err) + validSchemaAccess := linter.RunLinterRule(support.ErrorSev, path, validateSchemaAccess(err)) + + if !validSchemaAccess { + continue } // convert to YAML to JSON, validated above so should be ok @@ -171,9 +173,16 @@ func validateYamlContent(err error) error { return nil } +func validateSchemaAccess(err error) error { + if err != nil { + return fmt.Errorf("can not access schema\n\t%s", err) + } + return nil +} + func validateSchema(err error) error { if err != nil { - return fmt.Errorf("schema validation failure - %s", err) + return fmt.Errorf("schema validation failure\n\t%s", err) } return nil } diff --git a/pkg/lint/rules/template_test.go b/pkg/lint/rules/template_test.go index 5a56e22c9..31b031ac6 100644 --- a/pkg/lint/rules/template_test.go +++ b/pkg/lint/rules/template_test.go @@ -25,7 +25,11 @@ import ( "k8s.io/helm/pkg/lint/support" ) -const templateTestBasedir = "./testdata/albatross" +const ( + albatrossTestBasedir = "./testdata/albatross" + badSchemaTestBasedir = "./testdata/badschema" + goodSchemaTestBasedir = "./testdata/goodschema" +) func TestValidateAllowedExtension(t *testing.T) { var failTest = []string{"/foo", "/test.yml", "/test.toml", "test.yml"} @@ -45,7 +49,7 @@ func TestValidateAllowedExtension(t *testing.T) { } func TestTemplateParsing(t *testing.T) { - linter := support.Linter{ChartDir: templateTestBasedir} + linter := support.Linter{ChartDir: albatrossTestBasedir} Templates(&linter) res := linter.Messages @@ -58,8 +62,8 @@ func TestTemplateParsing(t *testing.T) { } } -var wrongTemplatePath = filepath.Join(templateTestBasedir, "templates", "fail.yaml") -var ignoredTemplatePath = filepath.Join(templateTestBasedir, "fail.yaml.ignored") +var wrongTemplatePath = filepath.Join(albatrossTestBasedir, "templates", "fail.yaml") +var ignoredTemplatePath = filepath.Join(albatrossTestBasedir, "fail.yaml.ignored") // Test a template with all the existing features: // namespaces, partial templates @@ -68,7 +72,31 @@ func TestTemplateIntegrationHappyPath(t *testing.T) { os.Rename(wrongTemplatePath, ignoredTemplatePath) defer os.Rename(ignoredTemplatePath, wrongTemplatePath) - linter := support.Linter{ChartDir: templateTestBasedir} + linter := support.Linter{ChartDir: albatrossTestBasedir} + Templates(&linter) + res := linter.Messages + + if len(res) != 0 { + t.Fatalf("Expected no error, got %d, %v", len(res), res) + } +} + +func TestBadSchema(t *testing.T) { + linter := support.Linter{ChartDir: badSchemaTestBasedir} + Templates(&linter) + res := linter.Messages + + if len(res) != 1 { + t.Fatalf("Expected two error, got %d, %v", len(res), res) + } + + if !strings.Contains(res[0].Err.Error(), "schema validation failure") { + t.Errorf("Unexpected error: %s", res[0]) + } +} + +func TestGoodSchema(t *testing.T) { + linter := support.Linter{ChartDir: goodSchemaTestBasedir} Templates(&linter) res := linter.Messages diff --git a/pkg/lint/rules/testdata/albatross/templates/svc.yaml b/pkg/lint/rules/testdata/albatross/templates/svc.yaml index 167148112..580f364f6 100644 --- a/pkg/lint/rules/testdata/albatross/templates/svc.yaml +++ b/pkg/lint/rules/testdata/albatross/templates/svc.yaml @@ -12,7 +12,7 @@ metadata: tillerVersion: {{ .Capabilities.TillerVersion }} spec: ports: - - port: {{default 80 .Values.httpPort | quote}} + - port: {{default 80 .Values.httpPort}} targetPort: 80 protocol: TCP name: http diff --git a/pkg/lint/rules/testdata/badschema/Chart.yaml b/pkg/lint/rules/testdata/badschema/Chart.yaml new file mode 100644 index 000000000..7aa7c1cf7 --- /dev/null +++ b/pkg/lint/rules/testdata/badschema/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: badschema +version: 0.1.0 diff --git a/pkg/lint/rules/testdata/badschema/templates/pod.yaml b/pkg/lint/rules/testdata/badschema/templates/pod.yaml new file mode 100644 index 000000000..4fdb41a9e --- /dev/null +++ b/pkg/lint/rules/testdata/badschema/templates/pod.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: pod-example +spec: + aaa: zzz + containers: + - image: ubuntu:trusty + command: ["echo"] + args: ["Hello World"] diff --git a/pkg/lint/rules/testdata/badschema/values.yaml b/pkg/lint/rules/testdata/badschema/values.yaml new file mode 100644 index 000000000..ee3400b03 --- /dev/null +++ b/pkg/lint/rules/testdata/badschema/values.yaml @@ -0,0 +1,21 @@ +# Default values for badpod. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + diff --git a/pkg/lint/rules/testdata/goodschema/Chart.yaml b/pkg/lint/rules/testdata/goodschema/Chart.yaml new file mode 100644 index 000000000..423879419 --- /dev/null +++ b/pkg/lint/rules/testdata/goodschema/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: goodschema +version: 0.1.0 diff --git a/pkg/lint/rules/testdata/goodschema/templates/pod.yaml b/pkg/lint/rules/testdata/goodschema/templates/pod.yaml new file mode 100644 index 000000000..c4599e181 --- /dev/null +++ b/pkg/lint/rules/testdata/goodschema/templates/pod.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Pod +metadata: + name: pod-example +spec: + containers: + - name: pod-example + image: ubuntu:trusty + command: ["echo"] + args: ["Hello World"] diff --git a/pkg/lint/rules/testdata/goodschema/values.yaml b/pkg/lint/rules/testdata/goodschema/values.yaml new file mode 100644 index 000000000..f6d22057e --- /dev/null +++ b/pkg/lint/rules/testdata/goodschema/values.yaml @@ -0,0 +1,21 @@ +# Default values for goodpod. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent +service: + name: nginx + type: ClusterIP + externalPort: 80 + internalPort: 80 +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi +