forked from vbehar/helm3-unittest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_runner.go
264 lines (234 loc) · 6.69 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
package unittest
import (
"fmt"
"path/filepath"
"time"
"github.com/ruben-gp/helm3-unittest/unittest/snapshot"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
)
// 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) 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
Config TestConfig
suiteCounting testUnitCountingWithSnapshotFailed
testCounting testUnitCounting
chartCounting testUnitCounting
snapshotCounting totalSnapshotCounting
}
// Run test suites in chart in ChartPaths
func (tr *TestRunner) Run(ChartPaths []string) bool {
allPassed := true
start := time.Now()
for _, chartPath := range ChartPaths {
chart, err := loader.Load(chartPath)
if err != nil {
tr.printErroredChartHeader(err)
tr.countChart(false, err)
allPassed = false
continue
}
testSuites, err := tr.getTestSuites(chartPath, chart.Metadata.Name, chart)
if err != nil {
tr.printErroredChartHeader(err)
tr.countChart(false, err)
allPassed = false
continue
}
tr.printChartHeader(chart, chartPath)
chartPassed := tr.runSuitesOfChart(testSuites, chart)
tr.countChart(chartPassed, nil)
allPassed = allPassed && chartPassed
}
tr.printSnapshotSummary()
tr.printSummary(time.Now().Sub(start))
return allPassed
}
// getTestSuites return test files of the chart which matched patterns
func (tr *TestRunner) getTestSuites(chartPath, chartRoute string, chart *chart.Chart) ([]*TestSuite, error) {
filesSet := map[string]bool{}
for _, pattern := range tr.Config.TestFiles {
files, err := filepath.Glob(filepath.Join(chartPath, pattern))
if err != nil {
return nil, err
}
for _, file := range files {
filesSet[file] = true
}
}
resultSuites := make([]*TestSuite, 0, len(filesSet))
for file := range filesSet {
suite, err := ParseTestSuiteFile(file, chartRoute)
if err != nil {
tr.handleSuiteResult(&TestSuiteResult{
FilePath: file,
ExecError: err,
})
continue
}
resultSuites = append(resultSuites, suite)
}
if tr.Config.WithSubChart {
for _, subchart := range chart.Dependencies() {
subchartSuites, err := tr.getTestSuites(
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
}
// runSuitesOfChart runs suite files of the chart and print output
func (tr *TestRunner) runSuitesOfChart(suites []*TestSuite, chart *chart.Chart) bool {
chartPassed := true
for _, suite := range suites {
snapshotCache, err := snapshot.CreateSnapshotOfSuite(suite.definitionFile, tr.Config.UpdateSnapshot)
if err != nil {
tr.handleSuiteResult(&TestSuiteResult{
FilePath: suite.definitionFile,
ExecError: err,
})
continue
}
result := suite.Run(chart, snapshotCache, &TestSuiteResult{})
chartPassed = chartPassed && result.Passed
tr.handleSuiteResult(result)
snapshotCache.StoreToFileIfNeeded()
}
return chartPassed
}
// handleSuiteResult print suite result and count suites and tests status
func (tr *TestRunner) handleSuiteResult(result *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(chart *chart.Chart, path string) {
headerFormat := `
### Chart [ %s ] %s
`
header := fmt.Sprintf(
headerFormat,
tr.Printer.highlight(chart.Metadata.Name),
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 *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 *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++
}
}
}