From 51ca5706ab2074a624f8a2590a2a81e6a5821e48 Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Wed, 15 Sep 2021 15:30:18 -0700 Subject: [PATCH] [dev.fuzz] cmd/go: in 'go test' don't allow multiple packages with -fuzz Until we have a system for managing load across multiple fuzz targets in multiple test executables, we'll only support fuzzing one target in one package at a time. Users can still run multiple 'go test -fuzz' commands concurrently, but this may overwhelm some systems unless -parallel and -p are set carefully. For #46312 Change-Id: If84c58d1b3e60498ce955eae5ad4d52100dbd4b7 Reviewed-on: https://go-review.googlesource.com/c/go/+/350156 Trust: Jay Conrod Trust: Katie Hockman Run-TryBot: Jay Conrod TryBot-Result: Go Bot Reviewed-by: Katie Hockman --- src/cmd/go/internal/test/test.go | 3 ++ .../go/testdata/script/test_fuzz_match.txt | 20 -------- .../go/testdata/script/test_fuzz_multiple.txt | 51 +++++++++++++++++++ 3 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 src/cmd/go/testdata/script/test_fuzz_multiple.txt diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go index c36cb0b221f..173e8a2ee47 100644 --- a/src/cmd/go/internal/test/test.go +++ b/src/cmd/go/internal/test/test.go @@ -671,6 +671,9 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) { if testO != "" && len(pkgs) != 1 { base.Fatalf("cannot use -o flag with multiple packages") } + if testFuzz != "" && len(pkgs) != 1 { + base.Fatalf("cannot use -fuzz flag with multiple packages") + } if testProfile() != "" && len(pkgs) != 1 { base.Fatalf("cannot use %s flag with multiple packages", testProfile()) } diff --git a/src/cmd/go/testdata/script/test_fuzz_match.txt b/src/cmd/go/testdata/script/test_fuzz_match.txt index 47e143952ac..3a2ca631add 100644 --- a/src/cmd/go/testdata/script/test_fuzz_match.txt +++ b/src/cmd/go/testdata/script/test_fuzz_match.txt @@ -29,13 +29,6 @@ go test -run ThisWillNotMatch standalone_fuzz_test.go stdout '^ok.*no tests to run' ! stdout 'no targets to fuzz' -# Matches more than one fuzz target for fuzzing. -! go test -fuzz Fuzz -fuzztime 1x multiple_fuzz_test.go -! stdout 'no tests to run' -! stdout 'no targets to fuzz' -stdout FAIL -stdout 'will not fuzz, -fuzz matches more than one target' - -- standalone_fuzz_test.go -- package standalone_fuzz @@ -44,16 +37,3 @@ import "testing" func Fuzz(f *testing.F) { f.Fuzz(func (*testing.T, []byte) {}) } - --- multiple_fuzz_test.go -- -package multiple_fuzz - -import "testing" - -func FuzzA(f *testing.F) { - f.Fuzz(func (*testing.T, []byte) {}) -} - -func FuzzB(f *testing.F) { - f.Fuzz(func (*testing.T, []byte) {}) -} diff --git a/src/cmd/go/testdata/script/test_fuzz_multiple.txt b/src/cmd/go/testdata/script/test_fuzz_multiple.txt new file mode 100644 index 00000000000..6a7732f5149 --- /dev/null +++ b/src/cmd/go/testdata/script/test_fuzz_multiple.txt @@ -0,0 +1,51 @@ +# This test checks that 'go test' prints a reasonable error when fuzzing is +# enabled, and multiple package or multiple fuzz targets match. +# TODO(#46312): support fuzzing multiple targets in multiple packages. + +# TODO(jayconrod): support shared memory on more platforms. +[!darwin] [!linux] [!windows] skip + +[short] skip + +# With fuzzing disabled, multiple targets can be tested. +go test ./... + +# With fuzzing enabled, at most one package may be tested, +# even if only one package contains fuzz targets. +! go test -fuzz=. ./... +stderr '^cannot use -fuzz flag with multiple packages$' +! go test -fuzz=. ./zero ./one +stderr '^cannot use -fuzz flag with multiple packages$' +go test -fuzz=. -fuzztime=1x ./one + +# With fuzzing enabled, at most one target in the same package may match. +! go test -fuzz=. ./two +stdout '^testing: will not fuzz, -fuzz matches more than one target: \[FuzzOne FuzzTwo\]$' +go test -fuzz=FuzzTwo -fuzztime=1x ./two + +-- go.mod -- +module fuzz + +go 1.18 +-- zero/zero.go -- +package zero +-- one/one_test.go -- +package one + +import "testing" + +func FuzzOne(f *testing.F) { + f.Fuzz(func(*testing.T, []byte) {}) +} +-- two/two_test.go -- +package two + +import "testing" + +func FuzzOne(f *testing.F) { + f.Fuzz(func(*testing.T, []byte) {}) +} + +func FuzzTwo(f *testing.F) { + f.Fuzz(func(*testing.T, []byte) {}) +}