-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RenderVerifier for templates (#1327)
* Add package verifier that renders the template to detect invalid go template expressions Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
- Loading branch information
1 parent
15b143a
commit 2fb38e4
Showing
6 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package template | ||
|
||
import ( | ||
"github.com/kudobuilder/kudo/pkg/engine/renderer" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/verifier" | ||
) | ||
|
||
var _ verifier.PackageVerifier = &RenderVerifier{} | ||
|
||
// RenderVerifier checks that all templates are compilable and contain valid golang template syntax | ||
type RenderVerifier struct{} | ||
|
||
func (RenderVerifier) Verify(pf *packages.Files) verifier.Result { | ||
res := verifier.NewResult() | ||
res.Merge(templateCompilable(pf)) | ||
return res | ||
} | ||
|
||
func templateCompilable(pf *packages.Files) verifier.Result { | ||
params := make(map[string]string) | ||
for _, p := range pf.Params.Parameters { | ||
params[p.Name] = "default" | ||
} | ||
|
||
configs := make(map[string]interface{}) | ||
configs["OperatorName"] = "OperatorName" | ||
configs["Name"] = "Name" | ||
configs["Namespace"] = "Namespace" | ||
configs["Params"] = params | ||
configs["Pipes"] = make(map[string]string) | ||
configs["PlanName"] = "PlanName" | ||
configs["PhaseName"] = "PhaseName" | ||
configs["StepName"] = "StepName" | ||
configs["AppVersion"] = "AppVersion" | ||
|
||
res := verifier.NewResult() | ||
|
||
engine := renderer.New() | ||
for k, v := range pf.Templates { | ||
_, err := engine.Render(k, v, configs) | ||
if err != nil { | ||
res.AddErrors(err.Error()) | ||
} | ||
} | ||
|
||
return res | ||
} |
32 changes: 32 additions & 0 deletions
32
pkg/kudoctl/packages/verifier/template/verify_render_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package template | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages" | ||
) | ||
|
||
func TestTemplateRenderVerifier(t *testing.T) { | ||
params := make([]v1beta1.Parameter, 0) | ||
paramFile := packages.ParamsFile{Parameters: params} | ||
templates := make(map[string]string) | ||
templates["foo.yaml"] = ` | ||
{{ if eq }} | ||
{{ end }} | ||
` | ||
operator := packages.OperatorFile{} | ||
pf := packages.Files{ | ||
Templates: templates, | ||
Operator: &operator, | ||
Params: ¶mFile, | ||
} | ||
verifier := RenderVerifier{} | ||
res := verifier.Verify(&pf) | ||
|
||
assert.Equal(t, 0, len(res.Warnings)) | ||
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]) | ||
} |