mirror of
https://github.com/kopia/kopia.git
synced 2026-05-18 11:44:36 -04:00
chore(infra): added more leak checks to tests (#1953)
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
}()
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user