github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/cmd/dist/test.go (about)

     1  // Copyright 2015 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"flag"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"log"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  	"regexp"
    18  	"strconv"
    19  	"strings"
    20  	"time"
    21  )
    22  
    23  func cmdtest() {
    24  	var t tester
    25  	flag.BoolVar(&t.listMode, "list", false, "list available tests")
    26  	flag.BoolVar(&t.noRebuild, "no-rebuild", false, "don't rebuild std and cmd packages")
    27  	flag.BoolVar(&t.keepGoing, "k", false, "keep going even when error occurred")
    28  	flag.BoolVar(&t.race, "race", false, "run in race builder mode (different set of tests)")
    29  	flag.StringVar(&t.banner, "banner", "##### ", "banner prefix; blank means no section banners")
    30  	flag.StringVar(&t.runRxStr, "run", os.Getenv("GOTESTONLY"),
    31  		"run only those tests matching the regular expression; empty means to run all. "+
    32  			"Special exception: if the string begins with '!', the match is inverted.")
    33  	xflagparse(-1) // any number of args
    34  	t.run()
    35  }
    36  
    37  // tester executes cmdtest.
    38  type tester struct {
    39  	race      bool
    40  	listMode  bool
    41  	noRebuild bool
    42  	keepGoing bool
    43  	runRxStr  string
    44  	runRx     *regexp.Regexp
    45  	runRxWant bool     // want runRx to match (true) or not match (false)
    46  	runNames  []string // tests to run, exclusive with runRx; empty means all
    47  	banner    string   // prefix, or "" for none
    48  
    49  	goroot     string
    50  	goarch     string
    51  	gohostarch string
    52  	goos       string
    53  	gohostos   string
    54  	cgoEnabled bool
    55  	partial    bool
    56  	haveTime   bool // the 'time' binary is available
    57  
    58  	tests        []distTest
    59  	timeoutScale int
    60  }
    61  
    62  // A distTest is a test run by dist test.
    63  // Each test has a unique name and belongs to a group (heading)
    64  type distTest struct {
    65  	name    string // unique test name; may be filtered with -run flag
    66  	heading string // group section; this header is printed before the test is run.
    67  	fn      func() error
    68  }
    69  
    70  func mustEnv(k string) string {
    71  	v := os.Getenv(k)
    72  	if v == "" {
    73  		log.Fatalf("Unset environment variable %v", k)
    74  	}
    75  	return v
    76  }
    77  
    78  func (t *tester) run() {
    79  	t.goroot = mustEnv("GOROOT")
    80  	t.goos = mustEnv("GOOS")
    81  	t.gohostos = mustEnv("GOHOSTOS")
    82  	t.goarch = mustEnv("GOARCH")
    83  	t.gohostarch = mustEnv("GOHOSTARCH")
    84  	slurp, err := exec.Command("go", "env", "CGO_ENABLED").Output()
    85  	if err != nil {
    86  		log.Fatalf("Error running go env CGO_ENABLED: %v", err)
    87  	}
    88  	t.cgoEnabled, _ = strconv.ParseBool(strings.TrimSpace(string(slurp)))
    89  	if flag.NArg() > 0 && t.runRxStr != "" {
    90  		log.Fatalf("the -run regular expression flag is mutually exclusive with test name arguments")
    91  	}
    92  	t.runNames = flag.Args()
    93  
    94  	if t.hasBash() {
    95  		if _, err := exec.LookPath("time"); err == nil {
    96  			t.haveTime = true
    97  		}
    98  	}
    99  
   100  	if !t.noRebuild {
   101  		t.out("Building packages and commands.")
   102  		cmd := exec.Command("go", "install", "-a", "-v", "std", "cmd")
   103  		cmd.Stdout = os.Stdout
   104  		cmd.Stderr = os.Stderr
   105  		if err := cmd.Run(); err != nil {
   106  			log.Fatalf("building packages and commands: %v", err)
   107  		}
   108  	}
   109  
   110  	if t.iOS() {
   111  		// Install the Mach exception handler used to intercept
   112  		// EXC_BAD_ACCESS and convert it into a Go panic. This is
   113  		// necessary for a Go program running under lldb (the way
   114  		// we run tests). It is disabled by default because iOS
   115  		// apps are not allowed to access the exc_server symbol.
   116  		cmd := exec.Command("go", "install", "-a", "-tags", "lldb", "runtime/cgo")
   117  		cmd.Stdout = os.Stdout
   118  		cmd.Stderr = os.Stderr
   119  		if err := cmd.Run(); err != nil {
   120  			log.Fatalf("building mach exception handler: %v", err)
   121  		}
   122  
   123  		defer func() {
   124  			cmd := exec.Command("go", "install", "-a", "runtime/cgo")
   125  			cmd.Stdout = os.Stdout
   126  			cmd.Stderr = os.Stderr
   127  			if err := cmd.Run(); err != nil {
   128  				log.Fatalf("reverting mach exception handler: %v", err)
   129  			}
   130  		}()
   131  	}
   132  
   133  	t.timeoutScale = 1
   134  	if t.goarch == "arm" || t.goos == "windows" {
   135  		t.timeoutScale = 2
   136  	}
   137  	if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
   138  		t.timeoutScale, err = strconv.Atoi(s)
   139  		if err != nil {
   140  			log.Fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
   141  		}
   142  	}
   143  
   144  	if t.runRxStr != "" {
   145  		if t.runRxStr[0] == '!' {
   146  			t.runRxWant = false
   147  			t.runRxStr = t.runRxStr[1:]
   148  		} else {
   149  			t.runRxWant = true
   150  		}
   151  		t.runRx = regexp.MustCompile(t.runRxStr)
   152  	}
   153  
   154  	t.registerTests()
   155  	if t.listMode {
   156  		for _, tt := range t.tests {
   157  			fmt.Println(tt.name)
   158  		}
   159  		return
   160  	}
   161  
   162  	// we must unset GOROOT_FINAL before tests, because runtime/debug requires
   163  	// correct access to source code, so if we have GOROOT_FINAL in effect,
   164  	// at least runtime/debug test will fail.
   165  	os.Unsetenv("GOROOT_FINAL")
   166  
   167  	for _, name := range t.runNames {
   168  		if !t.isRegisteredTestName(name) {
   169  			log.Fatalf("unknown test %q", name)
   170  		}
   171  	}
   172  
   173  	var lastHeading string
   174  	ok := true
   175  	for _, dt := range t.tests {
   176  		if !t.shouldRunTest(dt.name) {
   177  			t.partial = true
   178  			continue
   179  		}
   180  		if dt.heading != "" && lastHeading != dt.heading {
   181  			lastHeading = dt.heading
   182  			t.out(dt.heading)
   183  		}
   184  		if vflag > 0 {
   185  			fmt.Printf("# go tool dist test -run=^%s$\n", dt.name)
   186  		}
   187  		if err := dt.fn(); err != nil {
   188  			ok = false
   189  			if t.keepGoing {
   190  				log.Printf("Failed: %v", err)
   191  			} else {
   192  				log.Fatalf("Failed: %v", err)
   193  			}
   194  		}
   195  	}
   196  	if !ok {
   197  		fmt.Println("\nFAILED")
   198  		os.Exit(1)
   199  	} else if t.partial {
   200  		fmt.Println("\nALL TESTS PASSED (some were excluded)")
   201  	} else {
   202  		fmt.Println("\nALL TESTS PASSED")
   203  	}
   204  }
   205  
   206  func (t *tester) shouldRunTest(name string) bool {
   207  	if t.runRx != nil {
   208  		return t.runRx.MatchString(name) == t.runRxWant
   209  	}
   210  	if len(t.runNames) == 0 {
   211  		return true
   212  	}
   213  	for _, runName := range t.runNames {
   214  		if runName == name {
   215  			return true
   216  		}
   217  	}
   218  	return false
   219  }
   220  
   221  func (t *tester) tags() string {
   222  	if t.iOS() {
   223  		return "-tags=lldb"
   224  	}
   225  	return "-tags="
   226  }
   227  
   228  func (t *tester) timeout(sec int) string {
   229  	return "-timeout=" + fmt.Sprint(time.Duration(sec)*time.Second*time.Duration(t.timeoutScale))
   230  }
   231  
   232  // ranGoTest and stdMatches are state closed over by the stdlib
   233  // testing func in registerStdTest below. The tests are run
   234  // sequentially, so there's no need for locks.
   235  //
   236  // ranGoBench and benchMatches are the same, but are only used
   237  // in -race mode.
   238  var (
   239  	ranGoTest  bool
   240  	stdMatches []string
   241  
   242  	ranGoBench   bool
   243  	benchMatches []string
   244  )
   245  
   246  func (t *tester) registerStdTest(pkg string) {
   247  	testName := "go_test:" + pkg
   248  	if t.runRx == nil || t.runRx.MatchString(testName) {
   249  		stdMatches = append(stdMatches, pkg)
   250  	}
   251  	t.tests = append(t.tests, distTest{
   252  		name:    testName,
   253  		heading: "Testing packages.",
   254  		fn: func() error {
   255  			if ranGoTest {
   256  				return nil
   257  			}
   258  			ranGoTest = true
   259  			args := []string{
   260  				"test",
   261  				"-short",
   262  				t.tags(),
   263  				t.timeout(180),
   264  				"-gcflags=" + os.Getenv("GO_GCFLAGS"),
   265  			}
   266  			if t.race {
   267  				args = append(args, "-race")
   268  			}
   269  			args = append(args, stdMatches...)
   270  			cmd := exec.Command("go", args...)
   271  			cmd.Stdout = os.Stdout
   272  			cmd.Stderr = os.Stderr
   273  			return cmd.Run()
   274  		},
   275  	})
   276  }
   277  
   278  func (t *tester) registerRaceBenchTest(pkg string) {
   279  	testName := "go_test_bench:" + pkg
   280  	if t.runRx == nil || t.runRx.MatchString(testName) {
   281  		benchMatches = append(benchMatches, pkg)
   282  	}
   283  	t.tests = append(t.tests, distTest{
   284  		name:    testName,
   285  		heading: "Running benchmarks briefly.",
   286  		fn: func() error {
   287  			if ranGoBench {
   288  				return nil
   289  			}
   290  			ranGoBench = true
   291  			args := []string{
   292  				"test",
   293  				"-short",
   294  				"-race",
   295  				"-run=^$", // nothing. only benchmarks.
   296  				"-bench=.*",
   297  				"-benchtime=.1s",
   298  				"-cpu=4",
   299  			}
   300  			args = append(args, benchMatches...)
   301  			cmd := exec.Command("go", args...)
   302  			cmd.Stdout = os.Stdout
   303  			cmd.Stderr = os.Stderr
   304  			return cmd.Run()
   305  		},
   306  	})
   307  }
   308  
   309  func (t *tester) registerTests() {
   310  	// Fast path to avoid the ~1 second of `go list std cmd` when
   311  	// the caller lists specific tests to run. (as the continuous
   312  	// build coordinator does).
   313  	if len(t.runNames) > 0 {
   314  		for _, name := range t.runNames {
   315  			if strings.HasPrefix(name, "go_test:") {
   316  				t.registerStdTest(strings.TrimPrefix(name, "go_test:"))
   317  			}
   318  			if strings.HasPrefix(name, "go_test_bench:") {
   319  				t.registerRaceBenchTest(strings.TrimPrefix(name, "go_test_bench:"))
   320  			}
   321  		}
   322  	} else {
   323  		// Use a format string to only list packages and commands that have tests.
   324  		const format = "{{if (or .TestGoFiles .XTestGoFiles)}}{{.ImportPath}}{{end}}"
   325  		cmd := exec.Command("go", "list", "-f", format, "std")
   326  		if !t.race {
   327  			cmd.Args = append(cmd.Args, "cmd")
   328  		}
   329  		all, err := cmd.CombinedOutput()
   330  		if err != nil {
   331  			log.Fatalf("Error running go list std cmd: %v, %s", err, all)
   332  		}
   333  		pkgs := strings.Fields(string(all))
   334  		for _, pkg := range pkgs {
   335  			t.registerStdTest(pkg)
   336  		}
   337  		if t.race {
   338  			for _, pkg := range pkgs {
   339  				t.registerRaceBenchTest(pkg)
   340  			}
   341  		}
   342  	}
   343  
   344  	if t.race {
   345  		return
   346  	}
   347  
   348  	// Runtime CPU tests.
   349  	testName := "runtime:cpu124"
   350  	t.tests = append(t.tests, distTest{
   351  		name:    testName,
   352  		heading: "GOMAXPROCS=2 runtime -cpu=1,2,4",
   353  		fn: func() error {
   354  			cmd := t.dirCmd("src", "go", "test", "-short", t.timeout(300), t.tags(), "-cpu=1,2,4", "runtime")
   355  			// We set GOMAXPROCS=2 in addition to -cpu=1,2,4 in order to test runtime bootstrap code,
   356  			// creation of first goroutines and first garbage collections in the parallel setting.
   357  			cmd.Env = mergeEnvLists([]string{"GOMAXPROCS=2"}, os.Environ())
   358  			return cmd.Run()
   359  		},
   360  	})
   361  
   362  	// Test that internal linking of standard packages does not
   363  	// require libgcc.  This ensures that we can install a Go
   364  	// release on a system that does not have a C compiler
   365  	// installed and still build Go programs (that don't use cgo).
   366  	for _, pkg := range cgoPackages {
   367  
   368  		// Internal linking is not currently supported on Dragonfly.
   369  		if t.goos == "dragonfly" {
   370  			break
   371  		}
   372  
   373  		// ARM libgcc may be Thumb, which internal linking does not support.
   374  		if t.goarch == "arm" {
   375  			break
   376  		}
   377  
   378  		// Darwin ARM64 fails with internal linking.
   379  		if t.goos == "darwin" && t.goarch == "arm64" {
   380  			break
   381  		}
   382  
   383  		pkg := pkg
   384  		t.tests = append(t.tests, distTest{
   385  			name:    "nolibgcc:" + pkg,
   386  			heading: "Testing without libgcc.",
   387  			fn: func() error {
   388  				return t.dirCmd("src", "go", "test", "-short", "-ldflags=-linkmode=internal -libgcc=none", t.tags(), pkg).Run()
   389  			},
   390  		})
   391  	}
   392  
   393  	// sync tests
   394  	t.tests = append(t.tests, distTest{
   395  		name:    "sync_cpu",
   396  		heading: "sync -cpu=10",
   397  		fn: func() error {
   398  			return t.dirCmd("src", "go", "test", "-short", t.timeout(120), t.tags(), "-cpu=10", "sync").Run()
   399  		},
   400  	})
   401  
   402  	if t.cgoEnabled && t.goos != "android" && !t.iOS() {
   403  		// Disabled on android and iOS. golang.org/issue/8345
   404  		t.tests = append(t.tests, distTest{
   405  			name:    "cgo_stdio",
   406  			heading: "../misc/cgo/stdio",
   407  			fn: func() error {
   408  				return t.dirCmd("misc/cgo/stdio",
   409  					"go", "run", filepath.Join(os.Getenv("GOROOT"), "test/run.go"), "-", ".").Run()
   410  			},
   411  		})
   412  		t.tests = append(t.tests, distTest{
   413  			name:    "cgo_life",
   414  			heading: "../misc/cgo/life",
   415  			fn: func() error {
   416  				return t.dirCmd("misc/cgo/life",
   417  					"go", "run", filepath.Join(os.Getenv("GOROOT"), "test/run.go"), "-", ".").Run()
   418  			},
   419  		})
   420  	}
   421  	if t.cgoEnabled && t.goos != "android" && !t.iOS() {
   422  		// TODO(crawshaw): reenable on android and iOS
   423  		// golang.org/issue/8345
   424  		//
   425  		// These tests are not designed to run off the host.
   426  		t.tests = append(t.tests, distTest{
   427  			name:    "cgo_test",
   428  			heading: "../misc/cgo/test",
   429  			fn:      t.cgoTest,
   430  		})
   431  	}
   432  
   433  	if t.raceDetectorSupported() {
   434  		t.tests = append(t.tests, distTest{
   435  			name:    "race",
   436  			heading: "Testing race detector",
   437  			fn:      t.raceTest,
   438  		})
   439  	}
   440  
   441  	if t.hasBash() && t.cgoEnabled && t.goos != "android" && t.goos != "darwin" {
   442  		t.registerTest("testgodefs", "../misc/cgo/testgodefs", "./test.bash")
   443  	}
   444  	if t.cgoEnabled {
   445  		if t.cgoTestSOSupported() {
   446  			t.tests = append(t.tests, distTest{
   447  				name:    "testso",
   448  				heading: "../misc/cgo/testso",
   449  				fn: func() error {
   450  					return t.cgoTestSO("misc/cgo/testso")
   451  				},
   452  			})
   453  			t.tests = append(t.tests, distTest{
   454  				name:    "testsovar",
   455  				heading: "../misc/cgo/testsovar",
   456  				fn: func() error {
   457  					return t.cgoTestSO("misc/cgo/testsovar")
   458  				},
   459  			})
   460  		}
   461  		if t.supportedBuildmode("c-archive") {
   462  			t.registerTest("testcarchive", "../misc/cgo/testcarchive", "./test.bash")
   463  		}
   464  		if t.supportedBuildmode("c-shared") {
   465  			t.registerTest("testcshared", "../misc/cgo/testcshared", "./test.bash")
   466  		}
   467  		if t.supportedBuildmode("shared") {
   468  			t.registerTest("testshared", "../misc/cgo/testshared", "go", "test")
   469  		}
   470  		if t.gohostos == "linux" && t.goarch == "amd64" {
   471  			t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", "main.go")
   472  		}
   473  		if t.gohostos == "linux" && t.goarch == "amd64" {
   474  			t.registerTest("testsanitizers", "../misc/cgo/testsanitizers", "./test.bash")
   475  		}
   476  		if t.hasBash() && t.goos != "android" && !t.iOS() && t.gohostos != "windows" {
   477  			t.registerTest("cgo_errors", "../misc/cgo/errors", "./test.bash")
   478  		}
   479  		if t.gohostos == "linux" && t.extLink() {
   480  			t.registerTest("testsigfwd", "../misc/cgo/testsigfwd", "go", "run", "main.go")
   481  		}
   482  	}
   483  	if t.hasBash() && t.goos != "nacl" && t.goos != "android" && !t.iOS() {
   484  		t.registerTest("doc_progs", "../doc/progs", "time", "go", "run", "run.go")
   485  		t.registerTest("wiki", "../doc/articles/wiki", "./test.bash")
   486  		t.registerTest("codewalk", "../doc/codewalk", "time", "./run")
   487  		for _, name := range t.shootoutTests() {
   488  			if name == "spectralnorm" {
   489  				switch os.Getenv("GO_BUILDER_NAME") {
   490  				case "linux-arm-arm5", "linux-mips64-minux":
   491  					// Heavy on floating point and takes over 20 minutes with
   492  					// softfloat on arm5 builder and over 33 minutes on MIPS64
   493  					// builder with kernel FPU emulator.
   494  					// Disabled per Issue 12688.
   495  					continue
   496  				}
   497  			}
   498  			t.registerTest("shootout:"+name, "../test/bench/shootout", "time", "./timing.sh", "-test", name)
   499  		}
   500  	}
   501  	if t.goos != "android" && !t.iOS() {
   502  		t.registerTest("bench_go1", "../test/bench/go1", "go", "test", t.timeout(600))
   503  	}
   504  	if t.goos != "android" && !t.iOS() {
   505  		const nShards = 5
   506  		for shard := 0; shard < nShards; shard++ {
   507  			shard := shard
   508  			t.tests = append(t.tests, distTest{
   509  				name:    fmt.Sprintf("test:%d_%d", shard, nShards),
   510  				heading: "../test",
   511  				fn:      func() error { return t.testDirTest(shard, nShards) },
   512  			})
   513  		}
   514  	}
   515  	if t.goos != "nacl" && t.goos != "android" && !t.iOS() {
   516  		t.tests = append(t.tests, distTest{
   517  			name:    "api",
   518  			heading: "API check",
   519  			fn: func() error {
   520  				return t.dirCmd("src", "go", "run", filepath.Join(t.goroot, "src/cmd/api/run.go")).Run()
   521  			},
   522  		})
   523  	}
   524  }
   525  
   526  // isRegisteredTestName reports whether a test named testName has already
   527  // been registered.
   528  func (t *tester) isRegisteredTestName(testName string) bool {
   529  	for _, tt := range t.tests {
   530  		if tt.name == testName {
   531  			return true
   532  		}
   533  	}
   534  	return false
   535  }
   536  
   537  func (t *tester) registerTest(name, dirBanner, bin string, args ...string) {
   538  	if bin == "time" && !t.haveTime {
   539  		bin, args = args[0], args[1:]
   540  	}
   541  	if t.isRegisteredTestName(name) {
   542  		panic("duplicate registered test name " + name)
   543  	}
   544  	t.tests = append(t.tests, distTest{
   545  		name:    name,
   546  		heading: dirBanner,
   547  		fn: func() error {
   548  			return t.dirCmd(filepath.Join(t.goroot, "src", dirBanner), bin, args...).Run()
   549  		},
   550  	})
   551  }
   552  
   553  func (t *tester) dirCmd(dir string, bin string, args ...string) *exec.Cmd {
   554  	cmd := exec.Command(bin, args...)
   555  	if filepath.IsAbs(dir) {
   556  		cmd.Dir = dir
   557  	} else {
   558  		cmd.Dir = filepath.Join(t.goroot, dir)
   559  	}
   560  	cmd.Stdout = os.Stdout
   561  	cmd.Stderr = os.Stderr
   562  	if vflag > 1 {
   563  		errprintf("%s\n", strings.Join(cmd.Args, " "))
   564  	}
   565  	return cmd
   566  }
   567  
   568  func (t *tester) iOS() bool {
   569  	return t.goos == "darwin" && (t.goarch == "arm" || t.goarch == "arm64")
   570  }
   571  
   572  func (t *tester) out(v string) {
   573  	if t.banner == "" {
   574  		return
   575  	}
   576  	fmt.Println("\n" + t.banner + v)
   577  }
   578  
   579  func (t *tester) extLink() bool {
   580  	pair := t.gohostos + "-" + t.goarch
   581  	switch pair {
   582  	case "android-arm",
   583  		"darwin-arm", "darwin-arm64",
   584  		"dragonfly-386", "dragonfly-amd64",
   585  		"freebsd-386", "freebsd-amd64", "freebsd-arm",
   586  		"linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le",
   587  		"netbsd-386", "netbsd-amd64",
   588  		"openbsd-386", "openbsd-amd64",
   589  		"windows-386", "windows-amd64":
   590  		return true
   591  	case "darwin-386", "darwin-amd64":
   592  		// linkmode=external fails on OS X 10.6 and earlier == Darwin
   593  		// 10.8 and earlier.
   594  		unameR, err := exec.Command("uname", "-r").Output()
   595  		if err != nil {
   596  			log.Fatalf("uname -r: %v", err)
   597  		}
   598  		major, _ := strconv.Atoi(string(unameR[:bytes.IndexByte(unameR, '.')]))
   599  		return major > 10
   600  	}
   601  	return false
   602  }
   603  
   604  func (t *tester) supportedBuildmode(mode string) bool {
   605  	pair := t.goos + "-" + t.goarch
   606  	switch mode {
   607  	case "c-archive":
   608  		if !t.extLink() {
   609  			return false
   610  		}
   611  		switch pair {
   612  		case "darwin-386", "darwin-amd64", "darwin-arm", "darwin-arm64",
   613  			"linux-amd64", "linux-386":
   614  			return true
   615  		}
   616  		return false
   617  	case "c-shared":
   618  		switch pair {
   619  		case "linux-386", "linux-amd64", "linux-arm", "linux-arm64",
   620  			"darwin-amd64",
   621  			"android-arm", "android-386":
   622  			return true
   623  		}
   624  		return false
   625  	case "shared":
   626  		switch pair {
   627  		case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le":
   628  			return true
   629  		}
   630  		return false
   631  	default:
   632  		log.Fatal("internal error: unknown buildmode %s", mode)
   633  		return false
   634  	}
   635  }
   636  
   637  func (t *tester) cgoTest() error {
   638  	env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ())
   639  
   640  	if t.goos == "android" || t.iOS() {
   641  		cmd := t.dirCmd("misc/cgo/test", "go", "test", t.tags())
   642  		cmd.Env = env
   643  		return cmd.Run()
   644  	}
   645  
   646  	cmd := t.dirCmd("misc/cgo/test", "go", "test", t.tags(), "-ldflags", "-linkmode=auto")
   647  	cmd.Env = env
   648  	if err := cmd.Run(); err != nil {
   649  		return err
   650  	}
   651  
   652  	if t.gohostos != "dragonfly" {
   653  		// linkmode=internal fails on dragonfly since errno is a TLS relocation.
   654  		cmd := t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", "-linkmode=internal")
   655  		cmd.Env = env
   656  		if err := cmd.Run(); err != nil {
   657  			return err
   658  		}
   659  	}
   660  
   661  	pair := t.gohostos + "-" + t.goarch
   662  	switch pair {
   663  	case "darwin-386", "darwin-amd64",
   664  		"openbsd-386", "openbsd-amd64",
   665  		"windows-386", "windows-amd64":
   666  		// test linkmode=external, but __thread not supported, so skip testtls.
   667  		if !t.extLink() {
   668  			break
   669  		}
   670  		cmd := t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", "-linkmode=external")
   671  		cmd.Env = env
   672  		if err := cmd.Run(); err != nil {
   673  			return err
   674  		}
   675  		cmd = t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", "-linkmode=external -s")
   676  		cmd.Env = env
   677  		if err := cmd.Run(); err != nil {
   678  			return err
   679  		}
   680  	case "android-arm",
   681  		"dragonfly-386", "dragonfly-amd64",
   682  		"freebsd-386", "freebsd-amd64", "freebsd-arm",
   683  		"linux-386", "linux-amd64", "linux-arm",
   684  		"netbsd-386", "netbsd-amd64":
   685  
   686  		cmd := t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", "-linkmode=external")
   687  		cmd.Env = env
   688  		if err := cmd.Run(); err != nil {
   689  			return err
   690  		}
   691  		cmd = t.dirCmd("misc/cgo/testtls", "go", "test", "-ldflags", "-linkmode=auto")
   692  		cmd.Env = env
   693  		if err := cmd.Run(); err != nil {
   694  			return err
   695  		}
   696  		cmd = t.dirCmd("misc/cgo/testtls", "go", "test", "-ldflags", "-linkmode=external")
   697  		cmd.Env = env
   698  		if err := cmd.Run(); err != nil {
   699  			return err
   700  		}
   701  
   702  		switch pair {
   703  		case "netbsd-386", "netbsd-amd64":
   704  			// no static linking
   705  		case "freebsd-arm":
   706  			// -fPIC compiled tls code will use __tls_get_addr instead
   707  			// of __aeabi_read_tp, however, on FreeBSD/ARM, __tls_get_addr
   708  			// is implemented in rtld-elf, so -fPIC isn't compatible with
   709  			// static linking on FreeBSD/ARM with clang. (cgo depends on
   710  			// -fPIC fundamentally.)
   711  		default:
   712  			cc := mustEnv("CC")
   713  			cmd := t.dirCmd("misc/cgo/test",
   714  				cc, "-xc", "-o", "/dev/null", "-static", "-")
   715  			cmd.Env = env
   716  			cmd.Stdin = strings.NewReader("int main() {}")
   717  			if err := cmd.Run(); err != nil {
   718  				fmt.Println("No support for static linking found (lacks libc.a?), skip cgo static linking test.")
   719  			} else {
   720  				cmd = t.dirCmd("misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`)
   721  				cmd.Env = env
   722  				if err := cmd.Run(); err != nil {
   723  					return err
   724  				}
   725  
   726  				cmd = t.dirCmd("misc/cgo/nocgo", "go", "test")
   727  				cmd.Env = env
   728  				if err := cmd.Run(); err != nil {
   729  					return err
   730  				}
   731  
   732  				cmd = t.dirCmd("misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external`)
   733  				cmd.Env = env
   734  				if err := cmd.Run(); err != nil {
   735  					return err
   736  				}
   737  
   738  				cmd = t.dirCmd("misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-static -pthread"`)
   739  				cmd.Env = env
   740  				if err := cmd.Run(); err != nil {
   741  					return err
   742  				}
   743  			}
   744  
   745  			if pair != "freebsd-amd64" { // clang -pie fails to link misc/cgo/test
   746  				cmd := t.dirCmd("misc/cgo/test",
   747  					cc, "-xc", "-o", "/dev/null", "-pie", "-")
   748  				cmd.Env = env
   749  				cmd.Stdin = strings.NewReader("int main() {}")
   750  				if err := cmd.Run(); err != nil {
   751  					fmt.Println("No support for -pie found, skip cgo PIE test.")
   752  				} else {
   753  					cmd = t.dirCmd("misc/cgo/test", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`)
   754  					cmd.Env = env
   755  					if err := cmd.Run(); err != nil {
   756  						return fmt.Errorf("pie cgo/test: %v", err)
   757  					}
   758  					cmd = t.dirCmd("misc/cgo/testtls", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`)
   759  					cmd.Env = env
   760  					if err := cmd.Run(); err != nil {
   761  						return fmt.Errorf("pie cgo/testtls: %v", err)
   762  					}
   763  					cmd = t.dirCmd("misc/cgo/nocgo", "go", "test", "-ldflags", `-linkmode=external -extldflags "-pie"`)
   764  					cmd.Env = env
   765  					if err := cmd.Run(); err != nil {
   766  						return fmt.Errorf("pie cgo/nocgo: %v", err)
   767  					}
   768  				}
   769  			}
   770  		}
   771  	}
   772  
   773  	return nil
   774  }
   775  
   776  func (t *tester) cgoTestSOSupported() bool {
   777  	if t.goos == "android" || t.iOS() {
   778  		// No exec facility on Android or iOS.
   779  		return false
   780  	}
   781  	if t.goarch == "ppc64" {
   782  		// External linking not implemented on ppc64 (issue #8912).
   783  		return false
   784  	}
   785  	if t.goarch == "mips64le" || t.goarch == "mips64" {
   786  		// External linking not implemented on mips64.
   787  		return false
   788  	}
   789  	return true
   790  }
   791  
   792  func (t *tester) cgoTestSO(testpath string) error {
   793  	dir := filepath.Join(t.goroot, testpath)
   794  
   795  	// build shared object
   796  	output, err := exec.Command("go", "env", "CC").Output()
   797  	if err != nil {
   798  		return fmt.Errorf("Error running go env CC: %v", err)
   799  	}
   800  	cc := strings.TrimSuffix(string(output), "\n")
   801  	if cc == "" {
   802  		return errors.New("CC environment variable (go env CC) cannot be empty")
   803  	}
   804  	output, err = exec.Command("go", "env", "GOGCCFLAGS").Output()
   805  	if err != nil {
   806  		return fmt.Errorf("Error running go env GOGCCFLAGS: %v", err)
   807  	}
   808  	gogccflags := strings.Split(strings.TrimSuffix(string(output), "\n"), " ")
   809  
   810  	ext := "so"
   811  	args := append(gogccflags, "-shared")
   812  	switch t.goos {
   813  	case "darwin":
   814  		ext = "dylib"
   815  		args = append(args, "-undefined", "suppress", "-flat_namespace")
   816  	case "windows":
   817  		ext = "dll"
   818  		args = append(args, "-DEXPORT_DLL")
   819  	}
   820  	sofname := "libcgosotest." + ext
   821  	args = append(args, "-o", sofname, "cgoso_c.c")
   822  
   823  	if err := t.dirCmd(dir, cc, args...).Run(); err != nil {
   824  		return err
   825  	}
   826  	defer os.Remove(filepath.Join(dir, sofname))
   827  
   828  	if err := t.dirCmd(dir, "go", "build", "-o", "main.exe", "main.go").Run(); err != nil {
   829  		return err
   830  	}
   831  	defer os.Remove(filepath.Join(dir, "main.exe"))
   832  
   833  	cmd := t.dirCmd(dir, "./main.exe")
   834  	if t.goos != "windows" {
   835  		s := "LD_LIBRARY_PATH"
   836  		if t.goos == "darwin" {
   837  			s = "DYLD_LIBRARY_PATH"
   838  		}
   839  		cmd.Env = mergeEnvLists([]string{s + "=."}, os.Environ())
   840  	}
   841  	return cmd.Run()
   842  }
   843  
   844  func (t *tester) hasBash() bool {
   845  	switch t.gohostos {
   846  	case "windows", "plan9":
   847  		return false
   848  	}
   849  	return true
   850  }
   851  
   852  func (t *tester) raceDetectorSupported() bool {
   853  	switch t.gohostos {
   854  	case "linux", "darwin", "freebsd", "windows":
   855  		return t.cgoEnabled && t.goarch == "amd64" && t.gohostos == t.goos
   856  	}
   857  	return false
   858  }
   859  
   860  func (t *tester) raceTest() error {
   861  	if err := t.dirCmd("src", "go", "test", "-race", "-i", "runtime/race", "flag", "os/exec").Run(); err != nil {
   862  		return err
   863  	}
   864  	if err := t.dirCmd("src", "go", "test", "-race", "-run=Output", "runtime/race").Run(); err != nil {
   865  		return err
   866  	}
   867  	if err := t.dirCmd("src", "go", "test", "-race", "-short", "flag", "os/exec").Run(); err != nil {
   868  		return err
   869  	}
   870  	if t.cgoEnabled {
   871  		env := mergeEnvLists([]string{"GOTRACEBACK=2"}, os.Environ())
   872  		cmd := t.dirCmd("misc/cgo/test", "go", "test", "-race", "-short")
   873  		cmd.Env = env
   874  		if err := cmd.Run(); err != nil {
   875  			return err
   876  		}
   877  	}
   878  	if t.extLink() {
   879  		// Test with external linking; see issue 9133.
   880  		if err := t.dirCmd("src", "go", "test", "-race", "-short", "-ldflags=-linkmode=external", "flag", "os/exec").Run(); err != nil {
   881  			return err
   882  		}
   883  	}
   884  	return nil
   885  }
   886  
   887  func (t *tester) testDirTest(shard, shards int) error {
   888  	const runExe = "runtest.exe" // named exe for Windows, but harmless elsewhere
   889  	cmd := t.dirCmd("test", "go", "build", "-o", runExe, "run.go")
   890  	cmd.Env = mergeEnvLists([]string{"GOOS=" + t.gohostos, "GOARCH=" + t.gohostarch, "GOMAXPROCS="}, os.Environ())
   891  	if err := cmd.Run(); err != nil {
   892  		return err
   893  	}
   894  	absExe := filepath.Join(cmd.Dir, runExe)
   895  	defer os.Remove(absExe)
   896  	return t.dirCmd("test", absExe,
   897  		fmt.Sprintf("--shard=%d", shard),
   898  		fmt.Sprintf("--shards=%d", shards),
   899  	).Run()
   900  }
   901  
   902  func (t *tester) shootoutTests() []string {
   903  	sh, err := ioutil.ReadFile(filepath.Join(t.goroot, "test", "bench", "shootout", "timing.sh"))
   904  	if err != nil {
   905  		log.Fatal(err)
   906  	}
   907  	m := regexp.MustCompile(`(?m)^\s+run="([\w+ ]+)"\s*$`).FindSubmatch(sh)
   908  	if m == nil {
   909  		log.Fatal("failed to find run=\"...\" line in test/bench/shootout/timing.sh")
   910  	}
   911  	return strings.Fields(string(m[1]))
   912  }
   913  
   914  // mergeEnvLists merges the two environment lists such that
   915  // variables with the same name in "in" replace those in "out".
   916  // out may be mutated.
   917  func mergeEnvLists(in, out []string) []string {
   918  NextVar:
   919  	for _, inkv := range in {
   920  		k := strings.SplitAfterN(inkv, "=", 2)[0]
   921  		for i, outkv := range out {
   922  			if strings.HasPrefix(outkv, k) {
   923  				out[i] = inkv
   924  				continue NextVar
   925  			}
   926  		}
   927  		out = append(out, inkv)
   928  	}
   929  	return out
   930  }
   931  
   932  // cgoPackages is the standard packages that use cgo.
   933  var cgoPackages = []string{
   934  	"crypto/x509",
   935  	"net",
   936  	"os/user",
   937  }