| 1 |
// Copyright 2019 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 contains helper functions for skipping tests |
| 6 |
// based on which tools are present in the environment. |
| 7 |
package testenv |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"fmt" |
| 12 |
"go/build" |
| 13 |
"os" |
| 14 |
"runtime" |
| 15 |
"strings" |
| 16 |
"sync" |
| 17 |
"time" |
| 18 |
|
| 19 |
exec "golang.org/x/sys/execabs" |
| 20 |
) |
| 21 |
|
| 22 |
// Testing is an abstraction of a *testing.T. |
| 23 |
type Testing interface { |
| 24 |
Skipf(format string, args ...any) |
| 25 |
Fatalf(format string, args ...any) |
| 26 |
} |
| 27 |
|
| 28 |
type helperer interface { |
| 29 |
Helper() |
| 30 |
} |
| 31 |
|
| 32 |
// packageMainIsDevel reports whether the module containing package main |
| 33 |
// is a development version (if module information is available). |
| 34 |
// |
| 35 |
// Builds in GOPATH mode and builds that lack module information are assumed to |
| 36 |
// be development versions. |
| 37 |
var packageMainIsDevel = func() bool { return true } |
| 38 |
|
| 39 |
var checkGoGoroot struct { |
| 40 |
once sync.Once |
| 41 |
err error |
| 42 |
} |
| 43 |
|
| 44 |
func hasTool(tool string) error { |
| 45 |
if tool == "cgo" { |
| 46 |
enabled, err := cgoEnabled(false) |
| 47 |
if err != nil { |
| 48 |
return fmt.Errorf("checking cgo: %v", err) |
| 49 |
} |
| 50 |
if !enabled { |
| 51 |
return fmt.Errorf("cgo not enabled") |
| 52 |
} |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
_, err := exec.LookPath(tool) |
| 57 |
if err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
|
| 61 |
switch tool { |
| 62 |
case "patch": |
| 63 |
// check that the patch tools supports the -o argument |
| 64 |
temp, err := os.CreateTemp("", "patch-test") |
| 65 |
if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
temp.Close() |
| 69 |
defer os.Remove(temp.Name()) |
| 70 |
cmd := exec.Command(tool, "-o", temp.Name()) |
| 71 |
if err := cmd.Run(); err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
|
| 75 |
case "go": |
| 76 |
checkGoGoroot.once.Do(func() { |
| 77 |
// Ensure that the 'go' command found by exec.LookPath is from the correct |
| 78 |
// GOROOT. Otherwise, 'some/path/go test ./...' will test against some |
| 79 |
// version of the 'go' binary other than 'some/path/go', which is almost |
| 80 |
// certainly not what the user intended. |
| 81 |
out, err := exec.Command(tool, "env", "GOROOT").CombinedOutput() |
| 82 |
if err != nil { |
| 83 |
checkGoGoroot.err = err |
| 84 |
return |
| 85 |
} |
| 86 |
GOROOT := strings.TrimSpace(string(out)) |
| 87 |
//lint:ignore SA1019 runtime.GOROOT is deprecated, specifically to allow copying binaries. What should we do |
| 88 |
// about this? |
| 89 |
if runtimeGOROOT := runtime.GOROOT(); GOROOT != runtimeGOROOT { |
| 90 |
checkGoGoroot.err = fmt.Errorf("'go env GOROOT' does not match runtime.GOROOT:\n\tgo env: %s\n\tGOROOT: %s", GOROOT, runtimeGOROOT) |
| 91 |
} |
| 92 |
}) |
| 93 |
if checkGoGoroot.err != nil { |
| 94 |
return checkGoGoroot.err |
| 95 |
} |
| 96 |
|
| 97 |
case "diff": |
| 98 |
// Check that diff is the GNU version, needed for the -u argument and |
| 99 |
// to report missing newlines at the end of files. |
| 100 |
out, err := exec.Command(tool, "-version").Output() |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
if !bytes.Contains(out, []byte("GNU diffutils")) { |
| 105 |
return fmt.Errorf("diff is not the GNU version") |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
func cgoEnabled(bypassEnvironment bool) (bool, error) { |
| 113 |
cmd := exec.Command("go", "env", "CGO_ENABLED") |
| 114 |
if bypassEnvironment { |
| 115 |
cmd.Env = append(os.Environ(), "CGO_ENABLED=") |
| 116 |
} |
| 117 |
out, err := cmd.CombinedOutput() |
| 118 |
if err != nil { |
| 119 |
return false, err |
| 120 |
} |
| 121 |
enabled := strings.TrimSpace(string(out)) |
| 122 |
return enabled == "1", nil |
| 123 |
} |
| 124 |
|
| 125 |
func allowMissingTool(tool string) bool { |
| 126 |
if runtime.GOOS == "android" { |
| 127 |
// Android builds generally run tests on a separate machine from the build, |
| 128 |
// so don't expect any external tools to be available. |
| 129 |
return true |
| 130 |
} |
| 131 |
|
| 132 |
switch tool { |
| 133 |
case "cgo": |
| 134 |
if strings.HasSuffix(os.Getenv("GO_BUILDER_NAME"), "-nocgo") { |
| 135 |
// Explicitly disabled on -nocgo builders. |
| 136 |
return true |
| 137 |
} |
| 138 |
if enabled, err := cgoEnabled(true); err == nil && !enabled { |
| 139 |
// No platform support. |
| 140 |
return true |
| 141 |
} |
| 142 |
case "go": |
| 143 |
if os.Getenv("GO_BUILDER_NAME") == "illumos-amd64-joyent" { |
| 144 |
// Work around a misconfigured builder (see https://golang.org/issue/33950). |
| 145 |
return true |
| 146 |
} |
| 147 |
case "diff": |
| 148 |
if os.Getenv("GO_BUILDER_NAME") != "" { |
| 149 |
return true |
| 150 |
} |
| 151 |
case "patch": |
| 152 |
if os.Getenv("GO_BUILDER_NAME") != "" { |
| 153 |
return true |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// If a developer is actively working on this test, we expect them to have all |
| 158 |
// of its dependencies installed. However, if it's just a dependency of some |
| 159 |
// other module (for example, being run via 'go test all'), we should be more |
| 160 |
// tolerant of unusual environments. |
| 161 |
return !packageMainIsDevel() |
| 162 |
} |
| 163 |
|
| 164 |
// NeedsTool skips t if the named tool is not present in the path. |
| 165 |
// As a special case, "cgo" means "go" is present and can compile cgo programs. |
| 166 |
func NeedsTool(t Testing, tool string) { |
| 167 |
if t, ok := t.(helperer); ok { |
| 168 |
t.Helper() |
| 169 |
} |
| 170 |
err := hasTool(tool) |
| 171 |
if err == nil { |
| 172 |
return |
| 173 |
} |
| 174 |
if allowMissingTool(tool) { |
| 175 |
t.Skipf("skipping because %s tool not available: %v", tool, err) |
| 176 |
} else { |
| 177 |
t.Fatalf("%s tool not available: %v", tool, err) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// NeedsGoPackages skips t if the go/packages driver (or 'go' tool) implied by |
| 182 |
// the current process environment is not present in the path. |
| 183 |
func NeedsGoPackages(t Testing) { |
| 184 |
if t, ok := t.(helperer); ok { |
| 185 |
t.Helper() |
| 186 |
} |
| 187 |
|
| 188 |
tool := os.Getenv("GOPACKAGESDRIVER") |
| 189 |
switch tool { |
| 190 |
case "off": |
| 191 |
// "off" forces go/packages to use the go command. |
| 192 |
tool = "go" |
| 193 |
case "": |
| 194 |
if _, err := exec.LookPath("gopackagesdriver"); err == nil { |
| 195 |
tool = "gopackagesdriver" |
| 196 |
} else { |
| 197 |
tool = "go" |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
NeedsTool(t, tool) |
| 202 |
} |
| 203 |
|
| 204 |
// NeedsGoPackagesEnv skips t if the go/packages driver (or 'go' tool) implied |
| 205 |
// by env is not present in the path. |
| 206 |
func NeedsGoPackagesEnv(t Testing, env []string) { |
| 207 |
if t, ok := t.(helperer); ok { |
| 208 |
t.Helper() |
| 209 |
} |
| 210 |
|
| 211 |
for _, v := range env { |
| 212 |
if after, ok := strings.CutPrefix(v, "GOPACKAGESDRIVER="); ok { |
| 213 |
tool := after |
| 214 |
if tool == "off" { |
| 215 |
NeedsTool(t, "go") |
| 216 |
} else { |
| 217 |
NeedsTool(t, tool) |
| 218 |
} |
| 219 |
return |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
NeedsGoPackages(t) |
| 224 |
} |
| 225 |
|
| 226 |
// NeedsGoBuild skips t if the current system can't build programs with “go build” |
| 227 |
// and then run them with os.StartProcess or exec.Command. |
| 228 |
// android, and darwin/arm systems don't have the userspace go build needs to run, |
| 229 |
// and js/wasm doesn't support running subprocesses. |
| 230 |
func NeedsGoBuild(t Testing) { |
| 231 |
if t, ok := t.(helperer); ok { |
| 232 |
t.Helper() |
| 233 |
} |
| 234 |
|
| 235 |
NeedsTool(t, "go") |
| 236 |
|
| 237 |
switch runtime.GOOS { |
| 238 |
case "android", "js": |
| 239 |
t.Skipf("skipping test: %v can't build and run Go binaries", runtime.GOOS) |
| 240 |
case "darwin": |
| 241 |
if strings.HasPrefix(runtime.GOARCH, "arm") { |
| 242 |
t.Skipf("skipping test: darwin/arm can't build and run Go binaries") |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// ExitIfSmallMachine emits a helpful diagnostic and calls os.Exit(0) if the |
| 248 |
// current machine is a builder known to have scarce resources. |
| 249 |
// |
| 250 |
// It should be called from within a TestMain function. |
| 251 |
func ExitIfSmallMachine() { |
| 252 |
switch b := os.Getenv("GO_BUILDER_NAME"); b { |
| 253 |
case "linux-arm-scaleway": |
| 254 |
// "linux-arm" was renamed to "linux-arm-scaleway" in CL 303230. |
| 255 |
fmt.Fprintln(os.Stderr, "skipping test: linux-arm-scaleway builder lacks sufficient memory (https://golang.org/issue/32834)") |
| 256 |
case "plan9-arm": |
| 257 |
fmt.Fprintln(os.Stderr, "skipping test: plan9-arm builder lacks sufficient memory (https://golang.org/issue/38772)") |
| 258 |
case "netbsd-arm-bsiegert", "netbsd-arm64-bsiegert": |
| 259 |
// As of 2021-06-02, these builders are running with GO_TEST_TIMEOUT_SCALE=10, |
| 260 |
// and there is only one of each. We shouldn't waste those scarce resources |
| 261 |
// running very slow tests. |
| 262 |
fmt.Fprintf(os.Stderr, "skipping test: %s builder is very slow\n", b) |
| 263 |
case "dragonfly-amd64": |
| 264 |
// As of 2021-11-02, this builder is running with GO_TEST_TIMEOUT_SCALE=2, |
| 265 |
// and seems to have unusually slow disk performance. |
| 266 |
fmt.Fprintln(os.Stderr, "skipping test: dragonfly-amd64 has slow disk (https://golang.org/issue/45216)") |
| 267 |
case "linux-riscv64-unmatched": |
| 268 |
// As of 2021-11-03, this builder is empirically not fast enough to run |
| 269 |
// gopls tests. Ideally we should make the tests faster in short mode |
| 270 |
// and/or fix them to not assume arbitrary deadlines. |
| 271 |
// For now, we'll skip them instead. |
| 272 |
fmt.Fprintf(os.Stderr, "skipping test: %s builder is too slow (https://golang.org/issue/49321)\n", b) |
| 273 |
default: |
| 274 |
return |
| 275 |
} |
| 276 |
os.Exit(0) |
| 277 |
} |
| 278 |
|
| 279 |
// Go1Point returns the x in Go 1.x. |
| 280 |
func Go1Point() int { |
| 281 |
for i := len(build.Default.ReleaseTags) - 1; i >= 0; i-- { |
| 282 |
var version int |
| 283 |
if _, err := fmt.Sscanf(build.Default.ReleaseTags[i], "go1.%d", &version); err != nil { |
| 284 |
continue |
| 285 |
} |
| 286 |
return version |
| 287 |
} |
| 288 |
panic("bad release tags") |
| 289 |
} |
| 290 |
|
| 291 |
// NeedsGo1Point skips t if the Go version used to run the test is older than |
| 292 |
// 1.x. |
| 293 |
func NeedsGo1Point(t Testing, x int) { |
| 294 |
if t, ok := t.(helperer); ok { |
| 295 |
t.Helper() |
| 296 |
} |
| 297 |
if Go1Point() < x { |
| 298 |
t.Skipf("running Go version %q is version 1.%d, older than required 1.%d", runtime.Version(), Go1Point(), x) |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// SkipAfterGo1Point skips t if the Go version used to run the test is newer than |
| 303 |
// 1.x. |
| 304 |
func SkipAfterGo1Point(t Testing, x int) { |
| 305 |
if t, ok := t.(helperer); ok { |
| 306 |
t.Helper() |
| 307 |
} |
| 308 |
if Go1Point() > x { |
| 309 |
t.Skipf("running Go version %q is version 1.%d, newer than maximum 1.%d", runtime.Version(), Go1Point(), x) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
// Deadline returns the deadline of t, if known, |
| 314 |
// using the Deadline method added in Go 1.15. |
| 315 |
func Deadline(t Testing) (time.Time, bool) { |
| 316 |
td, ok := t.(interface { |
| 317 |
Deadline() (time.Time, bool) |
| 318 |
}) |
| 319 |
if !ok { |
| 320 |
return time.Time{}, false |
| 321 |
} |
| 322 |
return td.Deadline() |
| 323 |
} |