diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-05-24 16:08:31 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-05-30 09:09:53 +0300 |
commit | e1af944eded9eacf33a1b1506b32ffc3b5f9ba80 (patch) | |
tree | aac7ece9886b65cbbb9f4339dc1e1839bca4220e | |
parent | 1d4c4f66e2e1be872d11c4c6c59415607a992845 (diff) |
gitaly-lfs-smudge: Add tests which exercise binary on a high-level
While we do have tests which verify that parts of the gitaly-lfs-smudge
command run as expected, we don't have any which test the final binary.
This makes it hard to test for some specific scenarios and doesn't give
us an easy way to verify that it works as expected.
Add such tests to improve our test coverage.
-rw-r--r-- | cmd/gitaly-lfs-smudge/main_test.go | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/cmd/gitaly-lfs-smudge/main_test.go b/cmd/gitaly-lfs-smudge/main_test.go new file mode 100644 index 000000000..55866505c --- /dev/null +++ b/cmd/gitaly-lfs-smudge/main_test.go @@ -0,0 +1,171 @@ +package main + +import ( + "bytes" + "encoding/json" + "io" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v15/internal/command" + "gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config" + "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" + "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testcfg" +) + +const logName = "gitaly_lfs_smudge.log" + +func TestGitalyLFSSmudge(t *testing.T) { + ctx := testhelper.Context(t) + cfg := testcfg.Build(t) + binary := testcfg.BuildGitalyLFSSmudge(t, cfg) + + gitlabCfg, cleanup := runTestServer(t, defaultOptions) + defer cleanup() + + marshalledGitlabCfg, err := json.Marshal(gitlabCfg) + require.NoError(t, err) + + marshalledTLSCfg, err := json.Marshal(config.TLS{ + CertPath: certPath, + KeyPath: keyPath, + }) + require.NoError(t, err) + + standardEnv := func(logDir string) []string { + return []string{ + "GL_REPOSITORY=project-1", + "GL_INTERNAL_CONFIG=" + string(marshalledGitlabCfg), + "GITALY_LOG_DIR=" + logDir, + "GITALY_TLS=" + string(marshalledTLSCfg), + } + } + + for _, tc := range []struct { + desc string + setup func(t *testing.T) ([]string, string) + stdin io.Reader + expectedErr string + expectedStdout string + expectedStderr string + expectedLogRegexp string + }{ + { + desc: "success", + setup: func(t *testing.T) ([]string, string) { + logDir := testhelper.TempDir(t) + return standardEnv(logDir), filepath.Join(logDir, logName) + }, + stdin: strings.NewReader(lfsPointer), + expectedStdout: "hello world", + expectedLogRegexp: "Finished HTTP request", + }, + { + desc: "missing Gitlab repository", + setup: func(t *testing.T) ([]string, string) { + logDir := testhelper.TempDir(t) + + return []string{ + "GL_INTERNAL_CONFIG=" + string(marshalledGitlabCfg), + "GITALY_LOG_DIR=" + logDir, + "GITALY_TLS=" + string(marshalledTLSCfg), + }, filepath.Join(logDir, logName) + }, + stdin: strings.NewReader(lfsPointer), + expectedErr: "exit status 1", + expectedLogRegexp: "error loading project: GL_REPOSITORY is not defined", + }, + { + desc: "missing Gitlab configuration", + setup: func(t *testing.T) ([]string, string) { + logDir := testhelper.TempDir(t) + + return []string{ + "GL_REPOSITORY=project-1", + "GITALY_LOG_DIR=" + logDir, + "GITALY_TLS=" + string(marshalledTLSCfg), + }, filepath.Join(logDir, logName) + }, + stdin: strings.NewReader(lfsPointer), + expectedErr: "exit status 1", + expectedLogRegexp: "unable to retrieve GL_INTERNAL_CONFIG", + }, + { + desc: "missing TLS configuration", + setup: func(t *testing.T) ([]string, string) { + logDir := testhelper.TempDir(t) + + return []string{ + "GL_REPOSITORY=project-1", + "GL_INTERNAL_CONFIG=" + string(marshalledGitlabCfg), + "GITALY_LOG_DIR=" + logDir, + }, filepath.Join(logDir, logName) + }, + stdin: strings.NewReader(lfsPointer), + expectedErr: "exit status 1", + expectedLogRegexp: "unable to retrieve GITALY_TLS", + }, + { + desc: "missing stdin", + setup: func(t *testing.T) ([]string, string) { + logDir := testhelper.TempDir(t) + return standardEnv(logDir), filepath.Join(logDir, logName) + }, + expectedErr: "exit status 1", + expectedStdout: "Cannot read from STDIN. This command should be run by the Git 'smudge' filter\n", + }, + { + desc: "non-LFS-pointer input", + setup: func(t *testing.T) ([]string, string) { + logDir := testhelper.TempDir(t) + return standardEnv(logDir), filepath.Join(logDir, logName) + }, + stdin: strings.NewReader("somethingsomething"), + expectedStdout: "somethingsomething", + expectedLogRegexp: "^$", + }, + { + desc: "mixed input", + setup: func(t *testing.T) ([]string, string) { + logDir := testhelper.TempDir(t) + return standardEnv(logDir), filepath.Join(logDir, logName) + }, + stdin: strings.NewReader(lfsPointer + "\nsomethingsomething\n"), + expectedStdout: lfsPointer + "\nsomethingsomething\n", + expectedLogRegexp: "^$", + }, + } { + t.Run(tc.desc, func(t *testing.T) { + env, logFile := tc.setup(t) + + var stdout, stderr bytes.Buffer + cmd, err := command.New(ctx, + exec.Command(binary), + tc.stdin, + &stdout, + &stderr, + env..., + ) + require.NoError(t, err) + + err = cmd.Wait() + if tc.expectedErr == "" { + require.NoError(t, err) + } else { + require.EqualError(t, err, tc.expectedErr) + } + require.Equal(t, tc.expectedStdout, stdout.String()) + require.Equal(t, tc.expectedStderr, stderr.String()) + + if tc.expectedLogRegexp == "" { + require.NoFileExists(t, logFile) + } else { + logData := testhelper.MustReadFile(t, logFile) + require.Regexp(t, tc.expectedLogRegexp, string(logData)) + } + }) + } +} |