Skip to content

Commit 04e2472

Browse files
committed
cmd/dist: introduce test variants
This introduces the concept of test variants in dist, which are different configurations of the same package. The variant of a test is a short string summarizing the configuration. The "variant name" of a test is either the package name if the variant is empty, or package:variant if not. Currently this isn't used for anything, but soon we'll use this as the Package field of the test JSON output so that we can disambiguate output from differently configured runs of the same test package, and naturally flow this through to any test result viewer. The long-term plan is to use variant names as dist's own test names and eliminate the ad hoc names it has right now. Unfortunately, the build coordinator is aware of many of the ad hoc dist test names, so some more work is needed to get to that point. This CL keeps almost all test names the same, with the exception of tests registered by registerCgoTests, where we regularize test names a bit using variants to avoid some unnecessary complexity (I believe nothing depends on the names of these tests). For #37486. Change-Id: I119fec2872e40b12c1973cf2cddc7f413d62a48c Reviewed-on: https://go-review.googlesource.com/c/go/+/495016 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 3cf8e8e commit 04e2472

File tree

1 file changed

+79
-33
lines changed

1 file changed

+79
-33
lines changed

src/cmd/dist/test.go

+79-33
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type tester struct {
7474
testNames map[string]bool
7575
timeoutScale int
7676

77+
variantNames map[string]bool // check that pkg[:variant] names are unique
78+
7779
worklist []*work
7880
}
7981

