From a61927e0894ef50b3c783ab445ae69491fef5cee Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Sun, 15 May 2022 23:37:57 -0700 Subject: [PATCH] chore(infra): added more leak checks to tests (#1953) --- cli/command_repository_sync.go | 2 ++ cli/command_server_control_test.go | 4 ++-- cli/command_snapshot_migrate.go | 2 ++ internal/testutil/testutil.go | 22 +++++++--------------- tests/end_to_end_test/acl_test.go | 2 +- tests/end_to_end_test/main_test.go | 5 +---- tests/htmlui_e2e_test/htmlui_e2e_test.go | 2 +- tests/testenv/cli_test_env.go | 6 +++--- 8 files changed, 19 insertions(+), 26 deletions(-) diff --git a/cli/command_repository_sync.go b/cli/command_repository_sync.go index 42b23ddcb..408b5f2d5 100644 --- a/cli/command_repository_sync.go +++ b/cli/command_repository_sync.go @@ -67,6 +67,8 @@ func (c *commandRepositorySyncTo) setup(svc advancedAppServices, parent commandP return errors.Wrap(err, "open repository") } + defer rep.Close(ctx) // nolint:errcheck + dr, ok := rep.(repo.DirectRepository) if !ok { return errors.Errorf("sync only supports directly-connected repositories") diff --git a/cli/command_server_control_test.go b/cli/command_server_control_test.go index 25d405f87..7dc718ed8 100644 --- a/cli/command_server_control_test.go +++ b/cli/command_server_control_test.go @@ -31,12 +31,12 @@ func TestServerControl(t *testing.T) { var sp testutil.ServerParameters go func() { - kill := env.RunAndProcessStderr(t, sp.ProcessOutput, + wait, _ := env.RunAndProcessStderr(t, sp.ProcessOutput, "server", "start", "--insecure", "--random-server-control-password", "--address=127.0.0.1:0") close(serverStarted) - defer kill() + wait() close(serverStopped) }() diff --git a/cli/command_snapshot_migrate.go b/cli/command_snapshot_migrate.go index 38e479472..b7f7acb6d 100644 --- a/cli/command_snapshot_migrate.go +++ b/cli/command_snapshot_migrate.go @@ -50,6 +50,8 @@ func (c *commandSnapshotMigrate) run(ctx context.Context, destRepo repo.Reposito return errors.Wrap(err, "can't open source repository") } + defer sourceRepo.Close(ctx) // nolint:errcheck + sources, err := c.getSourcesToMigrate(ctx, sourceRepo) if err != nil { return errors.Wrap(err, "can't retrieve sources") diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index 6f2fcf26f..9ae71ddb3 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -103,27 +103,19 @@ func ShouldSkipLongFilenames() bool { } // MyTestMain runs tests and verifies some post-run invariants. -func MyTestMain(m *testing.M) { +func MyTestMain(m *testing.M, cleanups ...func()) { releasable.EnableTracking("persistent-cache") v := m.Run() - totalLeaked := 0 + if err := releasable.Verify(); err != nil { + log.Printf("found leaks: %v", err) - for itemKind, active := range releasable.Active() { - if len(active) > 0 { - log.Printf("found %v leaked %v:", len(active), itemKind) + v = 1 + } - for _, stack := range active { - log.Println(" - " + stack) - } - - totalLeaked++ - } - - if totalLeaked > 0 { - os.Exit(1) - } + for _, c := range cleanups { + c() } os.Exit(v) diff --git a/tests/end_to_end_test/acl_test.go b/tests/end_to_end_test/acl_test.go index ffa8956f9..a4dab401f 100644 --- a/tests/end_to_end_test/acl_test.go +++ b/tests/end_to_end_test/acl_test.go @@ -42,7 +42,7 @@ func TestACL(t *testing.T) { var sp testutil.ServerParameters - kill := serverEnvironment.RunAndProcessStderr(t, sp.ProcessOutput, + _, kill := serverEnvironment.RunAndProcessStderr(t, sp.ProcessOutput, "server", "start", "--address=localhost:0", "--server-control-username=admin-user", diff --git a/tests/end_to_end_test/main_test.go b/tests/end_to_end_test/main_test.go index b50675f34..7dca74914 100644 --- a/tests/end_to_end_test/main_test.go +++ b/tests/end_to_end_test/main_test.go @@ -79,8 +79,5 @@ func TestMain(m *testing.M) { log.Fatalf("error setting up test: %v", err) } - result := m.Run() - - oneTimeCleanup() - os.Exit(result) + testutil.MyTestMain(m, oneTimeCleanup) } diff --git a/tests/htmlui_e2e_test/htmlui_e2e_test.go b/tests/htmlui_e2e_test/htmlui_e2e_test.go index 5860cf4ac..c97eee0bd 100644 --- a/tests/htmlui_e2e_test/htmlui_e2e_test.go +++ b/tests/htmlui_e2e_test/htmlui_e2e_test.go @@ -42,7 +42,7 @@ func runInBrowser(t *testing.T, run func(ctx context.Context, sp *testutil.Serve var sp testutil.ServerParameters - kill := e.RunAndProcessStderr(t, sp.ProcessOutput, + _, kill := e.RunAndProcessStderr(t, sp.ProcessOutput, "server", "start", "--ui", "--address=localhost:0", diff --git a/tests/testenv/cli_test_env.go b/tests/testenv/cli_test_env.go index 8928687af..81f2c8444 100644 --- a/tests/testenv/cli_test_env.go +++ b/tests/testenv/cli_test_env.go @@ -100,10 +100,10 @@ func (e *CLITest) RunAndExpectSuccess(t *testing.T, args ...string) []string { } // RunAndProcessStderr runs the given command, and streams its output line-by-line to a given function until it returns false. -func (e *CLITest) RunAndProcessStderr(t *testing.T, callback func(line string) bool, args ...string) (kill func()) { +func (e *CLITest) RunAndProcessStderr(t *testing.T, callback func(line string) bool, args ...string) (wait func() error, kill func()) { t.Helper() - stdout, stderr, _, kill := e.Runner.Start(t, e.cmdArgs(args)) + stdout, stderr, wait, kill := e.Runner.Start(t, e.cmdArgs(args)) go io.Copy(io.Discard, stdout) scanner := bufio.NewScanner(stderr) @@ -120,7 +120,7 @@ func (e *CLITest) RunAndProcessStderr(t *testing.T, callback func(line string) b } }() - return kill + return wait, kill } // RunAndExpectSuccessWithErrOut runs the given command, expects it to succeed and returns its stdout and stderr lines.