David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 18 | "bufio" |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 19 | "bytes" |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 20 | "errors" |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 21 | "flag" |
| 22 | "fmt" |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 23 | "math/rand" |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 24 | "os" |
| 25 | "os/exec" |
| 26 | "path" |
Adam Langley | 31fa5a4 | 2017-04-11 17:07:27 -0700 | [diff] [blame] | 27 | "runtime" |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 28 | "strconv" |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 29 | "strings" |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 30 | "sync" |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 31 | "syscall" |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 32 | |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 33 | "boringssl.googlesource.com/boringssl/util/testconfig" |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 34 | "boringssl.googlesource.com/boringssl/util/testresult" |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | // TODO(davidben): Link tests with the malloc shim and port -malloc-test to this runner. |
| 38 | |
| 39 | var ( |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 40 | useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind") |
Steven Valdez | ab14a4a | 2016-02-29 16:58:26 -0500 | [diff] [blame] | 41 | useCallgrind = flag.Bool("callgrind", false, "If true, run code under valgrind to generate callgrind traces.") |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 42 | useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb") |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 43 | useSDE = flag.Bool("sde", false, "If true, run BoringSSL code under Intel's SDE for each supported chip") |
David Benjamin | 799676c | 2017-05-10 16:56:02 -0400 | [diff] [blame] | 44 | sdePath = flag.String("sde-path", "sde", "The path to find the sde binary.") |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 45 | buildDir = flag.String("build-dir", "build", "The build directory to run the tests from.") |
Adam Langley | 31fa5a4 | 2017-04-11 17:07:27 -0700 | [diff] [blame] | 46 | numWorkers = flag.Int("num-workers", runtime.NumCPU(), "Runs the given number of workers when testing.") |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 47 | jsonOutput = flag.String("json-output", "", "The file to output JSON results to.") |
| 48 | mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.") |
| 49 | mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask each test to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.") |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 50 | simulateARMCPUs = flag.Bool("simulate-arm-cpus", simulateARMCPUsDefault(), "If true, runs tests simulating different ARM CPUs.") |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 51 | ) |
| 52 | |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 53 | func simulateARMCPUsDefault() bool { |
David Benjamin | e1148bd | 2020-01-16 18:58:14 -0500 | [diff] [blame] | 54 | return (runtime.GOOS == "linux" || runtime.GOOS == "android") && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 57 | type test struct { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 58 | testconfig.Test |
| 59 | |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 60 | shard, numShards int |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 61 | // cpu, if not empty, contains a code to simulate. For SDE, run `sde64 |
| 62 | // -help` to get a list of these codes. For ARM, see gtest_main.cc for |
| 63 | // the supported values. |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 64 | cpu string |
| 65 | } |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 66 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 67 | type result struct { |
| 68 | Test test |
| 69 | Passed bool |
| 70 | Error error |
| 71 | } |
| 72 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 73 | // sdeCPUs contains a list of CPU code that we run all tests under when *useSDE |
| 74 | // is true. |
| 75 | var sdeCPUs = []string{ |
| 76 | "p4p", // Pentium4 Prescott |
| 77 | "mrm", // Merom |
| 78 | "pnr", // Penryn |
| 79 | "nhm", // Nehalem |
| 80 | "wsm", // Westmere |
| 81 | "snb", // Sandy Bridge |
| 82 | "ivb", // Ivy Bridge |
| 83 | "hsw", // Haswell |
| 84 | "bdw", // Broadwell |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 85 | "slt", // Saltwell |
| 86 | "slm", // Silvermont |
| 87 | "glm", // Goldmont |
Adam Langley | 4143943 | 2020-04-08 09:16:07 -0700 | [diff] [blame] | 88 | "glp", // Goldmont Plus |
| 89 | "tnt", // Tremont |
| 90 | "skl", // Skylake |
| 91 | "cnl", // Cannon Lake |
| 92 | "icl", // Ice Lake |
| 93 | "skx", // Skylake server |
| 94 | "clx", // Cascade Lake |
| 95 | "cpx", // Cooper Lake |
| 96 | "icx", // Ice Lake server |
| 97 | "knl", // Knights landing |
| 98 | "knm", // Knights mill |
| 99 | "tgl", // Tiger Lake |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 100 | } |
| 101 | |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 102 | var armCPUs = []string{ |
| 103 | "none", // No support for any ARM extensions. |
| 104 | "neon", // Support for NEON. |
| 105 | "crypto", // Support for NEON and crypto extensions. |
| 106 | } |
| 107 | |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 108 | func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd { |
David Benjamin | d2ba889 | 2016-09-20 19:41:04 -0400 | [diff] [blame] | 109 | valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full", "--quiet"} |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 110 | if dbAttach { |
| 111 | valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p") |
| 112 | } |
| 113 | valgrindArgs = append(valgrindArgs, path) |
| 114 | valgrindArgs = append(valgrindArgs, args...) |
| 115 | |
| 116 | return exec.Command("valgrind", valgrindArgs...) |
| 117 | } |
| 118 | |
Steven Valdez | ab14a4a | 2016-02-29 16:58:26 -0500 | [diff] [blame] | 119 | func callgrindOf(path string, args ...string) *exec.Cmd { |
| 120 | valgrindArgs := []string{"-q", "--tool=callgrind", "--dump-instr=yes", "--collect-jumps=yes", "--callgrind-out-file=" + *buildDir + "/callgrind/callgrind.out.%p"} |
| 121 | valgrindArgs = append(valgrindArgs, path) |
| 122 | valgrindArgs = append(valgrindArgs, args...) |
| 123 | |
| 124 | return exec.Command("valgrind", valgrindArgs...) |
| 125 | } |
| 126 | |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 127 | func gdbOf(path string, args ...string) *exec.Cmd { |
| 128 | xtermArgs := []string{"-e", "gdb", "--args"} |
| 129 | xtermArgs = append(xtermArgs, path) |
| 130 | xtermArgs = append(xtermArgs, args...) |
| 131 | |
| 132 | return exec.Command("xterm", xtermArgs...) |
| 133 | } |
| 134 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 135 | func sdeOf(cpu, path string, args ...string) *exec.Cmd { |
David Benjamin | c5304e4 | 2017-07-17 16:15:16 -0400 | [diff] [blame] | 136 | sdeArgs := []string{"-" + cpu} |
| 137 | // The kernel's vdso code for gettimeofday sometimes uses the RDTSCP |
| 138 | // instruction. Although SDE has a -chip_check_vsyscall flag that |
| 139 | // excludes such code by default, it does not seem to work. Instead, |
| 140 | // pass the -chip_check_exe_only flag which retains test coverage when |
| 141 | // statically linked and excludes the vdso. |
| 142 | if cpu == "p4p" || cpu == "pnr" || cpu == "mrm" || cpu == "slt" { |
| 143 | sdeArgs = append(sdeArgs, "-chip_check_exe_only") |
| 144 | } |
| 145 | sdeArgs = append(sdeArgs, "--", path) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 146 | sdeArgs = append(sdeArgs, args...) |
David Benjamin | 799676c | 2017-05-10 16:56:02 -0400 | [diff] [blame] | 147 | return exec.Command(*sdePath, sdeArgs...) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 148 | } |
| 149 | |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 150 | var ( |
| 151 | errMoreMallocs = errors.New("child process did not exhaust all allocation calls") |
| 152 | errTestSkipped = errors.New("test was skipped") |
| 153 | ) |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 154 | |
| 155 | func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 156 | prog := path.Join(*buildDir, test.Cmd[0]) |
| 157 | args := append([]string{}, test.Cmd[1:]...) |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 158 | if *simulateARMCPUs && test.cpu != "" { |
Adam Langley | 7de9498 | 2019-10-17 09:15:37 -0700 | [diff] [blame] | 159 | args = append(args, "--cpu="+test.cpu) |
David Benjamin | 17d553d | 2018-12-21 17:58:36 -0600 | [diff] [blame] | 160 | } |
| 161 | if *useSDE { |
| 162 | // SDE is neither compatible with the unwind tester nor automatically |
| 163 | // detected. |
| 164 | args = append(args, "--no_unwind_tests") |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 165 | } |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 166 | var cmd *exec.Cmd |
| 167 | if *useValgrind { |
| 168 | cmd = valgrindOf(false, prog, args...) |
Steven Valdez | ab14a4a | 2016-02-29 16:58:26 -0500 | [diff] [blame] | 169 | } else if *useCallgrind { |
| 170 | cmd = callgrindOf(prog, args...) |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 171 | } else if *useGDB { |
| 172 | cmd = gdbOf(prog, args...) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 173 | } else if *useSDE { |
| 174 | cmd = sdeOf(test.cpu, prog, args...) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 175 | } else { |
| 176 | cmd = exec.Command(prog, args...) |
| 177 | } |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 178 | if test.Env != nil { |
Adam Langley | 7de9498 | 2019-10-17 09:15:37 -0700 | [diff] [blame] | 179 | cmd.Env = make([]string, len(os.Environ())) |
| 180 | copy(cmd.Env, os.Environ()) |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 181 | cmd.Env = append(cmd.Env, test.Env...) |
Adam Langley | 7de9498 | 2019-10-17 09:15:37 -0700 | [diff] [blame] | 182 | } |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 183 | var outBuf bytes.Buffer |
| 184 | cmd.Stdout = &outBuf |
| 185 | cmd.Stderr = &outBuf |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 186 | if mallocNumToFail >= 0 { |
| 187 | cmd.Env = os.Environ() |
| 188 | cmd.Env = append(cmd.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10)) |
| 189 | if *mallocTestDebug { |
| 190 | cmd.Env = append(cmd.Env, "MALLOC_ABORT_ON_FAIL=1") |
| 191 | } |
| 192 | cmd.Env = append(cmd.Env, "_MALLOC_CHECK=1") |
| 193 | } |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 194 | |
| 195 | if err := cmd.Start(); err != nil { |
| 196 | return false, err |
| 197 | } |
| 198 | if err := cmd.Wait(); err != nil { |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 199 | if exitError, ok := err.(*exec.ExitError); ok { |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 200 | switch exitError.Sys().(syscall.WaitStatus).ExitStatus() { |
| 201 | case 88: |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 202 | return false, errMoreMallocs |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 203 | case 89: |
| 204 | fmt.Print(string(outBuf.Bytes())) |
| 205 | return false, errTestSkipped |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 208 | fmt.Print(string(outBuf.Bytes())) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 209 | return false, err |
| 210 | } |
| 211 | |
| 212 | // Account for Windows line-endings. |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 213 | stdout := bytes.Replace(outBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 214 | |
| 215 | if bytes.HasSuffix(stdout, []byte("PASS\n")) && |
| 216 | (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') { |
| 217 | return true, nil |
| 218 | } |
David Benjamin | 9662843 | 2017-01-19 19:05:47 -0500 | [diff] [blame] | 219 | |
| 220 | // Also accept a googletest-style pass line. This is left here in |
| 221 | // transition until the tests are all converted and this script made |
| 222 | // unnecessary. |
| 223 | if bytes.Contains(stdout, []byte("\n[ PASSED ]")) { |
| 224 | return true, nil |
| 225 | } |
| 226 | |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 227 | fmt.Print(string(outBuf.Bytes())) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 228 | return false, nil |
| 229 | } |
| 230 | |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 231 | func runTest(test test) (bool, error) { |
| 232 | if *mallocTest < 0 { |
| 233 | return runTestOnce(test, -1) |
| 234 | } |
| 235 | |
| 236 | for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ { |
| 237 | if passed, err := runTestOnce(test, mallocNumToFail); err != errMoreMallocs { |
| 238 | if err != nil { |
| 239 | err = fmt.Errorf("at malloc %d: %s", mallocNumToFail, err) |
| 240 | } |
| 241 | return passed, err |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 246 | // setWorkingDirectory walks up directories as needed until the current working |
| 247 | // directory is the top of a BoringSSL checkout. |
| 248 | func setWorkingDirectory() { |
| 249 | for i := 0; i < 64; i++ { |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 250 | if _, err := os.Stat("BUILDING.md"); err == nil { |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 251 | return |
| 252 | } |
| 253 | os.Chdir("..") |
| 254 | } |
| 255 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 256 | panic("Couldn't find BUILDING.md in a parent directory!") |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 259 | func worker(tests <-chan test, results chan<- result, done *sync.WaitGroup) { |
| 260 | defer done.Done() |
| 261 | for test := range tests { |
| 262 | passed, err := runTest(test) |
| 263 | results <- result{test, passed, err} |
| 264 | } |
| 265 | } |
| 266 | |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 267 | func (t test) shortName() string { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 268 | return t.Cmd[0] + t.shardMsg() + t.cpuMsg() + t.envMsg() |
Adam Langley | 7de9498 | 2019-10-17 09:15:37 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | func SpaceIf(returnSpace bool) string { |
| 272 | if !returnSpace { |
| 273 | return "" |
| 274 | } |
| 275 | return " " |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | func (t test) longName() string { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 279 | return strings.Join(t.Env, " ") + SpaceIf(len(t.Env) != 0) + strings.Join(t.Cmd, " ") + t.cpuMsg() |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | func (t test) shardMsg() string { |
| 283 | if t.numShards == 0 { |
| 284 | return "" |
| 285 | } |
| 286 | |
| 287 | return fmt.Sprintf(" [shard %d/%d]", t.shard+1, t.numShards) |
| 288 | } |
| 289 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 290 | func (t test) cpuMsg() string { |
| 291 | if len(t.cpu) == 0 { |
| 292 | return "" |
| 293 | } |
| 294 | |
| 295 | return fmt.Sprintf(" (for CPU %q)", t.cpu) |
| 296 | } |
| 297 | |
Adam Langley | 7de9498 | 2019-10-17 09:15:37 -0700 | [diff] [blame] | 298 | func (t test) envMsg() string { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 299 | if len(t.Env) == 0 { |
Adam Langley | 7de9498 | 2019-10-17 09:15:37 -0700 | [diff] [blame] | 300 | return "" |
| 301 | } |
| 302 | |
| 303 | return " (custom environment)" |
| 304 | } |
| 305 | |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 306 | func (t test) getGTestShards() ([]test, error) { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 307 | if *numWorkers == 1 || len(t.Cmd) != 1 { |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 308 | return []test{t}, nil |
| 309 | } |
| 310 | |
| 311 | // Only shard the three GTest-based tests. |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 312 | if t.Cmd[0] != "crypto/crypto_test" && t.Cmd[0] != "ssl/ssl_test" && t.Cmd[0] != "decrepit/decrepit_test" { |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 313 | return []test{t}, nil |
| 314 | } |
| 315 | |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 316 | prog := path.Join(*buildDir, t.Cmd[0]) |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 317 | cmd := exec.Command(prog, "--gtest_list_tests") |
| 318 | var stdout bytes.Buffer |
| 319 | cmd.Stdout = &stdout |
| 320 | if err := cmd.Start(); err != nil { |
| 321 | return nil, err |
| 322 | } |
| 323 | if err := cmd.Wait(); err != nil { |
| 324 | return nil, err |
| 325 | } |
| 326 | |
| 327 | var group string |
| 328 | var tests []string |
| 329 | scanner := bufio.NewScanner(&stdout) |
| 330 | for scanner.Scan() { |
| 331 | line := scanner.Text() |
| 332 | |
| 333 | // Remove the parameter comment and trailing space. |
| 334 | if idx := strings.Index(line, "#"); idx >= 0 { |
| 335 | line = line[:idx] |
| 336 | } |
| 337 | line = strings.TrimSpace(line) |
| 338 | if len(line) == 0 { |
| 339 | continue |
| 340 | } |
| 341 | |
| 342 | if line[len(line)-1] == '.' { |
| 343 | group = line |
| 344 | continue |
| 345 | } |
| 346 | |
| 347 | if len(group) == 0 { |
| 348 | return nil, fmt.Errorf("found test case %q without group", line) |
| 349 | } |
| 350 | tests = append(tests, group+line) |
| 351 | } |
| 352 | |
| 353 | const testsPerShard = 20 |
| 354 | if len(tests) <= testsPerShard { |
| 355 | return []test{t}, nil |
| 356 | } |
| 357 | |
| 358 | // Slow tests which process large test vector files tend to be grouped |
| 359 | // together, so shuffle the order. |
| 360 | shuffled := make([]string, len(tests)) |
| 361 | perm := rand.Perm(len(tests)) |
| 362 | for i, j := range perm { |
| 363 | shuffled[i] = tests[j] |
| 364 | } |
| 365 | |
| 366 | var shards []test |
| 367 | for i := 0; i < len(shuffled); i += testsPerShard { |
| 368 | n := len(shuffled) - i |
| 369 | if n > testsPerShard { |
| 370 | n = testsPerShard |
| 371 | } |
| 372 | shard := t |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 373 | shard.Cmd = []string{shard.Cmd[0], "--gtest_filter=" + strings.Join(shuffled[i:i+n], ":")} |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 374 | shard.shard = len(shards) |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 375 | shards = append(shards, shard) |
| 376 | } |
| 377 | |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 378 | for i := range shards { |
| 379 | shards[i].numShards = len(shards) |
| 380 | } |
| 381 | |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 382 | return shards, nil |
| 383 | } |
| 384 | |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 385 | func main() { |
| 386 | flag.Parse() |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 387 | setWorkingDirectory() |
| 388 | |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 389 | testCases, err := testconfig.ParseTestConfig("util/all_tests.json") |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 390 | if err != nil { |
| 391 | fmt.Printf("Failed to parse input: %s\n", err) |
| 392 | os.Exit(1) |
| 393 | } |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 394 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 395 | var wg sync.WaitGroup |
| 396 | tests := make(chan test, *numWorkers) |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 397 | results := make(chan result, *numWorkers) |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 398 | |
| 399 | for i := 0; i < *numWorkers; i++ { |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 400 | wg.Add(1) |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 401 | go worker(tests, results, &wg) |
| 402 | } |
| 403 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 404 | go func() { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 405 | for _, baseTest := range testCases { |
| 406 | test := test{Test: baseTest} |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 407 | if *useSDE { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 408 | if test.SkipSDE { |
| 409 | continue |
| 410 | } |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 411 | // SDE generates plenty of tasks and gets slower |
| 412 | // with additional sharding. |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 413 | for _, cpu := range sdeCPUs { |
| 414 | testForCPU := test |
| 415 | testForCPU.cpu = cpu |
| 416 | tests <- testForCPU |
| 417 | } |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 418 | } else if *simulateARMCPUs { |
| 419 | // This mode is run instead of the default path, |
| 420 | // so also include the native flow. |
| 421 | tests <- test |
| 422 | for _, cpu := range armCPUs { |
| 423 | testForCPU := test |
| 424 | testForCPU.cpu = cpu |
| 425 | tests <- testForCPU |
| 426 | } |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 427 | } else { |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 428 | shards, err := test.getGTestShards() |
| 429 | if err != nil { |
| 430 | fmt.Printf("Error listing tests: %s\n", err) |
| 431 | os.Exit(1) |
| 432 | } |
| 433 | for _, shard := range shards { |
| 434 | tests <- shard |
| 435 | } |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 436 | } |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 437 | } |
| 438 | close(tests) |
| 439 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 440 | wg.Wait() |
| 441 | close(results) |
| 442 | }() |
| 443 | |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 444 | testOutput := testresult.NewResults() |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 445 | var failed, skipped []test |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 446 | for testResult := range results { |
| 447 | test := testResult.Test |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 448 | args := test.Cmd |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 449 | |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 450 | if testResult.Error == errTestSkipped { |
| 451 | fmt.Printf("%s\n", test.longName()) |
| 452 | fmt.Printf("%s was skipped\n", args[0]) |
| 453 | skipped = append(skipped, test) |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 454 | testOutput.AddSkip(test.longName()) |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 455 | } else if testResult.Error != nil { |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 456 | fmt.Printf("%s\n", test.longName()) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 457 | fmt.Printf("%s failed to complete: %s\n", args[0], testResult.Error) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 458 | failed = append(failed, test) |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 459 | testOutput.AddResult(test.longName(), "CRASH") |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 460 | } else if !testResult.Passed { |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 461 | fmt.Printf("%s\n", test.longName()) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 462 | fmt.Printf("%s failed to print PASS on the last line.\n", args[0]) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 463 | failed = append(failed, test) |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 464 | testOutput.AddResult(test.longName(), "FAIL") |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 465 | } else { |
David Benjamin | ced6e76 | 2017-09-28 15:17:05 -0400 | [diff] [blame] | 466 | fmt.Printf("%s\n", test.shortName()) |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 467 | testOutput.AddResult(test.longName(), "PASS") |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 471 | if *jsonOutput != "" { |
David Benjamin | ce61710 | 2018-11-14 17:31:02 -0600 | [diff] [blame] | 472 | if err := testOutput.WriteToFile(*jsonOutput); err != nil { |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 473 | fmt.Fprintf(os.Stderr, "Error: %s\n", err) |
| 474 | } |
| 475 | } |
David Benjamin | 2ab7a86 | 2015-04-04 17:02:18 -0400 | [diff] [blame] | 476 | |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 477 | if len(skipped) > 0 { |
| 478 | fmt.Printf("\n%d of %d tests were skipped:\n", len(skipped), len(testCases)) |
| 479 | for _, test := range skipped { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 480 | fmt.Printf("\t%s%s\n", strings.Join(test.Cmd, " "), test.cpuMsg()) |
David Benjamin | 6ce93cc | 2018-10-31 14:48:23 -0500 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
David Benjamin | 2ab7a86 | 2015-04-04 17:02:18 -0400 | [diff] [blame] | 484 | if len(failed) > 0 { |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 485 | fmt.Printf("\n%d of %d tests failed:\n", len(failed), len(testCases)) |
David Benjamin | 2ab7a86 | 2015-04-04 17:02:18 -0400 | [diff] [blame] | 486 | for _, test := range failed { |
David Benjamin | aadb463 | 2020-01-16 18:25:37 -0500 | [diff] [blame] | 487 | fmt.Printf("\t%s%s\n", strings.Join(test.Cmd, " "), test.cpuMsg()) |
David Benjamin | 2ab7a86 | 2015-04-04 17:02:18 -0400 | [diff] [blame] | 488 | } |
| 489 | os.Exit(1) |
| 490 | } |
| 491 | |
| 492 | fmt.Printf("\nAll tests passed!\n") |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 493 | } |