add schema tests, fix albatross service value

reviewable/pr2585/r1
Wil Reichert 8 years ago
parent 3ea2ff4a5a
commit a6c9b74a63

@ -124,8 +124,10 @@ func Templates(linter *support.Linter) {
// get the schema validator // get the schema validator
schema, err := f.Validator(true, kubeClient.SchemaCacheDir) schema, err := f.Validator(true, kubeClient.SchemaCacheDir)
if err != nil { validSchemaAccess := linter.RunLinterRule(support.ErrorSev, path, validateSchemaAccess(err))
panic(err)
if !validSchemaAccess {
continue
} }
// convert to YAML to JSON, validated above so should be ok // convert to YAML to JSON, validated above so should be ok
@ -171,9 +173,16 @@ func validateYamlContent(err error) error {
return nil 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 { func validateSchema(err error) error {
if err != nil { if err != nil {
return fmt.Errorf("schema validation failure - %s", err) return fmt.Errorf("schema validation failure\n\t%s", err)
} }
return nil return nil
} }

@ -25,7 +25,11 @@ import (
"k8s.io/helm/pkg/lint/support" "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) { func TestValidateAllowedExtension(t *testing.T) {
var failTest = []string{"/foo", "/test.yml", "/test.toml", "test.yml"} var failTest = []string{"/foo", "/test.yml", "/test.toml", "test.yml"}
@ -45,7 +49,7 @@ func TestValidateAllowedExtension(t *testing.T) {
} }
func TestTemplateParsing(t *testing.T) { func TestTemplateParsing(t *testing.T) {
linter := support.Linter{ChartDir: templateTestBasedir} linter := support.Linter{ChartDir: albatrossTestBasedir}
Templates(&linter) Templates(&linter)
res := linter.Messages res := linter.Messages
@ -58,8 +62,8 @@ func TestTemplateParsing(t *testing.T) {
} }
} }
var wrongTemplatePath = filepath.Join(templateTestBasedir, "templates", "fail.yaml") var wrongTemplatePath = filepath.Join(albatrossTestBasedir, "templates", "fail.yaml")
var ignoredTemplatePath = filepath.Join(templateTestBasedir, "fail.yaml.ignored") var ignoredTemplatePath = filepath.Join(albatrossTestBasedir, "fail.yaml.ignored")
// Test a template with all the existing features: // Test a template with all the existing features:
// namespaces, partial templates // namespaces, partial templates
@ -68,7 +72,31 @@ func TestTemplateIntegrationHappyPath(t *testing.T) {
os.Rename(wrongTemplatePath, ignoredTemplatePath) os.Rename(wrongTemplatePath, ignoredTemplatePath)
defer os.Rename(ignoredTemplatePath, wrongTemplatePath) 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) Templates(&linter)
res := linter.Messages res := linter.Messages

@ -12,7 +12,7 @@ metadata:
tillerVersion: {{ .Capabilities.TillerVersion }} tillerVersion: {{ .Capabilities.TillerVersion }}
spec: spec:
ports: ports:
- port: {{default 80 .Values.httpPort | quote}} - port: {{default 80 .Values.httpPort}}
targetPort: 80 targetPort: 80
protocol: TCP protocol: TCP
name: http name: http

@ -0,0 +1,4 @@
apiVersion: v1
description: A Helm chart for Kubernetes
name: badschema
version: 0.1.0

@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: pod-example
spec:
aaa: zzz
containers:
- image: ubuntu:trusty
command: ["echo"]
args: ["Hello World"]

@ -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

@ -0,0 +1,4 @@
apiVersion: v1
description: A Helm chart for Kubernetes
name: goodschema
version: 0.1.0

@ -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"]

@ -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
Loading…
Cancel
Save