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.
26 gogcflags = os.Getenv("GO_GCFLAGS")
32 flag.BoolVar(&t.listMode, "list", false, "list available tests")
33 flag.BoolVar(&t.rebuild, "rebuild", false, "rebuild everything first")
34 flag.BoolVar(&noRebuild, "no-rebuild", false, "overrides -rebuild (historical dreg)")
35 flag.BoolVar(&t.keepGoing, "k", false, "keep going even when error occurred")
36 flag.BoolVar(&t.race, "race", false, "run in race builder mode (different set of tests)")
37 flag.BoolVar(&t.compileOnly, "compile-only", false, "compile tests, but don't run them. This is for some builders. Not all dist tests respect this flag, but most do.")
38 flag.StringVar(&t.banner, "banner", "##### ", "banner prefix; blank means no section banners")
39 flag.StringVar(&t.runRxStr, "run", "",
40 "run only those tests matching the regular expression; empty means to run all. "+
41 "Special exception: if the string begins with '!', the match is inverted.")
42 flag.BoolVar(&t.msan, "msan", false, "run in memory sanitizer builder mode")
43 flag.BoolVar(&t.asan, "asan", false, "run in address sanitizer builder mode")
45 xflagparse(-1) // any number of args
53 // tester executes cmdtest.
62 compileOnly bool // just try to compile all tests, but no need to run
65 runRxWant bool // want runRx to match (true) or not match (false)
66 runNames []string // tests to run, exclusive with runRx; empty means all
67 banner string // prefix, or "" for none
68 lastHeading string // last dir heading printed
74 goExe string // For host tests
75 goTmpDir string // For host tests
92 // A distTest is a test run by dist test.
93 // Each test has a unique name and belongs to a group (heading)
94 type distTest struct {
95 name string // unique test name; may be filtered with -run flag
96 heading string // group section; this header is printed before the test is run.
97 fn func(*distTest) error
100 func (t *tester) run() {
101 timelog("start", "dist test")
103 os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH")))
106 if v := os.Getenv("GO_TEST_SHORT"); v != "" {
107 short, err := strconv.ParseBool(v)
109 fatalf("invalid GO_TEST_SHORT %q: %v", v, err)
114 cmd := exec.Command(gorootBinGo, "env", "CGO_ENABLED", "GOEXE", "GOTMPDIR")
115 cmd.Stderr = new(bytes.Buffer)
116 slurp, err := cmd.Output()
118 fatalf("Error running %s: %v\n%s", cmd, err, cmd.Stderr)
120 parts := strings.Split(string(slurp), "\n")
122 fatalf("Error running %s: output contains <3 lines\n%s", cmd, cmd.Stderr)
124 t.cgoEnabled, _ = strconv.ParseBool(parts[0])
126 t.goTmpDir = parts[2]
128 if flag.NArg() > 0 && t.runRxStr != "" {
129 fatalf("the -run regular expression flag is mutually exclusive with test name arguments")
132 t.runNames = flag.Args()
134 // Set GOTRACEBACK to system if the user didn't set a level explicitly.
135 // Since we're running tests for Go, we want as much detail as possible
136 // if something goes wrong.
138 // Set it before running any commands just in case something goes wrong.
139 if ok := isEnvSet("GOTRACEBACK"); !ok {
140 if err := os.Setenv("GOTRACEBACK", "system"); err != nil {
142 log.Printf("Failed to set GOTRACEBACK: %v", err)
144 fatalf("Failed to set GOTRACEBACK: %v", err)
150 t.out("Building packages and commands.")
151 // Force rebuild the whole toolchain.
152 goInstall(toolenv(), gorootBinGo, append([]string{"-a"}, toolchain...)...)
156 if builder := os.Getenv("GO_BUILDER_NAME"); builder == "" {
157 // Ensure that installed commands are up to date, even with -no-rebuild,
158 // so that tests that run commands end up testing what's actually on disk.
159 // If everything is up-to-date, this is a no-op.
160 // We first build the toolchain twice to allow it to converge,
161 // as when we first bootstrap.
162 // See cmdbootstrap for a description of the overall process.
164 // On the builders, we skip this step: we assume that 'dist test' is
165 // already using the result of a clean build, and because of test sharding
166 // and virtualization we usually start with a clean GOCACHE, so we would
167 // end up rebuilding large parts of the standard library that aren't
168 // otherwise relevant to the actual set of packages under test.
169 goInstall(toolenv(), gorootBinGo, toolchain...)
170 goInstall(toolenv(), gorootBinGo, toolchain...)
171 goInstall(toolenv(), gorootBinGo, "cmd")
176 if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
177 t.timeoutScale, err = strconv.Atoi(s)
179 fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
183 if t.runRxStr != "" {
184 if t.runRxStr[0] == '!' {
186 t.runRxStr = t.runRxStr[1:]
190 t.runRx = regexp.MustCompile(t.runRxStr)
195 for _, tt := range t.tests {
201 for _, name := range t.runNames {
202 if !t.isRegisteredTestName(name) {
203 fatalf("unknown test %q", name)
207 // On a few builders, make GOROOT unwritable to catch tests writing to it.
208 if strings.HasPrefix(os.Getenv("GO_BUILDER_NAME"), "linux-") {
209 if os.Getuid() == 0 {
210 // Don't bother making GOROOT unwritable:
211 // we're running as root, so permissions would have no effect.
213 xatexit(t.makeGOROOTUnwritable())
217 if err := t.maybeLogMetadata(); err != nil {
220 log.Printf("Failed logging metadata: %v", err)
222 fatalf("Failed logging metadata: %v", err)
226 for _, dt := range t.tests {
227 if !t.shouldRunTest(dt.name) {
231 dt := dt // dt used in background after this iteration
232 if err := dt.fn(&dt); err != nil {
233 t.runPending(&dt) // in case that hasn't been done yet
236 log.Printf("Failed: %v", err)
238 fatalf("Failed: %v", err)
243 timelog("end", "dist test")
246 fmt.Println("\nFAILED")
248 } else if t.partial {
249 fmt.Println("\nALL TESTS PASSED (some were excluded)")
251 fmt.Println("\nALL TESTS PASSED")
255 func (t *tester) shouldRunTest(name string) bool {
257 return t.runRx.MatchString(name) == t.runRxWant
259 if len(t.runNames) == 0 {
262 for _, runName := range t.runNames {
270 func (t *tester) maybeLogMetadata() error {
272 // We need to run a subprocess to log metadata. Don't do that
273 // on compile-only runs.
276 t.out("Test execution environment.")
277 // Helper binary to print system metadata (CPU model, etc). This is a
278 // separate binary from dist so it need not build with the bootstrap
281 // TODO(prattmic): If we split dist bootstrap and dist test then this
282 // could be simplified to directly use internal/sysinfo here.
283 return t.dirCmd(filepath.Join(goroot, "src/cmd/internal/metadata"), gorootBinGo, []string{"run", "main.go"}).Run()
286 // goTest represents all options to a "go test" command. The final command will
287 // combine configuration from goTest and tester flags.
289 timeout time.Duration // If non-zero, override timeout
290 short bool // If true, force -short
291 tags []string // Build tags
292 race bool // Force -race
293 bench bool // Run benchmarks (briefly), not tests.
294 runTests string // Regexp of tests to run
295 cpu string // If non-empty, -cpu flag
296 goroot string // If non-empty, use alternate goroot for go command
298 gcflags string // If non-empty, build with -gcflags=all=X
299 ldflags string // If non-empty, build with -ldflags=X
300 buildmode string // If non-empty, -buildmode flag
302 dir string // If non-empty, run in GOROOT/src-relative directory dir
303 env []string // Environment variables to add, as KEY=VAL. KEY= unsets a variable
305 // We have both pkg and pkgs as a convenience. Both may be set, in which
306 // case they will be combined. If both are empty, the default is ".".
307 pkgs []string // Multiple packages to test
308 pkg string // A single package to test
310 testFlags []string // Additional flags accepted by this test
313 // bgCommand returns a go test Cmd. The result has Stdout and Stderr set to nil
314 // and is intended to be added to the work queue.
315 func (opts *goTest) bgCommand(t *tester) *exec.Cmd {
316 goCmd, build, run, pkgs, setupCmd := opts.buildArgs(t)
318 // Combine the flags.
319 args := append([]string{"test"}, build...)
321 // We can't pass -c with multiple packages, so run the tests but
322 // tell them not to do anything.
323 args = append(args, "-run=^$")
325 args = append(args, run...)
327 args = append(args, pkgs...)
329 args = append(args, opts.testFlags...)
332 cmd := exec.Command(goCmd, args...)
338 // command returns a go test Cmd intended to be run immediately.
339 func (opts *goTest) command(t *tester) *exec.Cmd {
340 cmd := opts.bgCommand(t)
341 cmd.Stdout = os.Stdout
342 cmd.Stderr = os.Stderr
346 func (opts *goTest) run(t *tester) error {
347 return opts.command(t).Run()
350 // runHostTest runs a test that should be built and run on the host GOOS/GOARCH,
351 // but run with GOOS/GOARCH set to the target GOOS/GOARCH. This is for tests
352 // that do nothing but compile and run other binaries. If the host and target
353 // are different, then the assumption is that the target is running in an
354 // emulator and does not have a Go toolchain at all, so the test needs to run on
355 // the host, but its resulting binaries will be run through a go_exec wrapper
356 // that runs them on the target.
357 func (opts *goTest) runHostTest(t *tester) error {
358 goCmd, build, run, pkgs, setupCmd := opts.buildArgs(t)
360 // Build the host test binary
362 // We can't compile more than one package.
363 panic("host tests must have a single test package")
365 if len(opts.env) != 0 {
366 // It's not clear if these are for the host or the target.
367 panic("host tests must not have environment variables")
370 f, err := os.CreateTemp(t.goTmpDir, "test.test-*"+t.goExe)
372 fatalf("failed to create temporary file: %s", err)
376 xatexit(func() { os.Remove(bin) })
378 args := append([]string{"test", "-c", "-o", bin}, build...)
379 args = append(args, pkgs...)
380 cmd := exec.Command(goCmd, args...)
382 cmd.Stdout = os.Stdout
383 cmd.Stderr = os.Stderr
384 setEnv(cmd, "GOARCH", gohostarch)
385 setEnv(cmd, "GOOS", gohostos)
387 errprintf("%s\n", cmd)
389 if err := cmd.Run(); err != nil {
397 // Transform run flags to be passed directly to a test binary.
398 for i, f := range run {
399 if !strings.HasPrefix(f, "-") {
400 panic("run flag does not start with -: " + f)
402 run[i] = "-test." + f[1:]
406 args = append(run, opts.testFlags...)
407 cmd = exec.Command(bin, args...)
409 cmd.Stdout = os.Stdout
410 cmd.Stderr = os.Stderr
412 errprintf("%s\n", cmd)
417 // buildArgs is in internal helper for goTest that constructs the elements of
418 // the "go test" command line. goCmd is the path to the go command to use. build
419 // is the flags for building the test. run is the flags for running the test.
420 // pkgs is the list of packages to build and run.
422 // The caller is responsible for adding opts.testFlags, and must call setupCmd
423 // on the resulting exec.Cmd to set its directory and environment.
424 func (opts *goTest) buildArgs(t *tester) (goCmd string, build, run, pkgs []string, setupCmd func(*exec.Cmd)) {
426 if opts.goroot != "" {
427 goCmd = filepath.Join(opts.goroot, "bin", "go")
430 run = append(run, "-count=1") // Disallow caching
431 if opts.timeout != 0 {
432 d := opts.timeout * time.Duration(t.timeoutScale)
433 run = append(run, "-timeout="+d.String())
435 if opts.short || t.short {
436 run = append(run, "-short")
440 tags = append(tags, "lldb")
443 tags = append(tags, "noopt")
445 tags = append(tags, opts.tags...)
447 build = append(build, "-tags="+strings.Join(tags, ","))
449 if t.race || opts.race {
450 build = append(build, "-race")
453 build = append(build, "-msan")
456 build = append(build, "-asan")
460 run = append(run, "-run=^$")
461 // Run benchmarks as a smoke test
462 run = append(run, "-bench=.*", "-benchtime=.1s")
463 } else if opts.runTests != "" {
464 run = append(run, "-run="+opts.runTests)
467 run = append(run, "-cpu="+opts.cpu)
470 if opts.gcflags != "" {
471 build = append(build, "-gcflags=all="+opts.gcflags)
473 if opts.ldflags != "" {
474 build = append(build, "-ldflags="+opts.ldflags)
476 if opts.buildmode != "" {
477 build = append(build, "-buildmode="+opts.buildmode)
482 pkgs = append(pkgs[:len(pkgs):len(pkgs)], opts.pkg)
489 if opts.goroot != "" {
490 thisGoroot = opts.goroot
494 if filepath.IsAbs(opts.dir) {
495 panic("dir must be relative, got: " + opts.dir)
497 dir = filepath.Join(thisGoroot, "src", opts.dir)
499 dir = filepath.Join(thisGoroot, "src")
501 setupCmd = func(cmd *exec.Cmd) {
503 if len(opts.env) != 0 {
504 for _, kv := range opts.env {
505 if i := strings.Index(kv, "="); i < 0 {
506 unsetEnv(cmd, kv[:len(kv)-1])
508 setEnv(cmd, kv[:i], kv[i+1:])
517 // ranGoTest and stdMatches are state closed over by the stdlib
518 // testing func in registerStdTest below. The tests are run
519 // sequentially, so there's no need for locks.
521 // ranGoBench and benchMatches are the same, but are only used
528 benchMatches []string
531 func (t *tester) registerStdTest(pkg string) {
532 heading := "Testing packages."
533 testPrefix := "go_test:"
536 testName := testPrefix + pkg
537 if t.runRx == nil || t.runRx.MatchString(testName) == t.runRxWant {
538 stdMatches = append(stdMatches, pkg)
541 t.tests = append(t.tests, distTest{
544 fn: func(dt *distTest) error {
549 timelog("start", dt.name)
550 defer timelog("end", dt.name)
553 timeoutSec := 180 * time.Second
554 for _, pkg := range stdMatches {
569 func (t *tester) registerRaceBenchTest(pkg string) {
570 testName := "go_test_bench:" + pkg
571 if t.runRx == nil || t.runRx.MatchString(testName) == t.runRxWant {
572 benchMatches = append(benchMatches, pkg)
574 t.tests = append(t.tests, distTest{
576 heading: "Running benchmarks briefly.",
577 fn: func(dt *distTest) error {
582 timelog("start", dt.name)
583 defer timelog("end", dt.name)
586 timeout: 1200 * time.Second, // longer timeout for race with benchmarks
596 func (t *tester) registerTests() {
597 // Fast path to avoid the ~1 second of `go list std cmd` when
598 // the caller lists specific tests to run. (as the continuous
599 // build coordinator does).
600 if len(t.runNames) > 0 {
601 for _, name := range t.runNames {
602 if strings.HasPrefix(name, "go_test:") {
603 t.registerStdTest(strings.TrimPrefix(name, "go_test:"))
605 if strings.HasPrefix(name, "go_test_bench:") {
606 t.registerRaceBenchTest(strings.TrimPrefix(name, "go_test_bench:"))
610 // Use a format string to only list packages and commands that have tests.
611 const format = "{{if (or .TestGoFiles .XTestGoFiles)}}{{.ImportPath}}{{end}}"
612 cmd := exec.Command(gorootBinGo, "list", "-f", format)
614 cmd.Args = append(cmd.Args, "-tags=race")
616 cmd.Args = append(cmd.Args, "std", "cmd")
617 cmd.Stderr = new(bytes.Buffer)
618 all, err := cmd.Output()
620 fatalf("Error running go list std cmd: %v:\n%s", err, cmd.Stderr)
622 pkgs := strings.Fields(string(all))
623 for _, pkg := range pkgs {
624 t.registerStdTest(pkg)
627 for _, pkg := range pkgs {
628 if t.packageHasBenchmarks(pkg) {
629 t.registerRaceBenchTest(pkg)
639 // Test the os/user package in the pure-Go mode too.
641 t.registerTest("osusergo", "os/user with tag osusergo",
643 timeout: 300 * time.Second,
644 tags: []string{"osusergo"},
649 // Test ios/amd64 for the iOS simulator.
650 if goos == "darwin" && goarch == "amd64" && t.cgoEnabled {
651 t.registerTest("amd64ios", "GOOS=ios on darwin/amd64",
653 timeout: 300 * time.Second,
654 runTests: "SystemRoots",
655 env: []string{"GOOS=ios", "CGO_ENABLED=1"},
660 // Runtime CPU tests.
661 if !t.compileOnly && goos != "js" { // js can't handle -cpu != 1
662 t.registerTest("runtime:cpu124", "GOMAXPROCS=2 runtime -cpu=1,2,4 -quick",
664 timeout: 300 * time.Second,
667 testFlags: []string{"-quick"},
668 // We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code,
669 // creation of first goroutines and first garbage collections in the parallel setting.
670 env: []string{"GOMAXPROCS=2"},
675 // morestack tests. We only run these on in long-test mode
676 // (with GO_TEST_SHORT=false) because the runtime test is
677 // already quite long and mayMoreStackMove makes it about
679 if !t.compileOnly && !t.short {
680 // hooks is the set of maymorestack hooks to test with.
681 hooks := []string{"mayMoreStackPreempt", "mayMoreStackMove"}
682 // pkgs is the set of test packages to run.
683 pkgs := []string{"runtime", "reflect", "sync"}
684 // hookPkgs is the set of package patterns to apply
685 // the maymorestack hook to.
686 hookPkgs := []string{"runtime/...", "reflect", "sync"}
687 // unhookPkgs is the set of package patterns to
688 // exclude from hookPkgs.
689 unhookPkgs := []string{"runtime/testdata/..."}
690 for _, hook := range hooks {
691 // Construct the build flags to use the
692 // maymorestack hook in the compiler and
693 // assembler. We pass this via the GOFLAGS
694 // environment variable so that it applies to
695 // both the test itself and to binaries built
697 goFlagsList := []string{}
698 for _, flag := range []string{"-gcflags", "-asmflags"} {
699 for _, hookPkg := range hookPkgs {
700 goFlagsList = append(goFlagsList, flag+"="+hookPkg+"=-d=maymorestack=runtime."+hook)
702 for _, unhookPkg := range unhookPkgs {
703 goFlagsList = append(goFlagsList, flag+"="+unhookPkg+"=")
706 goFlags := strings.Join(goFlagsList, " ")
708 for _, pkg := range pkgs {
709 t.registerTest(hook+":"+pkg, "maymorestack="+hook,
711 timeout: 600 * time.Second,
713 env: []string{"GOFLAGS=" + goFlags},
720 // On the builders only, test that a moved GOROOT still works.
721 // Fails on iOS because CC_FOR_TARGET refers to clangwrap.sh
722 // in the unmoved GOROOT.
723 // Fails on Android and js/wasm with an exec format error.
724 // Fails on plan9 with "cannot find GOROOT" (issue #21016).
725 if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() && goos != "plan9" && goos != "js" {
726 t.tests = append(t.tests, distTest{
727 name: "moved_goroot",
728 heading: "moved GOROOT",
729 fn: func(dt *distTest) error {
731 timelog("start", dt.name)
732 defer timelog("end", dt.name)
733 moved := goroot + "-moved"
734 if err := os.Rename(goroot, moved); err != nil {
735 if goos == "windows" {
736 // Fails on Windows (with "Access is denied") if a process
737 // or binary is in this directory. For instance, using all.bat
738 // when run from c:\workdir\go\src fails here
739 // if GO_BUILDER_NAME is set. Our builders invoke tests
740 // a different way which happens to work when sharding
741 // tests, but we should be tolerant of the non-sharded
743 log.Printf("skipping test on Windows")
749 // Run `go test fmt` in the moved GOROOT, without explicitly setting
750 // GOROOT in the environment. The 'go' command should find itself.
755 unsetEnv(cmd, "GOROOT")
758 if rerr := os.Rename(moved, goroot); rerr != nil {
759 fatalf("failed to restore GOROOT: %v", rerr)
766 // Test that internal linking of standard packages does not
767 // require libgcc. This ensures that we can install a Go
768 // release on a system that does not have a C compiler
769 // installed and still build Go programs (that don't use cgo).
770 for _, pkg := range cgoPackages {
771 if !t.internalLink() {
775 // ARM libgcc may be Thumb, which internal linking does not support.
780 // What matters is that the tests build and start up.
781 // Skip expensive tests, especially x509 TestSystemRoots.
784 run = "TestTCPStress"
786 t.registerTest("nolibgcc:"+pkg, "Testing without libgcc.",
788 ldflags: "-linkmode=internal -libgcc=none",
794 // Stub out following test on alpine until 54354 resolved.
795 builderName := os.Getenv("GO_BUILDER_NAME")
796 disablePIE := strings.HasSuffix(builderName, "-alpine")
798 // Test internal linking of PIE binaries where it is supported.
799 if t.internalLinkPIE() && !disablePIE {
800 t.registerTest("pie_internal", "internal linking of -buildmode=pie",
802 timeout: 60 * time.Second,
804 ldflags: "-linkmode=internal",
805 env: []string{"CGO_ENABLED=0"},
808 // Also test a cgo package.
809 if t.cgoEnabled && t.internalLink() && !disablePIE {
810 t.registerTest("pie_internal_cgo", "internal linking of -buildmode=pie",
812 timeout: 60 * time.Second,
814 ldflags: "-linkmode=internal",
821 if goos != "js" { // js doesn't support -cpu=10
822 t.registerTest("sync_cpu", "sync -cpu=10",
824 timeout: 120 * time.Second,
830 if t.raceDetectorSupported() {
831 t.registerRaceTests()
834 if t.cgoEnabled && !t.iOS() {
835 // Disabled on iOS. golang.org/issue/15919
836 t.registerTest("cgo_stdio", "", &goTest{dir: "../misc/cgo/stdio", timeout: 5 * time.Minute}, rtHostTest{})
837 t.registerTest("cgo_life", "", &goTest{dir: "../misc/cgo/life", timeout: 5 * time.Minute}, rtHostTest{})
838 if goos != "android" {
839 t.registerTest("cgo_fortran", "", &goTest{dir: "../misc/cgo/fortran", timeout: 5 * time.Minute}, rtHostTest{})
841 if t.hasSwig() && goos != "android" {
842 t.registerTest("swig_stdio", "", &goTest{dir: "../misc/swig/stdio"})
844 t.registerTest("swig_callback", "", &goTest{dir: "../misc/swig/callback"})
845 const cflags = "-flto -Wno-lto-type-mismatch -Wno-unknown-warning-option"
846 t.registerTest("swig_callback_lto", "",
848 dir: "../misc/swig/callback",
850 "CGO_CFLAGS=" + cflags,
851 "CGO_CXXFLAGS=" + cflags,
852 "CGO_LDFLAGS=" + cflags,
862 // Don't run these tests with $GO_GCFLAGS because most of them
863 // assume that they can run "go install" with no -gcflags and not
864 // recompile the entire standard library. If make.bash ran with
865 // special -gcflags, that's not true.
866 if t.cgoEnabled && gogcflags == "" {
867 t.registerTest("testgodefs", "", &goTest{dir: "../misc/cgo/testgodefs", timeout: 5 * time.Minute}, rtHostTest{})
869 t.registerTest("testso", "", &goTest{dir: "../misc/cgo/testso", timeout: 600 * time.Second})
870 t.registerTest("testsovar", "", &goTest{dir: "../misc/cgo/testsovar", timeout: 600 * time.Second})
871 if t.supportedBuildmode("c-archive") {
872 t.registerTest("testcarchive", "", &goTest{dir: "../misc/cgo/testcarchive", timeout: 5 * time.Minute}, rtHostTest{})
874 if t.supportedBuildmode("c-shared") {
875 t.registerTest("testcshared", "", &goTest{dir: "../misc/cgo/testcshared", timeout: 5 * time.Minute}, rtHostTest{})
877 if t.supportedBuildmode("shared") {
878 t.registerTest("testshared", "", &goTest{dir: "../misc/cgo/testshared", timeout: 600 * time.Second})
880 if t.supportedBuildmode("plugin") {
881 t.registerTest("testplugin", "", &goTest{dir: "../misc/cgo/testplugin", timeout: 600 * time.Second})
883 if goos == "linux" || (goos == "freebsd" && goarch == "amd64") {
884 // because Pdeathsig of syscall.SysProcAttr struct used in misc/cgo/testsanitizers is only
885 // supported on Linux and FreeBSD.
886 t.registerTest("testsanitizers", "", &goTest{dir: "../misc/cgo/testsanitizers", timeout: 5 * time.Minute}, rtHostTest{})
888 if t.hasBash() && goos != "android" && !t.iOS() && gohostos != "windows" {
889 t.registerTest("cgo_errors", "", &goTest{dir: "../misc/cgo/errors", timeout: 5 * time.Minute}, rtHostTest{})
893 if goos != "android" && !t.iOS() {
894 // There are no tests in this directory, only benchmarks.
895 // Check that the test binary builds.
896 t.registerTest("bench_go1", "", &goTest{dir: "../test/bench/go1"})
898 if goos != "android" && !t.iOS() {
899 // Only start multiple test dir shards on builders,
900 // where they get distributed to multiple machines.
901 // See issues 20141 and 31834.
903 if os.Getenv("GO_BUILDER_NAME") != "" {
906 if n, err := strconv.Atoi(os.Getenv("GO_TEST_SHARDS")); err == nil {
909 for shard := 0; shard < nShards; shard++ {
911 t.tests = append(t.tests, distTest{
912 name: fmt.Sprintf("test:%d_%d", shard, nShards),
914 fn: func(dt *distTest) error { return t.testDirTest(dt, shard, nShards) },
918 // Only run the API check on fast development platforms.
919 // Every platform checks the API on every GOOS/GOARCH/CGO_ENABLED combination anyway,
920 // so we really only need to run this check once anywhere to get adequate coverage.
921 // To help developers avoid trybot-only failures, we try to run on typical developer machines
922 // which is darwin,linux,windows/amd64 and darwin/arm64.
923 if goos == "darwin" || ((goos == "linux" || goos == "windows") && goarch == "amd64") {
924 t.registerTest("api", "", &goTest{dir: "cmd/api", timeout: 5 * time.Minute, testFlags: []string{"-check"}})
927 // Ensure that the toolchain can bootstrap itself.
928 // This test adds another ~45s to all.bash if run sequentially, so run it only on the builders.
929 if os.Getenv("GO_BUILDER_NAME") != "" && goos != "android" && !t.iOS() {
930 t.registerTest("reboot", "", &goTest{dir: "../misc/reboot", timeout: 5 * time.Minute}, rtHostTest{})
934 // isRegisteredTestName reports whether a test named testName has already
936 func (t *tester) isRegisteredTestName(testName string) bool {
937 for _, tt := range t.tests {
938 if tt.name == testName {
945 type registerTestOpt interface {
949 // rtSequential is a registerTest option that causes the registered test to run
951 type rtSequential struct{}
953 func (rtSequential) isRegisterTestOpt() {}
955 // rtPreFunc is a registerTest option that runs a pre function before running
957 type rtPreFunc struct {
958 pre func(*distTest) bool // Return false to skip the test
961 func (rtPreFunc) isRegisterTestOpt() {}
963 // rtHostTest is a registerTest option that indicates this is a host test that
964 // should be run using goTest.runHostTest. It implies rtSequential.
965 type rtHostTest struct{}
967 func (rtHostTest) isRegisterTestOpt() {}
969 // registerTest registers a test that runs the given goTest.
971 // If heading is "", it uses test.dir as the heading.
972 func (t *tester) registerTest(name, heading string, test *goTest, opts ...registerTestOpt) {
975 var preFunc func(*distTest) bool
976 for _, opt := range opts {
977 switch opt := opt.(type) {
983 seq, hostTest = true, true
986 if t.isRegisteredTestName(name) {
987 panic("duplicate registered test name " + name)
992 t.tests = append(t.tests, distTest{
995 fn: func(dt *distTest) error {
996 if preFunc != nil && !preFunc(dt) {
1002 return test.runHostTest(t)
1008 cmd: test.bgCommand(t),
1010 t.worklist = append(t.worklist, w)
1016 // bgDirCmd constructs a Cmd intended to be run in the background as
1017 // part of the worklist. The worklist runner will buffer its output
1018 // and replay it sequentially. The command will be run in dir.
1019 func (t *tester) bgDirCmd(dir, bin string, args ...string) *exec.Cmd {
1020 cmd := exec.Command(bin, args...)
1021 if filepath.IsAbs(dir) {
1024 setDir(cmd, filepath.Join(goroot, dir))
1029 // dirCmd constructs a Cmd intended to be run in the foreground.
1030 // The command will be run in dir, and Stdout and Stderr will go to os.Stdout
1032 func (t *tester) dirCmd(dir string, cmdline ...interface{}) *exec.Cmd {
1033 bin, args := flattenCmdline(cmdline)
1034 cmd := t.bgDirCmd(dir, bin, args...)
1035 cmd.Stdout = os.Stdout
1036 cmd.Stderr = os.Stderr
1038 errprintf("%s\n", strings.Join(cmd.Args, " "))
1043 // flattenCmdline flattens a mixture of string and []string as single list
1044 // and then interprets it as a command line: first element is binary, then args.
1045 func flattenCmdline(cmdline []interface{}) (bin string, args []string) {
1047 for _, x := range cmdline {
1048 switch x := x.(type) {
1050 list = append(list, x)
1052 list = append(list, x...)
1054 panic("invalid addCmd argument type: " + reflect.TypeOf(x).String())
1059 if !filepath.IsAbs(bin) {
1060 panic("command is not absolute: " + bin)
1062 return bin, list[1:]
1065 // addCmd adds a command to the worklist. Commands can be run in
1066 // parallel, but their output will be buffered and replayed in the
1067 // order they were added to worklist.
1068 func (t *tester) addCmd(dt *distTest, dir string, cmdline ...interface{}) *exec.Cmd {
1069 bin, args := flattenCmdline(cmdline)
1072 cmd: t.bgDirCmd(dir, bin, args...),
1074 t.worklist = append(t.worklist, w)
1078 func (t *tester) iOS() bool {
1079 return goos == "ios"
1082 func (t *tester) out(v string) {
1086 fmt.Println("\n" + t.banner + v)
1089 // extLink reports whether the current goos/goarch supports
1090 // external linking. This should match the test in determineLinkMode
1091 // in cmd/link/internal/ld/config.go.
1092 func (t *tester) extLink() bool {
1093 if goarch == "ppc64" && goos != "aix" {
1099 func (t *tester) internalLink() bool {
1100 if gohostos == "dragonfly" {
1101 // linkmode=internal fails on dragonfly since errno is a TLS relocation.
1104 if goos == "android" {
1110 if goos == "windows" && goarch == "arm64" {
1113 // Internally linking cgo is incomplete on some architectures.
1114 // https://golang.org/issue/10373
1115 // https://golang.org/issue/14449
1116 if goarch == "loong64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" || goarch == "riscv64" {
1120 // linkmode=internal isn't supported.
1126 func (t *tester) internalLinkPIE() bool {
1127 switch goos + "-" + goarch {
1128 case "darwin-amd64", "darwin-arm64",
1129 "linux-amd64", "linux-arm64", "linux-ppc64le",
1131 "windows-amd64", "windows-386", "windows-arm":
1137 // supportedBuildMode reports whether the given build mode is supported.
1138 func (t *tester) supportedBuildmode(mode string) bool {
1139 pair := goos + "-" + goarch
1146 case "aix", "darwin", "ios", "windows":
1150 case "386", "amd64", "arm", "armbe", "arm64", "arm64be", "ppc64", "ppc64le", "riscv64", "s390x":
1153 // Other targets do not support -shared,
1154 // per ParseFlags in
1155 // cmd/compile/internal/base/flag.go.
1156 // For c-archive the Go tool passes -shared,
1157 // so that the result is suitable for inclusion
1158 // in a PIE or shared library.
1162 return goarch == "amd64"
1167 case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-riscv64", "linux-s390x",
1168 "darwin-amd64", "darwin-arm64",
1170 "android-arm", "android-arm64", "android-386", "android-amd64",
1171 "windows-amd64", "windows-386", "windows-arm64":
1177 case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-s390x":
1183 case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-s390x", "linux-ppc64le":
1185 case "android-386", "android-amd64":
1187 case "darwin-amd64", "darwin-arm64":
1189 case "freebsd-amd64":
1196 "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-riscv64", "linux-s390x",
1197 "android-amd64", "android-arm", "android-arm64", "android-386":
1199 case "darwin-amd64", "darwin-arm64", "ios-amd64", "ios-arm64":
1201 case "windows-amd64", "windows-386", "windows-arm", "windows-arm64":
1203 case "freebsd-amd64":
1209 fatalf("internal error: unknown buildmode %s", mode)
1214 func (t *tester) registerCgoTests() {
1215 cgoTest := func(name string, subdir, linkmode, buildmode string, opts ...registerTestOpt) *goTest {
1217 dir: "../misc/cgo/" + subdir,
1218 buildmode: buildmode,
1219 ldflags: "-linkmode=" + linkmode,
1222 if linkmode == "internal" {
1223 gt.tags = append(gt.tags, "internal")
1224 if buildmode == "pie" {
1225 gt.tags = append(gt.tags, "internal_pie")
1228 if buildmode == "static" {
1229 // This isn't actually a Go buildmode, just a convenient way to tell
1230 // cgoTest we want static linking.
1232 if linkmode == "external" {
1233 gt.ldflags += ` -extldflags "-static -pthread"`
1234 } else if linkmode == "auto" {
1235 gt.env = append(gt.env, "CGO_LDFLAGS=-static -pthread")
1237 panic("unknown linkmode with static build: " + linkmode)
1239 gt.tags = append(gt.tags, "static")
1242 t.registerTest("cgo:"+name, "../misc/cgo/test", gt, opts...)
1246 cgoTest("test-auto", "test", "auto", "")
1248 // Stub out various buildmode=pie tests on alpine until 54354 resolved.
1249 builderName := os.Getenv("GO_BUILDER_NAME")
1250 disablePIE := strings.HasSuffix(builderName, "-alpine")
1252 if t.internalLink() {
1253 cgoTest("test-internal", "test", "internal", "")
1257 p := gohostos + "/" + goarch
1259 case os == "darwin", os == "windows":
1263 // test linkmode=external, but __thread not supported, so skip testtls.
1264 cgoTest("test-external", "test", "external", "")
1266 gt := cgoTest("test-external-s", "test", "external", "")
1269 if t.supportedBuildmode("pie") && !disablePIE {
1270 cgoTest("test-auto-pie", "test", "auto", "pie")
1271 if t.internalLink() && t.internalLinkPIE() {
1272 cgoTest("test-internal-pie", "test", "internal", "pie")
1276 case os == "aix", os == "android", os == "dragonfly", os == "freebsd", os == "linux", os == "netbsd", os == "openbsd":
1277 gt := cgoTest("test-external-g0", "test", "external", "")
1278 gt.env = append(gt.env, "CGO_CFLAGS=-g0 -fdiagnostics-color")
1280 cgoTest("testtls-auto", "testtls", "auto", "")
1281 cgoTest("testtls-external", "testtls", "external", "")
1284 // no static linking
1285 case p == "freebsd/arm":
1286 // -fPIC compiled tls code will use __tls_get_addr instead
1287 // of __aeabi_read_tp, however, on FreeBSD/ARM, __tls_get_addr
1288 // is implemented in rtld-elf, so -fPIC isn't compatible with
1289 // static linking on FreeBSD/ARM with clang. (cgo depends on
1290 // -fPIC fundamentally.)
1292 // Check for static linking support
1293 var staticCheck rtPreFunc
1294 ccName := compilerEnvLookup("CC", defaultcc, goos, goarch)
1295 cc, err := exec.LookPath(ccName)
1297 staticCheck.pre = func(*distTest) bool {
1298 fmt.Printf("$CC (%q) not found, skip cgo static linking test.\n", ccName)
1302 cmd := t.dirCmd("misc/cgo/test", cc, "-xc", "-o", "/dev/null", "-static", "-")
1303 cmd.Stdin = strings.NewReader("int main() {}")
1304 cmd.Stdout, cmd.Stderr = nil, nil // Discard output
1305 if err := cmd.Run(); err != nil {
1307 staticCheck.pre = func(*distTest) bool {
1308 fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.")
1314 // Static linking tests
1315 if goos != "android" && p != "netbsd/arm" {
1316 // TODO(#56629): Why does this fail on netbsd-arm?
1317 cgoTest("testtls-static", "testtls", "external", "static", staticCheck)
1319 cgoTest("nocgo-auto", "nocgo", "auto", "", staticCheck)
1320 cgoTest("nocgo-external", "nocgo", "external", "", staticCheck)
1321 if goos != "android" {
1322 cgoTest("nocgo-static", "nocgo", "external", "static", staticCheck)
1323 cgoTest("test-static", "test", "external", "static", staticCheck)
1324 // -static in CGO_LDFLAGS triggers a different code path
1325 // than -static in -extldflags, so test both.
1326 // See issue #16651.
1327 if goarch != "loong64" {
1328 // TODO(#56623): Why does this fail on loong64?
1329 cgoTest("test-static-env", "test", "auto", "static", staticCheck)
1333 // PIE linking tests
1334 if t.supportedBuildmode("pie") && !disablePIE {
1335 cgoTest("test-pie", "test", "auto", "pie")
1336 if t.internalLink() && t.internalLinkPIE() {
1337 cgoTest("test-pie-internal", "test", "internal", "pie")
1339 cgoTest("testtls-pie", "testtls", "auto", "pie")
1340 cgoTest("nocgo-pie", "nocgo", "auto", "pie")
1346 // run pending test commands, in parallel, emitting headers as appropriate.
1347 // When finished, emit header for nextTest, which is going to run after the
1348 // pending commands are done (and runPending returns).
1349 // A test should call runPending if it wants to make sure that it is not
1350 // running in parallel with earlier tests, or if it has some other reason
1351 // for needing the earlier tests to be done.
1352 func (t *tester) runPending(nextTest *distTest) {
1353 worklist := t.worklist
1355 for _, w := range worklist {
1356 w.start = make(chan bool)
1357 w.end = make(chan bool)
1360 timelog("skip", w.dt.name)
1361 w.out = []byte(fmt.Sprintf("skipped due to earlier error\n"))
1363 timelog("start", w.dt.name)
1364 w.out, w.err = w.cmd.CombinedOutput()
1366 if isUnsupportedVMASize(w) {
1367 timelog("skip", w.dt.name)
1368 w.out = []byte(fmt.Sprintf("skipped due to unsupported VMA\n"))
1373 timelog("end", w.dt.name)
1381 for ended < len(worklist) {
1382 for started < len(worklist) && started-ended < maxbg {
1383 w := worklist[started]
1385 w.start <- !t.failed || t.keepGoing
1387 w := worklist[ended]
1389 if dt.heading != "" && t.lastHeading != dt.heading {
1390 t.lastHeading = dt.heading
1394 // Assumes all the entries for a single dt are in one worklist.
1397 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1401 errprintf("%s\n", strings.Join(w.cmd.Args, " "))
1405 os.Stdout.Write(w.out)
1407 log.Printf("Failed: %v", w.err)
1411 if t.failed && !t.keepGoing {
1415 if dt := nextTest; dt != nil {
1416 if dt.heading != "" && t.lastHeading != dt.heading {
1417 t.lastHeading = dt.heading
1421 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1426 func (t *tester) hasBash() bool {
1428 case "windows", "plan9":
1434 func (t *tester) hasCxx() bool {
1435 cxx, _ := exec.LookPath(compilerEnvLookup("CXX", defaultcxx, goos, goarch))
1439 func (t *tester) hasSwig() bool {
1440 swig, err := exec.LookPath("swig")
1445 // Check that swig was installed with Go support by checking
1446 // that a go directory exists inside the swiglib directory.
1447 // See https://golang.org/issue/23469.
1448 output, err := exec.Command(swig, "-go", "-swiglib").Output()
1452 swigDir := strings.TrimSpace(string(output))
1454 _, err = os.Stat(filepath.Join(swigDir, "go"))
1459 // Check that swig has a new enough version.
1460 // See https://golang.org/issue/22858.
1461 out, err := exec.Command(swig, "-version").CombinedOutput()
1466 re := regexp.MustCompile(`[vV]ersion +(\d+)([.]\d+)?([.]\d+)?`)
1467 matches := re.FindSubmatch(out)
1469 // Can't find version number; hope for the best.
1473 major, err := strconv.Atoi(string(matches[1]))
1475 // Can't find version number; hope for the best.
1486 // We have SWIG version 3.x.
1487 if len(matches[2]) > 0 {
1488 minor, err := strconv.Atoi(string(matches[2][1:]))
1498 // We have SWIG version 3.0.x.
1499 if len(matches[3]) > 0 {
1500 patch, err := strconv.Atoi(string(matches[3][1:]))
1513 func (t *tester) raceDetectorSupported() bool {
1514 if gohostos != goos {
1520 if !raceDetectorSupported(goos, goarch) {
1523 // The race detector doesn't work on Alpine Linux:
1524 // golang.org/issue/14481
1525 if isAlpineLinux() {
1528 // NetBSD support is unfinished.
1529 // golang.org/issue/26403
1530 if goos == "netbsd" {
1536 func isAlpineLinux() bool {
1537 if runtime.GOOS != "linux" {
1540 fi, err := os.Lstat("/etc/alpine-release")
1541 return err == nil && fi.Mode().IsRegular()
1544 func (t *tester) registerRaceTests() {
1545 hdr := "Testing race detector"
1546 t.registerTest("race:runtime/race", hdr,
1550 pkg: "runtime/race",
1552 t.registerTest("race", hdr,
1555 runTests: "TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace|TestFdRace|TestFdReadRace|TestFileCloseRace",
1556 pkgs: []string{"flag", "net", "os", "os/exec", "encoding/gob"},
1558 // We don't want the following line, because it
1559 // slows down all.bash (by 10 seconds on my laptop).
1560 // The race builder should catch any error here, but doesn't.
1561 // TODO(iant): Figure out how to catch this.
1562 // t.registerTest("race:cmd/go", hdr, &goTest{race: true, runTests: "TestParallelTest", pkg: "cmd/go"})
1564 // Building misc/cgo/test takes a long time.
1565 // There are already cgo-enabled packages being tested with the race detector.
1566 // We shouldn't need to redo all of misc/cgo/test too.
1567 // The race buildler will take care of this.
1568 // t.registerTest("race:misc/cgo/test", hdr, &goTest{dir: "../misc/cgo/test", race: true, env: []string{"GOTRACEBACK=2"}})
1571 var oldWindows rtPreFunc
1572 if strings.HasPrefix(os.Getenv("GO_BUILDER_NAME"), "windows-amd64-2008") {
1573 oldWindows.pre = func(*distTest) bool {
1574 fmt.Println("skipping -race with external linkage on older windows builder, see https://github.com/golang/go/issues/56904 for details")
1578 // Test with external linking; see issue 9133.
1579 t.registerTest("race:external", hdr,
1582 ldflags: "-linkmode=external",
1583 runTests: "TestParse|TestEcho|TestStdinCloseRace",
1584 pkgs: []string{"flag", "os/exec"},
1589 var runtest struct {
1595 func (t *tester) testDirTest(dt *distTest, shard, shards int) error {
1597 f, err := os.CreateTemp("", "runtest-*.exe") // named exe for Windows, but harmless elsewhere
1604 runtest.exe = f.Name()
1606 os.Remove(runtest.exe)
1609 cmd := t.dirCmd("test", gorootBinGo, "build", "-o", runtest.exe, "run.go")
1610 setEnv(cmd, "GOOS", gohostos)
1611 setEnv(cmd, "GOARCH", gohostarch)
1612 runtest.err = cmd.Run()
1614 if runtest.err != nil {
1620 t.addCmd(dt, "test", runtest.exe,
1621 fmt.Sprintf("--shard=%d", shard),
1622 fmt.Sprintf("--shards=%d", shards),
1627 // cgoPackages is the standard packages that use cgo.
1628 var cgoPackages = []string{
1633 var funcBenchmark = []byte("\nfunc Benchmark")
1635 // packageHasBenchmarks reports whether pkg has benchmarks.
1636 // On any error, it conservatively returns true.
1638 // This exists just to eliminate work on the builders, since compiling
1639 // a test in race mode just to discover it has no benchmarks costs a
1640 // second or two per package, and this function returns false for
1641 // about 100 packages.
1642 func (t *tester) packageHasBenchmarks(pkg string) bool {
1643 pkgDir := filepath.Join(goroot, "src", pkg)
1644 d, err := os.Open(pkgDir)
1646 return true // conservatively
1649 names, err := d.Readdirnames(-1)
1651 return true // conservatively
1653 for _, name := range names {
1654 if !strings.HasSuffix(name, "_test.go") {
1657 slurp, err := os.ReadFile(filepath.Join(pkgDir, name))
1659 return true // conservatively
1661 if bytes.Contains(slurp, funcBenchmark) {
1668 // makeGOROOTUnwritable makes all $GOROOT files & directories non-writable to
1669 // check that no tests accidentally write to $GOROOT.
1670 func (t *tester) makeGOROOTUnwritable() (undo func()) {
1671 dir := os.Getenv("GOROOT")
1673 panic("GOROOT not set")
1676 type pathMode struct {
1680 var dirs []pathMode // in lexical order
1683 for i := range dirs {
1684 os.Chmod(dirs[i].path, dirs[i].mode) // best effort
1688 filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
1689 if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
1690 if suffix == ".git" {
1691 // Leave Git metadata in whatever state it was in. It may contain a lot
1692 // of files, and it is highly unlikely that a test will try to modify
1693 // anything within that directory.
1694 return filepath.SkipDir
1701 info, err := d.Info()
1707 if mode&0222 != 0 && (mode.IsDir() || mode.IsRegular()) {
1708 dirs = append(dirs, pathMode{path, mode})
1713 // Run over list backward to chmod children before parents.
1714 for i := len(dirs) - 1; i >= 0; i-- {
1715 err := os.Chmod(dirs[i].path, dirs[i].mode&^0222)
1717 dirs = dirs[i:] // Only undo what we did so far.
1719 fatalf("failed to make GOROOT read-only: %v", err)
1726 // raceDetectorSupported is a copy of the function
1727 // internal/platform.RaceDetectorSupported, which can't be used here
1728 // because cmd/dist can not import internal packages during bootstrap.
1729 // The race detector only supports 48-bit VMA on arm64. But we don't have
1730 // a good solution to check VMA size(See https://golang.org/issue/29948)
1731 // raceDetectorSupported will always return true for arm64. But race
1732 // detector tests may abort on non 48-bit VMA configuration, the tests
1733 // will be marked as "skipped" in this case.
1734 func raceDetectorSupported(goos, goarch string) bool {
1737 return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" || goarch == "s390x"
1739 return goarch == "amd64" || goarch == "arm64"
1740 case "freebsd", "netbsd", "openbsd", "windows":
1741 return goarch == "amd64"
1747 // isUnsupportedVMASize reports whether the failure is caused by an unsupported
1748 // VMA for the race detector (for example, running the race detector on an
1749 // arm64 machine configured with 39-bit VMA)
1750 func isUnsupportedVMASize(w *work) bool {
1751 unsupportedVMA := []byte("unsupported VMA range")
1752 return w.dt.name == "race" && bytes.Contains(w.out, unsupportedVMA)
1755 // isEnvSet reports whether the environment variable evar is
1756 // set in the environment.
1757 func isEnvSet(evar string) bool {
1758 evarEq := evar + "="
1759 for _, e := range os.Environ() {
1760 if strings.HasPrefix(e, evarEq) {