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.
27 gogcflags = os.Getenv("GO_GCFLAGS")
33 flag.BoolVar(&t.listMode, "list", false, "list available tests")
34 flag.BoolVar(&t.rebuild, "rebuild", false, "rebuild everything first")
35 flag.BoolVar(&noRebuild, "no-rebuild", false, "overrides -rebuild (historical dreg)")
36 flag.BoolVar(&t.keepGoing, "k", false, "keep going even when error occurred")
37 flag.BoolVar(&t.race, "race", false, "run in race builder mode (different set of tests)")
38 flag.BoolVar(&t.compileOnly, "compile-only", false, "compile tests, but don't run them")
39 flag.StringVar(&t.banner, "banner", "##### ", "banner prefix; blank means no section banners")
40 flag.StringVar(&t.runRxStr, "run", "",
41 "run only those tests matching the regular expression; empty means to run all. "+
42 "Special exception: if the string begins with '!', the match is inverted.")
43 flag.BoolVar(&t.msan, "msan", false, "run in memory sanitizer builder mode")
44 flag.BoolVar(&t.asan, "asan", false, "run in address sanitizer builder mode")
45 flag.BoolVar(&t.json, "json", false, "report test results in JSON")
47 xflagparse(-1) // any number of args
55 // tester executes cmdtest.
64 compileOnly bool // just try to compile all tests, but no need to run
67 runRxWant bool // want runRx to match (true) or not match (false)
68 runNames []string // tests to run, exclusive with runRx; empty means all
69 banner string // prefix, or "" for none
70 lastHeading string // last dir heading printed
77 tests []distTest // use addTest to extend
78 testNames map[string]bool
86 cmd *exec.Cmd // Must write stdout/stderr to work.out
87 flush func() // If non-nil, called after cmd.Run
94 // A distTest is a test run by dist test.
95 // Each test has a unique name and belongs to a group (heading)
96 type distTest struct {
97 name string // unique test name; may be filtered with -run flag
98 heading string // group section; this header is printed before the test is run.
99 fn func(*distTest) error
102 func (t *tester) run() {
103 timelog("start", "dist test")
105 os.Setenv("PATH", fmt.Sprintf("%s%c%s", gorootBin, os.PathListSeparator, os.Getenv("PATH")))
108 if v := os.Getenv("GO_TEST_SHORT"); v != "" {
109 short, err := strconv.ParseBool(v)
111 fatalf("invalid GO_TEST_SHORT %q: %v", v, err)
116 cmd := exec.Command(gorootBinGo, "env", "CGO_ENABLED")
117 cmd.Stderr = new(bytes.Buffer)
118 slurp, err := cmd.Output()
120 fatalf("Error running %s: %v\n%s", cmd, err, cmd.Stderr)
122 parts := strings.Split(string(slurp), "\n")
123 if nlines := len(parts) - 1; nlines < 1 {
124 fatalf("Error running %s: output contains <1 lines\n%s", cmd, cmd.Stderr)
126 t.cgoEnabled, _ = strconv.ParseBool(parts[0])
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.testNames[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())
218 if err := t.maybeLogMetadata(); err != nil {
221 log.Printf("Failed logging metadata: %v", err)
223 fatalf("Failed logging metadata: %v", err)
228 for _, dt := range t.tests {
229 if !t.shouldRunTest(dt.name) {
233 dt := dt // dt used in background after this iteration
234 if err := dt.fn(&dt); err != nil {
235 t.runPending(&dt) // in case that hasn't been done yet
238 log.Printf("Failed: %v", err)
240 fatalf("Failed: %v", err)
245 timelog("end", "dist test")
249 fmt.Println("\nFAILED")
250 } else if t.partial {
251 fmt.Println("\nALL TESTS PASSED (some were excluded)")
253 fmt.Println("\nALL TESTS PASSED")
261 func (t *tester) shouldRunTest(name string) bool {
263 return t.runRx.MatchString(name) == t.runRxWant
265 if len(t.runNames) == 0 {
268 for _, runName := range t.runNames {
276 func (t *tester) maybeLogMetadata() error {
278 // We need to run a subprocess to log metadata. Don't do that
279 // on compile-only runs.
282 t.out("Test execution environment.")
283 // Helper binary to print system metadata (CPU model, etc). This is a
284 // separate binary from dist so it need not build with the bootstrap
287 // TODO(prattmic): If we split dist bootstrap and dist test then this
288 // could be simplified to directly use internal/sysinfo here.
289 return t.dirCmd(filepath.Join(goroot, "src/cmd/internal/metadata"), gorootBinGo, []string{"run", "main.go"}).Run()
292 // testName returns the dist test name for a given package and variant.
293 func testName(pkg, variant string) string {
296 name += ":" + variant
301 // goTest represents all options to a "go test" command. The final command will
302 // combine configuration from goTest and tester flags.
304 timeout time.Duration // If non-zero, override timeout
305 short bool // If true, force -short
306 tags []string // Build tags
307 race bool // Force -race
308 bench bool // Run benchmarks (briefly), not tests.
309 runTests string // Regexp of tests to run
310 cpu string // If non-empty, -cpu flag
312 gcflags string // If non-empty, build with -gcflags=all=X
313 ldflags string // If non-empty, build with -ldflags=X
314 buildmode string // If non-empty, -buildmode flag
316 env []string // Environment variables to add, as KEY=VAL. KEY= unsets a variable
318 runOnHost bool // When cross-compiling, run this test on the host instead of guest
320 // variant, if non-empty, is a name used to distinguish different
321 // configurations of the same test package(s). If set and omitVariant is false,
322 // the Package field in test2json output is rewritten to pkg:variant.
324 // omitVariant indicates that variant is used solely for the dist test name and
325 // that the set of test names run by each variant (including empty) of a package
326 // is non-overlapping.
329 // We have both pkg and pkgs as a convenience. Both may be set, in which
330 // case they will be combined. At least one must be set.
331 pkgs []string // Multiple packages to test
332 pkg string // A single package to test
334 testFlags []string // Additional flags accepted by this test
337 // bgCommand returns a go test Cmd and a post-Run flush function. The result
338 // will write its output to stdout and stderr. If stdout==stderr, bgCommand
339 // ensures Writes are serialized. The caller should call flush() after Cmd exits.
340 func (opts *goTest) bgCommand(t *tester, stdout, stderr io.Writer) (cmd *exec.Cmd, flush func()) {
341 build, run, pkgs, testFlags, setupCmd := opts.buildArgs(t)
343 // Combine the flags.
344 args := append([]string{"test"}, build...)
346 args = append(args, "-c", "-o", os.DevNull)
348 args = append(args, run...)
350 args = append(args, pkgs...)
352 args = append(args, testFlags...)
355 cmd = exec.Command(gorootBinGo, args...)
357 if t.json && opts.variant != "" && !opts.omitVariant {
358 // Rewrite Package in the JSON output to be pkg:variant. When omitVariant
359 // is true, pkg.TestName is already unambiguous, so we don't need to
360 // rewrite the Package field.
362 // We only want to process JSON on the child's stdout. Ideally if
363 // stdout==stderr, we would also use the same testJSONFilter for
364 // cmd.Stdout and cmd.Stderr in order to keep the underlying
365 // interleaving of writes, but then it would see even partial writes
366 // interleaved, which would corrupt the JSON. So, we only process
367 // cmd.Stdout. This has another consequence though: if stdout==stderr,
368 // we have to serialize Writes in case the Writer is not concurrent
369 // safe. If we were just passing stdout/stderr through to exec, it would
370 // do this for us, but since we're wrapping stdout, we have to do it
372 if stdout == stderr {
373 stdout = &lockedWriter{w: stdout}
376 f := &testJSONFilter{w: stdout, variant: opts.variant}
388 // run runs a go test and returns an error if it does not succeed.
389 func (opts *goTest) run(t *tester) error {
390 cmd, flush := opts.bgCommand(t, os.Stdout, os.Stderr)
396 // buildArgs is in internal helper for goTest that constructs the elements of
397 // the "go test" command line. build is the flags for building the test. run is
398 // the flags for running the test. pkgs is the list of packages to build and
399 // run. testFlags is the list of flags to pass to the test package.
401 // The caller must call setupCmd on the resulting exec.Cmd to set its directory
403 func (opts *goTest) buildArgs(t *tester) (build, run, pkgs, testFlags []string, setupCmd func(*exec.Cmd)) {
404 run = append(run, "-count=1") // Disallow caching
405 if opts.timeout != 0 {
406 d := opts.timeout * time.Duration(t.timeoutScale)
407 run = append(run, "-timeout="+d.String())
409 if opts.short || t.short {
410 run = append(run, "-short")
414 tags = append(tags, "lldb")
417 tags = append(tags, "noopt")
419 tags = append(tags, opts.tags...)
421 build = append(build, "-tags="+strings.Join(tags, ","))
423 if t.race || opts.race {
424 build = append(build, "-race")
427 build = append(build, "-msan")
430 build = append(build, "-asan")
434 run = append(run, "-run=^$")
435 // Run benchmarks briefly as a smoke test.
436 run = append(run, "-bench=.*", "-benchtime=.1s")
437 } else if opts.runTests != "" {
438 run = append(run, "-run="+opts.runTests)
441 run = append(run, "-cpu="+opts.cpu)
444 run = append(run, "-json")
447 if opts.gcflags != "" {
448 build = append(build, "-gcflags=all="+opts.gcflags)
450 if opts.ldflags != "" {
451 build = append(build, "-ldflags="+opts.ldflags)
453 if opts.buildmode != "" {
454 build = append(build, "-buildmode="+opts.buildmode)
457 pkgs = opts.packages()
459 runOnHost := opts.runOnHost && (goarch != gohostarch || goos != gohostos)
460 needTestFlags := len(opts.testFlags) > 0 || runOnHost
462 testFlags = append([]string{"-args"}, opts.testFlags...)
465 // -target is a special flag understood by tests that can run on the host
466 testFlags = append(testFlags, "-target="+goos+"/"+goarch)
469 setupCmd = func(cmd *exec.Cmd) {
470 setDir(cmd, filepath.Join(goroot, "src"))
471 if len(opts.env) != 0 {
472 for _, kv := range opts.env {
473 if i := strings.Index(kv, "="); i < 0 {
474 unsetEnv(cmd, kv[:len(kv)-1])
476 setEnv(cmd, kv[:i], kv[i+1:])
481 setEnv(cmd, "GOARCH", gohostarch)
482 setEnv(cmd, "GOOS", gohostos)
489 // packages returns the full list of packages to be run by this goTest. This
490 // will always include at least one package.
491 func (opts *goTest) packages() []string {
494 pkgs = append(pkgs[:len(pkgs):len(pkgs)], opts.pkg)
502 // ranGoTest and stdMatches are state closed over by the stdlib
503 // testing func in registerStdTest below. The tests are run
504 // sequentially, so there's no need for locks.
506 // ranGoBench and benchMatches are the same, but are only used
513 benchMatches []string
516 func (t *tester) registerStdTest(pkg string) {
517 const stdTestHeading = "Testing packages." // known to addTest for a safety check
519 name := testName(pkg, "")
520 if t.runRx == nil || t.runRx.MatchString(name) == t.runRxWant {
521 stdMatches = append(stdMatches, pkg)
523 t.addTest(name, stdTestHeading, func(dt *distTest) error {
528 timelog("start", dt.name)
529 defer timelog("end", dt.name)
532 timeoutSec := 180 * time.Second
533 for _, pkg := range stdMatches {
547 func (t *tester) registerRaceBenchTest(pkg string) {
548 const raceBenchHeading = "Running benchmarks briefly." // known to addTest for a safety check
549 name := testName(pkg, "racebench")
550 if t.runRx == nil || t.runRx.MatchString(name) == t.runRxWant {
551 benchMatches = append(benchMatches, pkg)
553 t.addTest(name, raceBenchHeading, func(dt *distTest) error {
558 timelog("start", dt.name)
559 defer timelog("end", dt.name)
562 variant: "racebench",
563 omitVariant: true, // The only execution of benchmarks in dist; benchmark names are guaranteed not to overlap with test names.
564 timeout: 1200 * time.Second, // longer timeout for race with benchmarks
573 func (t *tester) registerTests() {
574 // registerStdTestSpecially tracks import paths in the standard library
575 // whose test registration happens in a special way.
576 registerStdTestSpecially := map[string]bool{
577 "runtime/internal/wasitest": true, // Registered at the bottom as a host test.
578 "cmd/internal/testdir": true, // Registered at the bottom with sharding.
581 // Fast path to avoid the ~1 second of `go list std cmd` when
582 // the caller lists specific tests to run. (as the continuous
583 // build coordinator does).
584 if len(t.runNames) > 0 {
585 for _, name := range t.runNames {
586 if !strings.Contains(name, ":") {
587 t.registerStdTest(name)
588 } else if strings.HasSuffix(name, ":racebench") {
589 t.registerRaceBenchTest(strings.TrimSuffix(name, ":racebench"))
593 // Use a format string to only list packages and commands that have tests.
594 const format = "{{if (or .TestGoFiles .XTestGoFiles)}}{{.ImportPath}}{{end}}"
595 cmd := exec.Command(gorootBinGo, "list", "-f", format)
597 cmd.Args = append(cmd.Args, "-tags=race")
599 cmd.Args = append(cmd.Args, "std", "cmd")
600 cmd.Stderr = new(bytes.Buffer)
601 all, err := cmd.Output()
603 fatalf("Error running go list std cmd: %v:\n%s", err, cmd.Stderr)
605 pkgs := strings.Fields(string(all))
606 for _, pkg := range pkgs {
607 if registerStdTestSpecially[pkg] {
610 t.registerStdTest(pkg)
613 for _, pkg := range pkgs {
614 if t.packageHasBenchmarks(pkg) {
615 t.registerRaceBenchTest(pkg)
625 // Test the os/user package in the pure-Go mode too.
627 t.registerTest("os/user with tag osusergo",
630 timeout: 300 * time.Second,
631 tags: []string{"osusergo"},
634 t.registerTest("hash/maphash purego implementation",
637 timeout: 300 * time.Second,
638 tags: []string{"purego"},
643 // Test ios/amd64 for the iOS simulator.
644 if goos == "darwin" && goarch == "amd64" && t.cgoEnabled {
645 t.registerTest("GOOS=ios on darwin/amd64",
648 timeout: 300 * time.Second,
649 runTests: "SystemRoots",
650 env: []string{"GOOS=ios", "CGO_ENABLED=1"},
655 // Runtime CPU tests.
656 if !t.compileOnly && t.hasParallelism() {
657 t.registerTest("GOMAXPROCS=2 runtime -cpu=1,2,4 -quick",
660 timeout: 300 * time.Second,
663 testFlags: []string{"-quick"},
664 // We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code,
665 // creation of first goroutines and first garbage collections in the parallel setting.
666 env: []string{"GOMAXPROCS=2"},
671 // morestack tests. We only run these on in long-test mode
672 // (with GO_TEST_SHORT=false) because the runtime test is
673 // already quite long and mayMoreStackMove makes it about
675 if !t.compileOnly && !t.short {
676 // hooks is the set of maymorestack hooks to test with.
677 hooks := []string{"mayMoreStackPreempt", "mayMoreStackMove"}
678 // hookPkgs is the set of package patterns to apply
679 // the maymorestack hook to.
680 hookPkgs := []string{"runtime/...", "reflect", "sync"}
681 // unhookPkgs is the set of package patterns to
682 // exclude from hookPkgs.
683 unhookPkgs := []string{"runtime/testdata/..."}
684 for _, hook := range hooks {
685 // Construct the build flags to use the
686 // maymorestack hook in the compiler and
687 // assembler. We pass this via the GOFLAGS
688 // environment variable so that it applies to
689 // both the test itself and to binaries built
691 goFlagsList := []string{}
692 for _, flag := range []string{"-gcflags", "-asmflags"} {
693 for _, hookPkg := range hookPkgs {
694 goFlagsList = append(goFlagsList, flag+"="+hookPkg+"=-d=maymorestack=runtime."+hook)
696 for _, unhookPkg := range unhookPkgs {
697 goFlagsList = append(goFlagsList, flag+"="+unhookPkg+"=")
700 goFlags := strings.Join(goFlagsList, " ")
702 t.registerTest("maymorestack="+hook,
705 timeout: 600 * time.Second,
707 env: []string{"GOFLAGS=" + goFlags},
708 pkgs: []string{"runtime", "reflect", "sync"},
713 // Test that internal linking of standard packages does not
714 // require libgcc. This ensures that we can install a Go
715 // release on a system that does not have a C compiler
716 // installed and still build Go programs (that don't use cgo).
717 for _, pkg := range cgoPackages {
718 if !t.internalLink() {
722 // ARM libgcc may be Thumb, which internal linking does not support.
727 // What matters is that the tests build and start up.
728 // Skip expensive tests, especially x509 TestSystemRoots.
731 run = "TestTCPStress"
733 t.registerTest("Testing without libgcc.",
736 ldflags: "-linkmode=internal -libgcc=none",
742 // Stub out following test on alpine until 54354 resolved.
743 builderName := os.Getenv("GO_BUILDER_NAME")
744 disablePIE := strings.HasSuffix(builderName, "-alpine")
746 // Test internal linking of PIE binaries where it is supported.
747 if t.internalLinkPIE() && !disablePIE {
748 t.registerTest("internal linking of -buildmode=pie",
750 variant: "pie_internal",
751 timeout: 60 * time.Second,
753 ldflags: "-linkmode=internal",
754 env: []string{"CGO_ENABLED=0"},
757 // Also test a cgo package.
758 if t.cgoEnabled && t.internalLink() && !disablePIE {
759 t.registerTest("internal linking of -buildmode=pie",
761 variant: "pie_internal",
762 timeout: 60 * time.Second,
764 ldflags: "-linkmode=internal",
771 if t.hasParallelism() {
772 t.registerTest("sync -cpu=10",
775 timeout: 120 * time.Second,
781 if t.raceDetectorSupported() {
782 t.registerRaceTests()
785 const cgoHeading = "Testing cgo"
787 t.registerCgoTests(cgoHeading)
790 if goos == "wasip1" {
791 t.registerTest("wasip1 host tests",
794 pkg: "runtime/internal/wasitest",
795 timeout: 1 * time.Minute,
800 if goos != "android" && !t.iOS() {
801 // Only start multiple test dir shards on builders,
802 // where they get distributed to multiple machines.
803 // See issues 20141 and 31834.
805 if os.Getenv("GO_BUILDER_NAME") != "" {
808 if n, err := strconv.Atoi(os.Getenv("GO_TEST_SHARDS")); err == nil {
811 for shard := 0; shard < nShards; shard++ {
812 id := fmt.Sprintf("%d_%d", shard, nShards)
813 t.registerTest("../test",
816 omitVariant: true, // Shards of the same Go package; tests are guaranteed not to overlap.
817 pkg: "cmd/internal/testdir",
818 testFlags: []string{fmt.Sprintf("-shard=%d", shard), fmt.Sprintf("-shards=%d", nShards)},
824 // Only run the API check on fast development platforms.
825 // Every platform checks the API on every GOOS/GOARCH/CGO_ENABLED combination anyway,
826 // so we really only need to run this check once anywhere to get adequate coverage.
827 // To help developers avoid trybot-only failures, we try to run on typical developer machines
828 // which is darwin,linux,windows/amd64 and darwin/arm64.
829 if goos == "darwin" || ((goos == "linux" || goos == "windows") && goarch == "amd64") {
830 t.registerTest("API check", &goTest{variant: "check", pkg: "cmd/api", timeout: 5 * time.Minute, testFlags: []string{"-check"}})
834 // addTest adds an arbitrary test callback to the test list.
836 // name must uniquely identify the test and heading must be non-empty.
837 func (t *tester) addTest(name, heading string, fn func(*distTest) error) {
838 if t.testNames[name] {
839 panic("duplicate registered test name " + name)
842 panic("empty heading")
844 // Two simple checks for cases that would conflict with the fast path in registerTests.
845 if !strings.Contains(name, ":") && heading != "Testing packages." {
846 panic("empty variant is reserved exclusively for registerStdTest")
847 } else if strings.HasSuffix(name, ":racebench") && heading != "Running benchmarks briefly." {
848 panic("racebench variant is reserved exclusively for registerRaceBenchTest")
850 if t.testNames == nil {
851 t.testNames = make(map[string]bool)
853 t.testNames[name] = true
854 t.tests = append(t.tests, distTest{
861 type registerTestOpt interface {
865 // rtSkipFunc is a registerTest option that runs a skip check function before
867 type rtSkipFunc struct {
868 skip func(*distTest) (string, bool) // Return message, true to skip the test
871 func (rtSkipFunc) isRegisterTestOpt() {}
873 // registerTest registers a test that runs the given goTest.
875 // Each Go package in goTest will have a corresponding test
876 // "<pkg>:<variant>", which must uniquely identify the test.
878 // heading and test.variant must be non-empty.
879 func (t *tester) registerTest(heading string, test *goTest, opts ...registerTestOpt) {
880 var skipFunc func(*distTest) (string, bool)
881 for _, opt := range opts {
882 switch opt := opt.(type) {
887 // Register each test package as a separate test.
888 register1 := func(test *goTest) {
889 if test.variant == "" {
890 panic("empty variant")
892 name := testName(test.pkg, test.variant)
893 t.addTest(name, heading, func(dt *distTest) error {
895 msg, skip := skipFunc(dt)
897 t.printSkip(test, msg)
902 w.cmd, w.flush = test.bgCommand(t, &w.out, &w.out)
903 t.worklist = append(t.worklist, w)
907 if test.pkg != "" && len(test.pkgs) == 0 {
908 // Common case. Avoid copying.
912 // TODO(dmitshur,austin): It might be better to unify the execution of 'go test pkg'
913 // invocations for the same variant to be done with a single 'go test pkg1 pkg2 pkg3'
914 // command, just like it's already done in registerStdTest and registerRaceBenchTest.
915 // Those methods accumulate matched packages in stdMatches and benchMatches slices,
916 // and we can extend that mechanism to work for all other equal variant registrations.
917 // Do the simple thing to start with.
918 for _, pkg := range test.packages() {
920 test1.pkg, test1.pkgs = pkg, nil
925 func (t *tester) printSkip(test *goTest, msg string) {
934 Output string `json:",omitempty"`
936 out := json.NewEncoder(os.Stdout)
937 for _, pkg := range test.packages() {
938 ev := event{Time: time.Now(), Package: testName(pkg, test.variant), Action: "start"}
949 // dirCmd constructs a Cmd intended to be run in the foreground.
950 // The command will be run in dir, and Stdout and Stderr will go to os.Stdout
952 func (t *tester) dirCmd(dir string, cmdline ...interface{}) *exec.Cmd {
953 bin, args := flattenCmdline(cmdline)
954 cmd := exec.Command(bin, args...)
955 if filepath.IsAbs(dir) {
958 setDir(cmd, filepath.Join(goroot, dir))
960 cmd.Stdout = os.Stdout
961 cmd.Stderr = os.Stderr
963 errprintf("%s\n", strings.Join(cmd.Args, " "))
968 // flattenCmdline flattens a mixture of string and []string as single list
969 // and then interprets it as a command line: first element is binary, then args.
970 func flattenCmdline(cmdline []interface{}) (bin string, args []string) {
972 for _, x := range cmdline {
973 switch x := x.(type) {
975 list = append(list, x)
977 list = append(list, x...)
979 panic("invalid dirCmd argument type: " + reflect.TypeOf(x).String())
984 if !filepath.IsAbs(bin) {
985 panic("command is not absolute: " + bin)
990 func (t *tester) iOS() bool {
994 func (t *tester) out(v string) {
1001 fmt.Println("\n" + t.banner + v)
1004 // extLink reports whether the current goos/goarch supports
1005 // external linking. This should match the test in determineLinkMode
1006 // in cmd/link/internal/ld/config.go.
1007 func (t *tester) extLink() bool {
1008 if goarch == "ppc64" && goos != "aix" {
1014 func (t *tester) internalLink() bool {
1015 if gohostos == "dragonfly" {
1016 // linkmode=internal fails on dragonfly since errno is a TLS relocation.
1019 if goos == "android" {
1025 if goos == "windows" && goarch == "arm64" {
1028 // Internally linking cgo is incomplete on some architectures.
1029 // https://golang.org/issue/10373
1030 // https://golang.org/issue/14449
1031 if goarch == "loong64" || goarch == "mips64" || goarch == "mips64le" || goarch == "mips" || goarch == "mipsle" || goarch == "riscv64" {
1035 // linkmode=internal isn't supported.
1041 func (t *tester) internalLinkPIE() bool {
1042 switch goos + "-" + goarch {
1043 case "darwin-amd64", "darwin-arm64",
1044 "linux-amd64", "linux-arm64", "linux-ppc64le",
1046 "windows-amd64", "windows-386", "windows-arm":
1052 // supportedBuildMode reports whether the given build mode is supported.
1053 func (t *tester) supportedBuildmode(mode string) bool {
1055 case "c-archive", "c-shared", "shared", "plugin", "pie":
1057 fatalf("internal error: unknown buildmode %s", mode)
1061 return buildModeSupported("gc", mode, goos, goarch)
1064 func (t *tester) registerCgoTests(heading string) {
1065 cgoTest := func(variant string, subdir, linkmode, buildmode string, opts ...registerTestOpt) *goTest {
1068 pkg: "cmd/cgo/internal/" + subdir,
1069 buildmode: buildmode,
1071 var ldflags []string
1072 if linkmode != "auto" {
1073 // "auto" is the default, so avoid cluttering the command line for "auto"
1074 ldflags = append(ldflags, "-linkmode="+linkmode)
1077 if linkmode == "internal" {
1078 gt.tags = append(gt.tags, "internal")
1079 if buildmode == "pie" {
1080 gt.tags = append(gt.tags, "internal_pie")
1083 if buildmode == "static" {
1084 // This isn't actually a Go buildmode, just a convenient way to tell
1085 // cgoTest we want static linking.
1087 if linkmode == "external" {
1088 ldflags = append(ldflags, `-extldflags "-static -pthread"`)
1089 } else if linkmode == "auto" {
1090 gt.env = append(gt.env, "CGO_LDFLAGS=-static -pthread")
1092 panic("unknown linkmode with static build: " + linkmode)
1094 gt.tags = append(gt.tags, "static")
1096 gt.ldflags = strings.Join(ldflags, " ")
1098 t.registerTest(heading, gt, opts...)
1102 // test, testtls, and testnocgo are run with linkmode="auto", buildmode=""
1103 // as part of go test cmd. Here we only have to register the non-default
1104 // build modes of these tests.
1106 // Stub out various buildmode=pie tests on alpine until 54354 resolved.
1107 builderName := os.Getenv("GO_BUILDER_NAME")
1108 disablePIE := strings.HasSuffix(builderName, "-alpine")
1110 if t.internalLink() {
1111 cgoTest("internal", "test", "internal", "")
1115 p := gohostos + "/" + goarch
1117 case os == "darwin", os == "windows":
1121 // test linkmode=external, but __thread not supported, so skip testtls.
1122 cgoTest("external", "test", "external", "")
1124 gt := cgoTest("external-s", "test", "external", "")
1127 if t.supportedBuildmode("pie") && !disablePIE {
1128 cgoTest("auto-pie", "test", "auto", "pie")
1129 if t.internalLink() && t.internalLinkPIE() {
1130 cgoTest("internal-pie", "test", "internal", "pie")
1134 case os == "aix", os == "android", os == "dragonfly", os == "freebsd", os == "linux", os == "netbsd", os == "openbsd":
1135 gt := cgoTest("external-g0", "test", "external", "")
1136 gt.env = append(gt.env, "CGO_CFLAGS=-g0 -fdiagnostics-color")
1138 cgoTest("external", "testtls", "external", "")
1141 // no static linking
1142 case p == "freebsd/arm":
1143 // -fPIC compiled tls code will use __tls_get_addr instead
1144 // of __aeabi_read_tp, however, on FreeBSD/ARM, __tls_get_addr
1145 // is implemented in rtld-elf, so -fPIC isn't compatible with
1146 // static linking on FreeBSD/ARM with clang. (cgo depends on
1147 // -fPIC fundamentally.)
1149 // Check for static linking support
1150 var staticCheck rtSkipFunc
1151 ccName := compilerEnvLookup("CC", defaultcc, goos, goarch)
1152 cc, err := exec.LookPath(ccName)
1154 staticCheck.skip = func(*distTest) (string, bool) {
1155 return fmt.Sprintf("$CC (%q) not found, skip cgo static linking test.", ccName), true
1158 cmd := t.dirCmd("src/cmd/cgo/internal/test", cc, "-xc", "-o", "/dev/null", "-static", "-")
1159 cmd.Stdin = strings.NewReader("int main() {}")
1160 cmd.Stdout, cmd.Stderr = nil, nil // Discard output
1161 if err := cmd.Run(); err != nil {
1163 staticCheck.skip = func(*distTest) (string, bool) {
1164 return "No support for static linking found (lacks libc.a?), skip cgo static linking test.", true
1169 // Doing a static link with boringcrypto gets
1170 // a C linker warning on Linux.
1171 // in function `bio_ip_and_port_to_socket_and_addr':
1172 // warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
1173 if staticCheck.skip == nil && goos == "linux" && strings.Contains(goexperiment, "boringcrypto") {
1174 staticCheck.skip = func(*distTest) (string, bool) {
1175 return "skipping static linking check on Linux when using boringcrypto to avoid C linker warning about getaddrinfo", true
1179 // Static linking tests
1180 if goos != "android" && p != "netbsd/arm" {
1181 // TODO(#56629): Why does this fail on netbsd-arm?
1182 cgoTest("static", "testtls", "external", "static", staticCheck)
1184 cgoTest("external", "testnocgo", "external", "", staticCheck)
1185 if goos != "android" {
1186 cgoTest("static", "testnocgo", "external", "static", staticCheck)
1187 cgoTest("static", "test", "external", "static", staticCheck)
1188 // -static in CGO_LDFLAGS triggers a different code path
1189 // than -static in -extldflags, so test both.
1190 // See issue #16651.
1191 if goarch != "loong64" {
1192 // TODO(#56623): Why does this fail on loong64?
1193 cgoTest("auto-static", "test", "auto", "static", staticCheck)
1197 // PIE linking tests
1198 if t.supportedBuildmode("pie") && !disablePIE {
1199 cgoTest("auto-pie", "test", "auto", "pie")
1200 if t.internalLink() && t.internalLinkPIE() {
1201 cgoTest("internal-pie", "test", "internal", "pie")
1203 cgoTest("auto-pie", "testtls", "auto", "pie")
1204 cgoTest("auto-pie", "testnocgo", "auto", "pie")
1210 // run pending test commands, in parallel, emitting headers as appropriate.
1211 // When finished, emit header for nextTest, which is going to run after the
1212 // pending commands are done (and runPending returns).
1213 // A test should call runPending if it wants to make sure that it is not
1214 // running in parallel with earlier tests, or if it has some other reason
1215 // for needing the earlier tests to be done.
1216 func (t *tester) runPending(nextTest *distTest) {
1217 worklist := t.worklist
1219 for _, w := range worklist {
1220 w.start = make(chan bool)
1221 w.end = make(chan bool)
1222 // w.cmd must be set up to write to w.out. We can't check that, but we
1223 // can check for easy mistakes.
1224 if w.cmd.Stdout == nil || w.cmd.Stdout == os.Stdout || w.cmd.Stderr == nil || w.cmd.Stderr == os.Stderr {
1225 panic("work.cmd.Stdout/Stderr must be redirected")
1229 timelog("skip", w.dt.name)
1230 w.out.WriteString("skipped due to earlier error\n")
1232 timelog("start", w.dt.name)
1238 if isUnsupportedVMASize(w) {
1239 timelog("skip", w.dt.name)
1241 w.out.WriteString("skipped due to unsupported VMA\n")
1246 timelog("end", w.dt.name)
1254 for ended < len(worklist) {
1255 for started < len(worklist) && started-ended < maxbg {
1256 w := worklist[started]
1258 w.start <- !t.failed || t.keepGoing
1260 w := worklist[ended]
1262 if t.lastHeading != dt.heading {
1263 t.lastHeading = dt.heading
1267 // Assumes all the entries for a single dt are in one worklist.
1270 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1274 errprintf("%s\n", strings.Join(w.cmd.Args, " "))
1278 os.Stdout.Write(w.out.Bytes())
1279 // We no longer need the output, so drop the buffer.
1280 w.out = bytes.Buffer{}
1282 log.Printf("Failed: %v", w.err)
1286 if t.failed && !t.keepGoing {
1290 if dt := nextTest; dt != nil {
1291 if t.lastHeading != dt.heading {
1292 t.lastHeading = dt.heading
1296 fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
1301 func (t *tester) hasBash() bool {
1303 case "windows", "plan9":
1309 // hasParallelism is a copy of the function
1310 // internal/testenv.HasParallelism, which can't be used here
1311 // because cmd/dist can not import internal packages during bootstrap.
1312 func (t *tester) hasParallelism() bool {
1314 case "js", "wasip1":
1320 func (t *tester) raceDetectorSupported() bool {
1321 if gohostos != goos {
1327 if !raceDetectorSupported(goos, goarch) {
1330 // The race detector doesn't work on Alpine Linux:
1331 // golang.org/issue/14481
1332 if isAlpineLinux() {
1335 // NetBSD support is unfinished.
1336 // golang.org/issue/26403
1337 if goos == "netbsd" {
1343 func isAlpineLinux() bool {
1344 if runtime.GOOS != "linux" {
1347 fi, err := os.Lstat("/etc/alpine-release")
1348 return err == nil && fi.Mode().IsRegular()
1351 func (t *tester) registerRaceTests() {
1352 hdr := "Testing race detector"
1358 pkg: "runtime/race",
1364 runTests: "TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace|TestFdRace|TestFdReadRace|TestFileCloseRace",
1365 pkgs: []string{"flag", "net", "os", "os/exec", "encoding/gob"},
1367 // We don't want the following line, because it
1368 // slows down all.bash (by 10 seconds on my laptop).
1369 // The race builder should catch any error here, but doesn't.
1370 // TODO(iant): Figure out how to catch this.
1371 // t.registerTest(hdr, &goTest{variant: "race", race: true, runTests: "TestParallelTest", pkg: "cmd/go"})
1373 // Building cmd/cgo/internal/test takes a long time.
1374 // There are already cgo-enabled packages being tested with the race detector.
1375 // We shouldn't need to redo all of cmd/cgo/internal/test too.
1376 // The race buildler will take care of this.
1377 // t.registerTest(hdr, &goTest{variant: "race", race: true, env: []string{"GOTRACEBACK=2"}, pkg: "cmd/cgo/internal/test"})
1380 // Test with external linking; see issue 9133.
1383 variant: "race-external",
1385 ldflags: "-linkmode=external",
1386 runTests: "TestParse|TestEcho|TestStdinCloseRace",
1387 pkgs: []string{"flag", "os/exec"},
1392 // cgoPackages is the standard packages that use cgo.
1393 var cgoPackages = []string{
1398 var funcBenchmark = []byte("\nfunc Benchmark")
1400 // packageHasBenchmarks reports whether pkg has benchmarks.
1401 // On any error, it conservatively returns true.
1403 // This exists just to eliminate work on the builders, since compiling
1404 // a test in race mode just to discover it has no benchmarks costs a
1405 // second or two per package, and this function returns false for
1406 // about 100 packages.
1407 func (t *tester) packageHasBenchmarks(pkg string) bool {
1408 pkgDir := filepath.Join(goroot, "src", pkg)
1409 d, err := os.Open(pkgDir)
1411 return true // conservatively
1414 names, err := d.Readdirnames(-1)
1416 return true // conservatively
1418 for _, name := range names {
1419 if !strings.HasSuffix(name, "_test.go") {
1422 slurp, err := os.ReadFile(filepath.Join(pkgDir, name))
1424 return true // conservatively
1426 if bytes.Contains(slurp, funcBenchmark) {
1433 // makeGOROOTUnwritable makes all $GOROOT files & directories non-writable to
1434 // check that no tests accidentally write to $GOROOT.
1435 func (t *tester) makeGOROOTUnwritable() (undo func()) {
1436 dir := os.Getenv("GOROOT")
1438 panic("GOROOT not set")
1441 type pathMode struct {
1445 var dirs []pathMode // in lexical order
1448 for i := range dirs {
1449 os.Chmod(dirs[i].path, dirs[i].mode) // best effort
1453 filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
1454 if suffix := strings.TrimPrefix(path, dir+string(filepath.Separator)); suffix != "" {
1455 if suffix == ".git" {
1456 // Leave Git metadata in whatever state it was in. It may contain a lot
1457 // of files, and it is highly unlikely that a test will try to modify
1458 // anything within that directory.
1459 return filepath.SkipDir
1466 info, err := d.Info()
1472 if mode&0222 != 0 && (mode.IsDir() || mode.IsRegular()) {
1473 dirs = append(dirs, pathMode{path, mode})
1478 // Run over list backward to chmod children before parents.
1479 for i := len(dirs) - 1; i >= 0; i-- {
1480 err := os.Chmod(dirs[i].path, dirs[i].mode&^0222)
1482 dirs = dirs[i:] // Only undo what we did so far.
1484 fatalf("failed to make GOROOT read-only: %v", err)
1491 // raceDetectorSupported is a copy of the function
1492 // internal/platform.RaceDetectorSupported, which can't be used here
1493 // because cmd/dist can not import internal packages during bootstrap.
1494 // The race detector only supports 48-bit VMA on arm64. But we don't have
1495 // a good solution to check VMA size(See https://golang.org/issue/29948)
1496 // raceDetectorSupported will always return true for arm64. But race
1497 // detector tests may abort on non 48-bit VMA configuration, the tests
1498 // will be marked as "skipped" in this case.
1499 func raceDetectorSupported(goos, goarch string) bool {
1502 return goarch == "amd64" || goarch == "ppc64le" || goarch == "arm64" || goarch == "s390x"
1504 return goarch == "amd64" || goarch == "arm64"
1505 case "freebsd", "netbsd", "openbsd", "windows":
1506 return goarch == "amd64"
1512 // buildModeSupports is a copy of the function
1513 // internal/platform.BuildModeSupported, which can't be used here
1514 // because cmd/dist can not import internal packages during bootstrap.
1515 func buildModeSupported(compiler, buildmode, goos, goarch string) bool {
1516 if compiler == "gccgo" {
1520 platform := goos + "/" + goarch
1528 case "aix", "darwin", "ios", "windows":
1532 case "386", "amd64", "arm", "armbe", "arm64", "arm64be", "loong64", "ppc64le", "riscv64", "s390x":
1533 // linux/ppc64 not supported because it does
1534 // not support external linking mode yet.
1537 // Other targets do not support -shared,
1538 // per ParseFlags in
1539 // cmd/compile/internal/base/flag.go.
1540 // For c-archive the Go tool passes -shared,
1541 // so that the result is suitable for inclusion
1542 // in a PIE or shared library.
1546 return goarch == "amd64"
1552 case "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/386", "linux/ppc64le", "linux/riscv64", "linux/s390x",
1553 "android/amd64", "android/arm", "android/arm64", "android/386",
1555 "darwin/amd64", "darwin/arm64",
1556 "windows/amd64", "windows/386", "windows/arm64":
1569 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/ppc64le", "linux/riscv64", "linux/s390x",
1570 "android/amd64", "android/arm", "android/arm64", "android/386",
1572 "darwin/amd64", "darwin/arm64",
1573 "ios/amd64", "ios/arm64",
1575 "windows/386", "windows/amd64", "windows/arm", "windows/arm64":
1582 case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
1589 case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
1590 "android/amd64", "android/386",
1591 "darwin/amd64", "darwin/arm64",
1602 // isUnsupportedVMASize reports whether the failure is caused by an unsupported
1603 // VMA for the race detector (for example, running the race detector on an
1604 // arm64 machine configured with 39-bit VMA)
1605 func isUnsupportedVMASize(w *work) bool {
1606 unsupportedVMA := []byte("unsupported VMA range")
1607 return w.dt.name == "race" && bytes.Contains(w.out.Bytes(), unsupportedVMA)
1610 // isEnvSet reports whether the environment variable evar is
1611 // set in the environment.
1612 func isEnvSet(evar string) bool {
1613 evarEq := evar + "="
1614 for _, e := range os.Environ() {
1615 if strings.HasPrefix(e, evarEq) {