Skip to content

Commit

Permalink
Verify that a rendered template is valid YAML (#1362)
Browse files Browse the repository at this point in the history
* Verify that a rendered template is valid YAML

Co-Authored-By: Jan Schlicht <jan@d2iq.com>
Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
  • Loading branch information
ANeumann82 and Jan Schlicht committed Feb 25, 2020
1 parent 262a562 commit 8616529
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ test-clean:

.PHONY: lint
lint:
ifeq (, $(shell which golangci-lint))
./hack/install-golangcilint.sh
endif
golangci-lint run
if [ ! -f ${GOPATH}/bin/golangci-lint ]; then ./hack/install-golangcilint.sh; fi
${GOPATH}/bin/golangci-lint run

.PHONY: download
download:
Expand Down
10 changes: 9 additions & 1 deletion pkg/kudoctl/packages/verifier/template/verify_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ func templateCompilable(pf *packages.Files) verifier.Result {

engine := renderer.New()
for k, v := range pf.Templates {
_, err := engine.Render(k, v, configs)
// Render the template
s, err := engine.Render(k, v, configs)
if err != nil {
res.AddErrors(err.Error())
}

// Try to parse rendered template as valid Kubernetes objects
_, err = renderer.YamlToObject(s)
if err != nil {
res.AddErrors(err.Error())
}

}

return res
Expand Down
32 changes: 32 additions & 0 deletions pkg/kudoctl/packages/verifier/template/verify_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,35 @@ func TestTemplateRenderVerifier(t *testing.T) {
assert.Equal(t, 1, len(res.Errors))
assert.Equal(t, `error rendering template: template: foo.yaml:2:6: executing "foo.yaml" at <eq>: wrong number of args for eq: want at least 1 got 0`, res.Errors[0])
}

func TestTemplateRenderVerifier_InvalidYAML(t *testing.T) {
params := make([]v1beta1.Parameter, 0)
paramFile := packages.ParamsFile{Parameters: params}
templates := make(map[string]string)
templates["foo.yaml"] = `
apiVersion: batch/v1
kind: Job
metadata:
name: backup
spec:
template:
spec:
containers:
- name: backup
restartPolicy: Never
`
operator := packages.OperatorFile{}
pf := packages.Files{
Templates: templates,
Operator: &operator,
Params: &paramFile,
}
verifier := RenderVerifier{}
res := verifier.Verify(&pf)

assert.Equal(t, 0, len(res.Warnings))
assert.Equal(t, 1, len(res.Errors))
assert.Equal(t,
`decoding chunk "\napiVersion: batch/v1\nkind: Job\nmetadata:\n name: backup\nspec:\n template:\n spec:\n containers:\n - name: backup\n`+
` restartPolicy: Never\n" failed: error converting YAML to JSON: yaml: line 10: did not find expected key`, res.Errors[0])
}

0 comments on commit 8616529

Please sign in to comment.