Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2021-10-06 09:53:36 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-10-07 08:37:22 +0300
commitd8b43e951b747acc49a8d1fff1d59be9867a5eca (patch)
treeac836f0ff0cd7db109900e2f0bb7b10a0dfff835
parent27de9ce6c8a34f387ea2f3a00ca08fdbb4f74099 (diff)
testhelper: Unify setup of test suites
Setup of a test package's main function is quite repetitive: we call a separate `testMain()` function such that we can use `defer` calls, this function calls `MustHaveNoChildProcess()` and `Configure()`, and then we return the result of `m.Run()`. This is almost always exactly the same, with some exceptions where we need to have additional setup code. Unify this code via a single `testhelper.Run()` function which does all of this. It allows us greater flexibility in the setup code such that we can easily perform additional validation after the tests without having to modify all callsites. Note that this change removes calls to the goleak package in some places. These will resurface in a later commit in this patch series, where we instead move this call into `testhelper.Run()` such that it is executed by default.
-rw-r--r--STYLE.md20
-rw-r--r--client/client_test.go10
-rw-r--r--cmd/gitaly-backup/testhelper_test.go12
-rw-r--r--cmd/gitaly-git2go/conflicts/conflicts_test.go10
-rw-r--r--cmd/gitaly-git2go/main_test.go10
-rw-r--r--cmd/gitaly-hooks/hooks_test.go9
-rw-r--r--cmd/gitaly-lfs-smudge/lfs_smudge_test.go10
-rw-r--r--cmd/gitaly-ssh/testhelper_test.go12
-rw-r--r--cmd/praefect/main_test.go10
-rw-r--r--internal/backup/testhelper_test.go12
-rw-r--r--internal/bootstrap/testhelper_test.go9
-rw-r--r--internal/cache/testhelper_test.go10
-rw-r--r--internal/cgroups/cgroups_linux_test.go12
-rw-r--r--internal/dontpanic/testhelper_test.go10
-rw-r--r--internal/git/catfile/testhelper_test.go14
-rw-r--r--internal/git/gitpipe/testhelper_test.go12
-rw-r--r--internal/git/gittest/testhelper_test.go9
-rw-r--r--internal/git/helper_test.go10
-rw-r--r--internal/git/housekeeping/testhelper_test.go9
-rw-r--r--internal/git/localrepo/testhelper_test.go10
-rw-r--r--internal/git/objectpool/testhelper_test.go14
-rw-r--r--internal/git/packfile/packfile_test.go10
-rw-r--r--internal/git/quarantine/testhelper_test.go12
-rw-r--r--internal/git/remoterepo/helper_test.go10
-rw-r--r--internal/git/stats/testhelper_test.go10
-rw-r--r--internal/git/updateref/updateref_test.go14
-rw-r--r--internal/git2go/commit_test.go10
-rw-r--r--internal/gitaly/config/testhelper_test.go10
-rw-r--r--internal/gitaly/hook/testhelper_test.go10
-rw-r--r--internal/gitaly/linguist/linguist_test.go10
-rw-r--r--internal/gitaly/maintenance/main_test.go10
-rw-r--r--internal/gitaly/rubyserver/testhelper_test.go12
-rw-r--r--internal/gitaly/server/auth_test.go10
-rw-r--r--internal/gitaly/service/blob/testhelper_test.go12
-rw-r--r--internal/gitaly/service/cleanup/testhelper_test.go10
-rw-r--r--internal/gitaly/service/commit/testhelper_test.go10
-rw-r--r--internal/gitaly/service/conflicts/testhelper_test.go31
-rw-r--r--internal/gitaly/service/diff/testhelper_test.go10
-rw-r--r--internal/gitaly/service/hook/testhelper_test.go10
-rw-r--r--internal/gitaly/service/internalgitaly/testhelper_test.go10
-rw-r--r--internal/gitaly/service/namespace/namespace_test.go11
-rw-r--r--internal/gitaly/service/objectpool/testhelper_test.go9
-rw-r--r--internal/gitaly/service/operations/testhelper_test.go17
-rw-r--r--internal/gitaly/service/ref/testhelper_test.go21
-rw-r--r--internal/gitaly/service/remote/testhelper_test.go12
-rw-r--r--internal/gitaly/service/repository/testhelper_test.go11
-rw-r--r--internal/gitaly/service/server/server_test.go10
-rw-r--r--internal/gitaly/service/smarthttp/testhelper_test.go12
-rw-r--r--internal/gitaly/service/ssh/testhelper_test.go12
-rw-r--r--internal/gitaly/service/wiki/testhelper_test.go29
-rw-r--r--internal/gitaly/transaction/testhelper_test.go12
-rw-r--r--internal/gitalyssh/testhelper_test.go10
-rw-r--r--internal/gitlab/testhelper_test.go6
-rw-r--r--internal/helper/chunk/chunker_test.go10
-rw-r--r--internal/helper/repo_test.go10
-rw-r--r--internal/middleware/commandstatshandler/commandstatshandler_test.go7
-rw-r--r--internal/middleware/limithandler/limithandler_test.go10
-rw-r--r--internal/praefect/grpc-proxy/proxy/handler_ext_test.go14
-rw-r--r--internal/praefect/middleware/helper_test.go10
-rw-r--r--internal/praefect/nodes/init_test.go10
-rw-r--r--internal/praefect/replicator_test.go12
-rw-r--r--internal/praefect/repocleaner/init_test.go10
-rw-r--r--internal/safe/main_test.go10
-rw-r--r--internal/streamcache/testhelper_test.go8
-rw-r--r--internal/supervisor/supervisor_test.go54
-rw-r--r--internal/tempdir/testhelper_test.go11
-rw-r--r--internal/testhelper/configure.go72
-rw-r--r--internal/transaction/voting/testhelper_test.go12
68 files changed, 174 insertions, 713 deletions
diff --git a/STYLE.md b/STYLE.md
index a67515d6a..e663c397a 100644
--- a/STYLE.md
+++ b/STYLE.md
@@ -238,22 +238,10 @@ importantly, this includes any calls to `log.Fatal()` and related functions.
### Common setup
-When all tests require a common setup, we use the `TestMain()` function for
-this. `TestMain()` must call `os.Exit()` to indicate whether any tests failed.
-As this will cause deferred function calls to not be processed, we use the
-following pattern:
-
-```
-func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
-}
-```
+The `TestMain()` function shouldn't do any package-specific setup. Instead, all
+tests are supposed to set up required state as part of the tests themselves. All
+`TestMain()` functions must call `testhelper.Run()` though, which performs the
+setup of global state required for tests.
## Black box and white box testing
diff --git a/client/client_test.go b/client/client_test.go
index 6414626c1..e8083a74e 100644
--- a/client/client_test.go
+++ b/client/client_test.go
@@ -1,19 +1,11 @@
package client
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/cmd/gitaly-backup/testhelper_test.go b/cmd/gitaly-backup/testhelper_test.go
index 7fd4db4d4..64d8edae5 100644
--- a/cmd/gitaly-backup/testhelper_test.go
+++ b/cmd/gitaly-backup/testhelper_test.go
@@ -1,21 +1,11 @@
package main
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/cmd/gitaly-git2go/conflicts/conflicts_test.go b/cmd/gitaly-git2go/conflicts/conflicts_test.go
index 34b28bdf7..13a981365 100644
--- a/cmd/gitaly-git2go/conflicts/conflicts_test.go
+++ b/cmd/gitaly-git2go/conflicts/conflicts_test.go
@@ -4,7 +4,6 @@
package conflicts
import (
- "os"
"testing"
git "github.com/libgit2/git2go/v31"
@@ -17,14 +16,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestConflicts(t *testing.T) {
diff --git a/cmd/gitaly-git2go/main_test.go b/cmd/gitaly-git2go/main_test.go
index 75cfb525c..2113b2335 100644
--- a/cmd/gitaly-git2go/main_test.go
+++ b/cmd/gitaly-git2go/main_test.go
@@ -4,19 +4,11 @@
package main
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/cmd/gitaly-hooks/hooks_test.go b/cmd/gitaly-hooks/hooks_test.go
index 93d980bdd..811c6f6d8 100644
--- a/cmd/gitaly-hooks/hooks_test.go
+++ b/cmd/gitaly-hooks/hooks_test.go
@@ -96,14 +96,7 @@ func envForHooks(t testing.TB, ctx context.Context, cfg config.Cfg, repo *gitaly
}
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestHooksPrePostWithSymlinkedStoragePath(t *testing.T) {
diff --git a/cmd/gitaly-lfs-smudge/lfs_smudge_test.go b/cmd/gitaly-lfs-smudge/lfs_smudge_test.go
index 5136587d9..88f4c661f 100644
--- a/cmd/gitaly-lfs-smudge/lfs_smudge_test.go
+++ b/cmd/gitaly-lfs-smudge/lfs_smudge_test.go
@@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"net/http"
- "os"
"path/filepath"
"strings"
"testing"
@@ -52,14 +51,7 @@ type mapConfig struct {
}
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func (m *mapConfig) Get(key string) string {
diff --git a/cmd/gitaly-ssh/testhelper_test.go b/cmd/gitaly-ssh/testhelper_test.go
index 7fd4db4d4..64d8edae5 100644
--- a/cmd/gitaly-ssh/testhelper_test.go
+++ b/cmd/gitaly-ssh/testhelper_test.go
@@ -1,21 +1,11 @@
package main
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/cmd/praefect/main_test.go b/cmd/praefect/main_test.go
index 5fb0619e4..9a4e8949f 100644
--- a/cmd/praefect/main_test.go
+++ b/cmd/praefect/main_test.go
@@ -2,7 +2,6 @@ package main
import (
"errors"
- "os"
"testing"
"github.com/stretchr/testify/assert"
@@ -13,14 +12,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestNoConfigFlag(t *testing.T) {
diff --git a/internal/backup/testhelper_test.go b/internal/backup/testhelper_test.go
index 83b1e76cd..e7303c7cf 100644
--- a/internal/backup/testhelper_test.go
+++ b/internal/backup/testhelper_test.go
@@ -1,21 +1,11 @@
package backup
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/bootstrap/testhelper_test.go b/internal/bootstrap/testhelper_test.go
index baed8d01f..514236bd7 100644
--- a/internal/bootstrap/testhelper_test.go
+++ b/internal/bootstrap/testhelper_test.go
@@ -1,18 +1,11 @@
package bootstrap
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/cache/testhelper_test.go b/internal/cache/testhelper_test.go
index ed9467aae..687ad7d6e 100644
--- a/internal/cache/testhelper_test.go
+++ b/internal/cache/testhelper_test.go
@@ -1,19 +1,11 @@
package cache
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/cgroups/cgroups_linux_test.go b/internal/cgroups/cgroups_linux_test.go
index 7045a8484..9d8267bff 100644
--- a/internal/cgroups/cgroups_linux_test.go
+++ b/internal/cgroups/cgroups_linux_test.go
@@ -1,7 +1,6 @@
package cgroups
import (
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -10,16 +9,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func TestNewManager(t *testing.T) {
diff --git a/internal/dontpanic/testhelper_test.go b/internal/dontpanic/testhelper_test.go
index d4d8f7402..dd438042b 100644
--- a/internal/dontpanic/testhelper_test.go
+++ b/internal/dontpanic/testhelper_test.go
@@ -1,19 +1,11 @@
package dontpanic
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/git/catfile/testhelper_test.go b/internal/git/catfile/testhelper_test.go
index 4479adb85..fa977e099 100644
--- a/internal/git/catfile/testhelper_test.go
+++ b/internal/git/catfile/testhelper_test.go
@@ -2,7 +2,6 @@ package catfile
import (
"context"
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/command"
@@ -13,18 +12,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer func() {
- testhelper.MustHaveNoChildProcess()
- testhelper.MustHaveNoGoroutines()
- }()
-
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
type repoExecutor struct {
diff --git a/internal/git/gitpipe/testhelper_test.go b/internal/git/gitpipe/testhelper_test.go
index b3683135f..8b2532840 100644
--- a/internal/git/gitpipe/testhelper_test.go
+++ b/internal/git/gitpipe/testhelper_test.go
@@ -1,21 +1,11 @@
package gitpipe
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/git/gittest/testhelper_test.go b/internal/git/gittest/testhelper_test.go
index 89d810164..a16e984f6 100644
--- a/internal/git/gittest/testhelper_test.go
+++ b/internal/git/gittest/testhelper_test.go
@@ -12,14 +12,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
// setup sets up a test configuration and repository. Ideally we'd use our central test helpers to
diff --git a/internal/git/helper_test.go b/internal/git/helper_test.go
index eb7e925fd..f76065121 100644
--- a/internal/git/helper_test.go
+++ b/internal/git/helper_test.go
@@ -1,7 +1,6 @@
package git
import (
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -9,14 +8,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestValidateRevision(t *testing.T) {
diff --git a/internal/git/housekeeping/testhelper_test.go b/internal/git/housekeeping/testhelper_test.go
index 05d4da7f7..f0afe0397 100644
--- a/internal/git/housekeeping/testhelper_test.go
+++ b/internal/git/housekeeping/testhelper_test.go
@@ -1,18 +1,11 @@
package housekeeping
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/git/localrepo/testhelper_test.go b/internal/git/localrepo/testhelper_test.go
index 224d1566b..38486d705 100644
--- a/internal/git/localrepo/testhelper_test.go
+++ b/internal/git/localrepo/testhelper_test.go
@@ -1,19 +1,11 @@
package localrepo
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/git/objectpool/testhelper_test.go b/internal/git/objectpool/testhelper_test.go
index b252b37c1..bf79617d8 100644
--- a/internal/git/objectpool/testhelper_test.go
+++ b/internal/git/objectpool/testhelper_test.go
@@ -1,7 +1,6 @@
package objectpool
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/hooks"
@@ -9,13 +8,8 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- hooks.Override = "/"
- return m.Run()
+ testhelper.Run(m, testhelper.WithSetup(func() error {
+ hooks.Override = "/"
+ return nil
+ }))
}
diff --git a/internal/git/packfile/packfile_test.go b/internal/git/packfile/packfile_test.go
index c8d412827..ecf56563b 100644
--- a/internal/git/packfile/packfile_test.go
+++ b/internal/git/packfile/packfile_test.go
@@ -1,7 +1,6 @@
package packfile_test
import (
- "os"
"path/filepath"
"testing"
@@ -13,14 +12,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestList(t *testing.T) {
diff --git a/internal/git/quarantine/testhelper_test.go b/internal/git/quarantine/testhelper_test.go
index 7ad131635..38d896d42 100644
--- a/internal/git/quarantine/testhelper_test.go
+++ b/internal/git/quarantine/testhelper_test.go
@@ -1,21 +1,11 @@
package quarantine
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/git/remoterepo/helper_test.go b/internal/git/remoterepo/helper_test.go
index 555b5445d..619ff5b16 100644
--- a/internal/git/remoterepo/helper_test.go
+++ b/internal/git/remoterepo/helper_test.go
@@ -1,19 +1,11 @@
package remoterepo
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/git/stats/testhelper_test.go b/internal/git/stats/testhelper_test.go
index ce080ff27..b842dc949 100644
--- a/internal/git/stats/testhelper_test.go
+++ b/internal/git/stats/testhelper_test.go
@@ -1,19 +1,11 @@
package stats
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/git/updateref/updateref_test.go b/internal/git/updateref/updateref_test.go
index c2cf6c0d6..17c87fd16 100644
--- a/internal/git/updateref/updateref_test.go
+++ b/internal/git/updateref/updateref_test.go
@@ -3,7 +3,6 @@ package updateref
import (
"context"
"fmt"
- "os"
"strings"
"testing"
@@ -17,15 +16,10 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- hooks.Override = "/"
- return m.Run()
+ testhelper.Run(m, testhelper.WithSetup(func() error {
+ hooks.Override = "/"
+ return nil
+ }))
}
func setupUpdater(t *testing.T, ctx context.Context) (config.Cfg, *localrepo.Repo, *Updater) {
diff --git a/internal/git2go/commit_test.go b/internal/git2go/commit_test.go
index 04eb5334e..0f45235e2 100644
--- a/internal/git2go/commit_test.go
+++ b/internal/git2go/commit_test.go
@@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
- "os"
"strconv"
"strings"
"testing"
@@ -21,14 +20,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
type commit struct {
diff --git a/internal/gitaly/config/testhelper_test.go b/internal/gitaly/config/testhelper_test.go
index 88f7044fa..de452320d 100644
--- a/internal/gitaly/config/testhelper_test.go
+++ b/internal/gitaly/config/testhelper_test.go
@@ -1,19 +1,11 @@
package config_test
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/gitaly/hook/testhelper_test.go b/internal/gitaly/hook/testhelper_test.go
index 9ea5b4c4a..ed02a0c77 100644
--- a/internal/gitaly/hook/testhelper_test.go
+++ b/internal/gitaly/hook/testhelper_test.go
@@ -1,19 +1,11 @@
package hook
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/gitaly/linguist/linguist_test.go b/internal/gitaly/linguist/linguist_test.go
index 3caea5606..ea662ef60 100644
--- a/internal/gitaly/linguist/linguist_test.go
+++ b/internal/gitaly/linguist/linguist_test.go
@@ -2,7 +2,6 @@ package linguist
import (
"encoding/json"
- "os"
"path/filepath"
"testing"
@@ -13,14 +12,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestInstance_Stats_unmarshalJSONError(t *testing.T) {
diff --git a/internal/gitaly/maintenance/main_test.go b/internal/gitaly/maintenance/main_test.go
index f7b6eeaf1..2cf16c9b6 100644
--- a/internal/gitaly/maintenance/main_test.go
+++ b/internal/gitaly/maintenance/main_test.go
@@ -1,19 +1,11 @@
package maintenance
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/gitaly/rubyserver/testhelper_test.go b/internal/gitaly/rubyserver/testhelper_test.go
index 2d4c7deba..f304d96c2 100644
--- a/internal/gitaly/rubyserver/testhelper_test.go
+++ b/internal/gitaly/rubyserver/testhelper_test.go
@@ -1,21 +1,11 @@
package rubyserver
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/gitaly/server/auth_test.go b/internal/gitaly/server/auth_test.go
index 2dbde3056..43edf2c03 100644
--- a/internal/gitaly/server/auth_test.go
+++ b/internal/gitaly/server/auth_test.go
@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net"
- "os"
"testing"
"time"
@@ -39,14 +38,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestSanity(t *testing.T) {
diff --git a/internal/gitaly/service/blob/testhelper_test.go b/internal/gitaly/service/blob/testhelper_test.go
index 5d760d761..6dfffa6a4 100644
--- a/internal/gitaly/service/blob/testhelper_test.go
+++ b/internal/gitaly/service/blob/testhelper_test.go
@@ -1,7 +1,6 @@
package blob
import (
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -16,16 +15,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func setup(t *testing.T) (config.Cfg, *gitalypb.Repository, string, gitalypb.BlobServiceClient) {
diff --git a/internal/gitaly/service/cleanup/testhelper_test.go b/internal/gitaly/service/cleanup/testhelper_test.go
index 7cb00db68..220b46bc1 100644
--- a/internal/gitaly/service/cleanup/testhelper_test.go
+++ b/internal/gitaly/service/cleanup/testhelper_test.go
@@ -1,7 +1,6 @@
package cleanup
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
@@ -15,14 +14,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func setupCleanupService(t *testing.T) (config.Cfg, *gitalypb.Repository, string, gitalypb.CleanupServiceClient) {
diff --git a/internal/gitaly/service/commit/testhelper_test.go b/internal/gitaly/service/commit/testhelper_test.go
index e6f2c31cf..ece194fb2 100644
--- a/internal/gitaly/service/commit/testhelper_test.go
+++ b/internal/gitaly/service/commit/testhelper_test.go
@@ -2,7 +2,6 @@ package commit
import (
"io"
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -18,14 +17,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
// setupCommitService makes a basic configuration and starts the service with the client.
diff --git a/internal/gitaly/service/conflicts/testhelper_test.go b/internal/gitaly/service/conflicts/testhelper_test.go
index f85a2adc1..95797b072 100644
--- a/internal/gitaly/service/conflicts/testhelper_test.go
+++ b/internal/gitaly/service/conflicts/testhelper_test.go
@@ -1,11 +1,8 @@
package conflicts
import (
- "os"
- "path/filepath"
"testing"
- log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/hooks"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
@@ -23,30 +20,10 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- tempDir, err := os.MkdirTemp("", "gitaly")
- if err != nil {
- log.Error(err)
- return 1
- }
- defer func() {
- if err := os.RemoveAll(tempDir); err != nil {
- log.Error(err)
- }
- }()
-
- defer func(old string) { hooks.Override = old }(hooks.Override)
- hooks.Override = filepath.Join(tempDir, "hooks")
-
- return m.Run()
+ testhelper.Run(m, testhelper.WithSetup(func() error {
+ hooks.Override = "/"
+ return nil
+ }))
}
func SetupConfigAndRepo(t testing.TB, bare bool) (config.Cfg, *gitalypb.Repository, string) {
diff --git a/internal/gitaly/service/diff/testhelper_test.go b/internal/gitaly/service/diff/testhelper_test.go
index 1029cd3ed..06c12df5f 100644
--- a/internal/gitaly/service/diff/testhelper_test.go
+++ b/internal/gitaly/service/diff/testhelper_test.go
@@ -1,7 +1,6 @@
package diff
import (
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -15,14 +14,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func setupDiffService(t testing.TB, opt ...testserver.GitalyServerOpt) (config.Cfg, *gitalypb.Repository, string, gitalypb.DiffServiceClient) {
diff --git a/internal/gitaly/service/hook/testhelper_test.go b/internal/gitaly/service/hook/testhelper_test.go
index 35f7f416e..935a15d24 100644
--- a/internal/gitaly/service/hook/testhelper_test.go
+++ b/internal/gitaly/service/hook/testhelper_test.go
@@ -1,7 +1,6 @@
package hook
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
@@ -15,14 +14,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func setupHookService(t testing.TB) (config.Cfg, *gitalypb.Repository, string, gitalypb.HookServiceClient) {
diff --git a/internal/gitaly/service/internalgitaly/testhelper_test.go b/internal/gitaly/service/internalgitaly/testhelper_test.go
index d768d2d80..b1ba1d050 100644
--- a/internal/gitaly/service/internalgitaly/testhelper_test.go
+++ b/internal/gitaly/service/internalgitaly/testhelper_test.go
@@ -1,7 +1,6 @@
package internalgitaly
import (
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -14,14 +13,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func setupInternalGitalyService(t *testing.T, cfg config.Cfg, internalService gitalypb.InternalGitalyServer) gitalypb.InternalGitalyClient {
diff --git a/internal/gitaly/service/namespace/namespace_test.go b/internal/gitaly/service/namespace/namespace_test.go
index 8adab217a..7657e3b28 100644
--- a/internal/gitaly/service/namespace/namespace_test.go
+++ b/internal/gitaly/service/namespace/namespace_test.go
@@ -15,16 +15,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func TestNamespaceExists(t *testing.T) {
diff --git a/internal/gitaly/service/objectpool/testhelper_test.go b/internal/gitaly/service/objectpool/testhelper_test.go
index 8cdf1d180..2ffd56e74 100644
--- a/internal/gitaly/service/objectpool/testhelper_test.go
+++ b/internal/gitaly/service/objectpool/testhelper_test.go
@@ -25,14 +25,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func setup(t *testing.T, opts ...testserver.GitalyServerOpt) (config.Cfg, *gitalypb.Repository, string, storage.Locator, gitalypb.ObjectPoolServiceClient) {
diff --git a/internal/gitaly/service/operations/testhelper_test.go b/internal/gitaly/service/operations/testhelper_test.go
index b93c8b00e..c62315942 100644
--- a/internal/gitaly/service/operations/testhelper_test.go
+++ b/internal/gitaly/service/operations/testhelper_test.go
@@ -2,7 +2,6 @@ package operations
import (
"context"
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -32,21 +31,9 @@ var (
GitlabHooks []string
)
-func init() {
- GitlabHooks = append(GitlabHooks, append(gitlabPreHooks, gitlabPostHooks...)...)
-}
-
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ GitlabHooks = append(GitlabHooks, append(gitlabPreHooks, gitlabPostHooks...)...)
+ testhelper.Run(m)
}
func setupOperationsService(t testing.TB, ctx context.Context, options ...testserver.GitalyServerOpt) (context.Context, config.Cfg, *gitalypb.Repository, string, gitalypb.OperationServiceClient) {
diff --git a/internal/gitaly/service/ref/testhelper_test.go b/internal/gitaly/service/ref/testhelper_test.go
index 89f71285e..f530ca28a 100644
--- a/internal/gitaly/service/ref/testhelper_test.go
+++ b/internal/gitaly/service/ref/testhelper_test.go
@@ -2,7 +2,6 @@ package ref
import (
"bytes"
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -26,20 +25,12 @@ var localBranches = map[string]*gitalypb.GitCommit{
}
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- // Force small messages to test that fragmenting the
- // ref list works correctly
- lines.ItemsPerMessage = 3
-
- return m.Run()
+ testhelper.Run(m, testhelper.WithSetup(func() error {
+ // Force small messages to test that fragmenting the
+ // ref list works correctly
+ lines.ItemsPerMessage = 3
+ return nil
+ }))
}
func setupRefService(t testing.TB) (config.Cfg, *gitalypb.Repository, string, gitalypb.RefServiceClient) {
diff --git a/internal/gitaly/service/remote/testhelper_test.go b/internal/gitaly/service/remote/testhelper_test.go
index 35285044f..bf36a936d 100644
--- a/internal/gitaly/service/remote/testhelper_test.go
+++ b/internal/gitaly/service/remote/testhelper_test.go
@@ -1,7 +1,6 @@
package remote
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
@@ -15,16 +14,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func setupRemoteService(t *testing.T, opts ...testserver.GitalyServerOpt) (config.Cfg, *gitalypb.Repository, string, gitalypb.RemoteServiceClient) {
diff --git a/internal/gitaly/service/repository/testhelper_test.go b/internal/gitaly/service/repository/testhelper_test.go
index a50e3a7da..dbca91c4e 100644
--- a/internal/gitaly/service/repository/testhelper_test.go
+++ b/internal/gitaly/service/repository/testhelper_test.go
@@ -35,16 +35,7 @@ const testTimeString = "200601021504.05"
var testTime = time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func TestWithRubySidecar(t *testing.T) {
diff --git a/internal/gitaly/service/server/server_test.go b/internal/gitaly/service/server/server_test.go
index 95fca6a02..b2570c260 100644
--- a/internal/gitaly/service/server/server_test.go
+++ b/internal/gitaly/service/server/server_test.go
@@ -1,19 +1,11 @@
package server
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/gitaly/service/smarthttp/testhelper_test.go b/internal/gitaly/service/smarthttp/testhelper_test.go
index 3b4ef7cfc..3a6d1b70d 100644
--- a/internal/gitaly/service/smarthttp/testhelper_test.go
+++ b/internal/gitaly/service/smarthttp/testhelper_test.go
@@ -2,7 +2,6 @@ package smarthttp
import (
"context"
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -23,16 +22,7 @@ const (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func startSmartHTTPServer(t *testing.T, cfg config.Cfg, serverOpts ...ServerOpt) testserver.GitalyServer {
diff --git a/internal/gitaly/service/ssh/testhelper_test.go b/internal/gitaly/service/ssh/testhelper_test.go
index 7c5349c32..45ab030bf 100644
--- a/internal/gitaly/service/ssh/testhelper_test.go
+++ b/internal/gitaly/service/ssh/testhelper_test.go
@@ -1,7 +1,6 @@
package ssh
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
@@ -14,16 +13,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func runSSHServer(t *testing.T, cfg config.Cfg, serverOpts ...testserver.GitalyServerOpt) string {
diff --git a/internal/gitaly/service/wiki/testhelper_test.go b/internal/gitaly/service/wiki/testhelper_test.go
index 9302d6b14..247fd9ac8 100644
--- a/internal/gitaly/service/wiki/testhelper_test.go
+++ b/internal/gitaly/service/wiki/testhelper_test.go
@@ -2,12 +2,10 @@ package wiki
import (
"bytes"
- "os"
"reflect"
"runtime"
"testing"
- log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
@@ -33,29 +31,10 @@ type createWikiPageOpts struct {
var mockPageContent = bytes.Repeat([]byte("Mock wiki page content"), 10000)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- tempDir, err := os.MkdirTemp("", "gitaly")
- if err != nil {
- log.Error(err)
- return 1
- }
- defer func() {
- if err := os.RemoveAll(tempDir); err != nil {
- log.Error(err)
- }
- }()
-
- hooks.Override = tempDir + "/hooks"
-
- return m.Run()
+ testhelper.Run(m, testhelper.WithSetup(func() error {
+ hooks.Override = "/"
+ return nil
+ }))
}
func TestWithRubySidecar(t *testing.T) {
diff --git a/internal/gitaly/transaction/testhelper_test.go b/internal/gitaly/transaction/testhelper_test.go
index 314059345..3e17bd91e 100644
--- a/internal/gitaly/transaction/testhelper_test.go
+++ b/internal/gitaly/transaction/testhelper_test.go
@@ -1,21 +1,11 @@
package transaction
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/gitalyssh/testhelper_test.go b/internal/gitalyssh/testhelper_test.go
index 1f17e47a4..6af673482 100644
--- a/internal/gitalyssh/testhelper_test.go
+++ b/internal/gitalyssh/testhelper_test.go
@@ -1,19 +1,11 @@
package gitalyssh
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/gitlab/testhelper_test.go b/internal/gitlab/testhelper_test.go
index f8a245a2d..69a859a86 100644
--- a/internal/gitlab/testhelper_test.go
+++ b/internal/gitlab/testhelper_test.go
@@ -1,15 +1,11 @@
package gitlab
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- os.Exit(m.Run())
+ testhelper.Run(m)
}
diff --git a/internal/helper/chunk/chunker_test.go b/internal/helper/chunk/chunker_test.go
index c7f91fd85..7f13050d8 100644
--- a/internal/helper/chunk/chunker_test.go
+++ b/internal/helper/chunk/chunker_test.go
@@ -3,7 +3,6 @@ package chunk
import (
"io"
"net"
- "os"
"testing"
"github.com/stretchr/testify/require"
@@ -15,14 +14,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
type testSender struct {
diff --git a/internal/helper/repo_test.go b/internal/helper/repo_test.go
index fb5961c52..c65665131 100644
--- a/internal/helper/repo_test.go
+++ b/internal/helper/repo_test.go
@@ -1,7 +1,6 @@
package helper
import (
- "os"
"testing"
"github.com/stretchr/testify/assert"
@@ -10,14 +9,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func TestRepoPathEqual(t *testing.T) {
diff --git a/internal/middleware/commandstatshandler/commandstatshandler_test.go b/internal/middleware/commandstatshandler/commandstatshandler_test.go
index 2d923095e..184121228 100644
--- a/internal/middleware/commandstatshandler/commandstatshandler_test.go
+++ b/internal/middleware/commandstatshandler/commandstatshandler_test.go
@@ -25,6 +25,10 @@ import (
"google.golang.org/grpc/test/bufconn"
)
+func TestMain(m *testing.M) {
+ testhelper.Run(m)
+}
+
func createNewServer(t *testing.T, cfg config.Cfg) *grpc.Server {
logger := testhelper.NewTestLogger(t)
logrusEntry := logrus.NewEntry(logger).WithField("test", t.Name())
@@ -66,9 +70,6 @@ func getBufDialer(listener *bufconn.Listener) func(context.Context, string) (net
}
func TestInterceptor(t *testing.T) {
- cleanup := testhelper.Configure()
- defer cleanup()
-
cfg, repo, _ := testcfg.BuildWithRepo(t)
logBuffer := &bytes.Buffer{}
diff --git a/internal/middleware/limithandler/limithandler_test.go b/internal/middleware/limithandler/limithandler_test.go
index 743bb9d17..84df9f150 100644
--- a/internal/middleware/limithandler/limithandler_test.go
+++ b/internal/middleware/limithandler/limithandler_test.go
@@ -3,7 +3,6 @@ package limithandler_test
import (
"context"
"net"
- "os"
"sync"
"testing"
"time"
@@ -17,14 +16,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
func fixedLockKey(ctx context.Context) string {
diff --git a/internal/praefect/grpc-proxy/proxy/handler_ext_test.go b/internal/praefect/grpc-proxy/proxy/handler_ext_test.go
index 0aaea9cfe..35a11f150 100644
--- a/internal/praefect/grpc-proxy/proxy/handler_ext_test.go
+++ b/internal/praefect/grpc-proxy/proxy/handler_ext_test.go
@@ -12,7 +12,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
- "os"
"path/filepath"
"testing"
"time"
@@ -49,18 +48,7 @@ const (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer func() {
- testhelper.MustHaveNoChildProcess()
- testhelper.MustHaveNoGoroutines()
- }()
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
// asserting service is implemented on the server side and serves as a handler for stuff
diff --git a/internal/praefect/middleware/helper_test.go b/internal/praefect/middleware/helper_test.go
index e40f293dd..b733100c0 100644
--- a/internal/praefect/middleware/helper_test.go
+++ b/internal/praefect/middleware/helper_test.go
@@ -1,19 +1,11 @@
package middleware
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/praefect/nodes/init_test.go b/internal/praefect/nodes/init_test.go
index 678b275ea..1475680ed 100644
--- a/internal/praefect/nodes/init_test.go
+++ b/internal/praefect/nodes/init_test.go
@@ -1,19 +1,11 @@
package nodes
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) (code int) {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/praefect/replicator_test.go b/internal/praefect/replicator_test.go
index bf6abf37d..813a54043 100644
--- a/internal/praefect/replicator_test.go
+++ b/internal/praefect/replicator_test.go
@@ -2,7 +2,6 @@ package praefect
import (
"context"
- "os"
"path/filepath"
"strings"
"sync"
@@ -46,16 +45,7 @@ import (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
func TestReplMgr_ProcessBacklog(t *testing.T) {
diff --git a/internal/praefect/repocleaner/init_test.go b/internal/praefect/repocleaner/init_test.go
index 011d4f2a4..7b5501eab 100644
--- a/internal/praefect/repocleaner/init_test.go
+++ b/internal/praefect/repocleaner/init_test.go
@@ -1,19 +1,11 @@
package repocleaner
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) (code int) {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/safe/main_test.go b/internal/safe/main_test.go
index 3a903c513..7158dca83 100644
--- a/internal/safe/main_test.go
+++ b/internal/safe/main_test.go
@@ -1,19 +1,11 @@
package safe_test
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/streamcache/testhelper_test.go b/internal/streamcache/testhelper_test.go
index 1427567b4..03bcc2acf 100644
--- a/internal/streamcache/testhelper_test.go
+++ b/internal/streamcache/testhelper_test.go
@@ -1,15 +1,11 @@
package streamcache
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
-func TestMain(m *testing.M) { os.Exit(testMain(m)) }
-
-func testMain(m *testing.M) int {
- defer testhelper.Configure()()
- return m.Run()
+func TestMain(m *testing.M) {
+ testhelper.Run(m)
}
diff --git a/internal/supervisor/supervisor_test.go b/internal/supervisor/supervisor_test.go
index 9e2642d57..ccd8c1a93 100644
--- a/internal/supervisor/supervisor_test.go
+++ b/internal/supervisor/supervisor_test.go
@@ -12,7 +12,6 @@ import (
"testing"
"time"
- log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
@@ -24,45 +23,32 @@ var (
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
+ testhelper.Run(m, testhelper.WithSetup(func() error {
+ var err error
+ testDir, err = os.MkdirTemp("", "gitaly-supervisor-test")
+ if err != nil {
+ return err
+ }
- var err error
- testDir, err = os.MkdirTemp("", "gitaly-supervisor-test")
- if err != nil {
- log.Error(err)
- return 1
- }
- defer func() {
- if err := os.RemoveAll(testDir); err != nil {
- log.Error(err)
+ scriptPath, err := filepath.Abs("test-scripts/pid-server.go")
+ if err != nil {
+ return err
}
- }()
- scriptPath, err := filepath.Abs("test-scripts/pid-server.go")
- if err != nil {
- log.Error(err)
- return 1
- }
+ testExe = filepath.Join(testDir, "pid-server")
- testExe = filepath.Join(testDir, "pid-server")
- buildCmd := exec.Command("go", "build", "-o", testExe, scriptPath)
- buildCmd.Dir = filepath.Dir(scriptPath)
- buildCmd.Stderr = os.Stderr
- buildCmd.Stdout = os.Stdout
- if err := buildCmd.Run(); err != nil {
- log.Error(err)
- return 1
- }
+ buildCmd := exec.Command("go", "build", "-o", testExe, scriptPath)
+ buildCmd.Dir = filepath.Dir(scriptPath)
+ buildCmd.Stderr = os.Stderr
+ buildCmd.Stdout = os.Stdout
+ if err := buildCmd.Run(); err != nil {
+ return err
+ }
- socketPath = filepath.Join(testDir, "socket")
+ socketPath = filepath.Join(testDir, "socket")
- return m.Run()
+ return nil
+ }))
}
func TestRespawnAfterCrashWithoutCircuitBreaker(t *testing.T) {
diff --git a/internal/tempdir/testhelper_test.go b/internal/tempdir/testhelper_test.go
index 6c4397185..cdc9dc00e 100644
--- a/internal/tempdir/testhelper_test.go
+++ b/internal/tempdir/testhelper_test.go
@@ -1,20 +1,11 @@
package tempdir
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}
diff --git a/internal/testhelper/configure.go b/internal/testhelper/configure.go
index c18e77faf..521cb428b 100644
--- a/internal/testhelper/configure.go
+++ b/internal/testhelper/configure.go
@@ -8,6 +8,7 @@ import (
"runtime"
"strings"
"sync"
+ "testing"
log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
@@ -19,35 +20,88 @@ var (
testDirectory string
)
-// Configure sets up the global test configuration. On failure,
+// RunOption is an option that can be passed to Run.
+type RunOption func(*runConfig)
+
+type runConfig struct {
+ setup func() error
+}
+
+// WithSetup allows the caller of Run to pass a setup function that will be called after global
+// test state has been configured.
+func WithSetup(setup func() error) RunOption {
+ return func(cfg *runConfig) {
+ cfg.setup = setup
+ }
+}
+
+// Run sets up required testing state and executes the given test suite. It can optionally receive a
+// variable number of RunOptions.
+func Run(m *testing.M, opts ...RunOption) {
+ // Run tests in a separate function such that we can use deferred statements and still
+ // (indirectly) call `os.Exit()` in case the test setup failed.
+ if err := func() error {
+ var cfg runConfig
+ for _, opt := range opts {
+ opt(&cfg)
+ }
+
+ defer MustHaveNoChildProcess()
+
+ cleanup, err := configure()
+ if err != nil {
+ return err
+ }
+ defer cleanup()
+
+ if cfg.setup != nil {
+ if err := cfg.setup(); err != nil {
+ return fmt.Errorf("error calling setup function: %w", err)
+ }
+ }
+
+ m.Run()
+
+ return nil
+ }(); err != nil {
+ log.Fatalf("%v", err)
+ }
+}
+
+// configure sets up the global test configuration. On failure,
// terminates the program.
-func Configure() func() {
+func configure() (func(), error) {
+ var returnedErr error
configureOnce.Do(func() {
gitalylog.Configure(gitalylog.Loggers, "json", "panic")
testDirectory = getTestTmpDir()
for _, f := range []func() error{
- ConfigureGit,
+ configureGit,
} {
- if err := f(); err != nil {
+ if returnedErr = f(); returnedErr != nil {
if err := os.RemoveAll(testDirectory); err != nil {
log.Error(err)
}
- log.Fatalf("error configuring tests: %v", err)
+
+ return
}
}
})
+ if returnedErr != nil {
+ return nil, returnedErr
+ }
return func() {
if err := os.RemoveAll(testDirectory); err != nil {
- log.Fatalf("error removing test directory: %v", err)
+ log.Errorf("error removing test directory: %v", err)
}
- }
+ }, nil
}
-// ConfigureGit configures git for test purpose
-func ConfigureGit() error {
+// configureGit configures git for test purpose
+func configureGit() error {
// We cannot use gittest here given that we ain't got no config yet. We thus need to
// manually resolve the git executable, which is either stored in below envvar if
// executed via our Makefile, or else just git as resolved via PATH.
diff --git a/internal/transaction/voting/testhelper_test.go b/internal/transaction/voting/testhelper_test.go
index 0c8359afd..59ee6c4a8 100644
--- a/internal/transaction/voting/testhelper_test.go
+++ b/internal/transaction/voting/testhelper_test.go
@@ -1,21 +1,11 @@
package voting
import (
- "os"
"testing"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- defer testhelper.MustHaveNoChildProcess()
-
- cleanup := testhelper.Configure()
- defer cleanup()
-
- return m.Run()
+ testhelper.Run(m)
}