From b999292653176613f76afbe66e829a2f3341b5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Thu, 17 Dec 2020 16:44:32 +0100 Subject: UserCreateTag tests: use new update-ref mock to test update-ref error This fixes a TODO item in be093ba33 (User{Branch,Tag,Submodule}: ferry update-ref errors upwards, 2020-12-14) for UserDeleteTag, we now know what response we return for update-ref failures. I'm also adding tests for the new UserCreateTag. This is a Go-only test, since in the Ruby codepath we're using libgit2. I don't think we're going to realistically have both Go & Ruby tests for features using this deep-gutsy fakery, but at least here we can see what we'd do in this scenario. --- internal/gitaly/service/operations/tags_test.go | 78 +++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/internal/gitaly/service/operations/tags_test.go b/internal/gitaly/service/operations/tags_test.go index 9202af691..fe9da56bf 100644 --- a/internal/gitaly/service/operations/tags_test.go +++ b/internal/gitaly/service/operations/tags_test.go @@ -9,7 +9,9 @@ import ( "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/git/log" + "gitlab.com/gitlab-org/gitaly/internal/git/updateref" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" @@ -1253,6 +1255,82 @@ func testFailedUserCreateTagRequestDueToValidation(t *testing.T, ctx context.Con } } +//nolint: errcheck +func TestFailedUserDeleteTagRequestDueToUpdateRef(t *testing.T) { + ctx, cancel := testhelper.Context() + defer cancel() + + ctx = featureflag.OutgoingCtxWithFeatureFlagValue(ctx, featureflag.GoUserDeleteTag, "true") + + serverSocketPath, stop := runOperationServiceServer(t) + defer stop() + + client, conn := newOperationClient(t, serverSocketPath) + defer conn.Close() + + testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) + defer cleanupFn() + + tagName := "my-new-tag" + targetRevision := "c7fbe50c7c7419d9701eebe64b1fdacc3df5b9dd" + testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "tag", tagName, targetRevision) + + tagID := testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "rev-parse", tagName) + require.Equal(t, targetRevision, text.ChompBytes(tagID)) + + updateref.MungeMapForTestingAdd(targetRevision, "612036fac47c5d31c212b17268e2f3ba807bce1e") + request := &gitalypb.UserDeleteTagRequest{ + Repository: testRepo, + TagName: []byte(tagName), + User: testhelper.TestUser, + } + response, err := client.UserDeleteTag(ctx, request) + require.Equal(t, status.Errorf(codes.FailedPrecondition, "Could not update refs/tags/%s. Please refresh and try again.", tagName), err) + require.Nil(t, response) + + tags := testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "for-each-ref", "--", "refs/tags/"+tagName) + require.Contains(t, string(tags), targetRevision) +} + +//nolint: errcheck +func TestFailedUserCreateTagRequestDueToUpdateRef(t *testing.T) { + ctx, cancel := testhelper.Context() + defer cancel() + + ctx = featureflag.OutgoingCtxWithFeatureFlagValue(ctx, featureflag.GoUserCreateTag, "true") + + serverSocketPath, stop := runOperationServiceServer(t) + defer stop() + + client, conn := newOperationClient(t, serverSocketPath) + defer conn.Close() + + testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t) + defer cleanupFn() + + targetRevision := "c7fbe50c7c7419d9701eebe64b1fdacc3df5b9dd" + updateref.MungeMapForTestingAdd(git.NullSHA, "612036fac47c5d31c212b17268e2f3ba807bce1e") + + tagName := "my-new-tag" + request := &gitalypb.UserCreateTagRequest{ + Repository: testRepo, + TagName: []byte(tagName), + TargetRevision: []byte(targetRevision), + User: testhelper.TestUser, + } + response, err := client.UserCreateTag(ctx, request) + require.NoError(t, err) + require.Empty(t, response.PreReceiveError) + responseOk := &gitalypb.UserCreateTagResponse{ + Tag: nil, + Exists: true, + } + require.Equal(t, responseOk, response) + + tags := testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "for-each-ref", "--", "refs/tags/"+tagName) + require.Equal(t, string(tags), "") +} + func TestTagHookOutput(t *testing.T) { testhelper.NewFeatureSets([]featureflag.FeatureFlag{ featureflag.GoUserDeleteTag, -- cgit v1.2.3