2015-06-16 23:36:46 +03:00
///bin/true; exec /usr/bin/env go run "$0" "$@"
2017-05-06 02:21:12 +03:00
/ *
2019-10-12 10:55:55 +03:00
* Copyright 2019 The Vitess Authors .
2017-05-06 02:21:12 +03:00
2019-10-12 10:55:55 +03:00
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
2017-05-06 02:21:12 +03:00
2019-10-12 10:55:55 +03:00
* http : //www.apache.org/licenses/LICENSE-2.0
2017-05-06 02:21:12 +03:00
2019-10-12 10:55:55 +03:00
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2015-04-22 06:12:20 +03:00
/ *
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 :
2019-12-07 22:11:06 +03:00
$ go run test . go [ args ]
2015-04-22 06:12:20 +03:00
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 (
2015-08-24 04:26:02 +03:00
"bytes"
2015-04-22 06:12:20 +03:00
"encoding/json"
2015-08-23 15:43:21 +03:00
"errors"
2015-04-22 06:12:20 +03:00
"flag"
"fmt"
2015-06-14 23:03:14 +03:00
"io"
2015-04-22 06:12:20 +03:00
"io/ioutil"
"log"
2015-08-24 04:26:02 +03:00
"net/http"
"net/url"
2015-04-22 06:12:20 +03:00
"os"
"os/exec"
"os/signal"
"path"
2015-06-12 10:32:24 +03:00
"sort"
2015-08-24 04:26:02 +03:00
"strconv"
2015-08-23 12:56:01 +03:00
"strings"
2015-08-24 07:32:34 +03:00
"sync"
2015-04-22 06:12:20 +03:00
"syscall"
"time"
)
2015-06-12 10:32:24 +03:00
var usage = ` Usage of test . go :
2015-08-24 23:44:52 +03:00
go run test . go [ options ] [ test_name ... ] [ -- extra - py - test - args ]
2015-06-12 10:32:24 +03:00
If one or more test names are provided , run only those tests .
Otherwise , run all tests in test / config . json .
2015-08-24 23:44:52 +03:00
To pass extra args to Python tests ( test / * . py ) , terminate the
list of test names with -- and then add them at the end .
For example :
2017-11-16 02:01:44 +03:00
go run test . go test1 test2 -- -- topo - server - flavor = etcd2
2015-06-12 10:32:24 +03:00
`
2015-05-08 01:31:29 +03:00
// Flags
2015-04-22 06:12:20 +03:00
var (
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
flavor = flag . String ( "flavor" , "mysql57" , "comma-separated bootstrap flavor(s) to run against (when using Docker mode). Available flavors: all," + flavors )
runCount = flag . Int ( "runs" , 1 , "run each test this many times" )
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" )
pull = flag . Bool ( "pull" , true , "re-pull the bootstrap image, in case it's been updated" )
docker = flag . Bool ( "docker" , true , "run tests with Docker" )
useDockerCache = flag . Bool ( "use_docker_cache" , false , "if true, create a temporary Docker image to cache the source code and the binaries generated by 'make build'. Used for execution on Travis CI." )
shard = flag . Int ( "shard" , - 1 , "if N>=0, run the tests whose Shard field matches N" )
tag = flag . String ( "tag" , "" , "if provided, only run tests with the given tag. Can't be combined with -shard or explicit test list" )
exclude = flag . String ( "exclude" , "" , "if provided, exclude tests containing any of the given tags (comma delimited)" )
reshard = flag . Int ( "rebalance" , 0 , "if N>0, check the stats and group tests into N similarly-sized bins by average run time" )
keepData = flag . Bool ( "keep-data" , false , "don't delete the per-test VTDATAROOT subfolders" )
printLog = flag . Bool ( "print-log" , false , "print the log of each failed test (or all tests if -log-pass) to the console" )
follow = flag . Bool ( "follow" , false , "print test output as it runs, instead of waiting to see if it passes or fails" )
parallel = flag . Int ( "parallel" , 1 , "number of tests to run in parallel" )
2015-08-24 04:26:02 +03:00
remoteStats = flag . String ( "remote-stats" , "" , "url to send remote stats" )
2015-04-22 06:12:20 +03:00
)
2015-08-24 23:44:52 +03:00
var (
vtDataRoot = os . Getenv ( "VTDATAROOT" )
extraArgs [ ] string
)
2015-08-23 12:56:01 +03:00
2015-08-23 15:43:21 +03:00
const (
statsFileName = "test/stats.json"
configFileName = "test/config.json"
2019-04-11 10:03:51 +03:00
// List of flavors for which a bootstrap Docker image is available.
2019-12-22 23:19:02 +03:00
flavors = "mysql56,mysql57,mysql80,mariadb,mariadb103,percona,percona57,percona80"
2015-08-23 15:43:21 +03:00
)
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-08-24 23:44:52 +03:00
File string
Args , Command [ ] string
2015-05-08 01:31:29 +03:00
2015-08-23 13:33:46 +03:00
// Manual means it won't be run unless explicitly specified.
Manual bool
2015-08-23 15:43:21 +03:00
// Shard is used to split tests among workers.
Shard int
2015-10-29 23:27:23 +03:00
// RetryMax is the maximum number of times a test will be retried.
// If 0, flag *retryMax is used.
2015-08-26 10:09:18 +03:00
RetryMax int
2015-10-29 23:27:23 +03:00
// Tags is a list of tags that can be used to filter tests.
Tags [ ] string
2015-08-24 04:26:02 +03:00
name string
2016-06-24 04:37:30 +03:00
flavor string
2015-06-14 21:06:11 +03:00
runIndex int
2015-08-24 04:26:02 +03:00
pass , fail int
2015-04-22 06:12:20 +03:00
}
2015-10-29 23:27:23 +03:00
func ( t * Test ) hasTag ( want string ) bool {
for _ , got := range t . Tags {
if got == want {
return true
}
}
return false
}
2016-07-02 01:06:22 +03:00
func ( t * Test ) hasAnyTag ( want [ ] string ) bool {
for _ , tag := range want {
if t . hasTag ( tag ) {
return true
}
}
return false
}
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-08-23 12:56:01 +03:00
// dataDir is the VTDATAROOT to use for this run.
2015-06-14 23:03:14 +03:00
// returns the combined stdout+stderr and error.
2015-08-23 12:56:01 +03:00
func ( t * Test ) run ( dir , dataDir string ) ( [ ] byte , error ) {
2015-06-16 20:56:57 +03:00
testCmd := t . Command
2015-08-24 23:44:52 +03:00
if len ( testCmd ) == 0 {
2019-12-26 09:19:10 +03:00
if strings . Contains ( fmt . Sprintf ( "%v" , t . File ) , ".go" ) {
testCmd = [ ] string { "tools/e2e_go_test.sh" }
testCmd = append ( testCmd , t . Args ... )
} else {
testCmd = [ ] string { "test/" + t . File , "-v" , "--skip-build" , "--keep-logs" }
testCmd = append ( testCmd , t . Args ... )
}
2015-08-24 23:44:52 +03:00
testCmd = append ( testCmd , extraArgs ... )
2015-08-23 12:56:01 +03:00
if * docker {
// Teardown is unnecessary since Docker kills everything.
2015-08-24 23:44:52 +03:00
testCmd = append ( testCmd , "--skip-teardown" )
2015-06-16 20:56:57 +03:00
}
2015-06-05 01:54:29 +03:00
}
2015-08-23 12:56:01 +03:00
2015-08-24 23:44:52 +03:00
var cmd * exec . Cmd
2015-08-23 12:56:01 +03:00
if * docker {
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
var args [ ] string
testArgs := strings . Join ( testCmd , " " )
if * useDockerCache {
args = [ ] string { "--use_docker_cache" , cacheImage ( t . flavor ) , t . flavor , testArgs }
} else {
// If there is no cache, we have to call 'make build' before each test.
args = [ ] string { t . flavor , "make build && " + testArgs }
}
cmd = exec . Command ( path . Join ( dir , "docker/test/run.sh" ) , args ... )
2015-08-23 12:56:01 +03:00
} else {
2015-08-24 23:44:52 +03:00
cmd = exec . Command ( testCmd [ 0 ] , testCmd [ 1 : ] ... )
2015-08-23 12:56:01 +03:00
}
2015-08-24 23:44:52 +03:00
cmd . Dir = dir
2015-08-23 12:56:01 +03:00
// Put everything in a unique dir, so we can copy and/or safely delete it.
2015-08-24 07:32:34 +03:00
// Also try to make them use different port ranges
// to mitigate failures due to zombie processes.
2015-08-24 23:44:52 +03:00
cmd . Env = updateEnv ( os . Environ ( ) , map [ string ] string {
2019-12-07 22:11:06 +03:00
"VTROOT" : "/vt/src/vitess.io/vitess" ,
2015-08-24 07:32:34 +03:00
"VTDATAROOT" : dataDir ,
"VTPORTSTART" : strconv . FormatInt ( int64 ( getPortStart ( 100 ) ) , 10 ) ,
2015-08-23 12:56:01 +03:00
} )
2015-04-22 06:12:20 +03:00
2015-08-24 04:26:02 +03:00
// Capture test output.
buf := & bytes . Buffer { }
2015-08-24 23:44:52 +03:00
cmd . Stdout = buf
2015-08-24 04:26:02 +03:00
if * follow {
2015-08-24 23:44:52 +03:00
cmd . Stdout = io . MultiWriter ( cmd . Stdout , os . Stdout )
2015-08-24 04:26:02 +03:00
}
2015-08-24 23:44:52 +03:00
cmd . Stderr = cmd . Stdout
2015-08-24 04:26:02 +03:00
// Run the test.
2015-08-24 23:44:52 +03:00
done := make ( chan error , 1 )
go func ( ) {
done <- cmd . Run ( )
} ( )
2015-04-22 06:12:20 +03:00
2015-08-24 23:44:52 +03:00
// Wait for it to finish.
var runErr error
timer := time . NewTimer ( * timeout )
defer timer . Stop ( )
select {
case runErr = <- done :
if runErr == nil {
t . pass ++
} else {
t . fail ++
2015-05-08 01:31:29 +03:00
}
2015-08-24 23:44:52 +03:00
case <- timer . C :
t . logf ( "timeout exceeded" )
cmd . Process . Signal ( syscall . SIGINT )
t . fail ++
runErr = <- done
2015-05-08 01:31:29 +03:00
}
2015-08-24 23:44:52 +03:00
return buf . Bytes ( ) , runErr
2015-05-08 01:31:29 +03:00
}
func ( t * Test ) logf ( format string , v ... interface { } ) {
2015-06-14 21:06:11 +03:00
if * runCount > 1 {
2016-06-24 04:37:30 +03:00
log . Printf ( "%v.%v[%v/%v]: %v" , t . flavor , t . name , t . runIndex + 1 , * runCount , fmt . Sprintf ( format , v ... ) )
2015-06-14 21:06:11 +03:00
} else {
2016-06-24 04:37:30 +03:00
log . Printf ( "%v.%v: %v" , t . flavor , t . name , fmt . Sprintf ( format , v ... ) )
2015-06-14 21:06:11 +03:00
}
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 ( )
2016-06-24 05:13:09 +03:00
// Sanity checks.
2016-11-17 03:31:08 +03:00
if * docker {
if * flavor == "all" {
* flavor = flavors
}
if * flavor == "" {
log . Fatalf ( "Must provide at least one -flavor when using -docker mode. Available flavors: all,%v" , flavors )
}
2016-06-24 05:13:09 +03:00
}
if * parallel < 1 {
log . Fatalf ( "Invalid -parallel value: %v" , * parallel )
}
if * parallel > 1 && ! * docker {
log . Fatalf ( "Can't use -parallel value > 1 when -docker=false" )
}
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
if * useDockerCache && ! * docker {
log . Fatalf ( "Can't use -use_docker_cache when -docker=false" )
}
2016-06-24 05:13:09 +03:00
2015-06-12 10:32:24 +03:00
startTime := time . Now ( )
2015-06-14 23:03:14 +03:00
// Make output directory.
2016-06-24 04:37:30 +03:00
outDir := path . Join ( "_test" , fmt . Sprintf ( "%v.%v" , startTime . Format ( "20060102-150405" ) , os . Getpid ( ) ) )
2015-06-14 23:03:14 +03:00
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.
2015-08-23 15:43:21 +03:00
configData , err := ioutil . ReadFile ( configFileName )
2015-04-22 06:12:20 +03:00
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-08-23 12:56:01 +03:00
2015-08-23 15:43:21 +03:00
// Resharding.
if * reshard > 0 {
if err := reshardTests ( & config , * reshard ) ; err != nil {
2015-08-24 04:26:02 +03:00
log . Fatalf ( "resharding error: %v" , err )
}
log . Printf ( "Saving updated config..." )
data , err := json . MarshalIndent ( config , "" , "\t" )
if err != nil {
log . Fatalf ( "can't save new config: %v" , err )
}
if err := ioutil . WriteFile ( configFileName , data , 0644 ) ; err != nil {
log . Fatalf ( "can't write new config: %v" , err )
2015-08-23 15:43:21 +03:00
}
return
}
2016-06-24 04:37:30 +03:00
flavors := [ ] string { "local" }
2015-08-23 12:56:01 +03:00
if * docker {
2016-06-24 04:37:30 +03:00
log . Printf ( "Bootstrap flavor(s): %v" , * flavor )
flavors = strings . Split ( * flavor , "," )
2015-08-23 12:56:01 +03:00
2016-06-24 04:37:30 +03:00
// Re-pull image(s).
2015-08-24 06:21:05 +03:00
if * pull {
2016-06-24 04:37:30 +03:00
var wg sync . WaitGroup
for _ , flavor := range flavors {
wg . Add ( 1 )
go func ( flavor string ) {
defer wg . Done ( )
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: %v\n%s" , image , err , out )
}
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
log . Printf ( "Image %v pulled in %v" , image , round ( time . Since ( pullTime ) ) )
2016-06-24 04:37:30 +03:00
} ( flavor )
2015-08-23 12:56:01 +03:00
}
2016-06-24 04:37:30 +03:00
wg . Wait ( )
2015-08-23 12:56:01 +03:00
}
} else {
if vtDataRoot == "" {
log . Fatalf ( "VTDATAROOT env var must be set in -docker=false mode. Make sure to source dev.env." )
2015-06-27 04:27:25 +03:00
}
}
2015-08-23 15:43:21 +03:00
// Pick the tests to run.
2015-08-24 23:44:52 +03:00
var testArgs [ ] string
testArgs , extraArgs = splitArgs ( flag . Args ( ) , "--" )
tests := selectedTests ( testArgs , & config )
2015-06-14 21:06:11 +03:00
2016-06-24 04:37:30 +03:00
// Duplicate tests for run count.
2015-06-14 21:06:11 +03:00
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
}
2016-06-24 04:37:30 +03:00
// Duplicate tests for flavors.
var dup [ ] * Test
for _ , flavor := range flavors {
for _ , t := range tests {
test := * t
test . flavor = flavor
dup = append ( dup , & test )
}
}
tests = dup
2019-12-07 22:11:06 +03:00
vtRoot := "."
2015-08-23 12:56:01 +03:00
tmpDir := ""
if * docker {
// Copy working repo to tmpDir.
// This doesn't work outside Docker since it messes up GOROOT.
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 )
}
2019-12-07 22:11:06 +03:00
vtRoot = tmpDir
2015-08-24 23:44:52 +03:00
} else {
// Since we're sharing the working dir, do the build once for all tests.
log . Printf ( "Running make build..." )
2016-08-25 08:16:06 +03:00
if out , err := exec . Command ( "make" , "build" ) . CombinedOutput ( ) ; err != nil {
2015-08-24 23:44:52 +03:00
log . Fatalf ( "make build failed: %v\n%s" , err , out )
}
2015-05-08 01:31:29 +03:00
}
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
if * useDockerCache {
for _ , flavor := range flavors {
start := time . Now ( )
log . Printf ( "Creating Docker cache image for flavor '%s'..." , flavor )
if out , err := exec . Command ( "docker/test/run.sh" , "--create_docker_cache" , cacheImage ( flavor ) , flavor , "make build" ) . CombinedOutput ( ) ; err != nil {
log . Fatalf ( "Failed to create Docker cache image for flavor '%s': %v\n%s" , flavor , err , out )
}
log . Printf ( "Creating Docker cache image took %v" , round ( time . Since ( start ) ) )
}
}
2016-06-24 05:13:09 +03:00
// Keep stats for the overall run.
var mu sync . Mutex
2015-04-22 06:12:20 +03:00
failed := 0
passed := 0
flaky := 0
2015-05-08 01:31:29 +03:00
// Listen for signals.
2019-03-04 07:53:19 +03:00
sigchan := make ( chan os . Signal , 1 )
2015-08-24 23:44:52 +03:00
signal . Notify ( sigchan , syscall . SIGINT )
2015-04-22 06:12:20 +03:00
2015-05-08 01:31:29 +03:00
// Run tests.
2016-06-24 05:13:09 +03:00
stop := make ( chan struct { } ) // Close this to tell the runners to stop.
done := make ( chan struct { } ) // This gets closed when all runners have stopped.
next := make ( chan * Test ) // The next test to run.
var wg sync . WaitGroup
2015-05-08 01:31:29 +03:00
2016-06-24 05:13:09 +03:00
// Send all tests into the channel.
go func ( ) {
2015-06-12 10:32:24 +03:00
for _ , test := range tests {
2016-06-24 05:13:09 +03:00
next <- test
}
close ( next )
} ( )
2015-05-08 01:31:29 +03:00
2016-06-24 05:13:09 +03:00
// Start the requested number of parallel runners.
for i := 0 ; i < * parallel ; i ++ {
wg . Add ( 1 )
go func ( ) {
defer wg . Done ( )
2015-08-23 12:56:01 +03:00
2016-06-24 05:13:09 +03:00
for test := range next {
tryMax := * retryMax
if test . RetryMax != 0 {
tryMax = test . RetryMax
2015-08-23 12:56:01 +03:00
}
2016-06-24 05:13:09 +03:00
for try := 1 ; ; try ++ {
select {
case <- stop :
test . logf ( "cancelled" )
return
default :
}
if try > tryMax {
// Every try failed.
test . logf ( "retry limit exceeded" )
mu . Lock ( )
failed ++
mu . Unlock ( )
break
}
2015-08-23 12:56:01 +03:00
2016-06-24 05:13:09 +03:00
test . logf ( "running (try %v/%v)..." , try , tryMax )
2015-06-14 23:03:14 +03:00
2016-06-24 05:13:09 +03:00
// Make a unique VTDATAROOT.
dataDir , err := ioutil . TempDir ( vtDataRoot , "vt_" )
if err != nil {
test . logf ( "Failed to create temporary subdir in VTDATAROOT: %v" , vtDataRoot )
mu . Lock ( )
failed ++
mu . Unlock ( )
break
2015-08-24 04:26:02 +03:00
}
2016-06-24 05:13:09 +03:00
// Run the test.
start := time . Now ( )
2019-12-07 22:11:06 +03:00
output , err := test . run ( vtRoot , dataDir )
2016-06-24 05:13:09 +03:00
duration := time . Since ( start )
// Save/print test output.
if err != nil || * logPass {
if * printLog && ! * follow {
test . logf ( "%s\n" , output )
}
outFile := fmt . Sprintf ( "%v.%v-%v.%v.log" , test . flavor , test . name , test . runIndex + 1 , try )
outFilePath := path . Join ( outDir , outFile )
test . logf ( "saving test output to %v" , outFilePath )
if fileErr := ioutil . WriteFile ( outFilePath , output , os . FileMode ( 0644 ) ) ; fileErr != nil {
test . logf ( "WriteFile error: %v" , fileErr )
}
2015-06-14 23:03:14 +03:00
}
2016-06-24 05:13:09 +03:00
// Clean up the unique VTDATAROOT.
if ! * keepData {
if err := os . RemoveAll ( dataDir ) ; err != nil {
test . logf ( "WARNING: can't remove temporary VTDATAROOT: %v" , err )
}
2015-08-24 04:26:02 +03:00
}
2015-08-23 12:56:01 +03:00
2016-06-24 05:13:09 +03:00
if err != nil {
// This try failed.
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
test . logf ( "FAILED (try %v/%v) in %v: %v" , try , tryMax , round ( duration ) , err )
2016-06-24 05:13:09 +03:00
mu . Lock ( )
testFailed ( test . name )
mu . Unlock ( )
continue
}
2015-05-08 01:31:29 +03:00
2016-06-24 05:13:09 +03:00
mu . Lock ( )
testPassed ( test . name , duration )
if try == 1 {
// Passed on the first try.
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
test . logf ( "PASSED in %v" , round ( duration ) )
2016-06-24 05:13:09 +03:00
passed ++
} else {
// Passed, but not on the first try.
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
test . logf ( "FLAKY (1/%v passed in %v)" , try , round ( duration ) )
2016-06-24 05:13:09 +03:00
flaky ++
testFlaked ( test . name , try )
}
mu . Unlock ( )
break
2015-05-08 01:31:29 +03:00
}
2015-04-22 06:12:20 +03:00
}
2016-06-24 05:13:09 +03:00
} ( )
}
// Close the done channel when all the runners stop.
// This lets us select on wg.Wait().
go func ( ) {
wg . Wait ( )
close ( done )
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 :
2015-08-24 23:44:52 +03:00
log . Printf ( "interrupted: skip remaining tests and wait for current test to tear down" )
2016-06-24 05:13:09 +03:00
signal . Stop ( sigchan )
2015-08-24 23:44:52 +03:00
// Stop the test loop and wait for it to exit.
// Running tests already get the SIGINT themselves.
// We mustn't send it again, or it'll abort the teardown process too early.
2015-05-08 01:31:29 +03:00
close ( stop )
<- done
case <- done :
}
// Clean up temp dir.
2015-08-23 12:56:01 +03:00
if tmpDir != "" {
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
}
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
// Remove temporary Docker cache image.
if * useDockerCache {
for _ , flavor := range flavors {
log . Printf ( "Removing temporary Docker cache image for flavor '%s'" , flavor )
if out , err := exec . Command ( "docker" , "rmi" , cacheImage ( flavor ) ) . CombinedOutput ( ) ; err != nil {
log . Printf ( "WARNING: Failed to delete Docker cache image: %v\n%s" , err , out )
}
}
}
2015-04-22 06:12:20 +03:00
2015-08-24 04:26:02 +03:00
// Print summary.
2019-03-04 07:53:19 +03:00
log . Print ( strings . Repeat ( "=" , 60 ) )
2015-08-24 04:26:02 +03:00
for _ , t := range tests {
2016-06-24 04:37:30 +03:00
tname := t . flavor + "." + t . name
2015-08-24 04:26:02 +03:00
switch {
case t . pass > 0 && t . fail == 0 :
2016-06-24 04:37:30 +03:00
log . Printf ( "%-40s\tPASS" , tname )
2015-08-24 04:26:02 +03:00
case t . pass > 0 && t . fail > 0 :
2016-06-24 04:37:30 +03:00
log . Printf ( "%-40s\tFLAKY (%v/%v failed)" , tname , t . fail , t . pass + t . fail )
2015-08-24 04:26:02 +03:00
case t . pass == 0 && t . fail > 0 :
2016-06-24 04:37:30 +03:00
log . Printf ( "%-40s\tFAIL (%v tries)" , tname , t . fail )
2015-08-24 04:26:02 +03:00
case t . pass == 0 && t . fail == 0 :
2016-06-24 04:37:30 +03:00
log . Printf ( "%-40s\tSKIPPED" , tname )
2015-08-24 04:26:02 +03:00
}
}
2019-03-04 07:53:19 +03:00
log . Print ( strings . Repeat ( "=" , 60 ) )
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 )
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
log . Printf ( "Total time: %v" , round ( 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
}
2015-08-23 12:56:01 +03:00
func updateEnv ( orig [ ] string , updates map [ string ] string ) [ ] string {
var env [ ] string
for _ , v := range orig {
parts := strings . SplitN ( v , "=" , 2 )
if _ , ok := updates [ parts [ 0 ] ] ; ! ok {
env = append ( env , v )
}
}
for k , v := range updates {
env = append ( env , k + "=" + v )
}
return env
}
2015-08-23 15:43:21 +03:00
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
// cacheImage returns the flavor-specific name of the Docker cache image.
func cacheImage ( flavor string ) string {
return fmt . Sprintf ( "vitess/bootstrap:rm_%s_test_cache_do_NOT_push" , flavor )
}
2015-08-23 15:43:21 +03:00
type Stats struct {
TestStats map [ string ] TestStats
}
type TestStats struct {
Pass , Fail , Flake int
PassTime time . Duration
name string
}
2015-08-24 04:26:02 +03:00
func sendStats ( values url . Values ) {
if * remoteStats != "" {
log . Printf ( "Sending remote stats to %v" , * remoteStats )
if _ , err := http . PostForm ( * remoteStats , values ) ; err != nil {
log . Printf ( "Can't send remote stats: %v" , err )
}
}
}
2015-08-23 15:43:21 +03:00
func testPassed ( name string , passTime time . Duration ) {
2015-08-24 04:26:02 +03:00
sendStats ( url . Values {
"test" : { name } ,
"result" : { "pass" } ,
"duration" : { passTime . String ( ) } ,
} )
2015-08-23 15:43:21 +03:00
updateTestStats ( name , func ( ts * TestStats ) {
totalTime := int64 ( ts . PassTime ) * int64 ( ts . Pass ) + int64 ( passTime )
ts . Pass ++
ts . PassTime = time . Duration ( totalTime / int64 ( ts . Pass ) )
} )
}
func testFailed ( name string ) {
2015-08-24 04:26:02 +03:00
sendStats ( url . Values {
"test" : { name } ,
"result" : { "fail" } ,
} )
2015-08-23 15:43:21 +03:00
updateTestStats ( name , func ( ts * TestStats ) {
ts . Fail ++
} )
}
func testFlaked ( name string , try int ) {
2015-08-24 04:26:02 +03:00
sendStats ( url . Values {
"test" : { name } ,
"result" : { "flake" } ,
"try" : { strconv . FormatInt ( int64 ( try ) , 10 ) } ,
} )
2015-08-23 15:43:21 +03:00
updateTestStats ( name , func ( ts * TestStats ) {
ts . Flake += try - 1
} )
}
func updateTestStats ( name string , update func ( * TestStats ) ) {
var stats Stats
data , err := ioutil . ReadFile ( statsFileName )
if err != nil {
log . Print ( "Can't read stats file, starting new one." )
} else {
if err := json . Unmarshal ( data , & stats ) ; err != nil {
log . Printf ( "Can't parse stats file: %v" , err )
return
}
}
if stats . TestStats == nil {
stats . TestStats = make ( map [ string ] TestStats )
}
ts := stats . TestStats [ name ]
update ( & ts )
stats . TestStats [ name ] = ts
data , err = json . MarshalIndent ( stats , "" , "\t" )
if err != nil {
log . Printf ( "Can't encode stats file: %v" , err )
return
}
2015-08-24 04:26:02 +03:00
if err := ioutil . WriteFile ( statsFileName , data , 0644 ) ; err != nil {
log . Printf ( "Can't write stats file: %v" , err )
}
2015-08-23 15:43:21 +03:00
}
func reshardTests ( config * Config , numShards int ) error {
var stats Stats
2015-08-24 04:26:02 +03:00
var data [ ] byte
if * remoteStats != "" {
log . Printf ( "Using remote stats for resharding: %v" , * remoteStats )
resp , err := http . Get ( * remoteStats )
if err != nil {
return err
}
2018-06-18 03:32:52 +03:00
defer resp . Body . Close ( )
2015-08-24 04:26:02 +03:00
if data , err = ioutil . ReadAll ( resp . Body ) ; err != nil {
return err
}
} else {
var err error
data , err = ioutil . ReadFile ( statsFileName )
if err != nil {
return errors . New ( "can't read stats file" )
}
2015-08-23 15:43:21 +03:00
}
2015-08-24 04:26:02 +03:00
2015-08-23 15:43:21 +03:00
if err := json . Unmarshal ( data , & stats ) ; err != nil {
return fmt . Errorf ( "can't parse stats file: %v" , err )
}
// Sort tests by PassTime.
var tests [ ] TestStats
var totalTime int64
for name , test := range stats . TestStats {
test . name = name
tests = append ( tests , test )
totalTime += int64 ( test . PassTime )
}
sort . Sort ( ByPassTime ( tests ) )
// Group into shards.
max := totalTime / int64 ( numShards )
shards := make ( [ ] [ ] TestStats , numShards )
sums := make ( [ ] int64 , numShards )
2015-08-24 05:51:42 +03:00
// First pass: greedy approximation.
2015-08-24 06:56:12 +03:00
firstPass :
2015-08-24 05:51:42 +03:00
for len ( tests ) > 0 {
2015-08-23 15:43:21 +03:00
v := int64 ( tests [ 0 ] . PassTime )
2015-08-24 05:51:42 +03:00
for n := range shards {
if sums [ n ] + v < max {
shards [ n ] = append ( shards [ n ] , tests [ 0 ] )
sums [ n ] += v
tests = tests [ 1 : ]
2015-08-24 06:56:12 +03:00
continue firstPass
2015-08-24 05:51:42 +03:00
}
}
2015-08-24 06:56:12 +03:00
// None of the bins has room. Go to second pass.
break
2015-08-23 15:43:21 +03:00
}
2015-08-24 05:51:42 +03:00
// Second pass: distribute the remainder.
for len ( tests ) > 0 {
nmin := 0
min := sums [ 0 ]
for n := range sums {
if sums [ n ] < min {
nmin = n
min = sums [ n ]
}
}
shards [ nmin ] = append ( shards [ nmin ] , tests [ 0 ] )
sums [ nmin ] += int64 ( tests [ 0 ] . PassTime )
tests = tests [ 1 : ]
}
2015-08-23 15:43:21 +03:00
2015-08-24 04:26:02 +03:00
// Update config and print results.
2015-08-23 15:43:21 +03:00
for i , tests := range shards {
for _ , t := range tests {
2015-09-23 03:17:17 +03:00
ct , ok := config . Tests [ t . name ]
if ! ok {
log . Printf ( "WARNING: skipping unknown test: %v" , t . name )
continue
}
ct . Shard = i
2015-10-29 23:27:23 +03:00
if ct . Args == nil {
ct . Args = [ ] string { }
}
if ct . Command == nil {
ct . Command = [ ] string { }
}
if ct . Tags == nil {
ct . Tags = [ ] string { }
}
2019-02-28 12:49:05 +03:00
log . Printf ( "%v:\t%v\n" , t . name , t . PassTime )
2015-08-23 15:43:21 +03:00
}
log . Printf ( "Shard %v total: %v\n" , i , time . Duration ( sums [ i ] ) )
}
return nil
}
type ByPassTime [ ] TestStats
func ( a ByPassTime ) Len ( ) int { return len ( a ) }
func ( a ByPassTime ) Swap ( i , j int ) { a [ i ] , a [ j ] = a [ j ] , a [ i ] }
func ( a ByPassTime ) Less ( i , j int ) bool { return a [ i ] . PassTime > a [ j ] . PassTime }
2015-08-24 04:26:02 +03:00
func getTestsSorted ( names [ ] string , testMap map [ string ] * Test ) [ ] * Test {
sort . Strings ( names )
var tests [ ] * Test
for _ , name := range names {
t := testMap [ name ]
t . name = name
tests = append ( tests , t )
}
return tests
}
2015-08-24 23:44:52 +03:00
func selectedTests ( args [ ] string , config * Config ) [ ] * Test {
2015-08-23 15:43:21 +03:00
var tests [ ] * Test
2019-02-28 12:49:05 +03:00
excludedTests := strings . Split ( * exclude , "," )
2015-08-23 15:43:21 +03:00
if * shard >= 0 {
// Run the tests in a given shard.
// This can be combined with positional args.
2015-08-24 04:26:02 +03:00
var names [ ] string
for name , t := range config . Tests {
2019-02-28 12:49:05 +03:00
if t . Shard == * shard && ! t . Manual && ( * exclude == "" || ! t . hasAnyTag ( excludedTests ) ) {
2015-08-24 04:26:02 +03:00
t . name = name
names = append ( names , name )
2015-08-23 15:43:21 +03:00
}
}
2015-08-24 04:26:02 +03:00
tests = getTestsSorted ( names , config . Tests )
2015-08-23 15:43:21 +03:00
}
2015-08-24 23:44:52 +03:00
if len ( args ) > 0 {
2015-08-23 15:43:21 +03:00
// Positional args for manual selection.
2015-08-24 23:44:52 +03:00
for _ , name := range args {
2015-08-23 15:43:21 +03:00
t , ok := config . Tests [ name ]
if ! ok {
2019-02-28 12:35:02 +03:00
tests := make ( [ ] string , len ( config . Tests ) )
i := 0
for k := range config . Tests {
tests [ i ] = k
i ++
}
sort . Strings ( tests )
log . Fatalf ( "Unknown test: %v\nAvailable tests are: %v" , name , strings . Join ( tests , ", " ) )
2015-08-23 15:43:21 +03:00
}
2015-08-24 04:26:02 +03:00
t . name = name
2015-08-23 15:43:21 +03:00
tests = append ( tests , t )
}
}
2015-08-24 23:44:52 +03:00
if len ( args ) == 0 && * shard < 0 {
2015-08-23 15:43:21 +03:00
// Run all tests.
var names [ ] string
2015-10-29 23:27:23 +03:00
for name , t := range config . Tests {
2019-02-28 12:49:05 +03:00
if ! t . Manual && ( * tag == "" || t . hasTag ( * tag ) ) && ( * exclude == "" || ! t . hasAnyTag ( excludedTests ) ) {
2015-08-23 15:43:21 +03:00
names = append ( names , name )
}
}
2015-08-24 04:26:02 +03:00
tests = getTestsSorted ( names , config . Tests )
2015-08-23 15:43:21 +03:00
}
return tests
}
2015-08-24 07:32:34 +03:00
var (
port = 16000
portMutex sync . Mutex
)
func getPortStart ( size int ) int {
portMutex . Lock ( )
defer portMutex . Unlock ( )
start := port
port += size
return start
}
2015-08-24 23:44:52 +03:00
// splitArgs splits a list of args at the first appearance of tok.
func splitArgs ( all [ ] string , tok string ) ( args , extraArgs [ ] string ) {
extra := false
for _ , arg := range all {
if extra {
extraArgs = append ( extraArgs , arg )
continue
}
if arg == tok {
extra = true
continue
}
args = append ( args , arg )
}
return
}
travis: Run tests within our "mysql57" bootstrap Docker image instead of natively.
This will fix the recent test failures and undo the change from https://github.com/youtube/vitess/issues/3203 where we had to disable the tests "unit" and "mysql_server" because they were constantly failing. With this change, the two tests are enabled again.
More importantly, with this change we no longer have to maintain Vitess' dependencies in the Travis CI environment.
Instead, we'll depend on our "bootstrap" Docker images to run the test.
Note that I had to come up with a caching mechanism for "make build". By default, we provide the test within the Docker container with a clean copy of the Git repository and run "make build" before *each* test i.e. a Travis build with 10 tests in it would have run "make build" 10 times. To avoid this, I added a new option "-use_docker_cache" to test.go which will run "make build" before all tests and use "docker commit" to create a new temporary cache image.
Overall, this change has several consequences:
- Travis CI builds will be dependent on an up to date Docker image. For example, if somebody adds a dependency to vendor/vendor.json and forgets to rebuild and push the used Docker image, "make build" will fail. I'll add a test for this in a separate commit.
- We'll use MySQL 5.7 (instead of MariaDB) for the Travis test runs.
- We can no longer use Travis container infrastructure and have to use the VM infrastructure instead. That means an additional 20 seconds wait time until Travis has started a VM and runs the build.
- More overhead per Travis build because we need to pull the Docker image first (takes up to two minutes). Before, we used Travis' caching mechanism to cache dependencies between builds.
- Slightly slower tests because the VM infrastructure has less resources than the container one (~2 CPUs (bursted) instead of 2 CPUs, see https://docs.travis-ci.com/user/reference/overview/).
- The ".git" directory will be copied into the Docker image as well now because travis/check_make_proto.sh needs it. I'll create a separate commit to integrate this.
2017-09-21 03:47:58 +03:00
func round ( d time . Duration ) time . Duration {
return d . Round ( 100 * time . Millisecond )
}