1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package cleanup_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"google.golang.org/grpc/codes"
"gitlab.com/gitlab-org/gitaly/internal/git/log"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
)
func TestApplyBfgObjectMapSuccess(t *testing.T) {
server, serverSocketPath := runCleanupServiceServer(t)
defer server.Stop()
client, conn := newCleanupServiceClient(t, serverSocketPath)
defer conn.Close()
testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
ctx, cancel := testhelper.Context()
defer cancel()
headCommit, err := log.GetCommit(ctx, testRepo, "HEAD")
require.NoError(t, err)
// Create some refs pointing to HEAD
for _, ref := range []string{
"refs/environments/1", "refs/keep-around/1", "refs/merge-requests/1",
"refs/heads/_keep", "refs/tags/_keep", "refs/notes/_keep",
} {
testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "update-ref", ref, headCommit.Id)
}
objectMapData := fmt.Sprintf("%s %s\n", headCommit.Id, strings.Repeat("0", 40))
require.NoError(t, doRequest(ctx, t, testRepo, client, objectMapData))
// Ensure that the internal refs are gone, but the others still exist
refs := testhelper.GetRepositoryRefs(t, testRepoPath)
assert.NotContains(t, refs, "refs/environments/1")
assert.NotContains(t, refs, "refs/keep-around/1")
assert.NotContains(t, refs, "refs/merge-requests/1")
assert.Contains(t, refs, "refs/heads/_keep")
assert.Contains(t, refs, "refs/tags/_keep")
assert.Contains(t, refs, "refs/notes/_keep")
}
func TestApplyBfgObjectMapFailsOnInvalidInput(t *testing.T) {
server, serverSocketPath := runCleanupServiceServer(t)
defer server.Stop()
client, conn := newCleanupServiceClient(t, serverSocketPath)
defer conn.Close()
testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
ctx, cancel := testhelper.Context()
defer cancel()
err := doRequest(ctx, t, testRepo, client, "invalid-data here as you can see")
testhelper.RequireGrpcError(t, err, codes.InvalidArgument)
}
func doRequest(ctx context.Context, t *testing.T, repo *gitalypb.Repository, client gitalypb.CleanupServiceClient, objectMap string) error {
// Split the data across multiple requests
parts := strings.SplitN(objectMap, " ", 2)
req1 := &gitalypb.ApplyBfgObjectMapRequest{
Repository: repo,
ObjectMap: []byte(parts[0] + " "),
}
req2 := &gitalypb.ApplyBfgObjectMapRequest{ObjectMap: []byte(parts[1])}
stream, err := client.ApplyBfgObjectMap(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(req1))
require.NoError(t, stream.Send(req2))
_, err = stream.CloseAndRecv()
return err
}
|