@@ -298,6 +300,13 @@ type goTest struct {
298300

299301
runOnHost bool // When cross-compiling, run this test on the host instead of guest
300302

303+
// variant, if non-empty, is a name used to distinguish different
304+
// configurations of the same test package(s).
305+
variant string
306+
// sharded indicates that variant is used solely for sharding and that
307+
// the set of test names run by each variant of a package is non-overlapping.
308+
sharded bool
309+
301310
// We have both pkg and pkgs as a convenience. Both may be set, in which
302311
// case they will be combined. At least one must be set.
303312
pkgs []string // Multiple packages to test
@@ -405,13 +414,7 @@ func (opts *goTest) buildArgs(t *tester) (goCmd string, build, run, pkgs, testFl
405414
build = append(build, "-buildmode="+opts.buildmode)
406415
}
407416

408-
pkgs = opts.pkgs
409-
if opts.pkg != "" {
410-
pkgs = append(pkgs[:len(pkgs):len(pkgs)], opts.pkg)
411-
}
412-
if len(pkgs) == 0 {
413-
panic("no packages")
414-
}
417+
pkgs = opts.packages()
415418

416419
runOnHost := opts.runOnHost && (goarch != gohostarch || goos != gohostos)
417420
needTestFlags := len(opts.testFlags) > 0 || runOnHost
@@ -448,6 +451,19 @@ func (opts *goTest) buildArgs(t *tester) (goCmd string, build, run, pkgs, testFl
448451
return
449452
}
450453

454+
// packages returns the full list of packages to be run by this goTest. This
455+
// will always include at least one package.
456+
func (opts *goTest) packages() []string {
457+
pkgs := opts.pkgs
458+
if opts.pkg != "" {
459+
pkgs = append(pkgs[:len(pkgs):len(pkgs)], opts.pkg)
460+
}
461+
if len(pkgs) == 0 {
462+
panic("no packages")
463+
}
464+
return pkgs
465+
}
466+
451467
// ranGoTest and stdMatches are state closed over by the stdlib
452468
// testing func in registerStdTest below. The tests are run
453469
// sequentially, so there's no need for locks.
@@ -592,12 +608,14 @@ func (t *tester) registerTests() {
592608
if !t.compileOnly {
593609
t.registerTest("osusergo", "os/user with tag osusergo",
594610
&goTest{
611+
variant: "osusergo",
595612
timeout: 300 * time.Second,
596613
tags: []string{"osusergo"},
597614
pkg: "os/user",
598615
})
599616
t.registerTest("purego:hash/maphash", "hash/maphash purego implementation",
600617
&goTest{
618+
variant: "purego",
601619
timeout: 300 * time.Second,
602620
tags: []string{"purego"},
603621
pkg: "hash/maphash",
@@ -608,6 +626,7 @@ func (t *tester) registerTests() {
608626
if goos == "darwin" && goarch == "amd64" && t.cgoEnabled {
609627
t.registerTest("amd64ios", "GOOS=ios on darwin/amd64",
610628
&goTest{
629+
variant: "amd64ios",
611630
timeout: 300 * time.Second,
612631
runTests: "SystemRoots",
613632
env: []string{"GOOS=ios", "CGO_ENABLED=1"},
@@ -619,6 +638,7 @@ func (t *tester) registerTests() {
619638
if !t.compileOnly && t.hasParallelism() {
620639
t.registerTest("runtime:cpu124", "GOMAXPROCS=2 runtime -cpu=1,2,4 -quick",
621640
&goTest{
641+
variant: "cpu124",
622642
timeout: 300 * time.Second,
623643
cpu: "1,2,4",
624644
short: true,
@@ -666,6 +686,7 @@ func (t *tester) registerTests() {
666686
for _, pkg := range pkgs {
667687
t.registerTest(hook+":"+pkg, "maymorestack="+hook,
668688
&goTest{
689+
variant: hook,
669690
timeout: 600 * time.Second,
670691
short: true,
671692
env: []string{"GOFLAGS=" + goFlags},
@@ -704,8 +725,9 @@ func (t *tester) registerTests() {
704725
// Run `go test fmt` in the moved GOROOT, without explicitly setting
705726
// GOROOT in the environment. The 'go' command should find itself.
706727
cmd := (&goTest{
707-
goroot: moved,
708-
pkg: "fmt",
728+
variant: "moved_goroot",
729+
goroot: moved,
730+
pkg: "fmt",
709731
}).command(t)
710732
unsetEnv(cmd, "GOROOT")
711733
err := cmd.Run()
@@ -739,6 +761,7 @@ func (t *tester) registerTests() {
739761
}
740762
t.registerTest("nolibgcc:"+pkg, "Testing without libgcc.",
741763
&goTest{
764+
variant: "nolibgcc",
742765
ldflags: "-linkmode=internal -libgcc=none",
743766
runTests: run,
744767
pkg: pkg,
@@ -753,6 +776,7 @@ func (t *tester) registerTests() {
753776
if t.internalLinkPIE() && !disablePIE {
754777
t.registerTest("pie_internal", "internal linking of -buildmode=pie",
755778
&goTest{
779+
variant: "pie_internal",
756780
timeout: 60 * time.Second,
757781
buildmode: "pie",
758782
ldflags: "-linkmode=internal",
@@ -763,6 +787,7 @@ func (t *tester) registerTests() {
763787
if t.cgoEnabled && t.internalLink() && !disablePIE {
764788
t.registerTest("pie_internal_cgo", "internal linking of -buildmode=pie",
765789
&goTest{
790+
variant: "pie_internal",
766791
timeout: 60 * time.Second,
767792
buildmode: "pie",
768793
ldflags: "-linkmode=internal",
@@ -775,6 +800,7 @@ func (t *tester) registerTests() {
775800
if t.hasParallelism() {
776801
t.registerTest("sync_cpu", "sync -cpu=10",
777802
&goTest{
803+
variant: "cpu10",
778804
timeout: 120 * time.Second,
779805
cpu: "10",
780806
pkg: "sync",
@@ -837,10 +863,13 @@ func (t *tester) registerTests() {
837863
nShards = n
838864
}
839865
for shard := 0; shard < nShards; shard++ {
866+
id := fmt.Sprintf("%d_%d", shard, nShards)
840867
t.registerTest(
841-
fmt.Sprintf("test:%d_%d", shard, nShards),
868+
"test:"+id,
842869
"../test",
843870
&goTest{
871+
variant: id,
872+
sharded: true,
844873
pkg: "cmd/internal/testdir",
845874
testFlags: []string{fmt.Sprintf("-shard=%d", shard), fmt.Sprintf("-shards=%d", nShards)},
846875
runOnHost: true,
@@ -854,7 +883,7 @@ func (t *tester) registerTests() {
854883
// To help developers avoid trybot-only failures, we try to run on typical developer machines
855884
// which is darwin,linux,windows/amd64 and darwin/arm64.
856885
if goos == "darwin" || ((goos == "linux" || goos == "windows") && goarch == "amd64") {
857-
t.registerTest("api", "API check", &goTest{pkg: "cmd/api", timeout: 5 * time.Minute, testFlags: []string{"-check"}})
886+
t.registerTest("api", "API check", &goTest{variant: "check", pkg: "cmd/api", timeout: 5 * time.Minute, testFlags: []string{"-check"}})
858887
}
859888
}
860889

@@ -895,6 +924,19 @@ func (rtPreFunc) isRegisterTestOpt() {}
895924
//
896925
// name must uniquely identify the test and heading must be non-empty.
897926
func (t *tester) registerTest(name, heading string, test *goTest, opts ...registerTestOpt) {
927+
if t.variantNames == nil {
928+
t.variantNames = make(map[string]bool)
929+
}
930+
for _, pkg := range test.packages() {
931+
variantName := pkg
932+
if test.variant != "" {
933+
variantName += ":" + test.variant
934+
}
935+
if t.variantNames[variantName] {
936+
panic("duplicate variant name " + variantName)
937+
}
938+
t.variantNames[variantName] = true
939+
}
898940
var preFunc func(*distTest) bool
899941
for _, opt := range opts {
900942
switch opt := opt.(type) {
@@ -1049,8 +1091,9 @@ func (t *tester) supportedBuildmode(mode string) bool {
10491091
}
10501092

10511093
func (t *tester) registerCgoTests(heading string) {
1052-
cgoTest := func(name string, subdir, linkmode, buildmode string, opts ...registerTestOpt) *goTest {
1094+
cgoTest := func(variant string, subdir, linkmode, buildmode string, opts ...registerTestOpt) *goTest {
10531095
gt := &goTest{
1096+
variant: variant,
10541097
pkg: "cmd/cgo/internal/" + subdir,
10551098
buildmode: buildmode,
10561099
ldflags: "-linkmode=" + linkmode,
@@ -1076,18 +1119,18 @@ func (t *tester) registerCgoTests(heading string) {
10761119
gt.tags = append(gt.tags, "static")
10771120
}
10781121

1079-
t.registerTest("cgo:"+name, heading, gt, opts...)
1122+
t.registerTest("cgo:"+subdir+":"+variant, heading, gt, opts...)
10801123
return gt
10811124
}
10821125

1083-
cgoTest("test-auto", "test", "auto", "")
1126+
cgoTest("auto", "test", "auto", "")
10841127

10851128
// Stub out various buildmode=pie tests on alpine until 54354 resolved.
10861129
builderName := os.Getenv("GO_BUILDER_NAME")
10871130
disablePIE := strings.HasSuffix(builderName, "-alpine")
10881131

10891132
if t.internalLink() {
1090-
cgoTest("test-internal", "test", "internal", "")
1133+
cgoTest("internal", "test", "internal", "")
10911134
}
10921135

10931136
os := gohostos
@@ -1098,24 +1141,24 @@ func (t *tester) registerCgoTests(heading string) {
10981141
break
10991142
}
11001143
// test linkmode=external, but __thread not supported, so skip testtls.
1101-
cgoTest("test-external", "test", "external", "")
1144+
cgoTest("external", "test", "external", "")
11021145

1103-
gt := cgoTest("test-external-s", "test", "external", "")
1146+
gt := cgoTest("external-s", "test", "external", "")
11041147
gt.ldflags += " -s"
11051148

11061149
if t.supportedBuildmode("pie") && !disablePIE {
1107-
cgoTest("test-auto-pie", "test", "auto", "pie")
1150+
cgoTest("auto-pie", "test", "auto", "pie")
11081151
if t.internalLink() && t.internalLinkPIE() {
1109-
cgoTest("test-internal-pie", "test", "internal", "pie")
1152+
cgoTest("internal-pie", "test", "internal", "pie")
11101153
}
11111154
}
11121155

11131156
case os == "aix", os == "android", os == "dragonfly", os == "freebsd", os == "linux", os == "netbsd", os == "openbsd":
1114-
gt := cgoTest("test-external-g0", "test", "external", "")
1157+
gt := cgoTest("external-g0", "test", "external", "")
11151158
gt.env = append(gt.env, "CGO_CFLAGS=-g0 -fdiagnostics-color")
11161159

1117-
cgoTest("testtls-auto", "testtls", "auto", "")
1118-
cgoTest("testtls-external", "testtls", "external", "")
1160+
cgoTest("auto", "testtls", "auto", "")
1161+
cgoTest("external", "testtls", "external", "")
11191162
switch {
11201163
case os == "aix":
11211164
// no static linking
@@ -1162,30 +1205,30 @@ func (t *tester) registerCgoTests(heading string) {
11621205
// Static linking tests
11631206
if goos != "android" && p != "netbsd/arm" {
11641207
// TODO(#56629): Why does this fail on netbsd-arm?
1165-
cgoTest("testtls-static", "testtls", "external", "static", staticCheck)
1208+
cgoTest("static", "testtls", "external", "static", staticCheck)
11661209
}
1167-
cgoTest("nocgo-auto", "testnocgo", "auto", "", staticCheck)
1168-
cgoTest("nocgo-external", "testnocgo", "external", "", staticCheck)
1210+
cgoTest("auto", "testnocgo", "auto", "", staticCheck)
1211+
cgoTest("external", "testnocgo", "external", "", staticCheck)
11691212
if goos != "android" {
1170-
cgoTest("nocgo-static", "testnocgo", "external", "static", staticCheck)
1171-
cgoTest("test-static", "test", "external", "static", staticCheck)
1213+
cgoTest("static", "testnocgo", "external", "static", staticCheck)
1214+
cgoTest("static", "test", "external", "static", staticCheck)
11721215
// -static in CGO_LDFLAGS triggers a different code path
11731216
// than -static in -extldflags, so test both.
11741217
// See issue #16651.
11751218
if goarch != "loong64" {
11761219
// TODO(#56623): Why does this fail on loong64?
1177-
cgoTest("test-static-env", "test", "auto", "static", staticCheck)
1220+
cgoTest("auto-static", "test", "auto", "static", staticCheck)
11781221
}
11791222
}
11801223

11811224
// PIE linking tests
11821225
if t.supportedBuildmode("pie") && !disablePIE {
1183-
cgoTest("test-pie", "test", "auto", "pie")
1226+
cgoTest("auto-pie", "test", "auto", "pie")
11841227
if t.internalLink() && t.internalLinkPIE() {
1185-
cgoTest("test-pie-internal", "test", "internal", "pie")
1228+
cgoTest("internal-pie", "test", "internal", "pie")
11861229
}
1187-
cgoTest("testtls-pie", "testtls", "auto", "pie")
1188-
cgoTest("nocgo-pie", "testnocgo", "auto", "pie")
1230+
cgoTest("auto-pie", "testtls", "auto", "pie")
1231+
cgoTest("auto-pie", "testnocgo", "auto", "pie")
11891232
}
11901233
}
11911234
}
@@ -1325,12 +1368,14 @@ func (t *tester) registerRaceTests() {
13251368
hdr := "Testing race detector"
13261369
t.registerTest("race:runtime/race", hdr,
13271370
&goTest{
1371+
variant: "race",
13281372
race: true,
13291373
runTests: "Output",
13301374
pkg: "runtime/race",
13311375
})
13321376
t.registerTest("race", hdr,
13331377
&goTest{
1378+
variant: "race",
13341379
race: true,
13351380
runTests: "TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace|TestFdRace|TestFdReadRace|TestFileCloseRace",
13361381
pkgs: []string{"flag", "net", "os", "os/exec", "encoding/gob"},
@@ -1345,12 +1390,13 @@ func (t *tester) registerRaceTests() {
13451390
// There are already cgo-enabled packages being tested with the race detector.
13461391
// We shouldn't need to redo all of cmd/cgo/internal/test too.
13471392
// The race buildler will take care of this.
1348-
// t.registerTest("race:cmd/cgo/internal/test", hdr, &goTest{dir: "cmd/cgo/internal/test", race: true, env: []string{"GOTRACEBACK=2"}})
1393+
// t.registerTest("race:cmd/cgo/internal/test", hdr, &goTest{variant:"race", dir: "cmd/cgo/internal/test", race: true, env: []string{"GOTRACEBACK=2"}})
13491394
}
13501395
if t.extLink() {
13511396
// Test with external linking; see issue 9133.
13521397
t.registerTest("race:external", hdr,
13531398
&goTest{
1399+
variant: "race-external",
13541400
race: true,
13551401
ldflags: "-linkmode=external",
13561402
runTests: "TestParse|TestEcho|TestStdinCloseRace",

0 commit comments

Comments
 (0)