| 1 |
// Copyright 2015 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/json" |
| 10 |
"flag" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"io/fs" |
| 14 |
"log" |
| 15 |
"os" |
| 16 |
"os/exec" |
| 17 |
"path/filepath" |
| 18 |
"reflect" |
| 19 |
"regexp" |
| 20 |
"runtime" |
| 21 |
"slices" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
"time" |
| 25 |
) |
| 26 |
|
| 27 |
func cmdtest() { |
| 28 |
gogcflags = os.Getenv("GO_GCFLAGS") |
| 29 |
setNoOpt() |
| 30 |
|
| 31 |
var t tester |
| 32 |
|
| 33 |
var noRebuild bool |
| 34 |
flag.BoolVar(&t.listMode, "list", false, "list available tests") |
| 35 |
flag.BoolVar(&t.rebuild, "rebuild", false, "rebuild everything first") |
| 36 |
flag.BoolVar(&noRebuild, "no-rebuild", false, "overrides -rebuild (historical dreg)") |
| 37 |
flag.BoolVar(&t.keepGoing, "k", false, "keep going even when error occurred") |
| 38 |
flag.BoolVar(&t.race, "race", false, "run in race builder mode (different set of tests)") |
| 39 |
flag.BoolVar(&t.compileOnly, "compile-only", false, "compile tests, but don't run them") |
| 40 |
flag.StringVar(&t.banner, "banner", "##### ", "banner prefix; blank means no section banners") |
| 41 |
flag.StringVar(&t.runRxStr, "run", "", |
| 42 |
"run only those tests matching the regular expression; empty means to run all. "+ |
| 43 |
"Special exception: if the string begins with '!', the match is inverted.") |
| 44 |
flag.BoolVar(&t.msan, "msan", false, "run in memory sanitizer builder mode") |
| 45 |
flag.BoolVar(&t.asan, "asan", false, "run in address sanitizer builder mode") |
| 46 |
flag.BoolVar(&t.json, "json", false, "report test results in JSON") |
| 47 |
|
| 48 |
xflagparse(-1) // any number of args |
| 49 |
if noRebuild { |
| 50 |
t.rebuild = false |
| 51 |
} |
| 52 |
|
| 53 |
t.run() |
| 54 |
} |
| 55 |
|
| 56 |
// tester executes cmdtest. |
| 57 |
type tester struct { |
| 58 |
race bool |
| 59 |
msan bool |
| 60 |
asan bool |
| 61 |
listMode bool |
| 62 |
rebuild bool |
| 63 |
failed bool |
| 64 |
keepGoing bool |
| 65 |
compileOnly bool // just try to compile all tests, but no need to run |
| 66 |
runRxStr string |
| 67 |
runRx *regexp.Regexp |
| 68 |
runRxWant bool // want runRx to match (true) or not match (false) |
| 69 |
runNames []string // tests to run, exclusive with runRx; empty means all |
| 70 |
banner string // prefix, or "" for none |
| 71 |
lastHeading string // last dir heading printed |
| 72 |
|
| 73 |
short bool |
| 74 |
cgoEnabled bool |
| 75 |
json bool |
| 76 |
|
| 77 |
tests []distTest // use addTest to extend |
| 78 |
testNames map[string]bool |
| 79 |
timeoutScale int |
| 80 |
|
| 81 |
worklist []*work |
| 82 |
} |
| 83 |
|
| 84 |
// work tracks command execution for a test. |
| 85 |
type work struct { |
| 86 |
dt *distTest // unique test name, etc. |
| 87 |
cmd *exec.Cmd // must write stdout/stderr to out |
| 88 |
flush func() // if non-nil, called after cmd.Run |
| 89 |
start chan bool // a true means to start, a false means to skip |
| 90 |
out bytes.Buffer // combined stdout/stderr from cmd |
| 91 |
err error // work result |
| 92 |
end chan struct{} // a value means cmd ended (or was skipped) |
| 93 |
} |
| 94 |
|
| 95 |
// printSkip prints a skip message for all of work. |
| 96 |
func (w *work) printSkip(t *tester, msg string) { |
| 97 |
if t.json { |
| 98 |
synthesizeSkipEvent(json.NewEncoder(&w.out), w.dt.name, msg) |
| 99 |
return |
| 100 |
} |
| 101 |
fmt.Fprintln(&w.out, msg) |
| 102 |
} |
| 103 |
|
| 104 |
// A distTest is a test run by dist test. |
| 105 |
// Each test has a unique name and belongs to a group (heading) |
| 106 |
type distTest struct { |
| 107 |
name string // unique test name; may be filtered with -run flag |
| 108 |
heading string // group section; this header is printed before the test is run. |
| 109 |
fn func(*distTest) error |
| 110 |
} |
| 111 |
|
| 112 |
func (t *tester) run() { |
| 113 |
timelog("start", "dist test") |
| 114 |
|
| 115 |
os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH"))) |
| 116 |
|
| 117 |
t.short = true |
| 118 |
if v := os.Getenv("GO_TEST_SHORT"); v != "" { |
| 119 |
short, err := strconv.ParseBool(v) |
| 120 |
if err != nil { |
| 121 |
fatalf("invalid GO_TEST_SHORT %q: %v", v, err) |
| 122 |
} |
| 123 |
t.short = short |
| 124 |
} |
| 125 |
|
| 126 |
cmd := exec.Command(gorootBinGo, "env", "CGO_ENABLED") |
| 127 |
cmd.Stderr = new(bytes.Buffer) |
| 128 |
slurp, err := cmd.Output() |
| 129 |
if err != nil { |
| 130 |
fatalf("Error running %s: %v\n%s", cmd, err, cmd.Stderr) |
| 131 |
} |
| 132 |
parts := strings.Split(string(slurp), "\n") |
| 133 |
if nlines := len(parts) - 1; nlines < 1 { |
| 134 |
fatalf("Error running %s: output contains <1 lines\n%s", cmd, cmd.Stderr) |
| 135 |
} |
| 136 |
t.cgoEnabled, _ = strconv.ParseBool(parts[0]) |
| 137 |
|
| 138 |
if flag.NArg() > 0 && t.runRxStr != "" { |
| 139 |
fatalf("the -run regular expression flag is mutually exclusive with test name arguments") |
| 140 |
} |
| 141 |
|
| 142 |
t.runNames = flag.Args() |
| 143 |
|
| 144 |
// Set GOTRACEBACK to system if the user didn't set a level explicitly. |
| 145 |
// Since we're running tests for Go, we want as much detail as possible |
| 146 |
// if something goes wrong. |
| 147 |
// |
| 148 |
// Set it before running any commands just in case something goes wrong. |
| 149 |
if ok := isEnvSet("GOTRACEBACK"); !ok { |
| 150 |
if err := os.Setenv("GOTRACEBACK", "system"); err != nil { |
| 151 |
if t.keepGoing { |
| 152 |
log.Printf("Failed to set GOTRACEBACK: %v", err) |
| 153 |
} else { |
| 154 |
fatalf("Failed to set GOTRACEBACK: %v", err) |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if t.rebuild { |
| 160 |
t.out("Building packages and commands.") |
| 161 |
// Force rebuild the whole toolchain. |
| 162 |
goInstall(toolenv(), gorootBinGo, append([]string{"-a"}, toolchain...)...) |
| 163 |
} |
| 164 |
|
| 165 |
if !t.listMode { |
| 166 |
if builder := os.Getenv("GO_BUILDER_NAME"); builder == "" { |
| 167 |
// Ensure that installed commands are up to date, even with -no-rebuild, |
| 168 |
// so that tests that run commands end up testing what's actually on disk. |
| 169 |
// If everything is up-to-date, this is a no-op. |
| 170 |
// We first build the toolchain twice to allow it to converge, |
| 171 |
// as when we first bootstrap. |
| 172 |
// See cmdbootstrap for a description of the overall process. |
| 173 |
// |
| 174 |
// On the builders, we skip this step: we assume that 'dist test' is |
| 175 |
// already using the result of a clean build, and because of test sharding |
| 176 |
// and virtualization we usually start with a clean GOCACHE, so we would |
| 177 |
// end up rebuilding large parts of the standard library that aren't |
| 178 |
// otherwise relevant to the actual set of packages under test. |
| 179 |
goInstall(toolenv(), gorootBinGo, toolchain...) |
| 180 |
goInstall(toolenv(), gorootBinGo, toolchain...) |
| 181 |
goInstall(toolenv(), gorootBinGo, toolsToInstall...) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
t.timeoutScale = 1 |
| 186 |
if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" { |
| 187 |
t.timeoutScale, err = strconv.Atoi(s) |
| 188 |
if err != nil { |
| 189 |
fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if t.runRxStr != "" { |
| 194 |
if t.runRxStr[0] == '!' { |
| 195 |
t.runRxWant = false |
| 196 |
t.runRxStr = t.runRxStr[1:] |
| 197 |
} else { |
| 198 |
t.runRxWant = true |
| 199 |
} |
| 200 |
t.runRx = regexp.MustCompile(t.runRxStr) |
| 201 |
} |
| 202 |
|
| 203 |
t.registerTests() |
| 204 |
if t.listMode { |
| 205 |
for _, tt := range t.tests { |
| 206 |
fmt.Println(tt.name) |
| 207 |
} |
| 208 |
return |
| 209 |
} |
| 210 |
|
| 211 |
for _, name := range t.runNames { |
| 212 |
if !t.testNames[name] { |
| 213 |
fatalf("unknown test %q", name) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// On a few builders, make GOROOT unwritable to catch tests writing to it. |
| 218 |
if strings.HasPrefix(os.Getenv("GO_BUILDER_NAME"), "linux-") { |
| 219 |
if os.Getuid() == 0 { |
| 220 |
// Don't bother making GOROOT unwritable: |
| 221 |
// we're running as root, so permissions would have no effect. |
| 222 |
} else { |
| 223 |
xatexit(t.makeGOROOTUnwritable()) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if !t.json { |
| 228 |
if err := t.maybeLogMetadata(); err != nil { |
| 229 |
t.failed = true |
| 230 |
if t.keepGoing { |
| 231 |
log.Printf("Failed logging metadata: %v", err) |
| 232 |
} else { |
| 233 |
fatalf("Failed logging metadata: %v", err) |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
var anyIncluded, someExcluded bool |
| 239 |
for _, dt := range t.tests { |
| 240 |
if !t.shouldRunTest(dt.name) { |
| 241 |
someExcluded = true |
| 242 |
continue |
| 243 |
} |
| 244 |
anyIncluded = true |
| 245 |
dt := dt // dt used in background after this iteration |
| 246 |
if err := dt.fn(&dt); err != nil { |
| 247 |
t.runPending(&dt) // in case that hasn't been done yet |
| 248 |
t.failed = true |
| 249 |
if t.keepGoing { |
| 250 |
log.Printf("Failed: %v", err) |
| 251 |
} else { |
| 252 |
fatalf("Failed: %v", err) |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
t.runPending(nil) |
| 257 |
timelog("end", "dist test") |
| 258 |
|
| 259 |
if !t.json { |
| 260 |
if t.failed { |
| 261 |
fmt.Println("\nFAILED") |
| 262 |
} else if !anyIncluded { |
| 263 |
fmt.Println() |
| 264 |
errprintf("go tool dist: warning: %q matched no tests; use the -list flag to list available tests\n", t.runRxStr) |
| 265 |
fmt.Println("NO TESTS TO RUN") |
| 266 |
} else if someExcluded { |
| 267 |
fmt.Println("\nALL TESTS PASSED (some were excluded)") |
| 268 |
} else { |
| 269 |
fmt.Println("\nALL TESTS PASSED") |
| 270 |
} |
| 271 |
} |
| 272 |
if t.failed { |
| 273 |
xexit(1) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func (t *tester) shouldRunTest(name string) bool { |
| 278 |
if t.runRx != nil { |
| 279 |
return t.runRx.MatchString(name) == t.runRxWant |
| 280 |
} |
| 281 |
if len(t.runNames) == 0 { |
| 282 |
return true |
| 283 |
} |
| 284 |
return slices.Contains(t.runNames, name) |
| 285 |
} |
| 286 |
|
| 287 |
func (t *tester) maybeLogMetadata() error { |
| 288 |
if t.compileOnly { |
| 289 |
// We need to run a subprocess to log metadata. Don't do that |
| 290 |
// on compile-only runs. |
| 291 |
return nil |
| 292 |
} |
| 293 |
t.out("Test execution environment.") |
| 294 |
// Helper binary to print system metadata (CPU model, etc). This is a |
| 295 |
// separate binary from dist so it need not build with the bootstrap |
| 296 |
// toolchain. |
| 297 |
// |
| 298 |
// TODO(prattmic): If we split dist bootstrap and dist test then this |
| 299 |
// could be simplified to directly use internal/sysinfo here. |
| 300 |
return t.dirCmd(filepath.Join(goroot, "src/cmd/internal/metadata"), gorootBinGo, []string{"run", "main.go"}).Run() |
| 301 |
} |
| 302 |
|
| 303 |
// testName returns the dist test name for a given package and variant. |
| 304 |
func testName(pkg, variant string) string { |
| 305 |
name := pkg |
| 306 |
if variant != "" { |
| 307 |
name += ":" + variant |
| 308 |
} |
| 309 |
return name |
| 310 |
} |
| 311 |
|
| 312 |
// goTest represents all options to a "go test" command. The final command will |
| 313 |
// combine configuration from goTest and tester flags. |
| 314 |
type goTest struct { |
| 315 |
timeout time.Duration // If non-zero, override timeout |
| 316 |
short bool // If true, force -short |
| 317 |
tags []string // Build tags |
| 318 |
race bool // Force -race |
| 319 |
bench bool // Run benchmarks (briefly), not tests. |
| 320 |
runTests string // Regexp of tests to run |
| 321 |
cpu string // If non-empty, -cpu flag |
| 322 |
skip string // If non-empty, -skip flag |
| 323 |
|
| 324 |
gcflags string // If non-empty, build with -gcflags=all=X |
| 325 |
ldflags string // If non-empty, build with -ldflags=X |
| 326 |
buildmode string // If non-empty, -buildmode flag |
| 327 |
|
| 328 |
env []string // Environment variables to add, as KEY=VAL. KEY= unsets a variable |
| 329 |
|
| 330 |
runOnHost bool // When cross-compiling, run this test on the host instead of guest |
| 331 |
|
| 332 |
// variant, if non-empty, is a name used to distinguish different |
| 333 |
// configurations of the same test package(s). If set and omitVariant is false, |
| 334 |
// the Package field in test2json output is rewritten to pkg:variant. |
| 335 |
variant string |
| 336 |
// omitVariant indicates that variant is used solely for the dist test name and |
| 337 |
// that the set of test names run by each variant (including empty) of a package |
| 338 |
// is non-overlapping. |
| 339 |
// |
| 340 |
// TODO(mknyszek): Consider removing omitVariant as it is no longer set to true |
| 341 |
// by any test. It's too valuable to have timing information in ResultDB that |
| 342 |
// corresponds directly with dist names for tests. |
| 343 |
omitVariant bool |
| 344 |
|
| 345 |
// We have both pkg and pkgs as a convenience. Both may be set, in which |
| 346 |
// case they will be combined. At least one must be set. |
| 347 |
pkgs []string // Multiple packages to test |
| 348 |
pkg string // A single package to test |
| 349 |
|
| 350 |
testFlags []string // Additional flags accepted by this test |
| 351 |
} |
| 352 |
|
| 353 |
// compileOnly reports whether this test is only for compiling, |
| 354 |
// indicated by runTests being set to '^$' and bench being false. |
| 355 |
func (opts *goTest) compileOnly() bool { |
| 356 |
return opts.runTests == "^$" && !opts.bench |
| 357 |
} |
| 358 |
|
| 359 |
// bgCommand returns a go test Cmd and a post-Run flush function. The result |
| 360 |
// will write its output to stdout and stderr. If stdout==stderr, bgCommand |
| 361 |
// ensures Writes are serialized. The caller should call flush() after Cmd exits. |
| 362 |
func (opts *goTest) bgCommand(t *tester, stdout, stderr io.Writer) (cmd *exec.Cmd, flush func()) { |
| 363 |
build, run, pkgs, testFlags, setupCmd := opts.buildArgs(t) |
| 364 |
|
| 365 |
// Combine the flags. |
| 366 |
args := append([]string{"test"}, build...) |
| 367 |
if t.compileOnly || opts.compileOnly() { |
| 368 |
args = append(args, "-c", "-o", os.DevNull) |
| 369 |
} else { |
| 370 |
args = append(args, run...) |
| 371 |
} |
| 372 |
args = append(args, pkgs...) |
| 373 |
if !t.compileOnly && !opts.compileOnly() { |
| 374 |
args = append(args, testFlags...) |
| 375 |
} |
| 376 |
|
| 377 |
cmd = exec.Command(gorootBinGo, args...) |
| 378 |
setupCmd(cmd) |
| 379 |
if t.json && opts.variant != "" && !opts.omitVariant { |
| 380 |
// Rewrite Package in the JSON output to be pkg:variant. When omitVariant |
| 381 |
// is true, pkg.TestName is already unambiguous, so we don't need to |
| 382 |
// rewrite the Package field. |
| 383 |
// |
| 384 |
// We only want to process JSON on the child's stdout. Ideally if |
| 385 |
// stdout==stderr, we would also use the same testJSONFilter for |
| 386 |
// cmd.Stdout and cmd.Stderr in order to keep the underlying |
| 387 |
// interleaving of writes, but then it would see even partial writes |
| 388 |
// interleaved, which would corrupt the JSON. So, we only process |
| 389 |
// cmd.Stdout. This has another consequence though: if stdout==stderr, |
| 390 |
// we have to serialize Writes in case the Writer is not concurrent |
| 391 |
// safe. If we were just passing stdout/stderr through to exec, it would |
| 392 |
// do this for us, but since we're wrapping stdout, we have to do it |
| 393 |
// ourselves. |
| 394 |
if stdout == stderr { |
| 395 |
stdout = &lockedWriter{w: stdout} |
| 396 |
stderr = stdout |
| 397 |
} |
| 398 |
f := &testJSONFilter{w: stdout, variant: opts.variant} |
| 399 |
cmd.Stdout = f |
| 400 |
flush = f.Flush |
| 401 |
} else { |
| 402 |
cmd.Stdout = stdout |
| 403 |
flush = func() {} |
| 404 |
} |
| 405 |
cmd.Stderr = stderr |
| 406 |
|
| 407 |
return cmd, flush |
| 408 |
} |
| 409 |
|
| 410 |
// run runs a go test and returns an error if it does not succeed. |
| 411 |
func (opts *goTest) run(t *tester) error { |
| 412 |
cmd, flush := opts.bgCommand(t, os.Stdout, os.Stderr) |
| 413 |
err := cmd.Run() |
| 414 |
flush() |
| 415 |
return err |
| 416 |
} |
| 417 |
|
| 418 |
// buildArgs is in internal helper for goTest that constructs the elements of |
| 419 |
// the "go test" command line. build is the flags for building the test. run is |
| 420 |
// the flags for running the test. pkgs is the list of packages to build and |
| 421 |
// run. testFlags is the list of flags to pass to the test package. |
| 422 |
// |
| 423 |
// The caller must call setupCmd on the resulting exec.Cmd to set its directory |
| 424 |
// and environment. |
| 425 |
func (opts *goTest) buildArgs(t *tester) (build, run, pkgs, testFlags []string, setupCmd func(*exec.Cmd)) { |
| 426 |
run = append(run, "-count=1") // Disallow caching |
| 427 |
if opts.timeout != 0 { |
| 428 |
d := opts.timeout * time.Duration(t.timeoutScale) |
| 429 |
run = append(run, "-timeout="+d.String()) |
| 430 |
} else if t.timeoutScale != 1 { |
| 431 |
const goTestDefaultTimeout = 10 * time.Minute // Default value of go test -timeout flag. |
| 432 |
run = append(run, "-timeout="+(goTestDefaultTimeout*time.Duration(t.timeoutScale)).String()) |
| 433 |
} |
| 434 |
if opts.short || t.short { |
| 435 |
run = append(run, "-short") |
| 436 |
} |
| 437 |
var tags []string |
| 438 |
if t.iOS() { |
| 439 |
tags = append(tags, "lldb") |
| 440 |
} |
| 441 |
if noOpt { |
| 442 |
tags = append(tags, "noopt") |
| 443 |
} |
| 444 |
tags = append(tags, opts.tags...) |
| 445 |
if len(tags) > 0 { |
| 446 |
build = append(build, "-tags="+strings.Join(tags, ",")) |
| 447 |
} |
| 448 |
if t.race || opts.race { |
| 449 |
build = append(build, "-race") |
| 450 |
} |
| 451 |
if t.msan { |
| 452 |
build = append(build, "-msan") |
| 453 |
} |
| 454 |
if t.asan { |
| 455 |
build = append(build, "-asan") |
| 456 |
} |
| 457 |
if opts.bench { |
| 458 |
// Run no tests. |
| 459 |
run = append(run, "-run=^$") |
| 460 |
// Run benchmarks briefly as a smoke test. |
| 461 |
run = append(run, "-bench=.*", "-benchtime=.1s") |
| 462 |
} else if opts.runTests != "" { |
| 463 |
run = append(run, "-run="+opts.runTests) |
| 464 |
} |
| 465 |
if opts.cpu != "" { |
| 466 |
run = append(run, "-cpu="+opts.cpu) |
| 467 |
} |
| 468 |
if opts.skip != "" { |
| 469 |
run = append(run, "-skip="+opts.skip) |
| 470 |
} |
| 471 |
if t.json { |
| 472 |
run = append(run, "-json") |
| 473 |
} |
| 474 |
|
| 475 |
if opts.gcflags != "" { |
| 476 |
build = append(build, "-gcflags=all="+opts.gcflags) |
| 477 |
} |
| 478 |
if opts.ldflags != "" { |
| 479 |
build = append(build, "-ldflags="+opts.ldflags) |
| 480 |
} |
| 481 |
if opts.buildmode != "" { |
| 482 |
build = append(build, "-buildmode="+opts.buildmode) |
| 483 |
} |
| 484 |
|
| 485 |
pkgs = opts.packages() |
| 486 |
|
| 487 |
runOnHost := opts.runOnHost && (goarch != gohostarch || goos != gohostos) |
| 488 |
needTestFlags := len(opts.testFlags) > 0 || runOnHost |
| 489 |
if needTestFlags { |
| 490 |
testFlags = append([]string{"-args"}, opts.testFlags...) |
| 491 |
} |
| 492 |
if runOnHost { |
| 493 |
// -target is a special flag understood by tests that can run on the host |
| 494 |
testFlags = append(testFlags, "-target="+goos+"/"+goarch) |
| 495 |
} |
| 496 |
|
| 497 |
setupCmd = func(cmd *exec.Cmd) { |
| 498 |
setDir(cmd, filepath.Join(goroot, "src")) |
| 499 |
if len(opts.env) != 0 { |
| 500 |
for _, kv := range opts.env { |
| 501 |
if i := strings.Index(kv, "="); i < 0 { |
| 502 |
unsetEnv(cmd, kv[:len(kv)-1]) |
| 503 |
} else { |
| 504 |
setEnv(cmd, kv[:i], kv[i+1:]) |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
if runOnHost { |
| 509 |
setEnv(cmd, "GOARCH", gohostarch) |
| 510 |
setEnv(cmd, "GOOS", gohostos) |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
return |
| 515 |
} |
| 516 |
|
| 517 |
// packages returns the full list of packages to be run by this goTest. This |
| 518 |
// will always include at least one package. |
| 519 |
func (opts *goTest) packages() []string { |
| 520 |
pkgs := opts.pkgs |
| 521 |
if opts.pkg != "" { |
| 522 |
pkgs = append(pkgs[:len(pkgs):len(pkgs)], opts.pkg) |
| 523 |
} |
| 524 |
if len(pkgs) == 0 { |
| 525 |
panic("no packages") |
| 526 |
} |
| 527 |
return pkgs |
| 528 |
} |
| 529 |
|
| 530 |
// printSkip prints a skip message for all of goTest. |
| 531 |
func (opts *goTest) printSkip(t *tester, msg string) { |
| 532 |
if t.json { |
| 533 |
enc := json.NewEncoder(os.Stdout) |
| 534 |
for _, pkg := range opts.packages() { |
| 535 |
synthesizeSkipEvent(enc, pkg, msg) |
| 536 |
} |
| 537 |
return |
| 538 |
} |
| 539 |
fmt.Println(msg) |
| 540 |
} |
| 541 |
|
| 542 |
// ranGoTest and stdMatches are state closed over by the stdlib |
| 543 |
// testing func in registerStdTest below. The tests are run |
| 544 |
// sequentially, so there's no need for locks. |
| 545 |
// |
| 546 |
// ranGoBench and benchMatches are the same, but are only used |
| 547 |
// in -race mode. |
| 548 |
var ( |
| 549 |
ranGoTest bool |
| 550 |
stdMatches []string |
| 551 |
|
| 552 |
ranGoBench bool |
| 553 |
benchMatches []string |
| 554 |
) |
| 555 |
|
| 556 |
func (t *tester) registerStdTest(pkg string) { |
| 557 |
const stdTestHeading = "Testing packages." // known to addTest for a safety check |
| 558 |
gcflags := gogcflags |
| 559 |
name := testName(pkg, "") |
| 560 |
if t.runRx == nil || t.runRx.MatchString(name) == t.runRxWant { |
| 561 |
stdMatches = append(stdMatches, pkg) |
| 562 |
} |
| 563 |
t.addTest(name, stdTestHeading, func(dt *distTest) error { |
| 564 |
if ranGoTest { |
| 565 |
return nil |
| 566 |
} |
| 567 |
t.runPending(dt) |
| 568 |
timelog("start", dt.name) |
| 569 |
defer timelog("end", dt.name) |
| 570 |
ranGoTest = true |
| 571 |
|
| 572 |
timeoutSec := 180 * time.Second |
| 573 |
for _, pkg := range stdMatches { |
| 574 |
if pkg == "cmd/go" { |
| 575 |
timeoutSec *= 3 |
| 576 |
break |
| 577 |
} |
| 578 |
} |
| 579 |
return (&goTest{ |
| 580 |
timeout: timeoutSec, |
| 581 |
gcflags: gcflags, |
| 582 |
pkgs: stdMatches, |
| 583 |
}).run(t) |
| 584 |
}) |
| 585 |
} |
| 586 |
|
| 587 |
func (t *tester) registerRaceBenchTest(pkg string) { |
| 588 |
const raceBenchHeading = "Running benchmarks briefly." // known to addTest for a safety check |
| 589 |
name := testName(pkg, "racebench") |
| 590 |
if t.runRx == nil || t.runRx.MatchString(name) == t.runRxWant { |
| 591 |
benchMatches = append(benchMatches, pkg) |
| 592 |
} |
| 593 |
t.addTest(name, raceBenchHeading, func(dt *distTest) error { |
| 594 |
if ranGoBench { |
| 595 |
return nil |
| 596 |
} |
| 597 |
t.runPending(dt) |
| 598 |
timelog("start", dt.name) |
| 599 |
defer timelog("end", dt.name) |
| 600 |
ranGoBench = true |
| 601 |
return (&goTest{ |
| 602 |
variant: "racebench", |
| 603 |
// Include the variant even though there's no overlap in test names. |
| 604 |
// This makes the test targets distinct, allowing our build system to record |
| 605 |
// elapsed time for each one, which is useful for load-balancing test shards. |
| 606 |
omitVariant: false, |
| 607 |
timeout: 1200 * time.Second, // longer timeout for race with benchmarks |
| 608 |
race: true, |
| 609 |
bench: true, |
| 610 |
cpu: "4", |
| 611 |
pkgs: benchMatches, |
| 612 |
}).run(t) |
| 613 |
}) |
| 614 |
} |
| 615 |
|
| 616 |
func (t *tester) registerTests() { |
| 617 |
// registerStdTestSpecially tracks import paths in the standard library |
| 618 |
// whose test registration happens in a special way. |
| 619 |
// |
| 620 |
// These tests *must* be able to run normally as part of "go test std cmd", |
| 621 |
// even if they are also registered separately by dist, because users often |
| 622 |
// run go test directly. Use skips or build tags in preference to expanding |
| 623 |
// this list. |
| 624 |
registerStdTestSpecially := map[string]bool{ |
| 625 |
// testdir can run normally as part of "go test std cmd", but because |
| 626 |
// it's a very large test, we register is specially as several shards to |
| 627 |
// enable better load balancing on sharded builders. Ideally the build |
| 628 |
// system would know how to shard any large test package. |
| 629 |
"cmd/internal/testdir": true, |
| 630 |
} |
| 631 |
|
| 632 |
// Fast path to avoid the ~1 second of `go list std cmd` when |
| 633 |
// the caller lists specific tests to run. (as the continuous |
| 634 |
// build coordinator does). |
| 635 |
if len(t.runNames) > 0 { |
| 636 |
for _, name := range t.runNames { |
| 637 |
if !strings.Contains(name, ":") { |
| 638 |
t.registerStdTest(name) |
| 639 |
} else if strings.HasSuffix(name, ":racebench") { |
| 640 |
t.registerRaceBenchTest(strings.TrimSuffix(name, ":racebench")) |
| 641 |
} |
| 642 |
} |
| 643 |
} else { |
| 644 |
// Use 'go list std cmd' to get a list of all Go packages |
| 645 |
// that running 'go test std cmd' could find problems in. |
| 646 |
// (In race test mode, also set -tags=race.) |
| 647 |
// This includes vendored packages and other |
| 648 |
// packages without tests so that 'dist test' finds if any of |
| 649 |
// them don't build, have a problem reported by high-confidence |
| 650 |
// vet checks that come with 'go test', and anything else it |
| 651 |
// may check in the future. See go.dev/issue/60463. |
| 652 |
// Most packages have tests, so there is not much saved |
| 653 |
// by skipping non-test packages. |
| 654 |
// For the packages without any test files, |
| 655 |
// 'go test' knows not to actually build a test binary, |
| 656 |
// so the only cost is the vet, and we still want to run vet. |
| 657 |
cmd := exec.Command(gorootBinGo, "list") |
| 658 |
if t.race { |
| 659 |
cmd.Args = append(cmd.Args, "-tags=race") |
| 660 |
} |
| 661 |
cmd.Args = append(cmd.Args, "std", "cmd") |
| 662 |
cmd.Stderr = new(bytes.Buffer) |
| 663 |
all, err := cmd.Output() |
| 664 |
if err != nil { |
| 665 |
fatalf("Error running go list std cmd: %v:\n%s", err, cmd.Stderr) |
| 666 |
} |
| 667 |
pkgs := strings.Fields(string(all)) |
| 668 |
for _, pkg := range pkgs { |
| 669 |
if registerStdTestSpecially[pkg] { |
| 670 |
continue |
| 671 |
} |
| 672 |
if t.short && (strings.HasPrefix(pkg, "vendor/") || strings.HasPrefix(pkg, "cmd/vendor/")) { |
| 673 |
// Vendored code has no tests, and we don't care too much about vet errors |
| 674 |
// since we can't modify the code, so skip the tests in short mode. |
| 675 |
// We still let the longtest builders vet them. |
| 676 |
continue |
| 677 |
} |
| 678 |
t.registerStdTest(pkg) |
| 679 |
} |
| 680 |
if t.race { |
| 681 |
for _, pkg := range pkgs { |
| 682 |
if t.packageHasBenchmarks(pkg) { |
| 683 |
t.registerRaceBenchTest(pkg) |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
if t.race { |
| 690 |
return |
| 691 |
} |
| 692 |
|
| 693 |
// Test the os/user package in the pure-Go mode too. |
| 694 |
if !t.compileOnly { |
| 695 |
t.registerTest("os/user with tag osusergo", |
| 696 |
&goTest{ |
| 697 |
variant: "osusergo", |
| 698 |
timeout: 300 * time.Second, |
| 699 |
tags: []string{"osusergo"}, |
| 700 |
pkg: "os/user", |
| 701 |
}) |
| 702 |
t.registerTest("hash/maphash purego implementation", |
| 703 |
&goTest{ |
| 704 |
variant: "purego", |
| 705 |
timeout: 300 * time.Second, |
| 706 |
tags: []string{"purego"}, |
| 707 |
pkg: "hash/maphash", |
| 708 |
}) |
| 709 |
} |
| 710 |
|
| 711 |
// Check that all crypto packages compile with the purego build tag. |
| 712 |
t.registerTest("crypto with tag purego (build and vet only)", &goTest{ |
| 713 |
variant: "purego", |
| 714 |
tags: []string{"purego"}, |
| 715 |
pkg: "crypto/...", |
| 716 |
runTests: "^$", // only ensure they compile |
| 717 |
}) |
| 718 |
|
| 719 |
// Check that all crypto packages compile (and test correctly, in longmode) with fips. |
| 720 |
if t.fipsSupported() { |
| 721 |
// Test standard crypto packages with fips140=on. |
| 722 |
t.registerTest("GOFIPS140=latest go test crypto/...", &goTest{ |
| 723 |
variant: "gofips140", |
| 724 |
env: []string{"GOFIPS140=latest"}, |
| 725 |
pkg: "crypto/...", |
| 726 |
}) |
| 727 |
|
| 728 |
// Test that earlier FIPS snapshots build. |
| 729 |
// In long mode, test that they work too. |
| 730 |
for _, version := range fipsVersions(t.short) { |
| 731 |
suffix := " # (build and vet only)" |
| 732 |
run := "^$" // only ensure they compile |
| 733 |
if !t.short { |
| 734 |
suffix = "" |
| 735 |
run = "" |
| 736 |
} |
| 737 |
t.registerTest("GOFIPS140="+version+" go test crypto/..."+suffix, &goTest{ |
| 738 |
variant: "gofips140-" + version, |
| 739 |
pkg: "crypto/...", |
| 740 |
runTests: run, |
| 741 |
env: []string{"GOFIPS140=" + version, "GOMODCACHE=" + filepath.Join(workdir, "fips-"+version)}, |
| 742 |
}) |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
// Test GOEXPERIMENT=jsonv2. |
| 747 |
if !strings.Contains(goexperiment, "jsonv2") { |
| 748 |
t.registerTest("GOEXPERIMENT=jsonv2 go test encoding/json/...", &goTest{ |
| 749 |
variant: "jsonv2", |
| 750 |
env: []string{"GOEXPERIMENT=jsonv2"}, |
| 751 |
pkg: "encoding/json/...", |
| 752 |
}) |
| 753 |
} |
| 754 |
|
| 755 |
// Test ios/amd64 for the iOS simulator. |
| 756 |
if goos == "darwin" && goarch == "amd64" && t.cgoEnabled { |
| 757 |
t.registerTest("GOOS=ios on darwin/amd64", |
| 758 |
&goTest{ |
| 759 |
variant: "amd64ios", |
| 760 |
timeout: 300 * time.Second, |
| 761 |
runTests: "SystemRoots", |
| 762 |
env: []string{"GOOS=ios", "CGO_ENABLED=1"}, |
| 763 |
pkg: "crypto/x509", |
| 764 |
}) |
| 765 |
} |
| 766 |
|
| 767 |
// GC debug mode tests. We only run these in long-test mode |
| 768 |
// (with GO_TEST_SHORT=0) because this is just testing a |
| 769 |
// non-critical debug setting. |
| 770 |
if !t.compileOnly && !t.short { |
| 771 |
t.registerTest("GODEBUG=gcstoptheworld=2 archive/zip", |
| 772 |
&goTest{ |
| 773 |
variant: "runtime:gcstoptheworld2", |
| 774 |
timeout: 300 * time.Second, |
| 775 |
short: true, |
| 776 |
env: []string{"GODEBUG=gcstoptheworld=2"}, |
| 777 |
pkg: "archive/zip", |
| 778 |
}) |
| 779 |
t.registerTest("GODEBUG=gccheckmark=1 runtime", |
| 780 |
&goTest{ |
| 781 |
variant: "runtime:gccheckmark", |
| 782 |
timeout: 300 * time.Second, |
| 783 |
short: true, |
| 784 |
env: []string{"GODEBUG=gccheckmark=1"}, |
| 785 |
pkg: "runtime", |
| 786 |
}) |
| 787 |
} |
| 788 |
|
| 789 |
// morestack tests. We only run these in long-test mode |
| 790 |
// (with GO_TEST_SHORT=0) because the runtime test is |
| 791 |
// already quite long and mayMoreStackMove makes it about |
| 792 |
// twice as slow. |
| 793 |
if !t.compileOnly && !t.short { |
| 794 |
// hooks is the set of maymorestack hooks to test with. |
| 795 |
hooks := []string{"mayMoreStackPreempt", "mayMoreStackMove"} |
| 796 |
// hookPkgs is the set of package patterns to apply |
| 797 |
// the maymorestack hook to. |
| 798 |
hookPkgs := []string{"runtime/...", "reflect", "sync"} |
| 799 |
// unhookPkgs is the set of package patterns to |
| 800 |
// exclude from hookPkgs. |
| 801 |
unhookPkgs := []string{"runtime/testdata/..."} |
| 802 |
for _, hook := range hooks { |
| 803 |
// Construct the build flags to use the |
| 804 |
// maymorestack hook in the compiler and |
| 805 |
// assembler. We pass this via the GOFLAGS |
| 806 |
// environment variable so that it applies to |
| 807 |
// both the test itself and to binaries built |
| 808 |
// by the test. |
| 809 |
goFlagsList := []string{} |
| 810 |
for _, flag := range []string{"-gcflags", "-asmflags"} { |
| 811 |
for _, hookPkg := range hookPkgs { |
| 812 |
goFlagsList = append(goFlagsList, flag+"="+hookPkg+"=-d=maymorestack=runtime."+hook) |
| 813 |
} |
| 814 |
for _, unhookPkg := range unhookPkgs { |
| 815 |
goFlagsList = append(goFlagsList, flag+"="+unhookPkg+"=") |
| 816 |
} |
| 817 |
} |
| 818 |
goFlags := strings.Join(goFlagsList, " ") |
| 819 |
|
| 820 |
t.registerTest("maymorestack="+hook, |
| 821 |
&goTest{ |
| 822 |
variant: hook, |
| 823 |
timeout: 600 * time.Second, |
| 824 |
short: true, |
| 825 |
env: []string{"GOFLAGS=" + goFlags}, |
| 826 |
pkgs: []string{"runtime", "reflect", "sync"}, |
| 827 |
}) |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
// Test that internal linking of standard packages does not |
| 832 |
// require libgcc. This ensures that we can install a Go |
| 833 |
// release on a system that does not have a C compiler |
| 834 |
// installed and still build Go programs (that don't use cgo). |
| 835 |
for _, pkg := range cgoPackages { |
| 836 |
if !t.internalLink() { |
| 837 |
break |
| 838 |
} |
| 839 |
|
| 840 |
// ARM libgcc may be Thumb, which internal linking does not support. |
| 841 |
if goarch == "arm" { |
| 842 |
break |
| 843 |
} |
| 844 |
|
| 845 |
// What matters is that the tests build and start up. |
| 846 |
// Skip expensive tests, especially x509 TestSystemRoots. |
| 847 |
run := "^Test[^CS]" |
| 848 |
if pkg == "net" { |
| 849 |
run = "TestTCPStress" |
| 850 |
} |
| 851 |
t.registerTest("Testing without libgcc.", |
| 852 |
&goTest{ |
| 853 |
variant: "nolibgcc", |
| 854 |
ldflags: "-linkmode=internal -libgcc=none", |
| 855 |
runTests: run, |
| 856 |
pkg: pkg, |
| 857 |
}) |
| 858 |
} |
| 859 |
|
| 860 |
// Stub out following test on alpine until 54354 resolved. |
| 861 |
builderName := os.Getenv("GO_BUILDER_NAME") |
| 862 |
disablePIE := strings.HasSuffix(builderName, "-alpine") |
| 863 |
|
| 864 |
// Test internal linking of PIE binaries where it is supported. |
| 865 |
if t.internalLinkPIE() && !disablePIE { |
| 866 |
t.registerTest("internal linking, -buildmode=pie", |
| 867 |
&goTest{ |
| 868 |
variant: "pie_internal", |
| 869 |
timeout: 60 * time.Second, |
| 870 |
buildmode: "pie", |
| 871 |
ldflags: "-linkmode=internal", |
| 872 |
env: []string{"CGO_ENABLED=0"}, |
| 873 |
pkg: "reflect", |
| 874 |
}) |
| 875 |
t.registerTest("internal linking, -buildmode=pie", |
| 876 |
&goTest{ |
| 877 |
variant: "pie_internal", |
| 878 |
timeout: 60 * time.Second, |
| 879 |
buildmode: "pie", |
| 880 |
ldflags: "-linkmode=internal", |
| 881 |
env: []string{"CGO_ENABLED=0"}, |
| 882 |
pkg: "crypto/internal/fips140test", |
| 883 |
runTests: "TestFIPSCheck", |
| 884 |
}) |
| 885 |
// Also test a cgo package. |
| 886 |
if t.cgoEnabled && t.internalLink() && !disablePIE { |
| 887 |
t.registerTest("internal linking, -buildmode=pie", |
| 888 |
&goTest{ |
| 889 |
variant: "pie_internal", |
| 890 |
timeout: 60 * time.Second, |
| 891 |
buildmode: "pie", |
| 892 |
ldflags: "-linkmode=internal", |
| 893 |
pkg: "os/user", |
| 894 |
}) |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
if t.extLink() && !t.compileOnly { |
| 899 |
if goos != "android" { // Android does not support non-PIE linking |
| 900 |
t.registerTest("external linking, -buildmode=exe", |
| 901 |
&goTest{ |
| 902 |
variant: "exe_external", |
| 903 |
timeout: 60 * time.Second, |
| 904 |
buildmode: "exe", |
| 905 |
ldflags: "-linkmode=external", |
| 906 |
env: []string{"CGO_ENABLED=1"}, |
| 907 |
pkg: "crypto/internal/fips140test", |
| 908 |
runTests: "TestFIPSCheck", |
| 909 |
}) |
| 910 |
} |
| 911 |
if t.externalLinkPIE() && !disablePIE { |
| 912 |
t.registerTest("external linking, -buildmode=pie", |
| 913 |
&goTest{ |
| 914 |
variant: "pie_external", |
| 915 |
timeout: 60 * time.Second, |
| 916 |
buildmode: "pie", |
| 917 |
ldflags: "-linkmode=external", |
| 918 |
env: []string{"CGO_ENABLED=1"}, |
| 919 |
pkg: "crypto/internal/fips140test", |
| 920 |
runTests: "TestFIPSCheck", |
| 921 |
}) |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
// sync tests |
| 926 |
if t.hasParallelism() { |
| 927 |
t.registerTest("sync -cpu=10", |
| 928 |
&goTest{ |
| 929 |
variant: "cpu10", |
| 930 |
timeout: 120 * time.Second, |
| 931 |
cpu: "10", |
| 932 |
pkg: "sync", |
| 933 |
}) |
| 934 |
} |
| 935 |
|
| 936 |
const cgoHeading = "Testing cgo" |
| 937 |
if t.cgoEnabled { |
| 938 |
t.registerCgoTests(cgoHeading) |
| 939 |
} |
| 940 |
|
| 941 |
if goos == "wasip1" { |
| 942 |
t.registerTest("wasip1 host tests", |
| 943 |
&goTest{ |
| 944 |
variant: "host", |
| 945 |
pkg: "internal/runtime/wasitest", |
| 946 |
timeout: 1 * time.Minute, |
| 947 |
runOnHost: true, |
| 948 |
}) |
| 949 |
} |
| 950 |
|
| 951 |
// Only run the API check on fast development platforms. |
| 952 |
// Every platform checks the API on every GOOS/GOARCH/CGO_ENABLED combination anyway, |
| 953 |
// so we really only need to run this check once anywhere to get adequate coverage. |
| 954 |
// To help developers avoid trybot-only failures, we try to run on typical developer machines |
| 955 |
// which is darwin,linux,windows/amd64 and darwin/arm64. |
| 956 |
// |
| 957 |
// The same logic applies to the release notes that correspond to each api/next file. |
| 958 |
if goos == "darwin" || ((goos == "linux" || goos == "windows") && goarch == "amd64") { |
| 959 |
t.registerTest("API release note check", &goTest{variant: "check", pkg: "cmd/relnote", testFlags: []string{"-check"}}) |
| 960 |
t.registerTest("API check", &goTest{variant: "check", pkg: "cmd/api", timeout: 5 * time.Minute, testFlags: []string{"-check"}}) |
| 961 |
} |
| 962 |
|
| 963 |
// Runtime CPU tests. |
| 964 |
if !t.compileOnly && t.hasParallelism() { |
| 965 |
for i := 1; i <= 4; i *= 2 { |
| 966 |
t.registerTest(fmt.Sprintf("GOMAXPROCS=2 runtime -cpu=%d -quick", i), |
| 967 |
&goTest{ |
| 968 |
variant: "cpu" + strconv.Itoa(i), |
| 969 |
timeout: 300 * time.Second, |
| 970 |
cpu: strconv.Itoa(i), |
| 971 |
gcflags: gogcflags, |
| 972 |
short: true, |
| 973 |
testFlags: []string{"-quick"}, |
| 974 |
// We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code, |
| 975 |
// creation of first goroutines and first garbage collections in the parallel setting. |
| 976 |
env: []string{"GOMAXPROCS=2"}, |
| 977 |
pkg: "runtime", |
| 978 |
}) |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
if t.raceDetectorSupported() && !t.msan && !t.asan { |
| 983 |
// N.B. -race is incompatible with -msan and -asan. |
| 984 |
t.registerRaceTests() |
| 985 |
} |
| 986 |
|
| 987 |
if goos != "android" && !t.iOS() { |
| 988 |
// Only start multiple test dir shards on builders, |
| 989 |
// where they get distributed to multiple machines. |
| 990 |
// See issues 20141 and 31834. |
| 991 |
nShards := 1 |
| 992 |
if os.Getenv("GO_BUILDER_NAME") != "" { |
| 993 |
nShards = 10 |
| 994 |
} |
| 995 |
if n, err := strconv.Atoi(os.Getenv("GO_TEST_SHARDS")); err == nil { |
| 996 |
nShards = n |
| 997 |
} |
| 998 |
for shard := 0; shard < nShards; shard++ { |
| 999 |
id := fmt.Sprintf("%d_%d", shard, nShards) |
| 1000 |
t.registerTest("../test", |
| 1001 |
&goTest{ |
| 1002 |
variant: id, |
| 1003 |
// Include the variant even though there's no overlap in test names. |
| 1004 |
// This makes the test target more clearly distinct in our build |
| 1005 |
// results and is important for load-balancing test shards. |
| 1006 |
omitVariant: false, |
| 1007 |
pkg: "cmd/internal/testdir", |
| 1008 |
testFlags: []string{fmt.Sprintf("-shard=%d", shard), fmt.Sprintf("-shards=%d", nShards)}, |
| 1009 |
runOnHost: true, |
| 1010 |
}, |
| 1011 |
) |
| 1012 |
} |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
// addTest adds an arbitrary test callback to the test list. |
| 1017 |
// |
| 1018 |
// name must uniquely identify the test and heading must be non-empty. |
| 1019 |
func (t *tester) addTest(name, heading string, fn func(*distTest) error) { |
| 1020 |
if t.testNames[name] { |
| 1021 |
panic("duplicate registered test name " + name) |
| 1022 |
} |
| 1023 |
if heading == "" { |
| 1024 |
panic("empty heading") |
| 1025 |
} |
| 1026 |
// Two simple checks for cases that would conflict with the fast path in registerTests. |
| 1027 |
if !strings.Contains(name, ":") && heading != "Testing packages." { |
| 1028 |
panic("empty variant is reserved exclusively for registerStdTest") |
| 1029 |
} else if strings.HasSuffix(name, ":racebench") && heading != "Running benchmarks briefly." { |
| 1030 |
panic("racebench variant is reserved exclusively for registerRaceBenchTest") |
| 1031 |
} |
| 1032 |
if t.testNames == nil { |
| 1033 |
t.testNames = make(map[string]bool) |
| 1034 |
} |
| 1035 |
t.testNames[name] = true |
| 1036 |
t.tests = append(t.tests, distTest{ |
| 1037 |
name: name, |
| 1038 |
heading: heading, |
| 1039 |
fn: fn, |
| 1040 |
}) |
| 1041 |
} |
| 1042 |
|
| 1043 |
type registerTestOpt interface { |
| 1044 |
isRegisterTestOpt() |
| 1045 |
} |
| 1046 |
|
| 1047 |
// rtSkipFunc is a registerTest option that runs a skip check function before |
| 1048 |
// running the test. |
| 1049 |
type rtSkipFunc struct { |
| 1050 |
skip func(*distTest) (string, bool) // Return message, true to skip the test |
| 1051 |
} |
| 1052 |
|
| 1053 |
func (rtSkipFunc) isRegisterTestOpt() {} |
| 1054 |
|
| 1055 |
// registerTest registers a test that runs the given goTest. |
| 1056 |
// |
| 1057 |
// Each Go package in goTest will have a corresponding test |
| 1058 |
// "<pkg>:<variant>", which must uniquely identify the test. |
| 1059 |
// |
| 1060 |
// heading and test.variant must be non-empty. |
| 1061 |
func (t *tester) registerTest(heading string, test *goTest, opts ...registerTestOpt) { |
| 1062 |
var skipFunc func(*distTest) (string, bool) |
| 1063 |
for _, opt := range opts { |
| 1064 |
switch opt := opt.(type) { |
| 1065 |
case rtSkipFunc: |
| 1066 |
skipFunc = opt.skip |
| 1067 |
} |
| 1068 |
} |
| 1069 |
// Register each test package as a separate test. |
| 1070 |
register1 := func(test *goTest) { |
| 1071 |
if test.variant == "" { |
| 1072 |
panic("empty variant") |
| 1073 |
} |
| 1074 |
name := testName(test.pkg, test.variant) |
| 1075 |
t.addTest(name, heading, func(dt *distTest) error { |
| 1076 |
if skipFunc != nil { |
| 1077 |
msg, skip := skipFunc(dt) |
| 1078 |
if skip { |
| 1079 |
test.printSkip(t, msg) |
| 1080 |
return nil |
| 1081 |
} |
| 1082 |
} |
| 1083 |
w := &work{dt: dt} |
| 1084 |
w.cmd, w.flush = test.bgCommand(t, &w.out, &w.out) |
| 1085 |
t.worklist = append(t.worklist, w) |
| 1086 |
return nil |
| 1087 |
}) |
| 1088 |
} |
| 1089 |
if test.pkg != "" && len(test.pkgs) == 0 { |
| 1090 |
// Common case. Avoid copying. |
| 1091 |
register1(test) |
| 1092 |
return |
| 1093 |
} |
| 1094 |
// TODO(dmitshur,austin): It might be better to unify the execution of 'go test pkg' |
| 1095 |
// invocations for the same variant to be done with a single 'go test pkg1 pkg2 pkg3' |
| 1096 |
// command, just like it's already done in registerStdTest and registerRaceBenchTest. |
| 1097 |
// Those methods accumulate matched packages in stdMatches and benchMatches slices, |
| 1098 |
// and we can extend that mechanism to work for all other equal variant registrations. |
| 1099 |
// Do the simple thing to start with. |
| 1100 |
for _, pkg := range test.packages() { |
| 1101 |
test1 := *test |
| 1102 |
test1.pkg, test1.pkgs = pkg, nil |
| 1103 |
register1(&test1) |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
// dirCmd constructs a Cmd intended to be run in the foreground. |
| 1108 |
// The command will be run in dir, and Stdout and Stderr will go to os.Stdout |
| 1109 |
// and os.Stderr. |
| 1110 |
func (t *tester) dirCmd(dir string, cmdline ...interface{}) *exec.Cmd { |
| 1111 |
bin, args := flattenCmdline(cmdline) |
| 1112 |
cmd := exec.Command(bin, args...) |
| 1113 |
if filepath.IsAbs(dir) { |
| 1114 |
setDir(cmd, dir) |
| 1115 |
} else { |
| 1116 |
setDir(cmd, filepath.Join(goroot, dir)) |
| 1117 |
} |
| 1118 |
cmd.Stdout = os.Stdout |
| 1119 |
cmd.Stderr = os.Stderr |
| 1120 |
if vflag > 1 { |
| 1121 |
errprintf("%#q\n", cmd) |
| 1122 |
} |
| 1123 |
return cmd |
| 1124 |
} |
| 1125 |
|
| 1126 |
// flattenCmdline flattens a mixture of string and []string as single list |
| 1127 |
// and then interprets it as a command line: first element is binary, then args. |
| 1128 |
func flattenCmdline(cmdline []interface{}) (bin string, args []string) { |
| 1129 |
var list []string |
| 1130 |
for _, x := range cmdline { |
| 1131 |
switch x := x.(type) { |
| 1132 |
case string: |
| 1133 |
list = append(list, x) |
| 1134 |
case []string: |
| 1135 |
list = append(list, x...) |
| 1136 |
default: |
| 1137 |
panic("invalid dirCmd argument type: " + reflect.TypeOf(x).String()) |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
bin = list[0] |
| 1142 |
if !filepath.IsAbs(bin) { |
| 1143 |
panic("command is not absolute: " + bin) |
| 1144 |
} |
| 1145 |
return bin, list[1:] |
| 1146 |
} |
| 1147 |
|
| 1148 |
func (t *tester) iOS() bool { |
| 1149 |
return goos == "ios" |
| 1150 |
} |
| 1151 |
|
| 1152 |
func (t *tester) out(v string) { |
| 1153 |
if t.json { |
| 1154 |
return |
| 1155 |
} |
| 1156 |
if t.banner == "" { |
| 1157 |
return |
| 1158 |
} |
| 1159 |
fmt.Println("\n" + t.banner + v) |
| 1160 |
} |
| 1161 |
|
| 1162 |
// extLink reports whether the current goos/goarch supports |
| 1163 |
// external linking. |
| 1164 |
func (t *tester) extLink() bool { |
| 1165 |
if !cgoEnabled[goos+"/"+goarch] { |
| 1166 |
return false |
| 1167 |
} |
| 1168 |
if goarch == "ppc64" && goos != "aix" { |
| 1169 |
return false |
| 1170 |
} |
| 1171 |
return true |
| 1172 |
} |
| 1173 |
|
| 1174 |
func (t *tester) internalLink() bool { |
| 1175 |
if gohostos == "dragonfly" { |
| 1176 |
// linkmode=internal fails on dragonfly since errno is a TLS relocation. |
| 1177 |
return false |
| 1178 |
} |
| 1179 |
if goos == "android" { |
| 1180 |
return false |
| 1181 |
} |
| 1182 |
if goos == "ios" { |
| 1183 |
return false |
| 1184 |
} |
| 1185 |
if goos == "windows" && goarch == "arm64" { |
| 1186 |
return false |
| 1187 |
} |
| 1188 |
// Internally linking cgo is incomplete on some architectures. |
| 1189 |
// https://golang.org/issue/10373 |
| 1190 |
// https://golang.org/issue/14449 |
| 1191 |
if goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" || goarch == "riscv64" { |
| 1192 |
return false |
| 1193 |
} |
| 1194 |
if goos == "aix" { |
| 1195 |
// linkmode=internal isn't supported. |
| 1196 |
return false |
| 1197 |
} |
| 1198 |
if t.msan || t.asan { |
| 1199 |
// linkmode=internal isn't supported by msan or asan. |
| 1200 |
return false |
| 1201 |
} |
| 1202 |
return true |
| 1203 |
} |
| 1204 |
|
| 1205 |
func (t *tester) internalLinkPIE() bool { |
| 1206 |
if t.msan || t.asan { |
| 1207 |
// linkmode=internal isn't supported by msan or asan. |
| 1208 |
return false |
| 1209 |
} |
| 1210 |
switch goos + "-" + goarch { |
| 1211 |
case "darwin-amd64", "darwin-arm64", |
| 1212 |
"linux-amd64", "linux-arm64", "linux-loong64", "linux-ppc64le", |
| 1213 |
"android-arm64", |
| 1214 |
"windows-amd64", "windows-386", "windows-arm": |
| 1215 |
return true |
| 1216 |
} |
| 1217 |
return false |
| 1218 |
} |
| 1219 |
|
| 1220 |
func (t *tester) externalLinkPIE() bool { |
| 1221 |
// General rule is if -buildmode=pie and -linkmode=external both work, then they work together. |
| 1222 |
// Handle exceptions and then fall back to the general rule. |
| 1223 |
switch goos + "-" + goarch { |
| 1224 |
case "linux-s390x": |
| 1225 |
return true |
| 1226 |
} |
| 1227 |
return t.internalLinkPIE() && t.extLink() |
| 1228 |
} |
| 1229 |
|
| 1230 |
// supportedBuildMode reports whether the given build mode is supported. |
| 1231 |
func (t *tester) supportedBuildmode(mode string) bool { |
| 1232 |
switch mode { |
| 1233 |
case "c-archive", "c-shared", "shared", "plugin", "pie": |
| 1234 |
default: |
| 1235 |
fatalf("internal error: unknown buildmode %s", mode) |
| 1236 |
return false |
| 1237 |
} |
| 1238 |
|
| 1239 |
return buildModeSupported("gc", mode, goos, goarch) |
| 1240 |
} |
| 1241 |
|
| 1242 |
func (t *tester) registerCgoTests(heading string) { |
| 1243 |
cgoTest := func(variant string, subdir, linkmode, buildmode string, opts ...registerTestOpt) *goTest { |
| 1244 |
gt := &goTest{ |
| 1245 |
variant: variant, |
| 1246 |
pkg: "cmd/cgo/internal/" + subdir, |
| 1247 |
buildmode: buildmode, |
| 1248 |
} |
| 1249 |
var ldflags []string |
| 1250 |
if linkmode != "auto" { |
| 1251 |
// "auto" is the default, so avoid cluttering the command line for "auto" |
| 1252 |
ldflags = append(ldflags, "-linkmode="+linkmode) |
| 1253 |
} |
| 1254 |
|
| 1255 |
if linkmode == "internal" { |
| 1256 |
gt.tags = append(gt.tags, "internal") |
| 1257 |
if buildmode == "pie" { |
| 1258 |
gt.tags = append(gt.tags, "internal_pie") |
| 1259 |
} |
| 1260 |
} |
| 1261 |
if buildmode == "static" { |
| 1262 |
// This isn't actually a Go buildmode, just a convenient way to tell |
| 1263 |
// cgoTest we want static linking. |
| 1264 |
gt.buildmode = "" |
| 1265 |
if linkmode == "external" { |
| 1266 |
ldflags = append(ldflags, `-extldflags "-static -pthread"`) |
| 1267 |
} else if linkmode == "auto" { |
| 1268 |
gt.env = append(gt.env, "CGO_LDFLAGS=-static -pthread") |
| 1269 |
} else { |
| 1270 |
panic("unknown linkmode with static build: " + linkmode) |
| 1271 |
} |
| 1272 |
gt.tags = append(gt.tags, "static") |
| 1273 |
} |
| 1274 |
gt.ldflags = strings.Join(ldflags, " ") |
| 1275 |
|
| 1276 |
t.registerTest(heading, gt, opts...) |
| 1277 |
return gt |
| 1278 |
} |
| 1279 |
|
| 1280 |
// test, testtls, and testnocgo are run with linkmode="auto", buildmode="" |
| 1281 |
// as part of go test cmd. Here we only have to register the non-default |
| 1282 |
// build modes of these tests. |
| 1283 |
|
| 1284 |
// Stub out various buildmode=pie tests on alpine until 54354 resolved. |
| 1285 |
builderName := os.Getenv("GO_BUILDER_NAME") |
| 1286 |
disablePIE := strings.HasSuffix(builderName, "-alpine") |
| 1287 |
|
| 1288 |
if t.internalLink() { |
| 1289 |
cgoTest("internal", "test", "internal", "") |
| 1290 |
} |
| 1291 |
|
| 1292 |
os := gohostos |
| 1293 |
p := gohostos + "/" + goarch |
| 1294 |
switch { |
| 1295 |
case os == "darwin", os == "windows": |
| 1296 |
if !t.extLink() { |
| 1297 |
break |
| 1298 |
} |
| 1299 |
// test linkmode=external, but __thread not supported, so skip testtls. |
| 1300 |
cgoTest("external", "test", "external", "") |
| 1301 |
|
| 1302 |
gt := cgoTest("external-s", "test", "external", "") |
| 1303 |
gt.ldflags += " -s" |
| 1304 |
|
| 1305 |
if t.supportedBuildmode("pie") && !disablePIE { |
| 1306 |
cgoTest("auto-pie", "test", "auto", "pie") |
| 1307 |
if t.internalLink() && t.internalLinkPIE() { |
| 1308 |
cgoTest("internal-pie", "test", "internal", "pie") |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
case os == "aix", os == "android", os == "dragonfly", os == "freebsd", os == "linux", os == "netbsd", os == "openbsd": |
| 1313 |
gt := cgoTest("external-g0", "test", "external", "") |
| 1314 |
gt.env = append(gt.env, "CGO_CFLAGS=-g0 -fdiagnostics-color") |
| 1315 |
|
| 1316 |
cgoTest("external", "testtls", "external", "") |
| 1317 |
switch { |
| 1318 |
case os == "aix": |
| 1319 |
// no static linking |
| 1320 |
case p == "freebsd/arm": |
| 1321 |
// -fPIC compiled tls code will use __tls_get_addr instead |
| 1322 |
// of __aeabi_read_tp, however, on FreeBSD/ARM, __tls_get_addr |
| 1323 |
// is implemented in rtld-elf, so -fPIC isn't compatible with |
| 1324 |
// static linking on FreeBSD/ARM with clang. (cgo depends on |
| 1325 |
// -fPIC fundamentally.) |
| 1326 |
default: |
| 1327 |
// Check for static linking support |
| 1328 |
var staticCheck rtSkipFunc |
| 1329 |
ccName := compilerEnvLookup("CC", defaultcc, goos, goarch) |
| 1330 |
cc, err := exec.LookPath(ccName) |
| 1331 |
if err != nil { |
| 1332 |
staticCheck.skip = func(*distTest) (string, bool) { |
| 1333 |
return fmt.Sprintf("$CC (%q) not found, skip cgo static linking test.", ccName), true |
| 1334 |
} |
| 1335 |
} else { |
| 1336 |
cmd := t.dirCmd("src/cmd/cgo/internal/test", cc, "-xc", "-o", "/dev/null", "-static", "-") |
| 1337 |
cmd.Stdin = strings.NewReader("int main() {}") |
| 1338 |
cmd.Stdout, cmd.Stderr = nil, nil // Discard output |
| 1339 |
if err := cmd.Run(); err != nil { |
| 1340 |
// Skip these tests |
| 1341 |
staticCheck.skip = func(*distTest) (string, bool) { |
| 1342 |
return "No support for static linking found (lacks libc.a?), skip cgo static linking test.", true |
| 1343 |
} |
| 1344 |
} |
| 1345 |
} |
| 1346 |
|
| 1347 |
// Doing a static link with boringcrypto gets |
| 1348 |
// a C linker warning on Linux. |
| 1349 |
// in function `bio_ip_and_port_to_socket_and_addr': |
| 1350 |
// warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking |
| 1351 |
if staticCheck.skip == nil && goos == "linux" && strings.Contains(goexperiment, "boringcrypto") { |
| 1352 |
staticCheck.skip = func(*distTest) (string, bool) { |
| 1353 |
return "skipping static linking check on Linux when using boringcrypto to avoid C linker warning about getaddrinfo", true |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
// Static linking tests |
| 1358 |
if goos != "android" && p != "netbsd/arm" && !t.msan && !t.asan { |
| 1359 |
// TODO(#56629): Why does this fail on netbsd-arm? |
| 1360 |
// TODO(#70080): Why does this fail with msan? |
| 1361 |
// asan doesn't support static linking (this is an explicit build error on the C side). |
| 1362 |
cgoTest("static", "testtls", "external", "static", staticCheck) |
| 1363 |
} |
| 1364 |
cgoTest("external", "testnocgo", "external", "", staticCheck) |
| 1365 |
if goos != "android" && !t.msan && !t.asan { |
| 1366 |
// TODO(#70080): Why does this fail with msan? |
| 1367 |
// asan doesn't support static linking (this is an explicit build error on the C side). |
| 1368 |
cgoTest("static", "testnocgo", "external", "static", staticCheck) |
| 1369 |
cgoTest("static", "test", "external", "static", staticCheck) |
| 1370 |
// -static in CGO_LDFLAGS triggers a different code path |
| 1371 |
// than -static in -extldflags, so test both. |
| 1372 |
// See issue #16651. |
| 1373 |
if goarch != "loong64" && !t.msan && !t.asan { |
| 1374 |
// TODO(#56623): Why does this fail on loong64? |
| 1375 |
cgoTest("auto-static", "test", "auto", "static", staticCheck) |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|
| 1379 |
// PIE linking tests |
| 1380 |
if t.supportedBuildmode("pie") && !disablePIE { |
| 1381 |
cgoTest("auto-pie", "test", "auto", "pie") |
| 1382 |
if t.internalLink() && t.internalLinkPIE() { |
| 1383 |
cgoTest("internal-pie", "test", "internal", "pie") |
| 1384 |
} |
| 1385 |
cgoTest("auto-pie", "testtls", "auto", "pie") |
| 1386 |
cgoTest("auto-pie", "testnocgo", "auto", "pie") |
| 1387 |
} |
| 1388 |
} |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|
| 1392 |
// runPending runs pending test commands, in parallel, emitting headers as appropriate. |
| 1393 |
// When finished, it emits header for nextTest, which is going to run after the |
| 1394 |
// pending commands are done (and runPending returns). |
| 1395 |
// A test should call runPending if it wants to make sure that it is not |
| 1396 |
// running in parallel with earlier tests, or if it has some other reason |
| 1397 |
// for needing the earlier tests to be done. |
| 1398 |
func (t *tester) runPending(nextTest *distTest) { |
| 1399 |
worklist := t.worklist |
| 1400 |
t.worklist = nil |
| 1401 |
for _, w := range worklist { |
| 1402 |
w.start = make(chan bool) |
| 1403 |
w.end = make(chan struct{}) |
| 1404 |
// w.cmd must be set up to write to w.out. We can't check that, but we |
| 1405 |
// can check for easy mistakes. |
| 1406 |
if w.cmd.Stdout == nil || w.cmd.Stdout == os.Stdout || w.cmd.Stderr == nil || w.cmd.Stderr == os.Stderr { |
| 1407 |
panic("work.cmd.Stdout/Stderr must be redirected") |
| 1408 |
} |
| 1409 |
go func(w *work) { |
| 1410 |
if !<-w.start { |
| 1411 |
timelog("skip", w.dt.name) |
| 1412 |
w.printSkip(t, "skipped due to earlier error") |
| 1413 |
} else { |
| 1414 |
timelog("start", w.dt.name) |
| 1415 |
w.err = w.cmd.Run() |
| 1416 |
if w.flush != nil { |
| 1417 |
w.flush() |
| 1418 |
} |
| 1419 |
if w.err != nil { |
| 1420 |
if isUnsupportedVMASize(w) { |
| 1421 |
timelog("skip", w.dt.name) |
| 1422 |
w.out.Reset() |
| 1423 |
w.printSkip(t, "skipped due to unsupported VMA") |
| 1424 |
w.err = nil |
| 1425 |
} |
| 1426 |
} |
| 1427 |
} |
| 1428 |
timelog("end", w.dt.name) |
| 1429 |
w.end <- struct{}{} |
| 1430 |
}(w) |
| 1431 |
} |
| 1432 |
|
| 1433 |
maxbg := maxbg |
| 1434 |
// for runtime.NumCPU() < 4 || runtime.GOMAXPROCS(0) == 1, do not change maxbg. |
| 1435 |
// Because there is not enough CPU to parallel the testing of multiple packages. |
| 1436 |
if runtime.NumCPU() > 4 && runtime.GOMAXPROCS(0) != 1 { |
| 1437 |
for _, w := range worklist { |
| 1438 |
// See go.dev/issue/65164 |
| 1439 |
// because GOMAXPROCS=2 runtime CPU usage is low, |
| 1440 |
// so increase maxbg to avoid slowing down execution with low CPU usage. |
| 1441 |
// This makes testing a single package slower, |
| 1442 |
// but testing multiple packages together faster. |
| 1443 |
if strings.Contains(w.dt.heading, "GOMAXPROCS=2 runtime") { |
| 1444 |
maxbg = runtime.NumCPU() |
| 1445 |
break |
| 1446 |
} |
| 1447 |
} |
| 1448 |
} |
| 1449 |
|
| 1450 |
started := 0 |
| 1451 |
ended := 0 |
| 1452 |
var last *distTest |
| 1453 |
for ended < len(worklist) { |
| 1454 |
for started < len(worklist) && started-ended < maxbg { |
| 1455 |
w := worklist[started] |
| 1456 |
started++ |
| 1457 |
w.start <- !t.failed || t.keepGoing |
| 1458 |
} |
| 1459 |
w := worklist[ended] |
| 1460 |
dt := w.dt |
| 1461 |
if t.lastHeading != dt.heading { |
| 1462 |
t.lastHeading = dt.heading |
| 1463 |
t.out(dt.heading) |
| 1464 |
} |
| 1465 |
if dt != last { |
| 1466 |
// Assumes all the entries for a single dt are in one worklist. |
| 1467 |
last = w.dt |
| 1468 |
if vflag > 0 { |
| 1469 |
fmt.Printf("# go tool dist test -run=^%s$\n", dt.name) |
| 1470 |
} |
| 1471 |
} |
| 1472 |
if vflag > 1 { |
| 1473 |
errprintf("%#q\n", w.cmd) |
| 1474 |
} |
| 1475 |
ended++ |
| 1476 |
<-w.end |
| 1477 |
os.Stdout.Write(w.out.Bytes()) |
| 1478 |
// We no longer need the output, so drop the buffer. |
| 1479 |
w.out = bytes.Buffer{} |
| 1480 |
if w.err != nil { |
| 1481 |
log.Printf("Failed: %v", w.err) |
| 1482 |
t.failed = true |
| 1483 |
} |
| 1484 |
} |
| 1485 |
if t.failed && !t.keepGoing { |
| 1486 |
fatalf("FAILED") |
| 1487 |
} |
| 1488 |
|
| 1489 |
if dt := nextTest; dt != nil { |
| 1490 |
if t.lastHeading != dt.heading { |
| 1491 |
t.lastHeading = dt.heading |
| 1492 |
t.out(dt.heading) |
| 1493 |
} |
| 1494 |
if vflag > 0 { |
| 1495 |
fmt.Printf("# go tool dist test -run=^%s$\n", dt.name) |
| 1496 |
} |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
func (t *tester) hasBash() bool { |
| 1501 |
switch gohostos { |
| 1502 |
case "windows", "plan9": |
| 1503 |
return false |
| 1504 |
} |
| 1505 |
return true |
| 1506 |
} |
| 1507 |
|
| 1508 |
// hasParallelism is a copy of the function |
| 1509 |
// internal/testenv.HasParallelism, which can't be used here |
| 1510 |
// because cmd/dist can not import internal packages during bootstrap. |
| 1511 |
func (t *tester) hasParallelism() bool { |
| 1512 |
switch goos { |
| 1513 |
case "js", "wasip1": |
| 1514 |
return false |
| 1515 |
} |
| 1516 |
return true |
| 1517 |
} |
| 1518 |
|
| 1519 |
func (t *tester) raceDetectorSupported() bool { |
| 1520 |
if gohostos != goos { |
| 1521 |
return false |
| 1522 |
} |
| 1523 |
if !t.cgoEnabled { |
| 1524 |
return false |
| 1525 |
} |
| 1526 |
if !raceDetectorSupported(goos, goarch) { |
| 1527 |
return false |
| 1528 |
} |
| 1529 |
// The race detector doesn't work on Alpine Linux: |
| 1530 |
// golang.org/issue/14481 |
| 1531 |
if isAlpineLinux() { |
| 1532 |
return false |
| 1533 |
} |
| 1534 |
// NetBSD support is unfinished. |
| 1535 |
// golang.org/issue/26403 |
| 1536 |
if goos == "netbsd" { |
| 1537 |
return false |
| 1538 |
} |
| 1539 |
return true |
| 1540 |
} |
| 1541 |
|
| 1542 |
func isAlpineLinux() bool { |
| 1543 |
if runtime.GOOS != "linux" { |
| 1544 |
return false |
| 1545 |
} |
| 1546 |
fi, err := os.Lstat("/etc/alpine-release") |
| 1547 |
return err == nil && fi.Mode().IsRegular() |
| 1548 |
} |
| 1549 |
|
| 1550 |
func (t *tester) registerRaceTests() { |
| 1551 |
hdr := "Testing race detector" |
| 1552 |
t.registerTest(hdr, |
| 1553 |
&goTest{ |
| 1554 |
variant: "race", |
| 1555 |
race: true, |
| 1556 |
runTests: "Output", |
| 1557 |
pkg: "runtime/race", |
| 1558 |
}) |
| 1559 |
t.registerTest(hdr, |
| 1560 |
&goTest{ |
| 1561 |
variant: "race", |
| 1562 |
race: true, |
| 1563 |
runTests: "TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace|TestFdRace|TestFdReadRace|TestFileCloseRace", |
| 1564 |
pkgs: []string{"flag", "net", "os", "os/exec", "encoding/gob"}, |
| 1565 |
}) |
| 1566 |
// We don't want the following line, because it |
| 1567 |
// slows down all.bash (by 10 seconds on my laptop). |
| 1568 |
// The race builder should catch any error here, but doesn't. |
| 1569 |
// TODO(iant): Figure out how to catch this. |
| 1570 |
// t.registerTest(hdr, &goTest{variant: "race", race: true, runTests: "TestParallelTest", pkg: "cmd/go"}) |
| 1571 |
if t.cgoEnabled { |
| 1572 |
// Building cmd/cgo/internal/test takes a long time. |
| 1573 |
// There are already cgo-enabled packages being tested with the race detector. |
| 1574 |
// We shouldn't need to redo all of cmd/cgo/internal/test too. |
| 1575 |
// The race builder will take care of this. |
| 1576 |
// t.registerTest(hdr, &goTest{variant: "race", race: true, env: []string{"GOTRACEBACK=2"}, pkg: "cmd/cgo/internal/test"}) |
| 1577 |
} |
| 1578 |
if t.extLink() { |
| 1579 |
// Test with external linking; see issue 9133. |
| 1580 |
t.registerTest(hdr, |
| 1581 |
&goTest{ |
| 1582 |
variant: "race-external", |
| 1583 |
race: true, |
| 1584 |
ldflags: "-linkmode=external", |
| 1585 |
runTests: "TestParse|TestEcho|TestStdinCloseRace", |
| 1586 |
pkgs: []string{"flag", "os/exec"}, |
| 1587 |
}) |
| 1588 |
} |
| 1589 |
} |
| 1590 |
|
| 1591 |
// cgoPackages is the standard packages that use cgo. |
| 1592 |
var cgoPackages = []string{ |
| 1593 |
"net", |
| 1594 |
"os/user", |
| 1595 |
} |
| 1596 |
|
| 1597 |
var funcBenchmark = []byte("\nfunc Benchmark") |
| 1598 |
|
| 1599 |
// packageHasBenchmarks reports whether pkg has benchmarks. |
| 1600 |
// On any error, it conservatively returns true. |
| 1601 |
// |
| 1602 |
// This exists just to eliminate work on the builders, since compiling |
| 1603 |
// a test in race mode just to discover it has no benchmarks costs a |
| 1604 |
// second or two per package, and this function returns false for |
| 1605 |
// about 100 packages. |
| 1606 |
func (t *tester) packageHasBenchmarks(pkg string) bool { |
| 1607 |
pkgDir := filepath.Join(goroot, "src", pkg) |
| 1608 |
d, err := os.Open(pkgDir) |
| 1609 |
if err != nil { |
| 1610 |
return true // conservatively |
| 1611 |
} |
| 1612 |
defer d.Close() |
| 1613 |
names, err := d.Readdirnames(-1) |
| 1614 |
if err != nil { |
| 1615 |
return true // conservatively |
| 1616 |
} |
| 1617 |
for _, name := range names { |
| 1618 |
if !strings.HasSuffix(name, "_test.go") { |
| 1619 |
continue |
| 1620 |
} |
| 1621 |
slurp, err := os.ReadFile(filepath.Join(pkgDir, name)) |
| 1622 |
if err != nil { |
| 1623 |
return true // conservatively |
| 1624 |
} |
| 1625 |
if bytes.Contains(slurp, funcBenchmark) { |
| 1626 |
return true |
| 1627 |
} |
| 1628 |
} |
| 1629 |
return false |
| 1630 |
} |
| 1631 |
|
| 1632 |
// makeGOROOTUnwritable makes all $GOROOT files & directories non-writable to |
| 1633 |
// check that no tests accidentally write to $GOROOT. |
| 1634 |
func (t *tester) makeGOROOTUnwritable() (undo func()) { |
| 1635 |
dir := os.Getenv("GOROOT") |
| 1636 |
if dir == "" { |
| 1637 |
panic("GOROOT not set") |
| 1638 |
} |
| 1639 |
|
| 1640 |
type pathMode struct { |
| 1641 |
path string |
| 1642 |
mode os.FileMode |
| 1643 |
} |
| 1644 |
var dirs []pathMode // in lexical order |
| 1645 |
|
| 1646 |
undo = func() { |
| 1647 |
for i := range dirs { |
| 1648 |
os.Chmod(dirs[i].path, dirs[i].mode) // best effort |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 1653 |
if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" { |
| 1654 |
if suffix == ".git" { |
| 1655 |
// Leave Git metadata in whatever state it was in. It may contain a lot |
| 1656 |
// of files, and it is highly unlikely that a test will try to modify |
| 1657 |
// anything within that directory. |
| 1658 |
return filepath.SkipDir |
| 1659 |
} |
| 1660 |
} |
| 1661 |
if err != nil { |
| 1662 |
return nil |
| 1663 |
} |
| 1664 |
|
| 1665 |
info, err := d.Info() |
| 1666 |
if err != nil { |
| 1667 |
return nil |
| 1668 |
} |
| 1669 |
|
| 1670 |
mode := info.Mode() |
| 1671 |
if mode&0222 != 0 && (mode.IsDir() || mode.IsRegular()) { |
| 1672 |
dirs = append(dirs, pathMode{path, mode}) |
| 1673 |
} |
| 1674 |
return nil |
| 1675 |
}) |
| 1676 |
|
| 1677 |
// Run over list backward to chmod children before parents. |
| 1678 |
for i := len(dirs) - 1; i >= 0; i-- { |
| 1679 |
err := os.Chmod(dirs[i].path, dirs[i].mode&^0222) |
| 1680 |
if err != nil { |
| 1681 |
dirs = dirs[i:] // Only undo what we did so far. |
| 1682 |
undo() |
| 1683 |
fatalf("failed to make GOROOT read-only: %v", err) |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
return undo |
| 1688 |
} |
| 1689 |
|
| 1690 |
// raceDetectorSupported is a copy of the function |
| 1691 |
// internal/platform.RaceDetectorSupported, which can't be used here |
| 1692 |
// because cmd/dist can not import internal packages during bootstrap. |
| 1693 |
// The race detector only supports 48-bit VMA on arm64. But we don't have |
| 1694 |
// a good solution to check VMA size (see https://go.dev/issue/29948). |
| 1695 |
// raceDetectorSupported will always return true for arm64. But race |
| 1696 |
// detector tests may abort on non 48-bit VMA configuration, the tests |
| 1697 |
// will be marked as "skipped" in this case. |
| 1698 |
func raceDetectorSupported(goos, goarch string) bool { |
| 1699 |
switch goos { |
| 1700 |
case "linux": |
| 1701 |
return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" || goarch == "s390x" || goarch == "loong64" |
| 1702 |
case "darwin": |
| 1703 |
return goarch == "amd64" || goarch == "arm64" |
| 1704 |
case "freebsd", "netbsd", "windows": |
| 1705 |
return goarch == "amd64" |
| 1706 |
default: |
| 1707 |
return false |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
// buildModeSupported is a copy of the function |
| 1712 |
// internal/platform.BuildModeSupported, which can't be used here |
| 1713 |
// because cmd/dist can not import internal packages during bootstrap. |
| 1714 |
func buildModeSupported(compiler, buildmode, goos, goarch string) bool { |
| 1715 |
if compiler == "gccgo" { |
| 1716 |
return true |
| 1717 |
} |
| 1718 |
|
| 1719 |
platform := goos + "/" + goarch |
| 1720 |
|
| 1721 |
switch buildmode { |
| 1722 |
case "archive": |
| 1723 |
return true |
| 1724 |
|
| 1725 |
case "c-archive": |
| 1726 |
switch goos { |
| 1727 |
case "aix", "darwin", "ios", "windows": |
| 1728 |
return true |
| 1729 |
case "linux": |
| 1730 |
switch goarch { |
| 1731 |
case "386", "amd64", "arm", "armbe", "arm64", "arm64be", "loong64", "ppc64le", "riscv64", "s390x": |
| 1732 |
// linux/ppc64 not supported because it does |
| 1733 |
// not support external linking mode yet. |
| 1734 |
return true |
| 1735 |
default: |
| 1736 |
// Other targets do not support -shared, |
| 1737 |
// per ParseFlags in |
| 1738 |
// cmd/compile/internal/base/flag.go. |
| 1739 |
// For c-archive the Go tool passes -shared, |
| 1740 |
// so that the result is suitable for inclusion |
| 1741 |
// in a PIE or shared library. |
| 1742 |
return false |
| 1743 |
} |
| 1744 |
case "freebsd": |
| 1745 |
return goarch == "amd64" |
| 1746 |
} |
| 1747 |
return false |
| 1748 |
|
| 1749 |
case "c-shared": |
| 1750 |
switch platform { |
| 1751 |
case "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/386", "linux/ppc64le", "linux/riscv64", "linux/s390x", |
| 1752 |
"android/amd64", "android/arm", "android/arm64", "android/386", |
| 1753 |
"freebsd/amd64", |
| 1754 |
"darwin/amd64", "darwin/arm64", |
| 1755 |
"windows/amd64", "windows/386", "windows/arm64", |
| 1756 |
"wasip1/wasm": |
| 1757 |
return true |
| 1758 |
} |
| 1759 |
return false |
| 1760 |
|
| 1761 |
case "default": |
| 1762 |
return true |
| 1763 |
|
| 1764 |
case "exe": |
| 1765 |
return true |
| 1766 |
|
| 1767 |
case "pie": |
| 1768 |
switch platform { |
| 1769 |
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/ppc64le", "linux/riscv64", "linux/s390x", |
| 1770 |
"android/amd64", "android/arm", "android/arm64", "android/386", |
| 1771 |
"freebsd/amd64", |
| 1772 |
"darwin/amd64", "darwin/arm64", |
| 1773 |
"ios/amd64", "ios/arm64", |
| 1774 |
"aix/ppc64", |
| 1775 |
"openbsd/arm64", |
| 1776 |
"windows/386", "windows/amd64", "windows/arm", "windows/arm64": |
| 1777 |
return true |
| 1778 |
} |
| 1779 |
return false |
| 1780 |
|
| 1781 |
case "shared": |
| 1782 |
switch platform { |
| 1783 |
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x": |
| 1784 |
return true |
| 1785 |
} |
| 1786 |
return false |
| 1787 |
|
| 1788 |
case "plugin": |
| 1789 |
switch platform { |
| 1790 |
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/loong64", "linux/riscv64", "linux/s390x", "linux/ppc64le", |
| 1791 |
"android/amd64", "android/386", |
| 1792 |
"darwin/amd64", "darwin/arm64", |
| 1793 |
"freebsd/amd64": |
| 1794 |
return true |
| 1795 |
} |
| 1796 |
return false |
| 1797 |
|
| 1798 |
default: |
| 1799 |
return false |
| 1800 |
} |
| 1801 |
} |
| 1802 |
|
| 1803 |
// isUnsupportedVMASize reports whether the failure is caused by an unsupported |
| 1804 |
// VMA for the race detector (for example, running the race detector on an |
| 1805 |
// arm64 machine configured with 39-bit VMA). |
| 1806 |
func isUnsupportedVMASize(w *work) bool { |
| 1807 |
unsupportedVMA := []byte("unsupported VMA range") |
| 1808 |
return strings.Contains(w.dt.name, ":race") && bytes.Contains(w.out.Bytes(), unsupportedVMA) |
| 1809 |
} |
| 1810 |
|
| 1811 |
// isEnvSet reports whether the environment variable evar is |
| 1812 |
// set in the environment. |
| 1813 |
func isEnvSet(evar string) bool { |
| 1814 |
evarEq := evar + "=" |
| 1815 |
for _, e := range os.Environ() { |
| 1816 |
if strings.HasPrefix(e, evarEq) { |
| 1817 |
return true |
| 1818 |
} |
| 1819 |
} |
| 1820 |
return false |
| 1821 |
} |
| 1822 |
|
| 1823 |
func (t *tester) fipsSupported() bool { |
| 1824 |
// Keep this in sync with [crypto/internal/fips140.Supported]. |
| 1825 |
|
| 1826 |
// Use GOFIPS140 or GOEXPERIMENT=boringcrypto, but not both. |
| 1827 |
if strings.Contains(goexperiment, "boringcrypto") { |
| 1828 |
return false |
| 1829 |
} |
| 1830 |
|
| 1831 |
// If this goos/goarch does not support FIPS at all, return no versions. |
| 1832 |
// The logic here matches crypto/internal/fips140/check.Supported for now. |
| 1833 |
// In the future, if some snapshots add support for these, we will have |
| 1834 |
// to make a decision on a per-version basis. |
| 1835 |
switch { |
| 1836 |
case goarch == "wasm", |
| 1837 |
goos == "windows" && goarch == "386", |
| 1838 |
goos == "windows" && goarch == "arm", |
| 1839 |
goos == "openbsd", |
| 1840 |
goos == "aix": |
| 1841 |
return false |
| 1842 |
} |
| 1843 |
|
| 1844 |
// For now, FIPS+ASAN doesn't need to work. |
| 1845 |
// If this is made to work, also re-enable the test in check_test.go. |
| 1846 |
if t.asan { |
| 1847 |
return false |
| 1848 |
} |
| 1849 |
|
| 1850 |
return true |
| 1851 |
} |
| 1852 |
|
| 1853 |
// fipsVersions returns the list of versions available in lib/fips140. |
| 1854 |
func fipsVersions(short bool) []string { |
| 1855 |
var versions []string |
| 1856 |
zips, err := filepath.Glob(filepath.Join(goroot, "lib/fips140/*.zip")) |
| 1857 |
if err != nil { |
| 1858 |
fatalf("%v", err) |
| 1859 |
} |
| 1860 |
for _, zip := range zips { |
| 1861 |
versions = append(versions, strings.TrimSuffix(filepath.Base(zip), ".zip")) |
| 1862 |
} |
| 1863 |
txts, err := filepath.Glob(filepath.Join(goroot, "lib/fips140/*.txt")) |
| 1864 |
if err != nil { |
| 1865 |
fatalf("%v", err) |
| 1866 |
} |
| 1867 |
for _, txt := range txts { |
| 1868 |
versions = append(versions, strings.TrimSuffix(filepath.Base(txt), ".txt")) |
| 1869 |
} |
| 1870 |
return versions |
| 1871 |
} |