From 7b88776403eb0015ec701abdc1618fd4f1223cd4 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Wed, 8 Jan 2025 15:38:37 +0000 Subject: [PATCH] chore(testutil): add testutil.GoleakOptions (#16070) - Adds `testutil.GoleakOptions` and consolidates existing options to this location - Pre-emptively adds required ignore for this Dependabot PR to pass CI https://github.com/coder/coder/pull/16066 --- 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 f1dfcd8c42..7674c906ff 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 572f7b509d..0d6e41772c 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 dfe67290c3..76321e6e19 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 db31513d18..c214981387 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 c10c853769..f95ab04c1c 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 c700773028..3eb779376c 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 5cfdb33e1a..4c87a745ab 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 4d15961a63..c944628149 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 d4dfae6529..8bd4898fe2 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 0f732e3f17..8039d27233 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 671c65c687..4677602328 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 eae7759d20..c5c2d8f924 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 9cae941128..d4aaacdf90 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 c64c2436da..7d016f7978 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 b448dd2582..c424d81d79 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 1c4be51974..e404f4afb3 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 269b035d50..bc8fc3d6f5 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 e37aa52d8a..e0cbfd1cfa 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 4300d7d1b8..43eb62bfa8 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 afc0f57cbd..3e21309c5f 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 e43e297fd2..2803e5a532 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 73e169ff0d..6b872f3259 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 e64d29edd2..a890aae9b2 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 25cdd1cf10..5d0de5ae39 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 c3892697ac..1a9fd50e81 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 5dffdf030b..e17f1c61e2 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 bf8c46ae06..fae8d073fb 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 540ebe4c7a..ab6ff8b242 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 7cd874b7f6..77c7dad15c 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 094ba67f9d..4f6b8bce6f 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 c2f9d4b11d..0c7d2af354 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 14eb7b201e..c22d803fe7 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 4d551de764..57d4ad72d2 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 e986c1c121..0e491b13a3 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 0000000000..e93c46a04c --- /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 03a6ed0927..5985043307 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