in daisy_test_runner/main.go [609:727]
func main() {
addFlags(os.Args[1:])
flag.Parse()
varMap := map[string]string{}
flag.Visit(func(flg *flag.Flag) {
if strings.HasPrefix(flg.Name, varFlagPrefix) {
varMap[strings.TrimPrefix(flg.Name, varFlagPrefix)] = flg.Value.String()
}
})
if len(flag.Args()) == 0 {
fmt.Println("Not enough args, first arg needs to be the path to a test template.")
os.Exit(1)
}
var regex *regexp.Regexp
if *filter != "" {
var err error
regex, err = regexp.Compile(*filter)
if err != nil {
fmt.Println("-filter flag not valid:", err)
os.Exit(1)
}
}
ctx := context.Background()
ts, err := createTestSuite(ctx, flag.Arg(0), varMap, regex)
if err != nil {
log.Fatalln("test case creation error:", err)
}
if ts == nil {
return
}
errors := make(chan error, len(ts.Tests))
// Retry failed locks 2x as many tests in the test case.
retries := len(ts.Tests) * 2
if len(ts.Tests) == 0 {
fmt.Println("[TestRunner] Nothing to do")
return
}
if *print {
for n, t := range ts.Tests {
if t.w == nil {
continue
}
fmt.Printf("[TestRunner] Printing test case %q\n", n)
t.w.Print(ctx)
}
checkError(errors)
return
}
if *validate {
for n, t := range ts.Tests {
if t.w == nil {
continue
}
fmt.Printf("[TestRunner] Validating test case %q\n", n)
if err := t.w.Validate(ctx); err != nil {
errors <- fmt.Errorf("Error validating test case %s: %v", n, err)
}
}
checkError(errors)
return
}
if err := os.MkdirAll(filepath.Dir(*outPath), 0770); err != nil {
log.Fatal(err)
}
junit := &junitTestSuite{Name: ts.Name, Tests: len(ts.Tests)}
tests := make(chan *test, len(ts.Tests))
var wg sync.WaitGroup
for i := 0; i < ts.TestParallelCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for test := range tests {
tc := &junitTestCase{Classname: ts.Name, ID: test.testCase.id, Name: test.name}
junit.mx.Lock()
junit.TestCase = append(junit.TestCase, tc)
junit.mx.Unlock()
if test.testCase.w == nil {
junit.mx.Lock()
junit.Skipped++
junit.mx.Unlock()
tc.Skipped = &junitSkipped{Message: fmt.Sprintf("Test does not match filter: %q", regex.String())}
continue
}
runTestCase(ctx, test, tc, errors, retries)
}
}()
}
start := time.Now()
for n, t := range ts.Tests {
tests <- &test{name: n, testCase: t}
}
close(tests)
wg.Wait()
fmt.Printf("[TestRunner] Creating junit xml file: %q\n", *outPath)
junit.Time = time.Since(start).Seconds()
d, err := xml.MarshalIndent(junit, " ", " ")
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(*outPath, d, 0644); err != nil {
log.Fatal(err)
}
checkError(errors)
fmt.Println("[TestRunner] All test cases completed successfully.")
}