From ad097c94a04025c865b3956566ea5da7670ed437 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 21 Dec 2021 14:23:50 +0100 Subject: pkg/testutil: add package Add package with RaceEnabled const that can be used in test to skip long tests in race mode. Switch existing tests to use the new package. Update #2886 --- pkg/csource/csource_test.go | 14 ++++++++++++++ pkg/csource/race_test.go | 24 ------------------------ pkg/testutil/norace.go | 9 +++++++++ pkg/testutil/race.go | 9 +++++++++ prog/decl_test.go | 4 +++- prog/export_test.go | 6 ++++-- prog/mutation_test.go | 6 ++++-- prog/norace_test.go | 9 --------- prog/prio_test.go | 4 +++- prog/prog_test.go | 4 +++- prog/race_test.go | 9 --------- 11 files changed, 49 insertions(+), 49 deletions(-) delete mode 100644 pkg/csource/race_test.go create mode 100644 pkg/testutil/norace.go create mode 100644 pkg/testutil/race.go delete mode 100644 prog/norace_test.go delete mode 100644 prog/race_test.go diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go index fc3b21573..91bd2cccf 100644 --- a/pkg/csource/csource_test.go +++ b/pkg/csource/csource_test.go @@ -14,11 +14,25 @@ import ( "testing" "time" + "github.com/google/syzkaller/pkg/testutil" "github.com/google/syzkaller/prog" _ "github.com/google/syzkaller/sys" "github.com/google/syzkaller/sys/targets" ) +func init() { + // csource tests consume too much memory under race detector (>1GB), + // and periodically timeout on Travis. So we skip them. + if testutil.RaceEnabled { + for _, arg := range os.Args[1:] { + if strings.Contains(arg, "-test.short") { + fmt.Printf("skipping race testing in short mode\n") + os.Exit(0) + } + } + } +} + func TestGenerate(t *testing.T) { t.Parallel() checked := make(map[string]bool) diff --git a/pkg/csource/race_test.go b/pkg/csource/race_test.go deleted file mode 100644 index c41cef5d3..000000000 --- a/pkg/csource/race_test.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -//go:build race -// +build race - -package csource - -import ( - "fmt" - "os" - "strings" -) - -func init() { - // csource tests consume too much memory under race detector (>1GB), - // and periodically timeout on Travis. So we skip them. - for _, arg := range os.Args[1:] { - if strings.Contains(arg, "-test.short") { - fmt.Printf("skipping race testing in short mode\n") - os.Exit(0) - } - } -} diff --git a/pkg/testutil/norace.go b/pkg/testutil/norace.go new file mode 100644 index 000000000..ceb03928f --- /dev/null +++ b/pkg/testutil/norace.go @@ -0,0 +1,9 @@ +// Copyright 2018 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +//go:build !race +// +build !race + +package testutil + +const RaceEnabled = false diff --git a/pkg/testutil/race.go b/pkg/testutil/race.go new file mode 100644 index 000000000..96a9043bb --- /dev/null +++ b/pkg/testutil/race.go @@ -0,0 +1,9 @@ +// Copyright 2018 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +//go:build race +// +build race + +package testutil + +const RaceEnabled = true diff --git a/prog/decl_test.go b/prog/decl_test.go index 6cec10e27..b041bf8e9 100644 --- a/prog/decl_test.go +++ b/prog/decl_test.go @@ -6,10 +6,12 @@ package prog import ( "strings" "testing" + + "github.com/google/syzkaller/pkg/testutil" ) func TestResourceCtors(t *testing.T) { - if testing.Short() && raceEnabled { + if testing.Short() && testutil.RaceEnabled { t.Skip("too slow") } testEachTarget(t, func(t *testing.T, target *Target) { diff --git a/prog/export_test.go b/prog/export_test.go index a007ed3ce..415b03047 100644 --- a/prog/export_test.go +++ b/prog/export_test.go @@ -9,6 +9,8 @@ import ( "os" "testing" "time" + + "github.com/google/syzkaller/pkg/testutil" ) // Export guts for testing. @@ -37,7 +39,7 @@ func iterCount() int { if testing.Short() { iters /= 10 } - if raceEnabled { + if testutil.RaceEnabled { iters /= 10 } return iters @@ -88,7 +90,7 @@ func skipTargetRace(t *testing.T, target *Target) { // Race execution is slow and we are getting timeouts on CI. // For tests that run for all targets, leave only 2 targets, // this should be enough to detect some races. - if raceEnabled && (target.OS != "test" || target.Arch != "64" && target.Arch != "32") { + if testutil.RaceEnabled && (target.OS != "test" || target.Arch != "64" && target.Arch != "32") { t.Skip("skipping all but test/64 targets in race mode") } } diff --git a/prog/mutation_test.go b/prog/mutation_test.go index 6dfa15d63..a14719065 100644 --- a/prog/mutation_test.go +++ b/prog/mutation_test.go @@ -8,6 +8,8 @@ import ( "fmt" "math/rand" "testing" + + "github.com/google/syzkaller/pkg/testutil" ) func TestMutationFlags(t *testing.T) { @@ -81,7 +83,7 @@ mutate_integer(0x0, 0x1, 0x1, 0x1, 0x0, 0x1, 0x0, 0x0, 0x1)`, } func TestMutateArgument(t *testing.T) { - if raceEnabled { + if testutil.RaceEnabled { t.Skip("skipping in race mode, too slow") } tests := [][2]string{ @@ -391,7 +393,7 @@ func BenchmarkGenerate(b *testing.B) { } func runMutationTests(t *testing.T, tests [][2]string, valid bool) { - if raceEnabled { + if testutil.RaceEnabled { t.Skip("skipping in race mode, too slow") } target := initTargetTest(t, "test", "64") diff --git a/prog/norace_test.go b/prog/norace_test.go deleted file mode 100644 index 02b461925..000000000 --- a/prog/norace_test.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2018 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -//go:build !race -// +build !race - -package prog - -const raceEnabled = false diff --git a/prog/prio_test.go b/prog/prio_test.go index 21eb7f147..3f2fbc2ce 100644 --- a/prog/prio_test.go +++ b/prog/prio_test.go @@ -7,6 +7,8 @@ import ( "math/rand" "reflect" "testing" + + "github.com/google/syzkaller/pkg/testutil" ) func TestNormalizePrio(t *testing.T) { @@ -69,7 +71,7 @@ func TestStaticPriorities(t *testing.T) { } func TestPrioDeterminism(t *testing.T) { - if raceEnabled { + if testutil.RaceEnabled { t.Skip("skipping in race mode, too slow") } target, rs, iters := initTest(t) diff --git a/prog/prog_test.go b/prog/prog_test.go index 6586d1a1d..a21c464a0 100644 --- a/prog/prog_test.go +++ b/prog/prog_test.go @@ -9,6 +9,8 @@ import ( "math/rand" "strings" "testing" + + "github.com/google/syzkaller/pkg/testutil" ) func TestGeneration(t *testing.T) { @@ -142,7 +144,7 @@ func TestVmaType(t *testing.T) { // deserialized for another arch. This happens when managers exchange // programs via hub. func TestCrossTarget(t *testing.T) { - if raceEnabled { + if testutil.RaceEnabled { t.Skip("skipping in race mode, too slow") } t.Parallel() diff --git a/prog/race_test.go b/prog/race_test.go deleted file mode 100644 index 0c41a94f2..000000000 --- a/prog/race_test.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2018 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -//go:build race -// +build race - -package prog - -const raceEnabled = true -- cgit mrf-deployment