/
test.go
311 lines (264 loc) · 8.48 KB
/
test.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
package cmd
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
type test struct {
VMCount []int `toml:"vms"`
VMTags []string `toml:"vm_tags"`
SameVMs bool `toml:"samevms"` // test need the same Distribution
NeedAllPlatforms bool `toml:"needallplatforms"` // test need to run on all platforms
Variants []string `toml:"variants"` // only run on given variants, if empty all
Networks []virterNet `toml:"networks"` // Extra NIC to add to the VMs
}
type testRun struct {
testName string
testID string
outDir string
vms []vm
networks []virterNet
variant variant
}
type TestStatus string
const (
StatusSkipped TestStatus = "SKIPPED"
StatusSuccess TestStatus = "SUCCESS"
StatusCanceled TestStatus = "CANCELED"
StatusFailedTimeout TestStatus = "FAILED(TO)"
StatusFailed TestStatus = "FAILED"
StatusError TestStatus = "ERROR" // Error running test
)
type TestResulter interface {
ExecTime() time.Duration
Err() error
}
// collect information about individual test runs
// the interface is similar to the log package (which it also uses)
type testResult struct {
log bytes.Buffer // log messages of the framework (starting test, timing information,...)
testLog bytes.Buffer // output of the test itself ('virter vm exec' output)
execTime time.Duration
err error
status TestStatus
}
func (r testResult) ExecTime() time.Duration {
return r.execTime
}
func (r testResult) Err() error {
return r.err
}
func (r testResult) String() string {
return string(r.status)
}
func performTest(ctx context.Context, suiteRun *testSuiteRun, run *testRun, ids []int, networkNames []string) (string, testResult) {
if run.outDir != "" {
err := os.MkdirAll(run.outDir, 0755)
if err != nil {
return "", testResult{err: err}
}
}
var vms []vmInstance
for i, v := range run.vms {
var memory string
var vcpus uint
var bootCap string
var disks []string
var userName string
if v.Memory != "" {
memory = v.Memory
} else {
memory = "4G"
}
if v.VCPUs != 0 {
vcpus = v.VCPUs
} else {
vcpus = 4
}
if v.BootCap != "" {
bootCap = v.BootCap
} else {
bootCap = "10G"
}
if len(v.Disks) > 0 {
disks = v.Disks
} else {
disks = []string{"name=data,size=2G,bus=scsi"}
}
userName = v.UserName
instance := vmInstance{
ImageName: suiteRun.vmSpec.ImageName(&v),
nr: ids[i],
memory: memory,
vcpus: vcpus,
bootCap: bootCap,
disks: disks,
networkNames: networkNames,
UserName: userName,
}
vms = append(vms, instance)
}
testRes := execTest(ctx, suiteRun, run, networkNames[0], vms)
var report bytes.Buffer
fmt.Fprintln(&report, "|===================================================================================================")
fmt.Fprintf(&report, "| ** Results for %s - %s\n", run.testID, testRes.status)
artifactsUrl := getArtifactsUrl(run.outDir)
if artifactsUrl != "" {
fmt.Fprintf(&report, "| ** %s\n", artifactsUrl)
}
logViewerUrl := getLogViewUrl(run.testID)
if logViewerUrl != "" {
fmt.Fprintf(&report, "| ** %s\n", logViewerUrl)
}
fmt.Fprintln(&report, "|===================================================================================================")
logLines := strings.Split(strings.TrimSpace(testRes.log.String()), "\n")
for _, line := range logLines {
fmt.Fprintln(&report, "|", line)
}
testLog := testRes.testLog.Bytes()
if err := os.WriteFile(filepath.Join(run.outDir, "test.log"), testLog, 0644); err != nil {
fmt.Fprintf(&report, "| FAILED to write log; suppressing original error: %v\n", testRes.err)
testRes.err = err
}
resultsDir := filepath.Join(suiteRun.outDir, "test-results")
if err := XMLLog(resultsDir, run.testID, testRes, testLog); err != nil {
fmt.Fprintf(&report, "| FAILED to write XML log; suppressing original error: %v\n", testRes.err)
testRes.err = err
}
fmt.Fprintln(&report, "|===================================================================================================")
if err := os.WriteFile(filepath.Join(run.outDir, "report.log"), report.Bytes(), 0644); err != nil {
log.Errorf("Failed to write report; suppressing original error: %v\n", testRes.err)
testRes.err = err
}
return report.String(), testRes
}
func execTest(ctx context.Context, suiteRun *testSuiteRun, run *testRun, accessNetwork string, testnodes []vmInstance) testResult {
res := testResult{}
logger := TestLogger(run.testID, &res.log)
logger.Debugf("EXECUTING: %s Nodes(%+v)", run.testID, testnodes)
// Start VMs
start := time.Now()
err := startVMs(ctx, logger, run, testnodes...)
defer shutdownVMs(logger, run.outDir, &res, suiteRun, testnodes...)
if err != nil {
res.status = StatusError
res.err = fmt.Errorf("failed to start VMs: %w", err)
return res
}
logger.Debugf("EXECUTIONTIME: Starting VMs: %v", time.Since(start))
testNameEnv := fmt.Sprintf("env.TEST_NAME=%s", run.testName)
outDirValue := fmt.Sprintf("values.OutDir=%s", run.outDir)
argv := []string{"virter"}
if suiteRun.logFormatVirter != "" {
argv = append(argv, "--logformat", suiteRun.logFormatVirter)
}
argv = append(argv,
"vm", "exec",
"--provision", suiteRun.testSpec.TestSuiteFile,
"--set", testNameEnv,
"--set", outDirValue)
for _, override := range suiteRun.overrides {
argv = append(argv, "--set", override)
}
// variant variables
for key, value := range run.variant.Variables {
argv = append(argv, "--set", "values."+key+"="+value)
}
for _, vm := range testnodes {
argv = append(argv, vm.vmName())
}
cmd := exec.Command(argv[0], argv[1:]...)
cmd.Env = virterEnv(accessNetwork)
cmd.Stderr = &res.testLog
testCtx, cancel := context.WithTimeout(ctx, time.Duration(suiteRun.testSpec.TestTimeout))
defer cancel()
logger.Debugf("EXECUTING TEST: %s", argv)
start = time.Now()
res.err = cmdRunTerm(testCtx, logger, cmd)
timeout := testCtx.Err() != nil
res.execTime = time.Since(start)
logger.Debugf("EXECUTIONTIME: Running test %s: %v", run.testID, res.execTime)
if exitErr, ok := res.err.(*exec.ExitError); ok {
exitErr.Stderr = res.testLog.Bytes()
}
// copy artifacts from VMs
for _, vm := range testnodes {
for _, directory := range suiteRun.testSpec.Artifacts {
// tgtPath will be /outdir/logs/{testname}/{vmname}/copy/path
tgtPath := filepath.Join(run.outDir, vm.vmName(), filepath.Dir(directory))
os.MkdirAll(tgtPath, 0755)
if err := copyDir(logger, vm, run.outDir, directory, tgtPath); err != nil {
logger.Debugf("ARTIFACTCOPY: FAILED copy artifact directory %s: %s", directory, err.Error())
if logger.IsLevelEnabled(log.DebugLevel) {
dumpStderr(logger, err)
}
}
}
}
if ctx.Err() != nil {
res.status = StatusCanceled
res.err = fmt.Errorf("canceled")
} else if timeout {
res.status = StatusFailedTimeout
res.err = fmt.Errorf("timeout: %w", res.err)
} else if res.err != nil {
res.status = StatusFailed
} else {
res.status = StatusSuccess
}
return res
}
func copyDir(logger log.FieldLogger, vm vmInstance, logDir string, srcDir string, hostDir string) error {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
args := []string{"virter", "vm", "cp", vm.vmName() + ":" + srcDir, hostDir}
stderrPath := filepath.Join(logDir, fmt.Sprintf("vm_cp_%s_%s.log", vm.vmName(), strings.ReplaceAll(strings.TrimLeft(srcDir, "/"), "/", "-")))
logger.Debugf("EXECUTING VIRTER COPY: %s", args)
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = virterEnv(vm.networkNames[0])
return cmdStderrTerm(ctx, logger, stderrPath, "", cmd)
}
func getArtifactsUrl(outdir string) string {
jobURL := os.Getenv("CI_JOB_URL")
if jobURL == "" {
return ""
}
buildRoot := os.Getenv("CI_PROJECT_DIR")
if buildRoot == "" {
return ""
}
abs, err := filepath.Abs(outdir)
if err != nil {
return ""
}
relOut, err := filepath.Rel(buildRoot, abs)
if err != nil {
return ""
}
return fmt.Sprintf("%s/artifacts/browse/%s", jobURL, relOut)
}
func getLogViewUrl(testID string) string {
logViewURL := os.Getenv("LINBIT_LOG_VIEWER_URL")
if logViewURL == "" {
return ""
}
projectID := os.Getenv("CI_PROJECT_ID")
if projectID == "" {
return ""
}
jobURL := os.Getenv("CI_JOB_ID")
if jobURL == "" {
return ""
}
if testID == "" {
return fmt.Sprintf("%s/projects/%s/jobs/%s", logViewURL, projectID, jobURL)
} else {
return fmt.Sprintf("%s/projects/%s/jobs/%s/tests/%s", logViewURL, projectID, jobURL, testID)
}
}