2015-06-16 23:36:46 +03:00
|
|
|
///bin/true; exec /usr/bin/env go run "$0" "$@"
|
|
|
|
|
2015-04-22 06:12:20 +03:00
|
|
|
// Copyright 2015, Google Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
/*
|
|
|
|
test.go is a "Go script" for running Vitess tests. It runs each test in its own
|
|
|
|
Docker container for hermeticity and (potentially) parallelism. If a test fails,
|
|
|
|
this script will save the output in _test/ and continue with other tests.
|
|
|
|
|
|
|
|
Before using it, you should have Docker 1.5+ installed, and have your user in
|
|
|
|
the group that lets you run the docker command without sudo. The first time you
|
|
|
|
run against a given flavor, it may take some time for the corresponding
|
|
|
|
bootstrap image (vitess/bootstrap:<flavor>) to be downloaded.
|
|
|
|
|
|
|
|
It is meant to be run from the Vitess root, like so:
|
|
|
|
~/src/github.com/youtube/vitess$ go run test.go [args]
|
|
|
|
|
|
|
|
For a list of options, run:
|
|
|
|
$ go run test.go --help
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
// This Go script shouldn't rely on any packages that aren't in the standard
|
|
|
|
// library, since that would require the user to bootstrap before running it.
|
2015-04-22 06:12:20 +03:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-06-14 23:03:14 +03:00
|
|
|
"io"
|
2015-04-22 06:12:20 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
|
|
|
"path"
|
2015-06-12 10:32:24 +03:00
|
|
|
"sort"
|
2015-04-22 06:12:20 +03:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-06-12 10:32:24 +03:00
|
|
|
var usage = `Usage of test.go:
|
|
|
|
|
|
|
|
go run test.go [options] [test_name ...]
|
|
|
|
|
|
|
|
If one or more test names are provided, run only those tests.
|
|
|
|
Otherwise, run all tests in test/config.json.
|
|
|
|
`
|
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
// Flags
|
2015-04-22 06:12:20 +03:00
|
|
|
var (
|
|
|
|
flavor = flag.String("flavor", "mariadb", "bootstrap flavor to run against")
|
2015-06-12 10:41:52 +03:00
|
|
|
runCount = flag.Int("runs", 1, "run each test this many times")
|
2015-04-22 06:12:20 +03:00
|
|
|
retryMax = flag.Int("retry", 3, "max number of retries, to detect flaky tests")
|
|
|
|
logPass = flag.Bool("log-pass", false, "log test output even if it passes")
|
|
|
|
timeout = flag.Duration("timeout", 10*time.Minute, "timeout for each test")
|
2015-06-27 04:27:25 +03:00
|
|
|
pull = flag.Bool("pull", true, "re-pull the bootstrap image, in case it's been updated")
|
2015-06-05 01:54:29 +03:00
|
|
|
|
|
|
|
extraArgs = flag.String("extra-args", "", "extra args to pass to each test")
|
2015-04-22 06:12:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the overall object serialized in test/config.json.
|
|
|
|
type Config struct {
|
2015-06-12 10:32:24 +03:00
|
|
|
Tests map[string]*Test
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test is an entry from the test/config.json file.
|
|
|
|
type Test struct {
|
2015-06-16 20:56:57 +03:00
|
|
|
Name, File, Args, Command string
|
2015-05-08 01:31:29 +03:00
|
|
|
|
2015-06-14 21:06:11 +03:00
|
|
|
cmd *exec.Cmd
|
|
|
|
runIndex int
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// run executes a single try.
|
2015-05-08 01:31:29 +03:00
|
|
|
// dir is the location of the vitess repo to use.
|
2015-06-14 23:03:14 +03:00
|
|
|
// returns the combined stdout+stderr and error.
|
|
|
|
func (t *Test) run(dir string) ([]byte, error) {
|
2015-06-16 20:56:57 +03:00
|
|
|
testCmd := t.Command
|
|
|
|
if testCmd == "" {
|
|
|
|
// Teardown is unnecessary since Docker kills everything.
|
|
|
|
testCmd = fmt.Sprintf("make build && test/%s -v --skip-teardown %s", t.File, t.Args)
|
|
|
|
if *extraArgs != "" {
|
|
|
|
testCmd += " " + *extraArgs
|
|
|
|
}
|
2015-06-05 01:54:29 +03:00
|
|
|
}
|
2015-05-08 01:31:29 +03:00
|
|
|
dockerCmd := exec.Command(path.Join(dir, "docker/test/run.sh"), *flavor, testCmd)
|
|
|
|
dockerCmd.Dir = dir
|
|
|
|
t.cmd = dockerCmd
|
2015-04-22 06:12:20 +03:00
|
|
|
|
|
|
|
// Stop the test if it takes too long.
|
|
|
|
done := make(chan struct{})
|
|
|
|
timer := time.NewTimer(*timeout)
|
|
|
|
defer timer.Stop()
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-timer.C:
|
|
|
|
t.logf("timeout exceeded")
|
|
|
|
if dockerCmd.Process != nil {
|
|
|
|
dockerCmd.Process.Signal(syscall.SIGTERM)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-23 00:18:45 +03:00
|
|
|
// Run the test.
|
2015-06-14 23:03:14 +03:00
|
|
|
defer close(done)
|
|
|
|
return dockerCmd.CombinedOutput()
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
// stop will terminate the test if it's running.
|
|
|
|
// If the test is not running, it's a no-op.
|
|
|
|
func (t *Test) stop() {
|
|
|
|
if cmd := t.cmd; cmd != nil {
|
|
|
|
if proc := cmd.Process; proc != nil {
|
|
|
|
proc.Signal(syscall.SIGTERM)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Test) logf(format string, v ...interface{}) {
|
2015-06-14 21:06:11 +03:00
|
|
|
if *runCount > 1 {
|
|
|
|
log.Printf("%v[%v/%v]: %v", t.Name, t.runIndex+1, *runCount, fmt.Sprintf(format, v...))
|
|
|
|
} else {
|
|
|
|
log.Printf("%v: %v", t.Name, fmt.Sprintf(format, v...))
|
|
|
|
}
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-06-12 10:32:24 +03:00
|
|
|
flag.Usage = func() {
|
|
|
|
os.Stderr.WriteString(usage)
|
|
|
|
os.Stderr.WriteString("\nOptions:\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
2015-04-22 06:12:20 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-06-12 10:32:24 +03:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2015-06-14 23:03:14 +03:00
|
|
|
// Make output directory.
|
|
|
|
outDir := path.Join("_test", fmt.Sprintf("%v.%v.%v", *flavor, startTime.Format("20060102-150405"), os.Getpid()))
|
|
|
|
if err := os.MkdirAll(outDir, os.FileMode(0755)); err != nil {
|
|
|
|
log.Fatalf("Can't create output directory: %v", err)
|
|
|
|
}
|
|
|
|
logFile, err := os.OpenFile(path.Join(outDir, "test.log"), os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Can't create log file: %v", err)
|
|
|
|
}
|
|
|
|
log.SetOutput(io.MultiWriter(os.Stderr, logFile))
|
|
|
|
log.Printf("Output directory: %v", outDir)
|
|
|
|
|
2015-04-22 06:12:20 +03:00
|
|
|
// Get test configs.
|
|
|
|
configData, err := ioutil.ReadFile("test/config.json")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Can't read config file: %v", err)
|
|
|
|
}
|
|
|
|
var config Config
|
|
|
|
if err := json.Unmarshal(configData, &config); err != nil {
|
|
|
|
log.Fatalf("Can't parse config file: %v", err)
|
|
|
|
}
|
2015-04-23 00:18:45 +03:00
|
|
|
log.Printf("Bootstrap flavor: %v", *flavor)
|
2015-04-22 06:12:20 +03:00
|
|
|
|
2015-06-27 04:27:25 +03:00
|
|
|
// Re-pull image.
|
|
|
|
if *pull {
|
|
|
|
image := "vitess/bootstrap:" + *flavor
|
|
|
|
pullTime := time.Now()
|
|
|
|
log.Printf("Pulling %v...", image)
|
|
|
|
cmd := exec.Command("docker", "pull", image)
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
log.Fatalf("Can't pull image: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
log.Printf("Image pulled in %v", time.Since(pullTime))
|
|
|
|
}
|
|
|
|
|
2015-06-12 10:32:24 +03:00
|
|
|
// Positional args specify which tests to run.
|
|
|
|
// If none specified, run all tests in alphabetical order.
|
|
|
|
var tests []*Test
|
|
|
|
if flag.NArg() > 0 {
|
|
|
|
for _, name := range flag.Args() {
|
|
|
|
t, ok := config.Tests[name]
|
|
|
|
if !ok {
|
|
|
|
log.Fatalf("Unknown test: %v", name)
|
|
|
|
}
|
|
|
|
t.Name = name
|
2015-06-14 21:06:11 +03:00
|
|
|
tests = append(tests, t)
|
2015-06-12 10:32:24 +03:00
|
|
|
}
|
|
|
|
} else {
|
2015-06-12 10:41:52 +03:00
|
|
|
var names []string
|
2015-06-14 21:06:11 +03:00
|
|
|
for name := range config.Tests {
|
|
|
|
names = append(names, name)
|
2015-06-12 10:32:24 +03:00
|
|
|
}
|
|
|
|
sort.Strings(names)
|
2015-06-14 21:06:11 +03:00
|
|
|
for _, name := range names {
|
|
|
|
t := config.Tests[name]
|
|
|
|
t.Name = name
|
|
|
|
tests = append(tests, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate tests.
|
|
|
|
if *runCount > 1 {
|
|
|
|
var dup []*Test
|
|
|
|
for _, t := range tests {
|
2015-06-12 10:41:52 +03:00
|
|
|
for i := 0; i < *runCount; i++ {
|
2015-06-14 21:06:11 +03:00
|
|
|
// Make a copy, since they're pointers.
|
|
|
|
test := *t
|
|
|
|
test.runIndex = i
|
|
|
|
dup = append(dup, &test)
|
2015-06-12 10:41:52 +03:00
|
|
|
}
|
2015-06-12 10:32:24 +03:00
|
|
|
}
|
2015-06-14 21:06:11 +03:00
|
|
|
tests = dup
|
2015-06-12 10:32:24 +03:00
|
|
|
}
|
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
// Copy working repo to tmpDir.
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "vt_")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Can't create temp dir in %v", os.TempDir())
|
|
|
|
}
|
|
|
|
log.Printf("Copying working repo to temp dir %v", tmpDir)
|
|
|
|
if out, err := exec.Command("cp", "-R", ".", tmpDir).CombinedOutput(); err != nil {
|
|
|
|
log.Fatalf("Can't copy working repo to temp dir %v: %v: %s", tmpDir, err, out)
|
|
|
|
}
|
|
|
|
// The temp copy needs permissive access so the Docker user can read it.
|
|
|
|
if out, err := exec.Command("chmod", "-R", "go=u", tmpDir).CombinedOutput(); err != nil {
|
|
|
|
log.Printf("Can't set permissions on temp dir %v: %v: %s", tmpDir, err, out)
|
|
|
|
}
|
|
|
|
|
2015-04-22 06:12:20 +03:00
|
|
|
// Keep stats.
|
|
|
|
failed := 0
|
|
|
|
passed := 0
|
|
|
|
flaky := 0
|
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
// Listen for signals.
|
|
|
|
sigchan := make(chan os.Signal)
|
|
|
|
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
|
2015-04-22 06:12:20 +03:00
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
// Run tests.
|
|
|
|
stop := make(chan struct{}) // Close this to tell the loop to stop.
|
|
|
|
done := make(chan struct{}) // The loop closes this when it has stopped.
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
signal.Stop(sigchan)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2015-06-12 10:32:24 +03:00
|
|
|
for _, test := range tests {
|
2015-05-08 01:31:29 +03:00
|
|
|
for try := 1; ; try++ {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
test.logf("cancelled")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if try > *retryMax {
|
|
|
|
// Every try failed.
|
|
|
|
test.logf("retry limit exceeded")
|
|
|
|
failed++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
test.logf("running (try %v/%v)...", try, *retryMax)
|
|
|
|
start := time.Now()
|
2015-06-14 23:03:14 +03:00
|
|
|
output, err := test.run(tmpDir)
|
|
|
|
|
|
|
|
// Save test output.
|
|
|
|
if err != nil || *logPass {
|
|
|
|
outFile := fmt.Sprintf("%v-%v.%v.log", test.Name, test.runIndex+1, try)
|
|
|
|
test.logf("saving test output to %v", outFile)
|
|
|
|
if fileErr := ioutil.WriteFile(path.Join(outDir, outFile), output, os.FileMode(0644)); fileErr != nil {
|
|
|
|
test.logf("WriteFile error: %v", fileErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2015-05-08 01:31:29 +03:00
|
|
|
// This try failed.
|
2015-06-12 10:32:24 +03:00
|
|
|
test.logf("FAILED (try %v/%v) in %v: %v", try, *retryMax, time.Since(start), err)
|
2015-05-08 01:31:29 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if try == 1 {
|
|
|
|
// Passed on the first try.
|
|
|
|
test.logf("PASSED in %v", time.Since(start))
|
|
|
|
passed++
|
|
|
|
} else {
|
|
|
|
// Passed, but not on the first try.
|
2015-06-12 10:32:24 +03:00
|
|
|
test.logf("FLAKY (1/%v passed in %v)", try, time.Since(start))
|
2015-05-08 01:31:29 +03:00
|
|
|
flaky++
|
|
|
|
}
|
|
|
|
break
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|
2015-05-08 01:31:29 +03:00
|
|
|
}
|
|
|
|
}()
|
2015-04-22 06:12:20 +03:00
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
// Stop the loop and kill child processes if we get a signal.
|
|
|
|
select {
|
|
|
|
case <-sigchan:
|
|
|
|
log.Printf("received signal, quitting")
|
|
|
|
// Stop the test loop and wait for it to quit.
|
|
|
|
close(stop)
|
|
|
|
<-done
|
|
|
|
// Terminate all existing tests.
|
2015-06-14 21:06:11 +03:00
|
|
|
for _, t := range tests {
|
2015-05-08 01:31:29 +03:00
|
|
|
t.stop()
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|
2015-05-08 01:31:29 +03:00
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up temp dir.
|
|
|
|
log.Printf("Removing temp dir %v", tmpDir)
|
|
|
|
if err := os.RemoveAll(tmpDir); err != nil {
|
|
|
|
log.Printf("Failed to remove temp dir: %v", err)
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print stats.
|
2015-06-12 10:32:24 +03:00
|
|
|
skipped := len(tests) - passed - flaky - failed
|
2015-05-08 01:31:29 +03:00
|
|
|
log.Printf("%v PASSED, %v FLAKY, %v FAILED, %v SKIPPED", passed, flaky, failed, skipped)
|
2015-06-12 10:32:24 +03:00
|
|
|
log.Printf("Total time: %v", time.Since(startTime))
|
2015-04-23 01:27:35 +03:00
|
|
|
|
2015-05-08 01:31:29 +03:00
|
|
|
if failed > 0 || skipped > 0 {
|
2015-04-23 01:27:35 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-04-22 06:12:20 +03:00
|
|
|
}
|