Skip to content

Commit

Permalink
Merge pull request quintush#145 from mederel/bugfix/Templating-error-…
Browse files Browse the repository at this point in the history
…parsing-failure-with-helm-v3

Helm v3: templating error parsing failure
  • Loading branch information
quintush authored Jan 26, 2022
2 parents 14ee557 + 4df6e53 commit 4c179f8
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ untt.exe
_dist

.DS_Store

.idea
18 changes: 18 additions & 0 deletions pkg/unittest/.snapshots/TestV3RunJobWithFailingTempalte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(*results.TestJobResult)({
DisplayName: (string) (len=11) "should work",
Index: (int) 0,
Passed: (bool) true,
ExecError: (error) <nil>,
AssertsResult: ([]*results.AssertionResult) (len=1) {
(*results.AssertionResult)({
Index: (int) 0,
FailInfo: ([]string) {
},
Passed: (bool) true,
AssertType: (string) (len=14) "failedTemplate",
Not: (bool) false,
CustomInfo: (string) ""
})
},
Duration: (time.Duration) 0s
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

### Chart [ failing-template ] ../../test/data/v3/failing-template


PASS Test failing template ../../test/data/v3/failing-template/tests/configMap_test.yaml


Charts: 1 passed, 1 total
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshot: 0 passed, 0 total
Time: XX.XXXms


9 changes: 5 additions & 4 deletions pkg/unittest/test_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func scopeValuesWithRoutes(routes []string, values map[interface{}]interface{})
func parseV2RenderError(errorMessage string) (string, string) {
// Split the error into several groups.
// those groups are required to parse the correct value.
const regexPattern string = "^.+\"(.+)\":(.+:)* (.+)$"
const regexPattern string = "^.+\"(.+)\":(?:.+:)* (.+)$"

filePath, content := parseRenderError(regexPattern, errorMessage)

Expand All @@ -76,7 +76,8 @@ func parseV2RenderError(errorMessage string) (string, string) {
func parseV3RenderError(errorMessage string) (string, string) {
// Split the error into several groups.
// those groups are required to parse the correct value.
const regexPattern string = "^.+\\((.+):\\d+:\\d+\\):(.+:)* (.+)$"
// ^.+( |\()(.+):\d+:\d+\)?:(.+:)* (.+)$
const regexPattern string = "^.+(?: |\\()(.+):\\d+:\\d+\\)?:(?:.+:)* (.+)$"

filePath, content := parseRenderError(regexPattern, errorMessage)

Expand All @@ -90,9 +91,9 @@ func parseRenderError(regexPattern, errorMessage string) (string, string) {
r := regexp.MustCompile(regexPattern)
result := r.FindStringSubmatch(errorMessage)

if len(result) == 4 {
if len(result) == 3 {
filePath = result[1]
content = fmt.Sprintf("%s: %s", common.RAW, result[3])
content = fmt.Sprintf("%s: %s", common.RAW, result[2])
}

return filePath, content
Expand Down
22 changes: 22 additions & 0 deletions pkg/unittest/test_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,25 @@ asserts:
a.True(testResult.Passed)
a.Equal(2, len(testResult.AssertsResult))
}

func TestV3RunJobWithFailingTempalte(t *testing.T) {
c, _ := loader.Load(testV3WithFailingTemplateChart)
manifest := `
it: should work
template: configMap.yaml
asserts:
- failedTemplate:
errorMessage: no template "non-existing-named-template" associated with template "gotpl"
`
var tj TestJob
yaml.Unmarshal([]byte(manifest), &tj)

testResult := tj.RunV3(c, &snapshot.Cache{}, true, &results.TestJobResult{})

a := assert.New(t)
cupaloy.SnapshotT(t, makeTestJobResultSnapshotable(testResult))

a.Nil(testResult.ExecError)
a.True(testResult.Passed)
a.Equal(1, len(testResult.AssertsResult))
}
16 changes: 13 additions & 3 deletions pkg/unittest/test_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import (
)

var sectionBeginPattern = regexp.MustCompile("( PASS | FAIL |\n*###|\n*Charts:|\n*Snapshot Summary:)")
var timePattern = regexp.MustCompile(`Time:\s+([\d\.]+)(s|ms)`)
var timePattern = regexp.MustCompile(`(Time:\s+)(?:[\d\.]+)(s|ms|\xB5s)`) // B5 = micron for microseconds

func makeOutputSnapshotable(originalOutput string) []interface{} {
output := strings.ReplaceAll(originalOutput, "\\", "/")
timeLoc := timePattern.FindStringSubmatchIndex(output)[2:4]
timeAgnosticOutput := output[:timeLoc[0]] + "XX.XXX" + output[timeLoc[1]:]
timeAgnosticOutput := timePattern.ReplaceAllString(output, "${1}XX.XXXms")

sectionBeggingLocs := sectionBeginPattern.FindAllStringIndex(timeAgnosticOutput, -1)
sections := make([]string, len(sectionBeggingLocs))
Expand Down Expand Up @@ -208,6 +207,17 @@ func TestV3RunnerOkWithSubSubChartsPassedTests(t *testing.T) {
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}

func TestV3RunnerOkWithFailingTemplatePassedTest(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Printer: printer.NewPrinter(buffer, nil),
TestFiles: []string{testTestFiles},
}
passed := runner.RunV3([]string{testV3WithFailingTemplateChart})
assert.True(t, passed, buffer.String())
cupaloy.SnapshotT(t, makeOutputSnapshotable(buffer.String())...)
}

func TestV3RunnerOkWithOverrideValuesPassedTests(t *testing.T) {
buffer := new(bytes.Buffer)
runner := TestRunner{
Expand Down
1 change: 1 addition & 0 deletions pkg/unittest/test_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const testV3BasicChart string = "../../test/data/v3/basic"
const testV3WithSubChart string = "../../test/data/v3/with-subchart"
const testV3WithSubFolderChart string = "../../test/data/v3/with-subfolder"
const testV3WithSubSubFolderChart string = "../../test/data/v3/with-subsubcharts"
const testV3WithFailingTemplateChart string = "../../test/data/v3/failing-template"

var tmpdir, _ = ioutil.TempDir("", testSuiteTests)

Expand Down
5 changes: 5 additions & 0 deletions test/data/v3/failing-template/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v2
name: failing-template
description: Test failed template assert
type: application
version: 0.1.0
7 changes: 7 additions & 0 deletions test/data/v3/failing-template/templates/configMap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
data:
my-file.yaml: |
{{- include "non-existing-named-template" . | nindent 4 }}
8 changes: 8 additions & 0 deletions test/data/v3/failing-template/tests/configMap_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
suite: Test failing template
templates:
- configMap.yaml
tests:
- it: template should be failing
asserts:
- failedTemplate:
errorMessage: no template "non-existing-named-template" associated with template "gotpl"

0 comments on commit 4c179f8

Please sign in to comment.