diff options
author | Sean McGivern <sean@gitlab.com> | 2021-05-20 12:34:54 +0100 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2021-05-20 16:19:30 +0000 |
commit | 246059d720c703ee8c07a718d2acc6e07ac85bb4 (patch) | |
tree | b0d56517b75f1b620dacdcf45d2635d2a270ae2f /cmd | |
parent | 2600bf7ac3b70e7df0e164087241bfda0b73ea8f (diff) | |
download | gitlab-shell-246059d720c703ee8c07a718d2acc6e07ac85bb4.tar.gz |
Add a simple acceptance test for git-receive-pack
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/gitlab-sshd/acceptance_test.go | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/cmd/gitlab-sshd/acceptance_test.go b/cmd/gitlab-sshd/acceptance_test.go index 112247b..80a0838 100644 --- a/cmd/gitlab-sshd/acceptance_test.go +++ b/cmd/gitlab-sshd/acceptance_test.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "crypto/ed25519" + "encoding/json" "encoding/pem" "fmt" "io" @@ -16,19 +17,34 @@ import ( "path/filepath" "regexp" "runtime" + "strings" "testing" "github.com/mikesmitty/edkey" "github.com/pires/go-proxyproto" "github.com/stretchr/testify/require" + gitalyClient "gitlab.com/gitlab-org/gitaly/client" + pb "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/gitlab-shell/internal/testhelper" "golang.org/x/crypto/ssh" ) var ( - sshdPath = "" + sshdPath = "" + gitalyConnInfo *gitalyConnectionInfo ) +const ( + testRepo = "test-gitlab-shell/gitlab-test.git" + testRepoNamespace = "test-gitlab-shell" + testRepoImportUrl = "https://gitlab.com/gitlab-org/gitlab-test.git" +) + +type gitalyConnectionInfo struct { + Address string `json:"address"` + Storage string `json:"storage"` +} + func init() { rootDir := rootDir() sshdPath = filepath.Join(rootDir, "bin", "gitlab-sshd") @@ -36,6 +52,11 @@ func init() { if _, err := os.Stat(sshdPath); os.IsNotExist(err) { panic(fmt.Errorf("cannot find executable %s. Please run 'make compile'", sshdPath)) } + + gci, exists := os.LookupEnv("GITALY_CONNECTION_INFO") + if exists { + json.Unmarshal([]byte(gci), &gitalyConnInfo) + } } func rootDir() string { @@ -47,6 +68,29 @@ func rootDir() string { return filepath.Join(filepath.Dir(currentFile), "..", "..") } +func ensureGitalyRepository(t *testing.T) { + if os.Getenv("GITALY_CONNECTION_INFO") == "" { + t.Skip("GITALY_CONNECTION_INFO is not set") + } + + conn, err := gitalyClient.Dial(gitalyConnInfo.Address, gitalyClient.DefaultDialOpts) + require.NoError(t, err) + + namespace := pb.NewNamespaceServiceClient(conn) + repository := pb.NewRepositoryServiceClient(conn) + + // Remove the repository if it already exists, for consistency + rmNsReq := &pb.RemoveNamespaceRequest{StorageName: gitalyConnInfo.Storage, Name: testRepoNamespace} + _, err = namespace.RemoveNamespace(context.Background(), rmNsReq) + require.NoError(t, err) + + gl_repository := &pb.Repository{StorageName: gitalyConnInfo.Storage, RelativePath: testRepo} + createReq := &pb.CreateRepositoryFromURLRequest{Repository: gl_repository, Url: testRepoImportUrl} + + _, err = repository.CreateRepositoryFromURL(context.Background(), createReq) + require.NoError(t, err) +} + func successAPI(t *testing.T) http.Handler { t.Helper() @@ -73,7 +117,13 @@ func successAPI(t *testing.T) http.Handler { body, err := ioutil.ReadFile(filepath.Join(testhelper.TestRoot, "responses/allowed_without_console_messages.json")) require.NoError(t, err) - _, err = w.Write(body) + response := strings.Replace(string(body), "GITALY_REPOSITORY", testRepo, 1) + + if gitalyConnInfo != nil { + response = strings.Replace(response, "GITALY_ADDRESS", gitalyConnInfo.Address, 1) + } + + fmt.Fprint(w, response) require.NoError(t, err) case "/api/v4/internal/lfs_authenticate": fmt.Fprint(w, `{"username": "test-user", "lfs_token": "testlfstoken", "repo_path": "foo", "expires_in": 7200}`) @@ -335,3 +385,24 @@ func TestGitLfsAuthenticateSuccess(t *testing.T) { require.Equal(t, `{"header":{"Authorization":"Basic dGVzdC11c2VyOnRlc3RsZnN0b2tlbg=="},"href":"/info/lfs","expires_in":7200} `, string(output)) } + +func TestGitReceivePackSuccess(t *testing.T) { + ensureGitalyRepository(t) + + client := runSSHD(t, successAPI(t)) + + session, err := client.NewSession() + require.NoError(t, err) + defer session.Close() + + output, err := session.Output(fmt.Sprintf("git-receive-pack %s", testRepo)) + require.NoError(t, err) + + outputLines := strings.Split(string(output), "\n") + + for i := 0; i < (len(outputLines) - 1); i++ { + require.Regexp(t, "^[0-9a-f]{44} refs/(heads|tags)/[^ ]+", outputLines[i]) + } + + require.Equal(t, "0000", outputLines[len(outputLines)-1]) +} |