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>2023-02-08 15:16:21 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2023-02-16 10:48:00 +0300
commitb345e387f859617a93cff19ab4119d839471f923 (patch)
tree95291961764a7a824a04340e6e26f0874e100b61
parent90c6a45724c5a42c6ad9772671d747e405e7aa28 (diff)
remote: Refactor FindRemoteRepository tests to be table-driven
Refactor tests for FindRemoteRepository to be table-driven and generate test data at runtime starting from an empty repository.
-rw-r--r--internal/gitaly/service/remote/find_remote_repository_test.go107
-rw-r--r--internal/gitaly/service/remote/testdata/lsremotedata.txtbin7418 -> 0 bytes
2 files changed, 69 insertions, 38 deletions
diff --git a/internal/gitaly/service/remote/find_remote_repository_test.go b/internal/gitaly/service/remote/find_remote_repository_test.go
index 9279648b0..3488522fb 100644
--- a/internal/gitaly/service/remote/find_remote_repository_test.go
+++ b/internal/gitaly/service/remote/find_remote_repository_test.go
@@ -3,63 +3,94 @@
package remote
import (
- "bytes"
- "io"
+ "fmt"
"net/http"
"net/http/httptest"
+ "path/filepath"
"testing"
- "github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest"
+ "gitlab.com/gitlab-org/gitaly/v15/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
- "google.golang.org/grpc/codes"
)
func TestFindRemoteRepository(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
- _, repo, _, client := setupRemoteService(t, ctx)
+ cfg, _, _, client := setupRemoteService(t, ctx)
+ gitCmdFactory := gittest.NewCommandFactory(t, cfg)
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- infoRefs := testhelper.MustReadFile(t, "testdata/lsremotedata.txt")
- w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
- _, err := io.Copy(w, bytes.NewReader(infoRefs))
- require.NoError(t, err)
- }))
- defer ts.Close()
+ type setupData struct {
+ request *gitalypb.FindRemoteRepositoryRequest
+ expectedErr error
+ expectedResponse *gitalypb.FindRemoteRepositoryResponse
+ }
- resp, err := client.FindRemoteRepository(ctx, &gitalypb.FindRemoteRepositoryRequest{Remote: ts.URL, StorageName: repo.GetStorageName()})
- require.NoError(t, err)
+ for _, tc := range []struct {
+ desc string
+ setup func(t *testing.T) setupData
+ }{
+ {
+ desc: "empty remote",
+ setup: func(t *testing.T) setupData {
+ return setupData{
+ request: &gitalypb.FindRemoteRepositoryRequest{
+ Remote: "",
+ StorageName: cfg.Storages[0].Name,
+ },
+ expectedErr: structerr.NewInvalidArgument("empty remote can't be checked."),
+ }
+ },
+ },
+ {
+ desc: "nonexistent remote repository",
+ setup: func(t *testing.T) setupData {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ w.WriteHeader(404)
+ }))
+ t.Cleanup(server.Close)
- require.True(t, resp.Exists)
-}
+ return setupData{
+ request: &gitalypb.FindRemoteRepositoryRequest{
+ Remote: server.URL + "/does-not-exist.git",
+ StorageName: cfg.Storages[0].Name,
+ },
+ expectedResponse: &gitalypb.FindRemoteRepositoryResponse{},
+ }
+ },
+ },
+ {
+ desc: "successful",
+ setup: func(t *testing.T) setupData {
+ _, remoteRepoPath := gittest.CreateRepository(t, ctx, cfg)
+ gittest.WriteCommit(t, cfg, remoteRepoPath, gittest.WithBranch("main"))
-func TestFailedFindRemoteRepository(t *testing.T) {
- t.Parallel()
+ port := gittest.HTTPServer(t, ctx, gitCmdFactory, remoteRepoPath, nil)
- ctx := testhelper.Context(t)
- _, repo, _, client := setupRemoteService(t, ctx)
+ return setupData{
+ request: &gitalypb.FindRemoteRepositoryRequest{
+ Remote: fmt.Sprintf("http://127.0.0.1:%d/%s", port, filepath.Base(remoteRepoPath)),
+ StorageName: cfg.Storages[0].Name,
+ },
+ expectedResponse: &gitalypb.FindRemoteRepositoryResponse{
+ Exists: true,
+ },
+ }
+ },
+ },
+ } {
+ tc := tc
- testCases := []struct {
- description string
- remote string
- exists bool
- code codes.Code
- }{
- {"non existing remote", "http://example.com/test.git", false, codes.OK},
- {"empty remote", "", false, codes.InvalidArgument},
- }
+ t.Run(tc.desc, func(t *testing.T) {
+ t.Parallel()
- for _, tc := range testCases {
- resp, err := client.FindRemoteRepository(ctx, &gitalypb.FindRemoteRepositoryRequest{Remote: tc.remote, StorageName: repo.GetStorageName()})
- if tc.code == codes.OK {
- require.NoError(t, err)
- } else {
- testhelper.RequireGrpcCode(t, err, tc.code)
- continue
- }
+ setup := tc.setup(t)
- require.Equal(t, tc.exists, resp.GetExists(), tc.description)
+ response, err := client.FindRemoteRepository(ctx, setup.request)
+ testhelper.RequireGrpcError(t, setup.expectedErr, err)
+ testhelper.ProtoEqual(t, setup.expectedResponse, response)
+ })
}
}
diff --git a/internal/gitaly/service/remote/testdata/lsremotedata.txt b/internal/gitaly/service/remote/testdata/lsremotedata.txt
deleted file mode 100644
index 197c975d1..000000000
--- a/internal/gitaly/service/remote/testdata/lsremotedata.txt
+++ /dev/null
Binary files differ