Source file src/internal/testenv/testenv.go

     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 testenv provides information about what functionality
     6  // is available in different testing environments run by the Go team.
     7  //
     8  // It is an internal package because these details are specific
     9  // to the Go team's test setup (on build.golang.org) and not
    10  // fundamental to tests in general.
    11  package testenv
    12  
    13  import (
    14  	"bytes"
    15  	"errors"
    16  	"flag"
    17  	"fmt"
    18  	"internal/cfg"
    19  	"internal/goarch"
    20  	"internal/platform"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"runtime"
    25  	"strconv"
    26  	"strings"
    27  	"sync"
    28  	"testing"
    29  )
    30  
    31  // Save the original environment during init for use in checks. A test
    32  // binary may modify its environment before calling HasExec to change its
    33  // behavior (such as mimicking a command-line tool), and that modified
    34  // environment might cause environment checks to behave erratically.
    35  var origEnv = os.Environ()
    36  
    37  // Builder reports the name of the builder running this test. For example,
    38  // "gotip-linux-amd64_avx512-test_only" or "go1.24-windows-arm64" on LUCI,
    39  // or "linux-amd64" on our old infrastructure. Prefer using runtime.GOOS,
    40  // runtime.GOARCH, race.Enabled, reading the OS version, checking CPU
    41  // feature flags with internal/cpu, etc. over parsing builder names when
    42  // possible. When matching builder names, prefer a fuzzy match instead
    43  // of a strict comparison.
    44  // If the test is not running on the build infrastructure,
    45  // Builder returns the empty string.
    46  func Builder() string {
    47  	return os.Getenv("GO_BUILDER_NAME")
    48  }
    49  
    50  // HasGoBuild reports whether the current system can build programs with “go build”
    51  // and then run them with os.StartProcess or exec.Command.
    52  func HasGoBuild() bool {
    53  	if os.Getenv("GO_GCFLAGS") != "" {
    54  		// It's too much work to require every caller of the go command
    55  		// to pass along "-gcflags="+os.Getenv("GO_GCFLAGS").
    56  		// For now, if $GO_GCFLAGS is set, report that we simply can't
    57  		// run go build.
    58  		return false
    59  	}
    60  
    61  	return tryGoBuild() == nil
    62  }
    63  
    64  var tryGoBuild = sync.OnceValue(func() error {
    65  	// To run 'go build', we need to be able to exec a 'go' command.
    66  	// We somewhat arbitrarily choose to exec 'go tool -n compile' because that
    67  	// also confirms that cmd/go can find the compiler. (Before CL 472096,
    68  	// we sometimes ended up with cmd/go installed in the test environment
    69  	// without a cmd/compile it could use to actually build things.)
    70  	goTool, err := goTool()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	cmd := exec.Command(goTool, "tool", "-n", "compile")
    75  	cmd.Env = origEnv
    76  	out, err := cmd.Output()
    77  	if err != nil {
    78  		return fmt.Errorf("%v: %w", cmd, err)
    79  	}
    80  	out = bytes.TrimSpace(out)
    81  	if len(out) == 0 {
    82  		return fmt.Errorf("%v: no tool reported", cmd)
    83  	}
    84  	if _, err := exec.LookPath(string(out)); err != nil {
    85  		return err
    86  	}
    87  
    88  	if platform.MustLinkExternal(runtime.GOOS, runtime.GOARCH, false) {
    89  		// We can assume that we always have a complete Go toolchain available.
    90  		// However, this platform requires a C linker to build even pure Go
    91  		// programs, including tests. Do we have one in the test environment?
    92  		// (On Android, for example, the device running the test might not have a
    93  		// C toolchain installed.)
    94  		//
    95  		// If CC is set explicitly, assume that we do. Otherwise, use 'go env CC'
    96  		// to determine which toolchain it would use by default.
    97  		if os.Getenv("CC") == "" {
    98  			cmd := exec.Command(goTool, "env", "CC")
    99  			cmd.Env = origEnv
   100  			out, err := cmd.Output()
   101  			if err != nil {
   102  				return fmt.Errorf("%v: %w", cmd, err)
   103  			}
   104  			out = bytes.TrimSpace(out)
   105  			if len(out) == 0 {
   106  				return fmt.Errorf("%v: no CC reported", cmd)
   107  			}
   108  			_, err = exec.LookPath(string(out))
   109  			return err
   110  		}
   111  	}
   112  	return nil
   113  })
   114  
   115  // MustHaveGoBuild checks that the current system can build programs with “go build”
   116  // and then run them with os.StartProcess or exec.Command.
   117  // If not, MustHaveGoBuild calls t.Skip with an explanation.
   118  func MustHaveGoBuild(t testing.TB) {
   119  	if os.Getenv("GO_GCFLAGS") != "" {
   120  		t.Helper()
   121  		t.Skipf("skipping test: 'go build' not compatible with setting $GO_GCFLAGS")
   122  	}
   123  	if !HasGoBuild() {
   124  		t.Helper()
   125  		t.Skipf("skipping test: 'go build' unavailable: %v", tryGoBuild())
   126  	}
   127  }
   128  
   129  // HasGoRun reports whether the current system can run programs with “go run”.
   130  func HasGoRun() bool {
   131  	// For now, having go run and having go build are the same.
   132  	return HasGoBuild()
   133  }
   134  
   135  // MustHaveGoRun checks that the current system can run programs with “go run”.
   136  // If not, MustHaveGoRun calls t.Skip with an explanation.
   137  func MustHaveGoRun(t testing.TB) {
   138  	if !HasGoRun() {
   139  		t.Helper()
   140  		t.Skipf("skipping test: 'go run' not available on %s/%s", runtime.GOOS, runtime.GOARCH)
   141  	}
   142  }
   143  
   144  // HasParallelism reports whether the current system can execute multiple
   145  // threads in parallel.
   146  // There is a copy of this function in cmd/dist/test.go.
   147  func HasParallelism() bool {
   148  	switch runtime.GOOS {
   149  	case "js", "wasip1":
   150  		return false
   151  	}
   152  	return true
   153  }
   154  
   155  // MustHaveParallelism checks that the current system can execute multiple
   156  // threads in parallel. If not, MustHaveParallelism calls t.Skip with an explanation.
   157  func MustHaveParallelism(t testing.TB) {
   158  	if !HasParallelism() {
   159  		t.Helper()
   160  		t.Skipf("skipping test: no parallelism available on %s/%s", runtime.GOOS, runtime.GOARCH)
   161  	}
   162  }
   163  
   164  // GoToolPath reports the path to the Go tool.
   165  // It is a convenience wrapper around GoTool.
   166  // If the tool is unavailable GoToolPath calls t.Skip.
   167  // If the tool should be available and isn't, GoToolPath calls t.Fatal.
   168  func GoToolPath(t testing.TB) string {
   169  	MustHaveGoBuild(t)
   170  	path, err := GoTool()
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	// Add all environment variables that affect the Go command to test metadata.
   175  	// Cached test results will be invalidate when these variables change.
   176  	// See golang.org/issue/32285.
   177  	for _, envVar := range strings.Fields(cfg.KnownEnv) {
   178  		os.Getenv(envVar)
   179  	}
   180  	return path
   181  }
   182  
   183  var findGOROOT = sync.OnceValues(func() (path string, err error) {
   184  	if path := runtime.GOROOT(); path != "" {
   185  		// If runtime.GOROOT() is non-empty, assume that it is valid.
   186  		//
   187  		// (It might not be: for example, the user may have explicitly set GOROOT
   188  		// to the wrong directory. But this case is
   189  		// rare, and if that happens the user can fix what they broke.)
   190  		return path, nil
   191  	}
   192  
   193  	// runtime.GOROOT doesn't know where GOROOT is (perhaps because the test
   194  	// binary was built with -trimpath).
   195  	//
   196  	// Since this is internal/testenv, we can cheat and assume that the caller
   197  	// is a test of some package in a subdirectory of GOROOT/src. ('go test'
   198  	// runs the test in the directory containing the packaged under test.) That
   199  	// means that if we start walking up the tree, we should eventually find
   200  	// GOROOT/src/go.mod, and we can report the parent directory of that.
   201  	//
   202  	// Notably, this works even if we can't run 'go env GOROOT' as a
   203  	// subprocess.
   204  
   205  	cwd, err := os.Getwd()
   206  	if err != nil {
   207  		return "", fmt.Errorf("finding GOROOT: %w", err)
   208  	}
   209  
   210  	dir := cwd
   211  	for {
   212  		parent := filepath.Dir(dir)
   213  		if parent == dir {
   214  			// dir is either "." or only a volume name.
   215  			return "", fmt.Errorf("failed to locate GOROOT/src in any parent directory")
   216  		}
   217  
   218  		if base := filepath.Base(dir); base != "src" {
   219  			dir = parent
   220  			continue // dir cannot be GOROOT/src if it doesn't end in "src".
   221  		}
   222  
   223  		b, err := os.ReadFile(filepath.Join(dir, "go.mod"))
   224  		if err != nil {
   225  			if os.IsNotExist(err) {
   226  				dir = parent
   227  				continue
   228  			}
   229  			return "", fmt.Errorf("finding GOROOT: %w", err)
   230  		}
   231  		goMod := string(b)
   232  
   233  		for goMod != "" {
   234  			var line string
   235  			line, goMod, _ = strings.Cut(goMod, "\n")
   236  			fields := strings.Fields(line)
   237  			if len(fields) >= 2 && fields[0] == "module" && fields[1] == "std" {
   238  				// Found "module std", which is the module declaration in GOROOT/src!
   239  				return parent, nil
   240  			}
   241  		}
   242  	}
   243  })
   244  
   245  // GOROOT reports the path to the directory containing the root of the Go
   246  // project source tree. This is normally equivalent to runtime.GOROOT, but
   247  // works even if the test binary was built with -trimpath and cannot exec
   248  // 'go env GOROOT'.
   249  //
   250  // If GOROOT cannot be found, GOROOT skips t if t is non-nil,
   251  // or panics otherwise.
   252  func GOROOT(t testing.TB) string {
   253  	path, err := findGOROOT()
   254  	if err != nil {
   255  		if t == nil {
   256  			panic(err)
   257  		}
   258  		t.Helper()
   259  		t.Skip(err)
   260  	}
   261  	return path
   262  }
   263  
   264  // GoTool reports the path to the Go tool.
   265  func GoTool() (string, error) {
   266  	if !HasGoBuild() {
   267  		return "", errors.New("platform cannot run go tool")
   268  	}
   269  	return goTool()
   270  }
   271  
   272  var goTool = sync.OnceValues(func() (string, error) {
   273  	return exec.LookPath("go")
   274  })
   275  
   276  // MustHaveSource checks that the entire source tree is available under GOROOT.
   277  // If not, it calls t.Skip with an explanation.
   278  func MustHaveSource(t testing.TB) {
   279  	switch runtime.GOOS {
   280  	case "ios":
   281  		t.Helper()
   282  		t.Skip("skipping test: no source tree on " + runtime.GOOS)
   283  	}
   284  }
   285  
   286  // HasExternalNetwork reports whether the current system can use
   287  // external (non-localhost) networks.
   288  func HasExternalNetwork() bool {
   289  	return !testing.Short() && runtime.GOOS != "js" && runtime.GOOS != "wasip1"
   290  }
   291  
   292  // MustHaveExternalNetwork checks that the current system can use
   293  // external (non-localhost) networks.
   294  // If not, MustHaveExternalNetwork calls t.Skip with an explanation.
   295  func MustHaveExternalNetwork(t testing.TB) {
   296  	if runtime.GOOS == "js" || runtime.GOOS == "wasip1" {
   297  		t.Helper()
   298  		t.Skipf("skipping test: no external network on %s", runtime.GOOS)
   299  	}
   300  	if testing.Short() {
   301  		t.Helper()
   302  		t.Skipf("skipping test: no external network in -short mode")
   303  	}
   304  }
   305  
   306  // HasCGO reports whether the current system can use cgo.
   307  func HasCGO() bool {
   308  	return hasCgo()
   309  }
   310  
   311  var hasCgo = sync.OnceValue(func() bool {
   312  	goTool, err := goTool()
   313  	if err != nil {
   314  		return false
   315  	}
   316  	cmd := exec.Command(goTool, "env", "CGO_ENABLED")
   317  	cmd.Env = origEnv
   318  	out, err := cmd.Output()
   319  	if err != nil {
   320  		panic(fmt.Sprintf("%v: %v", cmd, out))
   321  	}
   322  	ok, err := strconv.ParseBool(string(bytes.TrimSpace(out)))
   323  	if err != nil {
   324  		panic(fmt.Sprintf("%v: non-boolean output %q", cmd, out))
   325  	}
   326  	return ok
   327  })
   328  
   329  // MustHaveCGO calls t.Skip if cgo is not available.
   330  func MustHaveCGO(t testing.TB) {
   331  	if !HasCGO() {
   332  		t.Helper()
   333  		t.Skipf("skipping test: no cgo")
   334  	}
   335  }
   336  
   337  // CanInternalLink reports whether the current system can link programs with
   338  // internal linking.
   339  func CanInternalLink(withCgo bool) bool {
   340  	return !platform.MustLinkExternal(runtime.GOOS, runtime.GOARCH, withCgo)
   341  }
   342  
   343  // SpecialBuildTypes are interesting build types that may affect linking.
   344  type SpecialBuildTypes struct {
   345  	Cgo  bool
   346  	Asan bool
   347  	Msan bool
   348  	Race bool
   349  }
   350  
   351  // NoSpecialBuildTypes indicates a standard, no cgo go build.
   352  var NoSpecialBuildTypes SpecialBuildTypes
   353  
   354  // MustInternalLink checks that the current system can link programs with internal
   355  // linking.
   356  // If not, MustInternalLink calls t.Skip with an explanation.
   357  func MustInternalLink(t testing.TB, with SpecialBuildTypes) {
   358  	if with.Asan || with.Msan || with.Race {
   359  		t.Skipf("skipping test: internal linking with sanitizers is not supported")
   360  	}
   361  	if !CanInternalLink(with.Cgo) {
   362  		t.Helper()
   363  		if with.Cgo && CanInternalLink(false) {
   364  			t.Skipf("skipping test: internal linking on %s/%s is not supported with cgo", runtime.GOOS, runtime.GOARCH)
   365  		}
   366  		t.Skipf("skipping test: internal linking on %s/%s is not supported", runtime.GOOS, runtime.GOARCH)
   367  	}
   368  }
   369  
   370  // MustInternalLinkPIE checks whether the current system can link PIE binary using
   371  // internal linking.
   372  // If not, MustInternalLinkPIE calls t.Skip with an explanation.
   373  func MustInternalLinkPIE(t testing.TB) {
   374  	if !platform.InternalLinkPIESupported(runtime.GOOS, runtime.GOARCH) {
   375  		t.Helper()
   376  		t.Skipf("skipping test: internal linking for buildmode=pie on %s/%s is not supported", runtime.GOOS, runtime.GOARCH)
   377  	}
   378  }
   379  
   380  // MustHaveBuildMode reports whether the current system can build programs in
   381  // the given build mode.
   382  // If not, MustHaveBuildMode calls t.Skip with an explanation.
   383  func MustHaveBuildMode(t testing.TB, buildmode string) {
   384  	if !platform.BuildModeSupported(runtime.Compiler, buildmode, runtime.GOOS, runtime.GOARCH) {
   385  		t.Helper()
   386  		t.Skipf("skipping test: build mode %s on %s/%s is not supported by the %s compiler", buildmode, runtime.GOOS, runtime.GOARCH, runtime.Compiler)
   387  	}
   388  }
   389  
   390  // HasSymlink reports whether the current system can use os.Symlink.
   391  func HasSymlink() bool {
   392  	ok, _ := hasSymlink()
   393  	return ok
   394  }
   395  
   396  // MustHaveSymlink reports whether the current system can use os.Symlink.
   397  // If not, MustHaveSymlink calls t.Skip with an explanation.
   398  func MustHaveSymlink(t testing.TB) {
   399  	ok, reason := hasSymlink()
   400  	if !ok {
   401  		t.Helper()
   402  		t.Skipf("skipping test: cannot make symlinks on %s/%s: %s", runtime.GOOS, runtime.GOARCH, reason)
   403  	}
   404  }
   405  
   406  // HasLink reports whether the current system can use os.Link.
   407  func HasLink() bool {
   408  	// From Android release M (Marshmallow), hard linking files is blocked
   409  	// and an attempt to call link() on a file will return EACCES.
   410  	// - https://code.google.com/p/android-developer-preview/issues/detail?id=3150
   411  	return runtime.GOOS != "plan9" && runtime.GOOS != "android"
   412  }
   413  
   414  // MustHaveLink reports whether the current system can use os.Link.
   415  // If not, MustHaveLink calls t.Skip with an explanation.
   416  func MustHaveLink(t testing.TB) {
   417  	if !HasLink() {
   418  		t.Helper()
   419  		t.Skipf("skipping test: hardlinks are not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
   420  	}
   421  }
   422  
   423  var flaky = flag.Bool("flaky", false, "run known-flaky tests too")
   424  
   425  func SkipFlaky(t testing.TB, issue int) {
   426  	if !*flaky {
   427  		t.Helper()
   428  		t.Skipf("skipping known flaky test without the -flaky flag; see golang.org/issue/%d", issue)
   429  	}
   430  }
   431  
   432  func SkipFlakyNet(t testing.TB) {
   433  	if v, _ := strconv.ParseBool(os.Getenv("GO_BUILDER_FLAKY_NET")); v {
   434  		t.Helper()
   435  		t.Skip("skipping test on builder known to have frequent network failures")
   436  	}
   437  }
   438  
   439  // CPUIsSlow reports whether the CPU running the test is suspected to be slow.
   440  func CPUIsSlow() bool {
   441  	switch runtime.GOARCH {
   442  	case "arm", "mips", "mipsle", "mips64", "mips64le", "wasm":
   443  		return true
   444  	}
   445  	return false
   446  }
   447  
   448  // SkipIfShortAndSlow skips t if -short is set and the CPU running the test is
   449  // suspected to be slow.
   450  //
   451  // (This is useful for CPU-intensive tests that otherwise complete quickly.)
   452  func SkipIfShortAndSlow(t testing.TB) {
   453  	if testing.Short() && CPUIsSlow() {
   454  		t.Helper()
   455  		t.Skipf("skipping test in -short mode on %s", runtime.GOARCH)
   456  	}
   457  }
   458  
   459  // SkipIfOptimizationOff skips t if optimization is disabled.
   460  func SkipIfOptimizationOff(t testing.TB) {
   461  	if OptimizationOff() {
   462  		t.Helper()
   463  		t.Skip("skipping test with optimization disabled")
   464  	}
   465  }
   466  
   467  // WriteImportcfg writes an importcfg file used by the compiler or linker to
   468  // dstPath containing entries for the file mappings in packageFiles, as well
   469  // as for the packages transitively imported by the package(s) in pkgs.
   470  //
   471  // pkgs may include any package pattern that is valid to pass to 'go list',
   472  // so it may also be a list of Go source files all in the same directory.
   473  func WriteImportcfg(t testing.TB, dstPath string, packageFiles map[string]string, pkgs ...string) {
   474  	t.Helper()
   475  
   476  	icfg := new(bytes.Buffer)
   477  	icfg.WriteString("# import config\n")
   478  	for k, v := range packageFiles {
   479  		fmt.Fprintf(icfg, "packagefile %s=%s\n", k, v)
   480  	}
   481  
   482  	if len(pkgs) > 0 {
   483  		// Use 'go list' to resolve any missing packages and rewrite the import map.
   484  		cmd := Command(t, GoToolPath(t), "list", "-export", "-deps", "-f", `{{if ne .ImportPath "command-line-arguments"}}{{if .Export}}{{.ImportPath}}={{.Export}}{{end}}{{end}}`)
   485  		cmd.Args = append(cmd.Args, pkgs...)
   486  		cmd.Stderr = new(strings.Builder)
   487  		out, err := cmd.Output()
   488  		if err != nil {
   489  			t.Fatalf("%v: %v\n%s", cmd, err, cmd.Stderr)
   490  		}
   491  
   492  		for line := range strings.SplitSeq(string(out), "\n") {
   493  			if line == "" {
   494  				continue
   495  			}
   496  			importPath, export, ok := strings.Cut(line, "=")
   497  			if !ok {
   498  				t.Fatalf("invalid line in output from %v:\n%s", cmd, line)
   499  			}
   500  			if packageFiles[importPath] == "" {
   501  				fmt.Fprintf(icfg, "packagefile %s=%s\n", importPath, export)
   502  			}
   503  		}
   504  	}
   505  
   506  	if err := os.WriteFile(dstPath, icfg.Bytes(), 0666); err != nil {
   507  		t.Fatal(err)
   508  	}
   509  }
   510  
   511  // SyscallIsNotSupported reports whether err may indicate that a system call is
   512  // not supported by the current platform or execution environment.
   513  func SyscallIsNotSupported(err error) bool {
   514  	return syscallIsNotSupported(err)
   515  }
   516  
   517  // ParallelOn64Bit calls t.Parallel() unless there is a case that cannot be parallel.
   518  // This function should be used when it is necessary to avoid t.Parallel on
   519  // 32-bit machines, typically because the test uses lots of memory.
   520  func ParallelOn64Bit(t *testing.T) {
   521  	if goarch.PtrSize == 4 {
   522  		return
   523  	}
   524  	t.Parallel()
   525  }
   526  
   527  // CPUProfilingBroken returns true if CPU profiling has known issues on this
   528  // platform.
   529  func CPUProfilingBroken() bool {
   530  	switch runtime.GOOS {
   531  	case "plan9":
   532  		// Profiling unimplemented.
   533  		return true
   534  	case "aix":
   535  		// See https://golang.org/issue/45170.
   536  		return true
   537  	case "ios", "dragonfly", "netbsd", "illumos", "solaris":
   538  		// See https://golang.org/issue/13841.
   539  		return true
   540  	case "openbsd":
   541  		if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
   542  			// See https://golang.org/issue/13841.
   543  			return true
   544  		}
   545  	}
   546  
   547  	return false
   548  }
   549  

View as plain text