Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify that a rendered template is valid YAML #1362

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

@nfnt nfnt Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that golangci-lint is in $GOPATH/bin. If it has been installed elsewhere (e.g. by using the homebrew tap), this will run the script anyways. OTOH,hack/install-golangcilint.sh assumes that $GOPATH/bin is in $PATH 🤷‍♂
How about adding $GOPATH/bin to $PATH in hack/install-golangcilint.sh?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's too late :(
Because hack/install-golangcilint.sh doesn't run golangci-lint. The path manipulation would have to be done in the Makefile. Let's not change it in the Makefile and let's keep your approach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... It's messy :(

${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 YAML
ANeumann82 marked this conversation as resolved.
Show resolved Hide resolved
_, 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])
}