diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-11-12 15:28:28 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-11-17 10:07:45 +0300 |
commit | 3d2d90b0b58d9fea04f26eee3ca402e5fdb919f7 (patch) | |
tree | 91877812d33c18edf5ce8c389ca9640bf069618c /internal | |
parent | e8fb9cd8975f62ab91dd015c8eafaf98b71c8067 (diff) |
tests: Unify test setup
There's been some proliferation in our test's main functions. Naturally
enough, this causes some of them to not work correctly, e.g. because
they use `defer` in the `TestMain` function, which is not getting
executed if `os.Exit` is called.
Unify existing setup functions to follow the same pattern, which is that
`TestMain()` calls out to `testMain()`.
Diffstat (limited to 'internal')
33 files changed, 184 insertions, 103 deletions
diff --git a/internal/cache/cachedb_test.go b/internal/cache/cachedb_test.go index 7031f6261..76708b00e 100644 --- a/internal/cache/cachedb_test.go +++ b/internal/cache/cachedb_test.go @@ -20,8 +20,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func TestStreamDBNaiveKeyer(t *testing.T) { diff --git a/internal/git/catfile/testhelper_test.go b/internal/git/catfile/testhelper_test.go index 8cbef4ef5..a1332dc1d 100644 --- a/internal/git/catfile/testhelper_test.go +++ b/internal/git/catfile/testhelper_test.go @@ -8,12 +8,11 @@ import ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() - + testhelper.Configure() return m.Run() } diff --git a/internal/git/helper_test.go b/internal/git/helper_test.go index df97021d2..70033750b 100644 --- a/internal/git/helper_test.go +++ b/internal/git/helper_test.go @@ -9,8 +9,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func TestValidateRevision(t *testing.T) { diff --git a/internal/git/log/commit_test.go b/internal/git/log/commit_test.go index 891443bb1..4d2d4fcae 100644 --- a/internal/git/log/commit_test.go +++ b/internal/git/log/commit_test.go @@ -14,8 +14,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func TestParseRawCommit(t *testing.T) { diff --git a/internal/git/objectpool/testhelper_test.go b/internal/git/objectpool/testhelper_test.go index 49b5d19ea..dab21cd9f 100644 --- a/internal/git/objectpool/testhelper_test.go +++ b/internal/git/objectpool/testhelper_test.go @@ -11,8 +11,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func NewTestObjectPool(ctx context.Context, t *testing.T, storageName string) (*ObjectPool, func()) { diff --git a/internal/git/packfile/packfile_test.go b/internal/git/packfile/packfile_test.go index d571a668b..4180f7024 100644 --- a/internal/git/packfile/packfile_test.go +++ b/internal/git/packfile/packfile_test.go @@ -10,8 +10,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func TestList(t *testing.T) { diff --git a/internal/git/remote/remote_test.go b/internal/git/remote/remote_test.go index b84588850..a0259163a 100644 --- a/internal/git/remote/remote_test.go +++ b/internal/git/remote/remote_test.go @@ -11,9 +11,14 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() testhelper.ConfigureGitalySSH() - os.Exit(m.Run()) + return m.Run() } func TestRemoveRemote(t *testing.T) { diff --git a/internal/git/stats/testhelper_test.go b/internal/git/stats/testhelper_test.go index 19eea9458..1247f2c8c 100644 --- a/internal/git/stats/testhelper_test.go +++ b/internal/git/stats/testhelper_test.go @@ -8,6 +8,11 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } diff --git a/internal/git/updateref/updateref_test.go b/internal/git/updateref/updateref_test.go index f8590ec2c..796192bc3 100644 --- a/internal/git/updateref/updateref_test.go +++ b/internal/git/updateref/updateref_test.go @@ -14,8 +14,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func setup(t *testing.T) (context.Context, *gitalypb.Repository, string, func()) { diff --git a/internal/git2go/commit_test.go b/internal/git2go/commit_test.go index 6a1eea7a0..7bba3aa31 100644 --- a/internal/git2go/commit_test.go +++ b/internal/git2go/commit_test.go @@ -19,9 +19,14 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() testhelper.ConfigureGitalyGit2Go() - os.Exit(m.Run()) + return m.Run() } type commit struct { diff --git a/internal/gitaly/hook/testhelper_test.go b/internal/gitaly/hook/testhelper_test.go index 9e4156e50..58fef1ed9 100644 --- a/internal/gitaly/hook/testhelper_test.go +++ b/internal/gitaly/hook/testhelper_test.go @@ -8,6 +8,11 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } diff --git a/internal/gitaly/linguist/linguist_test.go b/internal/gitaly/linguist/linguist_test.go index e428aefd2..55f62c9c0 100644 --- a/internal/gitaly/linguist/linguist_test.go +++ b/internal/gitaly/linguist/linguist_test.go @@ -12,8 +12,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func TestStatsUnmarshalJSONError(t *testing.T) { diff --git a/internal/gitaly/rubyserver/testhelper_test.go b/internal/gitaly/rubyserver/testhelper_test.go index 66575e48e..dfe218701 100644 --- a/internal/gitaly/rubyserver/testhelper_test.go +++ b/internal/gitaly/rubyserver/testhelper_test.go @@ -13,13 +13,13 @@ var ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() testRepo = testhelper.TestRepository() return m.Run() diff --git a/internal/gitaly/server/auth_test.go b/internal/gitaly/server/auth_test.go index f8c335364..e72034376 100644 --- a/internal/gitaly/server/auth_test.go +++ b/internal/gitaly/server/auth_test.go @@ -27,6 +27,17 @@ import ( healthpb "google.golang.org/grpc/health/grpc_health_v1" ) +func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() + testhelper.ConfigureGitalyHooksBinary() + return m.Run() +} + func TestSanity(t *testing.T) { serverSocketPath, clean := runServer(t) defer clean() @@ -288,17 +299,6 @@ func TestStreamingNoAuth(t *testing.T) { testhelper.RequireGrpcError(t, err, codes.Unauthenticated) } -func TestMain(m *testing.M) { - testhelper.Configure() - os.Exit(testMain(m)) -} - -func testMain(m *testing.M) int { - testhelper.ConfigureGitalyHooksBinary() - - return m.Run() -} - func TestAuthBeforeLimit(t *testing.T) { defer func(cfg config.Cfg) { config.Config = cfg diff --git a/internal/gitaly/service/blob/testhelper_test.go b/internal/gitaly/service/blob/testhelper_test.go index cf2db9532..7b3554d71 100644 --- a/internal/gitaly/service/blob/testhelper_test.go +++ b/internal/gitaly/service/blob/testhelper_test.go @@ -17,13 +17,13 @@ import ( var rubyServer = &rubyserver.Server{} func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() testhelper.ConfigureRuby() if err := rubyServer.Start(); err != nil { diff --git a/internal/gitaly/service/cleanup/testhelper_test.go b/internal/gitaly/service/cleanup/testhelper_test.go index 7b41fd123..5b1fb336a 100644 --- a/internal/gitaly/service/cleanup/testhelper_test.go +++ b/internal/gitaly/service/cleanup/testhelper_test.go @@ -12,8 +12,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func runCleanupServiceServer(t *testing.T) (string, func()) { diff --git a/internal/gitaly/service/commit/testhelper_test.go b/internal/gitaly/service/commit/testhelper_test.go index 290c4fca9..4b2792a05 100644 --- a/internal/gitaly/service/commit/testhelper_test.go +++ b/internal/gitaly/service/commit/testhelper_test.go @@ -14,13 +14,12 @@ import ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() - + testhelper.Configure() return m.Run() } diff --git a/internal/gitaly/service/conflicts/testhelper_test.go b/internal/gitaly/service/conflicts/testhelper_test.go index 9b3f8fdaa..86bbec0af 100644 --- a/internal/gitaly/service/conflicts/testhelper_test.go +++ b/internal/gitaly/service/conflicts/testhelper_test.go @@ -16,16 +16,16 @@ import ( "google.golang.org/grpc/reflection" ) +var RubyServer = &rubyserver.Server{} + func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } -var RubyServer = &rubyserver.Server{} - func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() testhelper.ConfigureGitalyGit2Go() tempDir, err := ioutil.TempDir("", "gitaly") diff --git a/internal/gitaly/service/diff/testhelper_test.go b/internal/gitaly/service/diff/testhelper_test.go index a701953f6..7a7a83aa0 100644 --- a/internal/gitaly/service/diff/testhelper_test.go +++ b/internal/gitaly/service/diff/testhelper_test.go @@ -13,13 +13,12 @@ import ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() - + testhelper.Configure() return m.Run() } diff --git a/internal/gitaly/service/hook/testhelper_test.go b/internal/gitaly/service/hook/testhelper_test.go index d0934d5ee..319ba9beb 100644 --- a/internal/gitaly/service/hook/testhelper_test.go +++ b/internal/gitaly/service/hook/testhelper_test.go @@ -15,8 +15,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func newHooksClient(t *testing.T, serverSocketPath string) (gitalypb.HookServiceClient, *grpc.ClientConn) { diff --git a/internal/gitaly/service/namespace/namespace_test.go b/internal/gitaly/service/namespace/namespace_test.go index 9e6ba06c1..20bfa7d37 100644 --- a/internal/gitaly/service/namespace/namespace_test.go +++ b/internal/gitaly/service/namespace/namespace_test.go @@ -16,11 +16,14 @@ import ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() + + testhelper.Configure() + config.Config.Storages = nil for _, st := range []string{"default", "other"} { diff --git a/internal/gitaly/service/objectpool/testhelper_test.go b/internal/gitaly/service/objectpool/testhelper_test.go index c848a882d..212e710bd 100644 --- a/internal/gitaly/service/objectpool/testhelper_test.go +++ b/internal/gitaly/service/objectpool/testhelper_test.go @@ -14,13 +14,12 @@ import ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() - + testhelper.Configure() return m.Run() } diff --git a/internal/gitaly/service/operations/testhelper_test.go b/internal/gitaly/service/operations/testhelper_test.go index 05adb36ad..71fb3caab 100644 --- a/internal/gitaly/service/operations/testhelper_test.go +++ b/internal/gitaly/service/operations/testhelper_test.go @@ -38,13 +38,14 @@ func init() { } func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() + cwd, err := os.Getwd() if err != nil { log.Fatal(err) diff --git a/internal/gitaly/service/ref/testhelper_test.go b/internal/gitaly/service/ref/testhelper_test.go index 150506d74..7fb871e7b 100644 --- a/internal/gitaly/service/ref/testhelper_test.go +++ b/internal/gitaly/service/ref/testhelper_test.go @@ -23,13 +23,14 @@ var ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() + // Force small messages to test that fragmenting the // ref list works correctly lines.ItemsPerMessage = 3 diff --git a/internal/gitaly/service/remote/testhelper_test.go b/internal/gitaly/service/remote/testhelper_test.go index aec62d020..c861b44f2 100644 --- a/internal/gitaly/service/remote/testhelper_test.go +++ b/internal/gitaly/service/remote/testhelper_test.go @@ -17,13 +17,13 @@ import ( var RubyServer = &rubyserver.Server{} func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() testhelper.ConfigureGitalySSH() if err := RubyServer.Start(); err != nil { diff --git a/internal/gitaly/service/repository/testhelper_test.go b/internal/gitaly/service/repository/testhelper_test.go index c32431271..fb74f0e10 100644 --- a/internal/gitaly/service/repository/testhelper_test.go +++ b/internal/gitaly/service/repository/testhelper_test.go @@ -34,6 +34,33 @@ var ( RubyServer = &rubyserver.Server{} ) +func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() + + testhelper.Configure() + + config.Config.Auth.Token = testhelper.RepositoryAuthToken + + var err error + config.Config.GitlabShell.Dir, err = filepath.Abs("testdata/gitlab-shell") + if err != nil { + log.Fatal(err) + } + + testhelper.ConfigureGitalySSH() + + if err := RubyServer.Start(); err != nil { + log.Fatal(err) + } + defer RubyServer.Stop() + + return m.Run() +} + func newRepositoryClient(t *testing.T, serverSocketPath string) (gitalypb.RepositoryServiceClient, *grpc.ClientConn) { connOpts := []grpc.DialOption{ grpc.WithInsecure(), @@ -125,29 +152,3 @@ func assertModTimeAfter(t *testing.T, afterTime time.Time, paths ...string) bool } return t.Failed() } - -func TestMain(m *testing.M) { - testhelper.Configure() - os.Exit(testMain(m)) -} - -func testMain(m *testing.M) int { - defer testhelper.MustHaveNoChildProcess() - - config.Config.Auth.Token = testhelper.RepositoryAuthToken - - var err error - config.Config.GitlabShell.Dir, err = filepath.Abs("testdata/gitlab-shell") - if err != nil { - log.Fatal(err) - } - - testhelper.ConfigureGitalySSH() - - if err := RubyServer.Start(); err != nil { - log.Fatal(err) - } - defer RubyServer.Stop() - - return m.Run() -} diff --git a/internal/gitaly/service/smarthttp/testhelper_test.go b/internal/gitaly/service/smarthttp/testhelper_test.go index 29551e301..eeff13553 100644 --- a/internal/gitaly/service/smarthttp/testhelper_test.go +++ b/internal/gitaly/service/smarthttp/testhelper_test.go @@ -24,11 +24,14 @@ const ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() + + testhelper.Configure() + hooks.Override = "/" cwd, err := os.Getwd() diff --git a/internal/gitaly/service/ssh/testhelper_test.go b/internal/gitaly/service/ssh/testhelper_test.go index a2f71bcfd..22415c71e 100644 --- a/internal/gitaly/service/ssh/testhelper_test.go +++ b/internal/gitaly/service/ssh/testhelper_test.go @@ -25,7 +25,6 @@ var ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } @@ -36,6 +35,8 @@ func testMain(m *testing.M) int { config.Config.Ruby.Dir = rubyDir }(config.Config.Ruby.Dir) + testhelper.Configure() + config.Config.Ruby.Dir = filepath.Join("../../../ruby", "git-hooks") err := os.RemoveAll(testPath) diff --git a/internal/gitaly/service/wiki/testhelper_test.go b/internal/gitaly/service/wiki/testhelper_test.go index cafceaf85..8ec53efe6 100644 --- a/internal/gitaly/service/wiki/testhelper_test.go +++ b/internal/gitaly/service/wiki/testhelper_test.go @@ -34,13 +34,14 @@ var ( ) func TestMain(m *testing.M) { - testhelper.Configure() os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() + testhelper.Configure() + tempDir, err := ioutil.TempDir("", "gitaly") if err != nil { log.Fatal(err) diff --git a/internal/gitalyssh/testhelper_test.go b/internal/gitalyssh/testhelper_test.go index 233792790..aff4bae94 100644 --- a/internal/gitalyssh/testhelper_test.go +++ b/internal/gitalyssh/testhelper_test.go @@ -8,6 +8,11 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } diff --git a/internal/helper/repo_test.go b/internal/helper/repo_test.go index 937dbe064..17e2496bf 100644 --- a/internal/helper/repo_test.go +++ b/internal/helper/repo_test.go @@ -13,8 +13,13 @@ import ( ) func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() testhelper.Configure() - os.Exit(m.Run()) + return m.Run() } func TestRepoPathEqual(t *testing.T) { diff --git a/internal/praefect/replicator_test.go b/internal/praefect/replicator_test.go index 82bfb1697..ac26045c2 100644 --- a/internal/praefect/replicator_test.go +++ b/internal/praefect/replicator_test.go @@ -48,6 +48,35 @@ import ( "google.golang.org/grpc/reflection" ) +var RubyServer = &rubyserver.Server{} + +func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() + + testhelper.Configure() + + gitaly_config.Config.Auth.Token = testhelper.RepositoryAuthToken + + var err error + gitaly_config.Config.GitlabShell.Dir, err = filepath.Abs("testdata/gitlab-shell") + if err != nil { + log.Fatal(err) + } + + testhelper.ConfigureGitalySSH() + + if err := RubyServer.Start(); err != nil { + log.Fatal(err) + } + defer RubyServer.Stop() + + return m.Run() +} + func TestReplMgr_ProcessBacklog(t *testing.T) { backupStorageName := "backup" @@ -1007,34 +1036,6 @@ func newRepositoryClient(t *testing.T, serverSocketPath string) (gitalypb.Reposi return gitalypb.NewRepositoryServiceClient(conn), conn } -var RubyServer = &rubyserver.Server{} - -func TestMain(m *testing.M) { - testhelper.Configure() - os.Exit(testMain(m)) -} - -func testMain(m *testing.M) int { - defer testhelper.MustHaveNoChildProcess() - - gitaly_config.Config.Auth.Token = testhelper.RepositoryAuthToken - - var err error - gitaly_config.Config.GitlabShell.Dir, err = filepath.Abs("testdata/gitlab-shell") - if err != nil { - log.Fatal(err) - } - - testhelper.ConfigureGitalySSH() - - if err := RubyServer.Start(); err != nil { - log.Fatal(err) - } - defer RubyServer.Stop() - - return m.Run() -} - func TestSubtractUint64(t *testing.T) { testCases := []struct { desc string diff --git a/internal/tempdir/testhelper_test.go b/internal/tempdir/testhelper_test.go index acb32d1e3..bf2008aa2 100644 --- a/internal/tempdir/testhelper_test.go +++ b/internal/tempdir/testhelper_test.go @@ -8,13 +8,11 @@ import ( ) func TestMain(m *testing.M) { - testhelper.Configure() - os.Exit(testMain(m)) } func testMain(m *testing.M) int { defer testhelper.MustHaveNoChildProcess() - + testhelper.Configure() return m.Run() } |