diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-11 14:28:58 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-08-12 15:07:58 +0300 |
commit | 3f437d181c3c0348775c34827168c2db9b26c8bf (patch) | |
tree | 4cfafef0fe71c903c91d472a9a5b900bc08e4f57 | |
parent | 4381bb65451677ff1e58bdb4c1b7450729edf624 (diff) |
testhelper: Move helpers to build binaries into separate file
Move all test helpers which build binaries into a separate file to make
it easy to single them out as one logical unit.
-rw-r--r-- | internal/testhelper/build.go | 106 | ||||
-rw-r--r-- | internal/testhelper/configure.go | 96 |
2 files changed, 106 insertions, 96 deletions
diff --git a/internal/testhelper/build.go b/internal/testhelper/build.go new file mode 100644 index 000000000..e43a17fe2 --- /dev/null +++ b/internal/testhelper/build.go @@ -0,0 +1,106 @@ +package testhelper + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "testing" + "time" + + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" + "gitlab.com/gitlab-org/gitaly/v14/internal/version" +) + +// ConfigureGitalyGit2GoBin configures the gitaly-git2go command for tests +func ConfigureGitalyGit2GoBin(t testing.TB, cfg config.Cfg) { + buildBinary(t, cfg.BinDir, "gitaly-git2go") + // The link is needed because gitaly uses version-named binary. + // Please check out https://gitlab.com/gitlab-org/gitaly/-/issues/3647 for more info. + if err := os.Link(filepath.Join(cfg.BinDir, "gitaly-git2go"), filepath.Join(cfg.BinDir, "gitaly-git2go-"+version.GetModuleVersion())); err != nil { + if errors.Is(err, os.ErrExist) { + return + } + require.NoError(t, err) + } +} + +// ConfigureGitalyLfsSmudge configures the gitaly-lfs-smudge command for tests +func ConfigureGitalyLfsSmudge(t *testing.T, outputDir string) { + buildCommand(t, outputDir, "gitaly-lfs-smudge") +} + +// ConfigureGitalyHooksBin builds gitaly-hooks command for tests for the cfg. +func ConfigureGitalyHooksBin(t testing.TB, cfg config.Cfg) { + buildBinary(t, cfg.BinDir, "gitaly-hooks") +} + +// ConfigureGitalySSHBin builds gitaly-ssh command for tests for the cfg. +func ConfigureGitalySSHBin(t testing.TB, cfg config.Cfg) { + buildBinary(t, cfg.BinDir, "gitaly-ssh") +} + +func buildBinary(t testing.TB, dstDir, name string) { + // binsPath is a shared between all tests location where all compiled binaries should be placed + binsPath := filepath.Join(testDirectory, "bins") + // binPath is a path to a specific binary file + binPath := filepath.Join(binsPath, name) + // lockPath is a path to the special lock file used to prevent parallel build runs + lockPath := binPath + ".lock" + + defer func() { + if !t.Failed() { + // copy compiled binary to the destination folder + require.NoError(t, os.MkdirAll(dstDir, os.ModePerm)) + MustRunCommand(t, nil, "cp", binPath, dstDir) + } + }() + + require.NoError(t, os.MkdirAll(binsPath, os.ModePerm)) + + lockFile, err := os.OpenFile(lockPath, os.O_CREATE|os.O_EXCL, 0600) + if err != nil { + if !errors.Is(err, os.ErrExist) { + require.FailNow(t, err.Error()) + } + // another process is creating the binary at the moment, wait for it to complete (5s) + for i := 0; i < 50; i++ { + if _, err := os.Stat(binPath); err != nil { + if !errors.Is(err, os.ErrNotExist) { + require.NoError(t, err) + } + time.Sleep(100 * time.Millisecond) + continue + } + // binary was created + return + } + require.FailNow(t, "another process is creating binary for too long") + } + defer func() { require.NoError(t, os.Remove(lockPath)) }() + require.NoError(t, lockFile.Close()) + + if _, err := os.Stat(binPath); err != nil { + if !errors.Is(err, os.ErrNotExist) { + // something went wrong and for some reason the binary already exists + require.FailNow(t, err.Error()) + } + buildCommand(t, binsPath, name) + } +} + +func buildCommand(t testing.TB, outputDir, cmd string) { + if outputDir == "" { + log.Fatal("BinDir must be set") + } + + goBuildArgs := []string{ + "build", + "-tags", "static,system_libgit2", + "-o", filepath.Join(outputDir, cmd), + fmt.Sprintf("gitlab.com/gitlab-org/gitaly/v14/cmd/%s", cmd), + } + MustRunCommand(t, nil, "go", goBuildArgs...) +} diff --git a/internal/testhelper/configure.go b/internal/testhelper/configure.go index 443bbda23..3ba25c47d 100644 --- a/internal/testhelper/configure.go +++ b/internal/testhelper/configure.go @@ -1,7 +1,6 @@ package testhelper import ( - "errors" "fmt" "io/ioutil" "os" @@ -10,14 +9,10 @@ import ( "runtime" "strings" "sync" - "testing" - "time" log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" gitalylog "gitlab.com/gitlab-org/gitaly/v14/internal/log" - "gitlab.com/gitlab-org/gitaly/v14/internal/version" ) var ( @@ -120,97 +115,6 @@ func ConfigureRuby(cfg *config.Cfg) error { return nil } -// ConfigureGitalyGit2GoBin configures the gitaly-git2go command for tests -func ConfigureGitalyGit2GoBin(t testing.TB, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "gitaly-git2go") - // The link is needed because gitaly uses version-named binary. - // Please check out https://gitlab.com/gitlab-org/gitaly/-/issues/3647 for more info. - if err := os.Link(filepath.Join(cfg.BinDir, "gitaly-git2go"), filepath.Join(cfg.BinDir, "gitaly-git2go-"+version.GetModuleVersion())); err != nil { - if errors.Is(err, os.ErrExist) { - return - } - require.NoError(t, err) - } -} - -// ConfigureGitalyLfsSmudge configures the gitaly-lfs-smudge command for tests -func ConfigureGitalyLfsSmudge(t *testing.T, outputDir string) { - buildCommand(t, outputDir, "gitaly-lfs-smudge") -} - -// ConfigureGitalyHooksBin builds gitaly-hooks command for tests for the cfg. -func ConfigureGitalyHooksBin(t testing.TB, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "gitaly-hooks") -} - -// ConfigureGitalySSHBin builds gitaly-ssh command for tests for the cfg. -func ConfigureGitalySSHBin(t testing.TB, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "gitaly-ssh") -} - -func buildBinary(t testing.TB, dstDir, name string) { - // binsPath is a shared between all tests location where all compiled binaries should be placed - binsPath := filepath.Join(testDirectory, "bins") - // binPath is a path to a specific binary file - binPath := filepath.Join(binsPath, name) - // lockPath is a path to the special lock file used to prevent parallel build runs - lockPath := binPath + ".lock" - - defer func() { - if !t.Failed() { - // copy compiled binary to the destination folder - require.NoError(t, os.MkdirAll(dstDir, os.ModePerm)) - MustRunCommand(t, nil, "cp", binPath, dstDir) - } - }() - - require.NoError(t, os.MkdirAll(binsPath, os.ModePerm)) - - lockFile, err := os.OpenFile(lockPath, os.O_CREATE|os.O_EXCL, 0600) - if err != nil { - if !errors.Is(err, os.ErrExist) { - require.FailNow(t, err.Error()) - } - // another process is creating the binary at the moment, wait for it to complete (5s) - for i := 0; i < 50; i++ { - if _, err := os.Stat(binPath); err != nil { - if !errors.Is(err, os.ErrNotExist) { - require.NoError(t, err) - } - time.Sleep(100 * time.Millisecond) - continue - } - // binary was created - return - } - require.FailNow(t, "another process is creating binary for too long") - } - defer func() { require.NoError(t, os.Remove(lockPath)) }() - require.NoError(t, lockFile.Close()) - - if _, err := os.Stat(binPath); err != nil { - if !errors.Is(err, os.ErrNotExist) { - // something went wrong and for some reason the binary already exists - require.FailNow(t, err.Error()) - } - buildCommand(t, binsPath, name) - } -} - -func buildCommand(t testing.TB, outputDir, cmd string) { - if outputDir == "" { - log.Fatal("BinDir must be set") - } - - goBuildArgs := []string{ - "build", - "-tags", "static,system_libgit2", - "-o", filepath.Join(outputDir, cmd), - fmt.Sprintf("gitlab.com/gitlab-org/gitaly/v14/cmd/%s", cmd), - } - MustRunCommand(t, nil, "go", goBuildArgs...) -} - func getTestTmpDir() string { testTmpDir := os.Getenv("TEST_TMP_DIR") if testTmpDir != "" { |