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 | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 20 | "encoding/json" |
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 | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 32 | "time" |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | // TODO(davidben): Link tests with the malloc shim and port -malloc-test to this runner. |
| 36 | |
| 37 | var ( |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 38 | useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind") |
Steven Valdez | ab14a4a | 2016-02-29 16:58:26 -0500 | [diff] [blame] | 39 | 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] | 40 | useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb") |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 41 | 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] | 42 | 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] | 43 | 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] | 44 | 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] | 45 | jsonOutput = flag.String("json-output", "", "The file to output JSON results to.") |
| 46 | mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.") |
| 47 | 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 | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 48 | ) |
| 49 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 50 | type test struct { |
| 51 | args []string |
| 52 | // cpu, if not empty, contains an Intel CPU code to simulate. Run |
| 53 | // `sde64 -help` to get a list of these codes. |
| 54 | cpu string |
| 55 | } |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 56 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 57 | type result struct { |
| 58 | Test test |
| 59 | Passed bool |
| 60 | Error error |
| 61 | } |
| 62 | |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 63 | // testOutput is a representation of Chromium's JSON test result format. See |
| 64 | // https://www.chromium.org/developers/the-json-test-results-format |
| 65 | type testOutput struct { |
| 66 | Version int `json:"version"` |
| 67 | Interrupted bool `json:"interrupted"` |
| 68 | PathDelimiter string `json:"path_delimiter"` |
| 69 | SecondsSinceEpoch float64 `json:"seconds_since_epoch"` |
| 70 | NumFailuresByType map[string]int `json:"num_failures_by_type"` |
| 71 | Tests map[string]testResult `json:"tests"` |
| 72 | } |
| 73 | |
| 74 | type testResult struct { |
David Benjamin | 7ead605 | 2015-04-04 03:57:26 -0400 | [diff] [blame] | 75 | Actual string `json:"actual"` |
| 76 | Expected string `json:"expected"` |
| 77 | IsUnexpected bool `json:"is_unexpected"` |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 80 | // sdeCPUs contains a list of CPU code that we run all tests under when *useSDE |
| 81 | // is true. |
| 82 | var sdeCPUs = []string{ |
| 83 | "p4p", // Pentium4 Prescott |
| 84 | "mrm", // Merom |
| 85 | "pnr", // Penryn |
| 86 | "nhm", // Nehalem |
| 87 | "wsm", // Westmere |
| 88 | "snb", // Sandy Bridge |
| 89 | "ivb", // Ivy Bridge |
| 90 | "hsw", // Haswell |
| 91 | "bdw", // Broadwell |
| 92 | "skx", // Skylake Server |
| 93 | "skl", // Skylake Client |
| 94 | "cnl", // Cannonlake |
| 95 | "knl", // Knights Landing |
| 96 | "slt", // Saltwell |
| 97 | "slm", // Silvermont |
| 98 | "glm", // Goldmont |
| 99 | } |
| 100 | |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 101 | func newTestOutput() *testOutput { |
| 102 | return &testOutput{ |
| 103 | Version: 3, |
| 104 | PathDelimiter: ".", |
| 105 | SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond), |
| 106 | NumFailuresByType: make(map[string]int), |
| 107 | Tests: make(map[string]testResult), |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func (t *testOutput) addResult(name, result string) { |
| 112 | if _, found := t.Tests[name]; found { |
| 113 | panic(name) |
| 114 | } |
David Benjamin | 7ead605 | 2015-04-04 03:57:26 -0400 | [diff] [blame] | 115 | t.Tests[name] = testResult{ |
| 116 | Actual: result, |
| 117 | Expected: "PASS", |
| 118 | IsUnexpected: result != "PASS", |
| 119 | } |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 120 | t.NumFailuresByType[result]++ |
| 121 | } |
| 122 | |
| 123 | func (t *testOutput) writeTo(name string) error { |
| 124 | file, err := os.Create(name) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | defer file.Close() |
| 129 | out, err := json.MarshalIndent(t, "", " ") |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | _, err = file.Write(out) |
| 134 | return err |
| 135 | } |
| 136 | |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 137 | func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd { |
David Benjamin | d2ba889 | 2016-09-20 19:41:04 -0400 | [diff] [blame] | 138 | valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full", "--quiet"} |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 139 | if dbAttach { |
| 140 | valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p") |
| 141 | } |
| 142 | valgrindArgs = append(valgrindArgs, path) |
| 143 | valgrindArgs = append(valgrindArgs, args...) |
| 144 | |
| 145 | return exec.Command("valgrind", valgrindArgs...) |
| 146 | } |
| 147 | |
Steven Valdez | ab14a4a | 2016-02-29 16:58:26 -0500 | [diff] [blame] | 148 | func callgrindOf(path string, args ...string) *exec.Cmd { |
| 149 | valgrindArgs := []string{"-q", "--tool=callgrind", "--dump-instr=yes", "--collect-jumps=yes", "--callgrind-out-file=" + *buildDir + "/callgrind/callgrind.out.%p"} |
| 150 | valgrindArgs = append(valgrindArgs, path) |
| 151 | valgrindArgs = append(valgrindArgs, args...) |
| 152 | |
| 153 | return exec.Command("valgrind", valgrindArgs...) |
| 154 | } |
| 155 | |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 156 | func gdbOf(path string, args ...string) *exec.Cmd { |
| 157 | xtermArgs := []string{"-e", "gdb", "--args"} |
| 158 | xtermArgs = append(xtermArgs, path) |
| 159 | xtermArgs = append(xtermArgs, args...) |
| 160 | |
| 161 | return exec.Command("xterm", xtermArgs...) |
| 162 | } |
| 163 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 164 | func sdeOf(cpu, path string, args ...string) *exec.Cmd { |
David Benjamin | c5304e4 | 2017-07-17 16:15:16 -0400 | [diff] [blame^] | 165 | sdeArgs := []string{"-" + cpu} |
| 166 | // The kernel's vdso code for gettimeofday sometimes uses the RDTSCP |
| 167 | // instruction. Although SDE has a -chip_check_vsyscall flag that |
| 168 | // excludes such code by default, it does not seem to work. Instead, |
| 169 | // pass the -chip_check_exe_only flag which retains test coverage when |
| 170 | // statically linked and excludes the vdso. |
| 171 | if cpu == "p4p" || cpu == "pnr" || cpu == "mrm" || cpu == "slt" { |
| 172 | sdeArgs = append(sdeArgs, "-chip_check_exe_only") |
| 173 | } |
| 174 | sdeArgs = append(sdeArgs, "--", path) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 175 | sdeArgs = append(sdeArgs, args...) |
David Benjamin | 799676c | 2017-05-10 16:56:02 -0400 | [diff] [blame] | 176 | return exec.Command(*sdePath, sdeArgs...) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 177 | } |
| 178 | |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 179 | type moreMallocsError struct{} |
| 180 | |
| 181 | func (moreMallocsError) Error() string { |
| 182 | return "child process did not exhaust all allocation calls" |
| 183 | } |
| 184 | |
| 185 | var errMoreMallocs = moreMallocsError{} |
| 186 | |
| 187 | func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) { |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 188 | prog := path.Join(*buildDir, test.args[0]) |
| 189 | args := test.args[1:] |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 190 | var cmd *exec.Cmd |
| 191 | if *useValgrind { |
| 192 | cmd = valgrindOf(false, prog, args...) |
Steven Valdez | ab14a4a | 2016-02-29 16:58:26 -0500 | [diff] [blame] | 193 | } else if *useCallgrind { |
| 194 | cmd = callgrindOf(prog, args...) |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 195 | } else if *useGDB { |
| 196 | cmd = gdbOf(prog, args...) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 197 | } else if *useSDE { |
| 198 | cmd = sdeOf(test.cpu, prog, args...) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 199 | } else { |
| 200 | cmd = exec.Command(prog, args...) |
| 201 | } |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 202 | var outBuf bytes.Buffer |
| 203 | cmd.Stdout = &outBuf |
| 204 | cmd.Stderr = &outBuf |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 205 | if mallocNumToFail >= 0 { |
| 206 | cmd.Env = os.Environ() |
| 207 | cmd.Env = append(cmd.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10)) |
| 208 | if *mallocTestDebug { |
| 209 | cmd.Env = append(cmd.Env, "MALLOC_ABORT_ON_FAIL=1") |
| 210 | } |
| 211 | cmd.Env = append(cmd.Env, "_MALLOC_CHECK=1") |
| 212 | } |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 213 | |
| 214 | if err := cmd.Start(); err != nil { |
| 215 | return false, err |
| 216 | } |
| 217 | if err := cmd.Wait(); err != nil { |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 218 | if exitError, ok := err.(*exec.ExitError); ok { |
| 219 | if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 { |
| 220 | return false, errMoreMallocs |
| 221 | } |
| 222 | } |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 223 | fmt.Print(string(outBuf.Bytes())) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 224 | return false, err |
| 225 | } |
| 226 | |
| 227 | // Account for Windows line-endings. |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 228 | stdout := bytes.Replace(outBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 229 | |
| 230 | if bytes.HasSuffix(stdout, []byte("PASS\n")) && |
| 231 | (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') { |
| 232 | return true, nil |
| 233 | } |
David Benjamin | 9662843 | 2017-01-19 19:05:47 -0500 | [diff] [blame] | 234 | |
| 235 | // Also accept a googletest-style pass line. This is left here in |
| 236 | // transition until the tests are all converted and this script made |
| 237 | // unnecessary. |
| 238 | if bytes.Contains(stdout, []byte("\n[ PASSED ]")) { |
| 239 | return true, nil |
| 240 | } |
| 241 | |
David Benjamin | 634b0e3 | 2017-02-04 11:14:57 -0500 | [diff] [blame] | 242 | fmt.Print(string(outBuf.Bytes())) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 243 | return false, nil |
| 244 | } |
| 245 | |
David Benjamin | 0b635c5 | 2015-05-15 19:08:49 -0400 | [diff] [blame] | 246 | func runTest(test test) (bool, error) { |
| 247 | if *mallocTest < 0 { |
| 248 | return runTestOnce(test, -1) |
| 249 | } |
| 250 | |
| 251 | for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ { |
| 252 | if passed, err := runTestOnce(test, mallocNumToFail); err != errMoreMallocs { |
| 253 | if err != nil { |
| 254 | err = fmt.Errorf("at malloc %d: %s", mallocNumToFail, err) |
| 255 | } |
| 256 | return passed, err |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
Martin Kreichgauer | 23aff6b | 2017-04-11 08:53:08 -0700 | [diff] [blame] | 261 | // shortTestName returns the short name of a test. Except for evp_test and |
| 262 | // cipher_test, it assumes that any argument which ends in .txt is a path to a |
| 263 | // data file and not relevant to the test's uniqueness. |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 264 | func shortTestName(test test) string { |
| 265 | var args []string |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 266 | for _, arg := range test.args { |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 267 | if test.args[0] == "crypto/evp/evp_test" || test.args[0] == "crypto/cipher_extra/cipher_test" || test.args[0] == "crypto/cipher_extra/aead_test" || !strings.HasSuffix(arg, ".txt") || strings.HasPrefix(arg, "--gtest_filter=") { |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 268 | args = append(args, arg) |
| 269 | } |
| 270 | } |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 271 | return strings.Join(args, " ") + test.cpuMsg() |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 274 | // setWorkingDirectory walks up directories as needed until the current working |
| 275 | // directory is the top of a BoringSSL checkout. |
| 276 | func setWorkingDirectory() { |
| 277 | for i := 0; i < 64; i++ { |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 278 | if _, err := os.Stat("BUILDING.md"); err == nil { |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 279 | return |
| 280 | } |
| 281 | os.Chdir("..") |
| 282 | } |
| 283 | |
David Benjamin | 95aaf4a | 2015-09-03 12:09:36 -0400 | [diff] [blame] | 284 | panic("Couldn't find BUILDING.md in a parent directory!") |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | func parseTestConfig(filename string) ([]test, error) { |
| 288 | in, err := os.Open(filename) |
| 289 | if err != nil { |
| 290 | return nil, err |
| 291 | } |
| 292 | defer in.Close() |
| 293 | |
| 294 | decoder := json.NewDecoder(in) |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 295 | var testArgs [][]string |
| 296 | if err := decoder.Decode(&testArgs); err != nil { |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 297 | return nil, err |
| 298 | } |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 299 | |
| 300 | var result []test |
| 301 | for _, args := range testArgs { |
| 302 | result = append(result, test{args: args}) |
| 303 | } |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 304 | return result, nil |
| 305 | } |
| 306 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 307 | func worker(tests <-chan test, results chan<- result, done *sync.WaitGroup) { |
| 308 | defer done.Done() |
| 309 | for test := range tests { |
| 310 | passed, err := runTest(test) |
| 311 | results <- result{test, passed, err} |
| 312 | } |
| 313 | } |
| 314 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 315 | func (t test) cpuMsg() string { |
| 316 | if len(t.cpu) == 0 { |
| 317 | return "" |
| 318 | } |
| 319 | |
| 320 | return fmt.Sprintf(" (for CPU %q)", t.cpu) |
| 321 | } |
| 322 | |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 323 | func (t test) getGTestShards() ([]test, error) { |
| 324 | if *numWorkers == 1 || len(t.args) != 1 { |
| 325 | return []test{t}, nil |
| 326 | } |
| 327 | |
| 328 | // Only shard the three GTest-based tests. |
| 329 | if t.args[0] != "crypto/crypto_test" && t.args[0] != "ssl/ssl_test" && t.args[0] != "decrepit/decrepit_test" { |
| 330 | return []test{t}, nil |
| 331 | } |
| 332 | |
| 333 | prog := path.Join(*buildDir, t.args[0]) |
| 334 | cmd := exec.Command(prog, "--gtest_list_tests") |
| 335 | var stdout bytes.Buffer |
| 336 | cmd.Stdout = &stdout |
| 337 | if err := cmd.Start(); err != nil { |
| 338 | return nil, err |
| 339 | } |
| 340 | if err := cmd.Wait(); err != nil { |
| 341 | return nil, err |
| 342 | } |
| 343 | |
| 344 | var group string |
| 345 | var tests []string |
| 346 | scanner := bufio.NewScanner(&stdout) |
| 347 | for scanner.Scan() { |
| 348 | line := scanner.Text() |
| 349 | |
| 350 | // Remove the parameter comment and trailing space. |
| 351 | if idx := strings.Index(line, "#"); idx >= 0 { |
| 352 | line = line[:idx] |
| 353 | } |
| 354 | line = strings.TrimSpace(line) |
| 355 | if len(line) == 0 { |
| 356 | continue |
| 357 | } |
| 358 | |
| 359 | if line[len(line)-1] == '.' { |
| 360 | group = line |
| 361 | continue |
| 362 | } |
| 363 | |
| 364 | if len(group) == 0 { |
| 365 | return nil, fmt.Errorf("found test case %q without group", line) |
| 366 | } |
| 367 | tests = append(tests, group+line) |
| 368 | } |
| 369 | |
| 370 | const testsPerShard = 20 |
| 371 | if len(tests) <= testsPerShard { |
| 372 | return []test{t}, nil |
| 373 | } |
| 374 | |
| 375 | // Slow tests which process large test vector files tend to be grouped |
| 376 | // together, so shuffle the order. |
| 377 | shuffled := make([]string, len(tests)) |
| 378 | perm := rand.Perm(len(tests)) |
| 379 | for i, j := range perm { |
| 380 | shuffled[i] = tests[j] |
| 381 | } |
| 382 | |
| 383 | var shards []test |
| 384 | for i := 0; i < len(shuffled); i += testsPerShard { |
| 385 | n := len(shuffled) - i |
| 386 | if n > testsPerShard { |
| 387 | n = testsPerShard |
| 388 | } |
| 389 | shard := t |
| 390 | shard.args = []string{shard.args[0], "--gtest_filter=" + strings.Join(shuffled[i:i+n], ":")} |
| 391 | shards = append(shards, shard) |
| 392 | } |
| 393 | |
| 394 | return shards, nil |
| 395 | } |
| 396 | |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 397 | func main() { |
| 398 | flag.Parse() |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 399 | setWorkingDirectory() |
| 400 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 401 | testCases, err := parseTestConfig("util/all_tests.json") |
Adam Langley | 117da41 | 2015-06-10 17:32:25 -0700 | [diff] [blame] | 402 | if err != nil { |
| 403 | fmt.Printf("Failed to parse input: %s\n", err) |
| 404 | os.Exit(1) |
| 405 | } |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 406 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 407 | var wg sync.WaitGroup |
| 408 | tests := make(chan test, *numWorkers) |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 409 | results := make(chan result, *numWorkers) |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 410 | |
| 411 | for i := 0; i < *numWorkers; i++ { |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 412 | wg.Add(1) |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 413 | go worker(tests, results, &wg) |
| 414 | } |
| 415 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 416 | go func() { |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 417 | for _, test := range testCases { |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 418 | if *useSDE { |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 419 | // SDE generates plenty of tasks and gets slower |
| 420 | // with additional sharding. |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 421 | for _, cpu := range sdeCPUs { |
| 422 | testForCPU := test |
| 423 | testForCPU.cpu = cpu |
| 424 | tests <- testForCPU |
| 425 | } |
| 426 | } else { |
David Benjamin | 3d14a15 | 2017-06-07 14:02:03 -0400 | [diff] [blame] | 427 | shards, err := test.getGTestShards() |
| 428 | if err != nil { |
| 429 | fmt.Printf("Error listing tests: %s\n", err) |
| 430 | os.Exit(1) |
| 431 | } |
| 432 | for _, shard := range shards { |
| 433 | tests <- shard |
| 434 | } |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 435 | } |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 436 | } |
| 437 | close(tests) |
| 438 | |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 439 | wg.Wait() |
| 440 | close(results) |
| 441 | }() |
| 442 | |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 443 | testOutput := newTestOutput() |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 444 | var failed []test |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 445 | for testResult := range results { |
| 446 | test := testResult.Test |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 447 | args := test.args |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 448 | |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 449 | fmt.Printf("%s%s\n", strings.Join(args, " "), test.cpuMsg()) |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 450 | name := shortTestName(test) |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 451 | if testResult.Error != nil { |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 452 | fmt.Printf("%s failed to complete: %s\n", args[0], testResult.Error) |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 453 | failed = append(failed, test) |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 454 | testOutput.addResult(name, "CRASHED") |
Steven Valdez | 3222394 | 2016-03-02 11:53:07 -0500 | [diff] [blame] | 455 | } else if !testResult.Passed { |
Adam Langley | e212f27 | 2017-02-03 08:52:21 -0800 | [diff] [blame] | 456 | 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] | 457 | failed = append(failed, test) |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 458 | testOutput.addResult(name, "FAIL") |
| 459 | } else { |
| 460 | testOutput.addResult(name, "PASS") |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
David Benjamin | 5f237bc | 2015-02-11 17:14:15 -0500 | [diff] [blame] | 464 | if *jsonOutput != "" { |
| 465 | if err := testOutput.writeTo(*jsonOutput); err != nil { |
| 466 | fmt.Fprintf(os.Stderr, "Error: %s\n", err) |
| 467 | } |
| 468 | } |
David Benjamin | 2ab7a86 | 2015-04-04 17:02:18 -0400 | [diff] [blame] | 469 | |
| 470 | if len(failed) > 0 { |
David Benjamin | 8b9e780 | 2016-03-02 18:23:21 -0500 | [diff] [blame] | 471 | 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] | 472 | for _, test := range failed { |
Adam Langley | e5adaef | 2017-05-02 14:48:47 -0700 | [diff] [blame] | 473 | fmt.Printf("\t%s%s\n", strings.Join(test.args, " "), test.cpuMsg()) |
David Benjamin | 2ab7a86 | 2015-04-04 17:02:18 -0400 | [diff] [blame] | 474 | } |
| 475 | os.Exit(1) |
| 476 | } |
| 477 | |
| 478 | fmt.Printf("\nAll tests passed!\n") |
David Benjamin | 491b921 | 2015-02-11 14:18:45 -0500 | [diff] [blame] | 479 | } |