/
test_runner.go
364 lines (323 loc) · 9.32 KB
/
test_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package unittest
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/helm-unittest/helm-unittest/internal/printer"
"github.com/helm-unittest/helm-unittest/pkg/unittest/formatter"
"github.com/helm-unittest/helm-unittest/pkg/unittest/results"
"github.com/helm-unittest/helm-unittest/pkg/unittest/snapshot"
"github.com/yargevad/filepathx"
v3chart "helm.sh/helm/v3/pkg/chart"
v3loader "helm.sh/helm/v3/pkg/chart/loader"
)
func getFiles(chartPath string, filePatterns []string, setAbsolute bool) ([]string, error) {
filesSet := make([]string, 0)
for _, pattern := range filePatterns {
if !filepath.IsAbs(pattern) {
files, err := filepathx.Glob(filepath.Join(chartPath, pattern))
if err != nil {
return nil, err
}
if setAbsolute {
for _, file := range files {
file, _ = filepath.Abs(file)
filesSet = append(filesSet, file)
}
} else {
filesSet = append(filesSet, files...)
}
} else {
filesSet = append(filesSet, pattern)
}
}
return filesSet, nil
}
// testUnitCounting stores counting numbers of test unit status
type testUnitCounting struct {
passed uint
failed uint
errored uint
}
// sprint returns string of counting result
func (counting testUnitCounting) sprint(printer *printer.Printer) string {
var failedLabel string
if counting.failed > 0 {
failedLabel = printer.Danger("%d failed, ", counting.failed)
}
var erroredLabel string
if counting.errored > 0 {
erroredLabel = fmt.Sprintf("%d errored, ", counting.errored)
}
return failedLabel + erroredLabel + fmt.Sprintf(
"%d passed, %d total",
counting.passed,
counting.passed+counting.failed,
)
}
// testUnitCountingWithSnapshotFailed store testUnitCounting with snapshotFailed field
type testUnitCountingWithSnapshotFailed struct {
testUnitCounting
snapshotFailed uint
}
// totalSnapshotCounting store testUnitCounting with snapshotFailed field
type totalSnapshotCounting struct {
testUnitCounting
created uint
vanished uint
}
// TestRunner stores basic settings and testing status for running all tests
type TestRunner struct {
Printer *printer.Printer
Formatter formatter.Formatter
UpdateSnapshot bool
WithSubChart bool
Strict bool
Failfast bool
TestFiles []string
ChartTestsPath string
ValuesFiles []string
OutputFile string
RenderPath string
suiteCounting testUnitCountingWithSnapshotFailed
testCounting testUnitCounting
chartCounting testUnitCounting
snapshotCounting totalSnapshotCounting
testResults []*results.TestSuiteResult
}
// RunV3 test suites in chart in ChartPaths.
func (tr *TestRunner) RunV3(ChartPaths []string) bool {
allPassed := true
start := time.Now()
for _, chartPath := range ChartPaths {
chart, err := v3loader.Load(chartPath)
if err != nil {
tr.printErroredChartHeader(err)
tr.countChart(false, err)
allPassed = false
if tr.Failfast {
break
}
continue
}
chartRoute := chart.Name()
testSuites, err := tr.getV3TestSuites(chartPath, chartRoute, chart)
if err != nil {
tr.printErroredChartHeader(err)
tr.countChart(false, err)
allPassed = false
if tr.Failfast {
break
}
continue
}
tr.printChartHeader(chart.Name(), chartPath)
chartPassed := tr.runV3SuitesOfChart(testSuites, chartPath)
tr.countChart(chartPassed, nil)
allPassed = allPassed && chartPassed
}
err := tr.writeTestOutput()
if err != nil {
tr.printErroredChartHeader(err)
}
tr.printSnapshotSummary()
tr.printSummary(time.Since(start))
return allPassed
}
func (tr *TestRunner) getTestSuites(chartPath, chartRoute string) ([]*TestSuite, error) {
testFilesSet, terr := getFiles(chartPath, tr.TestFiles, false)
if terr != nil {
return nil, terr
}
valuesFilesSet, verr := getFiles("", tr.ValuesFiles, true)
if verr != nil {
return nil, verr
}
var renderedTestSuites []*TestSuite
if len(tr.ChartTestsPath) > 0 {
helmTestsPath := filepath.Join(chartPath, tr.ChartTestsPath)
// Verify that there is a tests path - in the event of mixed testing environments
if _, err := os.Stat(helmTestsPath); errors.Is(err, nil) {
var renderErr error
renderedTestSuites, renderErr = RenderTestSuiteFiles(helmTestsPath, chartRoute, tr.Strict, valuesFilesSet, nil)
if renderErr != nil {
return nil, renderErr
}
}
}
resultSuites := make([]*TestSuite, 0, len(testFilesSet)+len(renderedTestSuites))
for _, file := range testFilesSet {
suites, err := ParseTestSuiteFile(file, chartRoute, tr.Strict, valuesFilesSet)
if err != nil {
tr.handleSuiteResult(&results.TestSuiteResult{
FilePath: file,
ExecError: err,
})
return nil, err
}
resultSuites = append(resultSuites, suites...)
}
resultSuites = append(resultSuites, renderedTestSuites...)
return resultSuites, nil
}
// getV3TestSuites return test files of the chart which matched patterns
func (tr *TestRunner) getV3TestSuites(chartPath, chartRoute string, chart *v3chart.Chart) ([]*TestSuite, error) {
resultSuites, err := tr.getTestSuites(chartPath, chartRoute)
if err != nil {
return nil, err
}
if tr.WithSubChart {
for _, subchart := range chart.Dependencies() {
subchartSuites, err := tr.getV3TestSuites(
filepath.Join(chartPath, "charts", subchart.Metadata.Name),
filepath.Join(chartRoute, "charts", subchart.Metadata.Name),
subchart,
)
if err != nil {
continue
}
resultSuites = append(resultSuites, subchartSuites...)
}
}
return resultSuites, nil
}
// runV3SuitesOfChart runs suite files of the chart and print output
func (tr *TestRunner) runV3SuitesOfChart(suites []*TestSuite, chartPath string) bool {
chartPassed := true
for _, suite := range suites {
snapshotCache, err := snapshot.CreateSnapshotOfSuite(suite.SnapshotFileUrl(), tr.UpdateSnapshot)
if err != nil {
tr.handleSuiteResult(&results.TestSuiteResult{
FilePath: suite.definitionFile,
ExecError: err,
})
chartPassed = false
continue
}
result := suite.RunV3(chartPath, snapshotCache, tr.Failfast, tr.RenderPath, &results.TestSuiteResult{})
chartPassed = chartPassed && result.Passed
tr.handleSuiteResult(result)
tr.testResults = append(tr.testResults, result)
snapshotCache.StoreToFileIfNeeded()
if !chartPassed && tr.Failfast {
break
}
}
return chartPassed
}
// handleSuiteResult print suite result and count suites and tests status
func (tr *TestRunner) handleSuiteResult(result *results.TestSuiteResult) {
result.Print(tr.Printer, 0)
tr.countSuite(result)
for _, testsResult := range result.TestsResult {
tr.countTest(testsResult)
}
}
// printSummary print summary footer
func (tr *TestRunner) printSummary(elapsed time.Duration) {
summaryFormat := `
Charts: %s
Test Suites: %s
Tests: %s
Snapshot: %s
Time: %s
`
tr.Printer.Println(
fmt.Sprintf(
summaryFormat,
tr.chartCounting.sprint(tr.Printer),
tr.suiteCounting.sprint(tr.Printer),
tr.testCounting.sprint(tr.Printer),
tr.snapshotCounting.sprint(tr.Printer),
elapsed.String(),
),
0,
)
}
// printChartHeader print header before suite result of a chart
func (tr *TestRunner) printChartHeader(chartName, path string) {
headerFormat := `
### Chart [ %s ] %s
`
header := fmt.Sprintf(
headerFormat,
tr.Printer.Highlight(chartName),
tr.Printer.Faint(path),
)
tr.Printer.Println(header, 0)
}
// printErroredChartHeader if chart has exexution error print header with error
func (tr *TestRunner) printErroredChartHeader(err error) {
headerFormat := `
### ` + tr.Printer.Danger("Error: ") + ` %s
`
header := fmt.Sprintf(headerFormat, err)
tr.Printer.Println(header, 0)
}
// printSnapshotSummary print snapshot summary in footer
func (tr *TestRunner) printSnapshotSummary() {
if tr.snapshotCounting.failed > 0 {
snapshotFormat := `
Snapshot Summary: %s`
summary := tr.Printer.Danger("%d snapshot failed", tr.snapshotCounting.failed) +
fmt.Sprintf(" in %d test suite.", tr.suiteCounting.snapshotFailed) +
tr.Printer.Faint(" Check changes and use `-u` to update snapshot.")
tr.Printer.Println(fmt.Sprintf(snapshotFormat, summary), 0)
}
}
func (tr *TestRunner) countSuite(suite *results.TestSuiteResult) {
if suite.Passed {
tr.suiteCounting.passed++
} else {
tr.suiteCounting.failed++
if suite.ExecError != nil {
tr.suiteCounting.errored++
}
if suite.SnapshotCounting.Failed > 0 {
tr.suiteCounting.snapshotFailed++
}
}
tr.snapshotCounting.failed += suite.SnapshotCounting.Failed
tr.snapshotCounting.passed += suite.SnapshotCounting.Total - suite.SnapshotCounting.Failed
tr.snapshotCounting.created += suite.SnapshotCounting.Created
tr.snapshotCounting.vanished += suite.SnapshotCounting.Vanished
}
func (tr *TestRunner) countTest(test *results.TestJobResult) {
if test.Passed {
tr.testCounting.passed++
} else {
tr.testCounting.failed++
if test.ExecError != nil {
tr.testCounting.errored++
}
}
}
func (tr *TestRunner) countChart(passed bool, err error) {
if passed {
tr.chartCounting.passed++
} else {
tr.chartCounting.failed++
if err != nil {
tr.chartCounting.errored++
}
}
}
func (tr *TestRunner) writeTestOutput() error {
// Check if formatter exits to write
if tr.Formatter != nil {
// Create outputfile for testsuite
writer, ferr := os.Create(tr.OutputFile)
if ferr != nil {
return ferr
}
defer writer.Close()
//
jerr := tr.Formatter.WriteTestOutput(tr.testResults, true, writer)
if jerr != nil {
return jerr
}
}
return nil
}