-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathrun.go
389 lines (346 loc) · 9.03 KB
/
run.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"time"
"github.com/fatih/color"
"github.com/nokia/ntt/control"
"github.com/nokia/ntt/control/k3r"
"github.com/nokia/ntt/control/printer"
"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/internal/log"
"github.com/nokia/ntt/internal/results"
"github.com/nokia/ntt/project"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/doc"
"github.com/nokia/ntt/ttcn3/syntax"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var (
RunCommand = &cobra.Command{
Use: "run [ <path>... ] [ -- <test id>... ]",
Short: "Build and run test suite",
Long: `Build and run a test suite.
The ntt run command first builds a test executable using the files or
directories passed as first argument list.
The test executable is then run with the tests specified as second argument
list. If no ids are specified, ntt run will run all tests in the test suite.
Running control functions is supported. For example:
ntt run -- test.A test.control test.B
Test baskets are also supported (see "ntt help list"). Bellow example will run
all tests with @stable-tag:
NTT_LIST_BASKETS=stable ntt run
`,
RunE: runTests,
}
RunAllTests bool
MaxWorkers int
MaxFail int
errorCount uint64
OutputDir string
ColorFatal = color.New(color.FgRed, color.Bold)
ColorFailure = color.New(color.FgRed, color.Bold)
ColorWarning = color.New(color.FgYellow, color.Bold)
ColorSuccess = color.New()
ColorStart = color.New()
ColorRunning = color.New(color.Faint)
Colors = func(v string) *color.Color {
switch v {
case "pass":
return ColorSuccess
case "inconc":
return ColorWarning
case "none":
return ColorWarning
case "done":
return color.New()
default:
return ColorFailure
}
}
ErrCommandFailed = fmt.Errorf("command failed")
)
func init() {
flags := RunCommand.Flags()
flags.AddFlagSet(BasketFlags())
flags.IntVarP(&MaxWorkers, "jobs", "j", runtime.NumCPU(), "Allow N test in parallel (default: number of CPU cores")
flags.IntVar(&MaxFail, "max-fail", 0, "Stop after N failures")
flags.StringVarP(&OutputDir, "output-dir", "o", "", "store test artefacts in DIR/ID")
flags.BoolVarP(&outputProgress, "progress", "P", false, "show progress")
flags.BoolVarP(&RunAllTests, "all-tests", "a", false, "run all tests instead of control parts")
flags.StringSliceVarP(&testsFiles, "tests-file", "t", nil, "read tests from FILE. If this option is used multiple times all contained tests will be executed in that order. When FILE is '-', read standard input")
}
// Run runs the given jobs in parallel.
func runTests(cmd *cobra.Command, args []string) error {
ctx, cancel := WithSignalHandler(context.Background())
defer cancel()
// Assure that that project binaries are up-to-date, before we execute the tests.
if err := project.Build(Project); err != nil {
return fmt.Errorf("building test suite failed: %w", err)
}
_, ids := splitArgs(args, cmd.ArgsLenAtDash())
plan, err := control.NewTestPlan(Project)
if err != nil {
return err
}
jobs, err := JobQueue(ctx, plan, cmd.Flags(), Project, testsFiles, ids, RunAllTests)
if err != nil {
return err
}
var runs []results.Run
os.Remove(Project.ResultsFile)
defer func() {
db := &results.DB{
Version: "1",
Sessions: []results.Session{
{
Id: "1",
MaxJobs: MaxWorkers,
ExpectedVerdict: "pass",
Runs: runs,
},
},
}
b, err := json.MarshalIndent(db, "", " ")
if err != nil {
return
}
err = ioutil.WriteFile(Project.ResultsFile, b, 0644)
}()
runner, err := control.New(
control.MaxWorkers(MaxWorkers),
control.WithFactory(k3r.Factory(jobs)),
)
if err != nil {
return err
}
var p printer.Printer
switch Format() {
case "plain":
p = printer.NewPlainPrinter()
case "json":
p = printer.NewJSONPrinter()
case "tap":
p = printer.NewTAPPrinter()
default:
p = printer.NewConsolePrinter()
}
for e := range runner.Run(ctx) {
p.Print(e)
switch e := e.(type) {
case control.ErrorEvent:
errorCount++
case control.StopEvent:
if e.Verdict != "pass" && e.Verdict != "done" {
errorCount++
}
r := results.Run{
Name: e.Name,
Verdict: e.Verdict,
Begin: results.Timestamp{Time: e.Begin},
End: results.Timestamp{Time: e.Time()},
WorkingDir: e.Job.Dir,
}
runs = append(runs, r)
}
if MaxFail > 0 && errorCount >= uint64(MaxFail) {
p.Print(control.NewErrorEvent(fmt.Errorf("too many errors. Exiting.")))
cancel()
break
}
}
if c, ok := p.(io.Closer); ok {
c.Close()
}
if errorCount > 0 {
return fmt.Errorf("%w: %d error(s) occurred", ErrCommandFailed, errorCount)
}
return nil
}
func JobQueue(ctx context.Context, plan *control.TestPlan, flags *pflag.FlagSet, conf *project.Config, testsFiles []string, tests []string, allTests bool) (<-chan *control.Job, error) {
basket, err := NewBasketWithFlags("run", flags)
if err != nil {
return nil, fmt.Errorf("creating basket failed: %w", err)
}
if err := basket.LoadFromEnvOrConfig(conf, "NTT_LIST_BASKETS"); err != nil {
return nil, fmt.Errorf("loading baskets failed: %w", err)
}
var tsts []string
for _, f := range testsFiles {
t, err := readTestsFromFile(f)
if err != nil {
return nil, fmt.Errorf("reading tests from file %s failed: %w", f, err)
}
tsts = append(tsts, t...)
}
srcs, err := fs.TTCN3Files(conf.Sources...)
if err != nil {
return nil, err
}
needTests := len(tests) == 0 && len(testsFiles) == 0
m := sync.Map{}
t := make([][]string, len(srcs))
wg := sync.WaitGroup{}
wg.Add(len(srcs))
start := time.Now()
for i, src := range srcs {
go func(src string, i int) {
defer wg.Done()
var (
mod string
modLvl, lvl int
)
root := ttcn3.ParseFile(src)
root.Inspect(func(n syntax.Node) bool {
if n == nil {
if lvl == modLvl {
mod = ""
modLvl = 0
}
lvl--
} else {
lvl++
}
switch n := n.(type) {
case *syntax.Module:
mod = n.Name.String()
modLvl = lvl
return true
case *syntax.FuncDecl:
if !n.IsTest() && !n.IsControl() {
return false
}
name := ttcn3.JoinNames(mod, n.Name.String())
m.Store(name, n)
if needTests {
if n.IsTest() && allTests || n.IsControl() && !allTests {
t[i] = append(t[i], name)
}
}
return false
case *syntax.ControlPart:
name := ttcn3.JoinNames(mod, n.Name.String())
m.Store(name, n)
if needTests && !allTests {
t[i] = append(t[i], name)
}
return false
default:
return true
}
})
}(src, i)
}
wg.Wait()
log.Debugf("Scanned all tests in %s.\n", time.Since(start))
testPlan := append(tsts, tests...)
if needTests {
for _, tests := range t {
testPlan = append(testPlan, tests...)
}
}
out := make(chan *control.Job)
go func() {
defer close(out)
names := make(map[string]int)
for _, name := range testPlan {
var tags [][]string
if def, ok := m.Load(name); ok {
tags = doc.FindAllTags(syntax.Doc(def.(syntax.Node)))
}
if !basket.Match(name, tags) {
continue
}
configs, err := conf.TestConfigs(name)
if err != nil {
log.Verbose(err.Error())
continue
}
if len(configs) == 0 {
log.Verbosef("no config for %s", name)
continue
}
for _, tc := range configs {
id := fmt.Sprintf("%s-%d", name, names[name])
names[name]++
job := &control.Job{
ID: id,
Name: name,
Config: conf,
Dir: OutputDir,
Timeout: tc.Timeout.Duration,
ModulePars: tc.Parameters,
}
select {
case out <- job:
case <-ctx.Done():
return
}
}
}
}()
return out, nil
}
// EntryPoints returns controls parts of the given TTCN-3 source file. When tests is true, it returns all testcases instead.
func EntryPoints(file string, tests bool) []*ttcn3.Node {
tree := ttcn3.ParseFile(file)
if tests {
return tree.Tests()
}
return tree.Controls()
}
// WithSignalHandler adds a signal handler for ^C to the context.
func WithSignalHandler(ctx context.Context) (context.Context, context.CancelFunc) {
ctx2, cancel := context.WithCancel(context.Background())
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
go func() {
select {
case <-signalChan: // first signal, cancel context
cancel()
case <-ctx.Done():
}
<-signalChan // second signal, hard exit
os.Exit(2)
}()
return ctx2, func() {
signal.Stop(signalChan)
cancel()
}
}
func readTestsFromFile(path string) ([]string, error) {
var (
lines []byte
err error
)
if path == "-" {
lines, err = ioutil.ReadAll(os.Stdin)
} else {
f, err := os.Open(path)
if err != nil {
return nil, err
}
lines, err = ioutil.ReadAll(f)
if err != nil {
return nil, err
}
}
var tests []string
for _, line := range strings.Split(string(lines), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, "//") {
continue
}
tests = append(tests, line)
}
return tests, err
}