diff options
author | James Fargher <jfargher@gitlab.com> | 2023-06-01 06:59:37 +0300 |
---|---|---|
committer | James Fargher <jfargher@gitlab.com> | 2023-06-06 07:19:22 +0300 |
commit | 05b5521d71867a2f8b77d873cbce64a5667533ca (patch) | |
tree | 6a7c6b090b4e06a7a3bc1bc717c3035d66df2f43 | |
parent | e6f48cdeae55e5001d4bebafce2cc132eabdb913 (diff) |
ref service: Unify FindDefaultBranchName tests
These tests were written in an out-of-date style. This commit updates
them to all be table driven.
-rw-r--r-- | internal/gitaly/service/ref/refs_test.go | 164 |
1 files changed, 114 insertions, 50 deletions
diff --git a/internal/gitaly/service/ref/refs_test.go b/internal/gitaly/service/ref/refs_test.go index 4305eb619..3af702173 100644 --- a/internal/gitaly/service/ref/refs_test.go +++ b/internal/gitaly/service/ref/refs_test.go @@ -3,6 +3,7 @@ package ref import ( + "context" "io" "strings" "testing" @@ -11,6 +12,7 @@ import ( "gitlab.com/gitlab-org/gitaly/v16/internal/git" "gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v16/internal/git/localrepo" + "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v16/internal/structerr" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb" @@ -19,75 +21,137 @@ import ( "google.golang.org/protobuf/types/known/timestamppb" ) -func TestSuccessfulFindDefaultBranchName(t *testing.T) { +func TestFindDefaultBranchName(t *testing.T) { t.Parallel() - ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupRefService(t, ctx) - rpcRequest := &gitalypb.FindDefaultBranchNameRequest{Repository: repo} - - // The testing repository has no main branch, so we create it and update - // HEAD to it - gittest.Exec(t, cfg, "-C", repoPath, "update-ref", "refs/heads/main", "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863") - gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/main") - r, err := client.FindDefaultBranchName(ctx, rpcRequest) - require.NoError(t, err) - - require.Equal(t, git.ReferenceName(r.GetName()), git.DefaultRef) -} - -func TestSuccessfulFindDefaultBranchNameLegacy(t *testing.T) { - t.Parallel() + type setupData struct { + request *gitalypb.FindDefaultBranchNameRequest + expectedErr error + } - ctx := testhelper.Context(t) - _, repo, _, client := setupRefService(t, ctx) + for _, tc := range []struct { + desc string + setup func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData + expectedName git.ReferenceName + }{ + { + desc: "successful", + setup: func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData { + repo, repoPath := gittest.CreateRepository(t, ctx, cfg) + + oid := gittest.WriteCommit(t, cfg, repoPath, gittest.WithReference(git.DefaultRef.String())) + gittest.WriteCommit(t, cfg, repoPath, gittest.WithReference(git.LegacyDefaultRef.String()), gittest.WithParents(oid)) + gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("apple"), gittest.WithParents(oid)) + gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("banana"), gittest.WithParents(oid)) + + return setupData{ + request: &gitalypb.FindDefaultBranchNameRequest{ + Repository: repo, + }, + } + }, + expectedName: git.DefaultRef, + }, + { + desc: "successful, single branch", + setup: func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData { + repo, repoPath := gittest.CreateRepository(t, ctx, cfg) - rpcRequest := &gitalypb.FindDefaultBranchNameRequest{Repository: repo} - r, err := client.FindDefaultBranchName(ctx, rpcRequest) - require.NoError(t, err) + gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("banana")) - require.Equal(t, git.ReferenceName(r.GetName()), git.LegacyDefaultRef) -} + return setupData{ + request: &gitalypb.FindDefaultBranchNameRequest{ + Repository: repo, + }, + } + }, + expectedName: "refs/heads/banana", + }, + { + desc: "successful, updated default", + setup: func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData { + repo, repoPath := gittest.CreateRepository(t, ctx, cfg) -func TestFindDefaultBranchName_validate(t *testing.T) { - t.Parallel() + oid := gittest.WriteCommit(t, cfg, repoPath, gittest.WithReference(git.LegacyDefaultRef.String())) + gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("apple"), gittest.WithParents(oid)) + gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("banana"), gittest.WithParents(oid)) - ctx := testhelper.Context(t) - cfg, repo, _, client := setupRefService(t, ctx) + gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/banana") - for _, tc := range []struct { - desc string - repo *gitalypb.Repository - expectedErr error - }{ + return setupData{ + request: &gitalypb.FindDefaultBranchNameRequest{ + Repository: repo, + }, + } + }, + expectedName: "refs/heads/banana", + }, + { + desc: "empty repository", + setup: func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData { + repo, _ := gittest.CreateRepository(t, ctx, cfg) + + return setupData{ + request: &gitalypb.FindDefaultBranchNameRequest{ + Repository: repo, + }, + } + }, + }, { desc: "repository not provided", - repo: nil, - expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect( - "empty Repository", - "repo scoped: empty Repository", - )), + setup: func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData { + return setupData{ + request: &gitalypb.FindDefaultBranchNameRequest{}, + expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect( + "empty Repository", + "repo scoped: empty Repository", + )), + } + }, }, { desc: "repository doesn't exist on disk", - repo: &gitalypb.Repository{StorageName: cfg.Storages[0].Name, RelativePath: "made/up/path"}, - expectedErr: status.Error(codes.NotFound, testhelper.GitalyOrPraefect( - `GetRepoPath: not a git repository: "`+cfg.Storages[0].Path+`/made/up/path"`, - `accessor call: route repository accessor: consistent storages: repository "default"/"made/up/path" not found`, - )), + setup: func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData { + return setupData{ + request: &gitalypb.FindDefaultBranchNameRequest{ + Repository: &gitalypb.Repository{StorageName: cfg.Storages[0].Name, RelativePath: "made/up/path"}, + }, + expectedErr: status.Error(codes.NotFound, testhelper.GitalyOrPraefect( + `GetRepoPath: not a git repository: "`+cfg.Storages[0].Path+`/made/up/path"`, + `accessor call: route repository accessor: consistent storages: repository "default"/"made/up/path" not found`, + )), + } + }, }, { desc: "unknown storage", - repo: &gitalypb.Repository{StorageName: "invalid", RelativePath: repo.GetRelativePath()}, - expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect( - `GetStorageByName: no such storage: "invalid"`, - "repo scoped: invalid Repository", - )), + setup: func(t *testing.T, ctx context.Context, cfg config.Cfg) setupData { + repo, _ := gittest.CreateRepository(t, ctx, cfg) + + return setupData{ + request: &gitalypb.FindDefaultBranchNameRequest{ + Repository: &gitalypb.Repository{StorageName: "invalid", RelativePath: repo.GetRelativePath()}, + }, + expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect( + `GetStorageByName: no such storage: "invalid"`, + "repo scoped: invalid Repository", + )), + } + }, }, } { + tc := tc t.Run(tc.desc, func(t *testing.T) { - _, err := client.FindDefaultBranchName(ctx, &gitalypb.FindDefaultBranchNameRequest{Repository: tc.repo}) - testhelper.RequireGrpcError(t, tc.expectedErr, err) + t.Parallel() + + ctx := testhelper.Context(t) + cfg, client := setupRefServiceWithoutRepo(t) + data := tc.setup(t, ctx, cfg) + + response, err := client.FindDefaultBranchName(ctx, data.request) + testhelper.RequireGrpcError(t, data.expectedErr, err) + require.Equal(t, tc.expectedName, git.ReferenceName(response.GetName())) }) } } |