From 8088f20a3aedee74d3b7fdf24fbc58edb7c4f6ec Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Wed, 8 Jan 2025 14:25:07 +0000 Subject: [PATCH] chore(testutil): add testutil.GoleakOptions --- agent/agent_test.go | 2 +- agent/agentscripts/agentscripts_test.go | 2 +- agent/agentssh/agentssh_test.go | 2 +- cli/clitest/clitest_test.go | 3 ++- cli/root_internal_test.go | 11 ++--------- coderd/autobuild/lifecycle_executor_test.go | 2 +- coderd/autobuild/notify/notifier_test.go | 2 +- coderd/coderd_test.go | 2 +- coderd/coderdtest/coderdtest_test.go | 3 ++- coderd/cryptokeys/cache_test.go | 2 +- coderd/database/dbpurge/dbpurge_test.go | 2 +- coderd/database/dbrollup/dbrollup_test.go | 2 +- coderd/database/dbtestutil/postgres_test.go | 3 ++- coderd/database/migrations/migrate_test.go | 2 +- coderd/notifications/dispatch/smtp_test.go | 2 +- coderd/notifications/notifications_test.go | 2 +- coderd/provisionerdserver/acquirer_test.go | 2 +- coderd/telemetry/telemetry_test.go | 2 +- coderd/unhanger/detector_test.go | 2 +- coderd/updatecheck/updatecheck_test.go | 2 +- coderd/workspacestats/tracker_test.go | 2 +- enterprise/coderd/coderd_test.go | 2 +- enterprise/derpmesh/derpmesh_test.go | 2 +- .../provisionerd/remoteprovisioners_test.go | 2 +- enterprise/replicasync/replicasync_test.go | 2 +- enterprise/tailnet/pgcoord_test.go | 2 +- provisionerd/provisionerd_test.go | 2 +- provisionersdk/serve_test.go | 2 +- pty/start_other_test.go | 3 ++- pty/start_windows_test.go | 3 ++- support/support_test.go | 2 +- tailnet/conn_test.go | 2 +- tailnet/tailnettest/tailnettest_test.go | 3 ++- testutil/eventually_test.go | 2 +- testutil/goleak.go | 18 ++++++++++++++++++ vpn/speaker_internal_test.go | 2 +- 36 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 testutil/goleak.go diff --git a/agent/agent_test.go b/agent/agent_test.go index f1dfcd8c42a02..7674c906ff486 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -58,7 +58,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } // NOTE: These tests only work when your default shell is bash for some reason. diff --git a/agent/agentscripts/agentscripts_test.go b/agent/agentscripts/agentscripts_test.go index 572f7b509d4d2..0d6e41772cdb7 100644 --- a/agent/agentscripts/agentscripts_test.go +++ b/agent/agentscripts/agentscripts_test.go @@ -24,7 +24,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestExecuteBasic(t *testing.T) { diff --git a/agent/agentssh/agentssh_test.go b/agent/agentssh/agentssh_test.go index dfe67290c358b..76321e6e19d85 100644 --- a/agent/agentssh/agentssh_test.go +++ b/agent/agentssh/agentssh_test.go @@ -29,7 +29,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestNewServer_ServeClient(t *testing.T) { diff --git a/cli/clitest/clitest_test.go b/cli/clitest/clitest_test.go index db31513d182c7..c2149813875dc 100644 --- a/cli/clitest/clitest_test.go +++ b/cli/clitest/clitest_test.go @@ -8,10 +8,11 @@ import ( "github.com/coder/coder/v2/cli/clitest" "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/pty/ptytest" + "github.com/coder/coder/v2/testutil" ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestCli(t *testing.T) { diff --git a/cli/root_internal_test.go b/cli/root_internal_test.go index c10c853769900..f95ab04c1c9ec 100644 --- a/cli/root_internal_test.go +++ b/cli/root_internal_test.go @@ -19,6 +19,7 @@ import ( "github.com/coder/coder/v2/cli/cliui" "github.com/coder/coder/v2/cli/telemetry" "github.com/coder/coder/v2/codersdk" + "github.com/coder/coder/v2/testutil" "github.com/coder/pretty" "github.com/coder/serpent" ) @@ -29,15 +30,7 @@ func TestMain(m *testing.M) { // See: https://github.com/coder/coder/issues/8954 os.Exit(m.Run()) } - goleak.VerifyTestMain(m, - // The lumberjack library is used by by agent and seems to leave - // goroutines after Close(), fails TestGitSSH tests. - // https://github.com/natefinch/lumberjack/pull/100 - goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"), - goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).mill.func1"), - // The pq library appears to leave around a goroutine after Close(). - goleak.IgnoreTopFunction("github.com/lib/pq.NewDialListener"), - ) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func Test_formatExamples(t *testing.T) { diff --git a/coderd/autobuild/lifecycle_executor_test.go b/coderd/autobuild/lifecycle_executor_test.go index c700773028d0a..3eb779376cc5c 100644 --- a/coderd/autobuild/lifecycle_executor_test.go +++ b/coderd/autobuild/lifecycle_executor_test.go @@ -1256,5 +1256,5 @@ func mustWorkspaceParameters(t *testing.T, client *codersdk.Client, workspaceID } func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } diff --git a/coderd/autobuild/notify/notifier_test.go b/coderd/autobuild/notify/notifier_test.go index 5cfdb33e1acd5..4c87a745aba0c 100644 --- a/coderd/autobuild/notify/notifier_test.go +++ b/coderd/autobuild/notify/notifier_test.go @@ -122,5 +122,5 @@ func durations(ds ...time.Duration) []time.Duration { } func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } diff --git a/coderd/coderd_test.go b/coderd/coderd_test.go index 4d15961a6388e..c94462814999e 100644 --- a/coderd/coderd_test.go +++ b/coderd/coderd_test.go @@ -39,7 +39,7 @@ import ( var updateGoldenFiles = flag.Bool("update", false, "Update golden files") func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestBuildInfo(t *testing.T) { diff --git a/coderd/coderdtest/coderdtest_test.go b/coderd/coderdtest/coderdtest_test.go index d4dfae6529e8b..8bd4898fe2f21 100644 --- a/coderd/coderdtest/coderdtest_test.go +++ b/coderd/coderdtest/coderdtest_test.go @@ -6,10 +6,11 @@ import ( "go.uber.org/goleak" "github.com/coder/coder/v2/coderd/coderdtest" + "github.com/coder/coder/v2/testutil" ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestNew(t *testing.T) { diff --git a/coderd/cryptokeys/cache_test.go b/coderd/cryptokeys/cache_test.go index 0f732e3f171bc..8039d27233b59 100644 --- a/coderd/cryptokeys/cache_test.go +++ b/coderd/cryptokeys/cache_test.go @@ -18,7 +18,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestCryptoKeyCache(t *testing.T) { diff --git a/coderd/database/dbpurge/dbpurge_test.go b/coderd/database/dbpurge/dbpurge_test.go index 671c65c68790e..4677602328c89 100644 --- a/coderd/database/dbpurge/dbpurge_test.go +++ b/coderd/database/dbpurge/dbpurge_test.go @@ -34,7 +34,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } // Ensures no goroutines leak. diff --git a/coderd/database/dbrollup/dbrollup_test.go b/coderd/database/dbrollup/dbrollup_test.go index eae7759d2059c..c5c2d8f9243b0 100644 --- a/coderd/database/dbrollup/dbrollup_test.go +++ b/coderd/database/dbrollup/dbrollup_test.go @@ -23,7 +23,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestRollup_Close(t *testing.T) { diff --git a/coderd/database/dbtestutil/postgres_test.go b/coderd/database/dbtestutil/postgres_test.go index 9cae9411289ad..d4aaacdf909d8 100644 --- a/coderd/database/dbtestutil/postgres_test.go +++ b/coderd/database/dbtestutil/postgres_test.go @@ -12,10 +12,11 @@ import ( "github.com/coder/coder/v2/coderd/database/dbtestutil" "github.com/coder/coder/v2/coderd/database/migrations" + "github.com/coder/coder/v2/testutil" ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestOpen(t *testing.T) { diff --git a/coderd/database/migrations/migrate_test.go b/coderd/database/migrations/migrate_test.go index c64c2436da18d..7d016f7978fb1 100644 --- a/coderd/database/migrations/migrate_test.go +++ b/coderd/database/migrations/migrate_test.go @@ -28,7 +28,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestMigrate(t *testing.T) { diff --git a/coderd/notifications/dispatch/smtp_test.go b/coderd/notifications/dispatch/smtp_test.go index b448dd2582e67..c424d81d79683 100644 --- a/coderd/notifications/dispatch/smtp_test.go +++ b/coderd/notifications/dispatch/smtp_test.go @@ -26,7 +26,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestSMTP(t *testing.T) { diff --git a/coderd/notifications/notifications_test.go b/coderd/notifications/notifications_test.go index 1c4be51974b05..e404f4afb3c19 100644 --- a/coderd/notifications/notifications_test.go +++ b/coderd/notifications/notifications_test.go @@ -56,7 +56,7 @@ import ( var updateGoldenFiles = flag.Bool("update", false, "Update golden files") func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } // TestBasicNotificationRoundtrip enqueues a message to the store, waits for it to be acquired by a notifier, diff --git a/coderd/provisionerdserver/acquirer_test.go b/coderd/provisionerdserver/acquirer_test.go index 269b035d50edd..bc8fc3d6f5869 100644 --- a/coderd/provisionerdserver/acquirer_test.go +++ b/coderd/provisionerdserver/acquirer_test.go @@ -28,7 +28,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } // TestAcquirer_Store tests that a database.Store is accepted as a provisionerdserver.AcquirerStore diff --git a/coderd/telemetry/telemetry_test.go b/coderd/telemetry/telemetry_test.go index e37aa52d8a73b..e0cbfd1cfa193 100644 --- a/coderd/telemetry/telemetry_test.go +++ b/coderd/telemetry/telemetry_test.go @@ -27,7 +27,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestTelemetry(t *testing.T) { diff --git a/coderd/unhanger/detector_test.go b/coderd/unhanger/detector_test.go index 4300d7d1b8661..43eb62bfa884b 100644 --- a/coderd/unhanger/detector_test.go +++ b/coderd/unhanger/detector_test.go @@ -28,7 +28,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestDetectorNoJobs(t *testing.T) { diff --git a/coderd/updatecheck/updatecheck_test.go b/coderd/updatecheck/updatecheck_test.go index afc0f57cbdd41..3e21309c5ff71 100644 --- a/coderd/updatecheck/updatecheck_test.go +++ b/coderd/updatecheck/updatecheck_test.go @@ -154,5 +154,5 @@ func TestChecker_Latest(t *testing.T) { } func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } diff --git a/coderd/workspacestats/tracker_test.go b/coderd/workspacestats/tracker_test.go index e43e297fd2ddd..2803e5a5322b3 100644 --- a/coderd/workspacestats/tracker_test.go +++ b/coderd/workspacestats/tracker_test.go @@ -219,5 +219,5 @@ func TestTracker_MultipleInstances(t *testing.T) { } func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } diff --git a/enterprise/coderd/coderd_test.go b/enterprise/coderd/coderd_test.go index 73e169ff0da0f..6b872f32591ca 100644 --- a/enterprise/coderd/coderd_test.go +++ b/enterprise/coderd/coderd_test.go @@ -55,7 +55,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestEntitlements(t *testing.T) { diff --git a/enterprise/derpmesh/derpmesh_test.go b/enterprise/derpmesh/derpmesh_test.go index e64d29edd219c..a890aae9b254c 100644 --- a/enterprise/derpmesh/derpmesh_test.go +++ b/enterprise/derpmesh/derpmesh_test.go @@ -23,7 +23,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestDERPMesh(t *testing.T) { diff --git a/enterprise/provisionerd/remoteprovisioners_test.go b/enterprise/provisionerd/remoteprovisioners_test.go index 25cdd1cf103b1..5d0de5ae396b7 100644 --- a/enterprise/provisionerd/remoteprovisioners_test.go +++ b/enterprise/provisionerd/remoteprovisioners_test.go @@ -20,7 +20,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestRemoteConnector_Mainline(t *testing.T) { diff --git a/enterprise/replicasync/replicasync_test.go b/enterprise/replicasync/replicasync_test.go index c3892697aca10..1a9fd50e81223 100644 --- a/enterprise/replicasync/replicasync_test.go +++ b/enterprise/replicasync/replicasync_test.go @@ -25,7 +25,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestReplica(t *testing.T) { diff --git a/enterprise/tailnet/pgcoord_test.go b/enterprise/tailnet/pgcoord_test.go index 5dffdf030b509..e17f1c61e28a2 100644 --- a/enterprise/tailnet/pgcoord_test.go +++ b/enterprise/tailnet/pgcoord_test.go @@ -31,7 +31,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestPGCoordinatorSingle_ClientWithoutAgent(t *testing.T) { diff --git a/provisionerd/provisionerd_test.go b/provisionerd/provisionerd_test.go index bf8c46ae06133..fae8d073fbfd0 100644 --- a/provisionerd/provisionerd_test.go +++ b/provisionerd/provisionerd_test.go @@ -30,7 +30,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func closedWithin(c chan struct{}, d time.Duration) func() bool { diff --git a/provisionersdk/serve_test.go b/provisionersdk/serve_test.go index 540ebe4c7af19..ab6ff8b242de9 100644 --- a/provisionersdk/serve_test.go +++ b/provisionersdk/serve_test.go @@ -17,7 +17,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestProvisionerSDK(t *testing.T) { diff --git a/pty/start_other_test.go b/pty/start_other_test.go index 7cd874b7f6267..77c7dad15c48b 100644 --- a/pty/start_other_test.go +++ b/pty/start_other_test.go @@ -15,10 +15,11 @@ import ( "github.com/coder/coder/v2/pty" "github.com/coder/coder/v2/pty/ptytest" + "github.com/coder/coder/v2/testutil" ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestStart(t *testing.T) { diff --git a/pty/start_windows_test.go b/pty/start_windows_test.go index 094ba67f9d9c8..4f6b8bce6f8a6 100644 --- a/pty/start_windows_test.go +++ b/pty/start_windows_test.go @@ -11,6 +11,7 @@ import ( "github.com/coder/coder/v2/pty" "github.com/coder/coder/v2/pty/ptytest" + "github.com/coder/coder/v2/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/goleak" @@ -18,7 +19,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestStart(t *testing.T) { diff --git a/support/support_test.go b/support/support_test.go index c2f9d4b11d00b..0c7d2af354044 100644 --- a/support/support_test.go +++ b/support/support_test.go @@ -30,7 +30,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestRun(t *testing.T) { diff --git a/tailnet/conn_test.go b/tailnet/conn_test.go index 14eb7b201ef30..c22d803fe74bc 100644 --- a/tailnet/conn_test.go +++ b/tailnet/conn_test.go @@ -19,7 +19,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestTailnet(t *testing.T) { diff --git a/tailnet/tailnettest/tailnettest_test.go b/tailnet/tailnettest/tailnettest_test.go index 4d551de764d4a..57d4ad72d2d3c 100644 --- a/tailnet/tailnettest/tailnettest_test.go +++ b/tailnet/tailnettest/tailnettest_test.go @@ -6,10 +6,11 @@ import ( "go.uber.org/goleak" "github.com/coder/coder/v2/tailnet/tailnettest" + "github.com/coder/coder/v2/testutil" ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestRunDERPAndSTUN(t *testing.T) { diff --git a/testutil/eventually_test.go b/testutil/eventually_test.go index e986c1c121e44..0e491b13a3631 100644 --- a/testutil/eventually_test.go +++ b/testutil/eventually_test.go @@ -11,7 +11,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } func TestEventually(t *testing.T) { diff --git a/testutil/goleak.go b/testutil/goleak.go new file mode 100644 index 0000000000000..e93c46a04c5f0 --- /dev/null +++ b/testutil/goleak.go @@ -0,0 +1,18 @@ +package testutil + +import "go.uber.org/goleak" + +// GoleakOptions is a common list of options to pass to goleak. This is useful if there is a known +// leaky function we want to exclude from goleak. +var GoleakOptions []goleak.Option = []goleak.Option{ + // seelog (indirect dependency of dd-trace-go) has a known goroutine leak (https://github.com/cihub/seelog/issues/182) + // When https://github.com/DataDog/dd-trace-go/issues/2987 is resolved, this can be removed. + goleak.IgnoreAnyFunction("github.com/cihub/seelog.(*asyncLoopLogger).processQueue"), + // The lumberjack library is used by by agent and seems to leave + // goroutines after Close(), fails TestGitSSH tests. + // https://github.com/natefinch/lumberjack/pull/100 + goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"), + goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).mill.func1"), + // The pq library appears to leave around a goroutine after Close(). + goleak.IgnoreTopFunction("github.com/lib/pq.NewDialListener"), +} diff --git a/vpn/speaker_internal_test.go b/vpn/speaker_internal_test.go index 03a6ed0927c35..5985043307107 100644 --- a/vpn/speaker_internal_test.go +++ b/vpn/speaker_internal_test.go @@ -19,7 +19,7 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + goleak.VerifyTestMain(m, testutil.GoleakOptions...) } // TestSpeaker_RawPeer tests the speaker with a peer that we simulate by directly making reads and