Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pre_fetch_test.go « repository « service « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d461b7b2aedc04434088fe255fd3a03121509a15 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package repository

import (
	"fmt"
	"math/rand"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testing"
	"time"

	"google.golang.org/grpc/codes"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/internal/git/objectpool"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
)

// getForkDestination creates a repo struct and path, but does not actually create the directory
func getForkDestination(t *testing.T) (*gitalypb.Repository, string, func()) {
	folder := fmt.Sprintf("%s_%s", t.Name(), strconv.Itoa(rand.New(rand.NewSource(time.Now().Unix())).Int()))
	forkRepoPath := filepath.Join(testhelper.GitlabTestStoragePath(), folder)
	forkedRepo := &gitalypb.Repository{StorageName: "default", RelativePath: folder, GlRepository: "project-1"}

	return forkedRepo, forkRepoPath, func() { os.RemoveAll(forkRepoPath) }
}

// getGitObjectDirSize gets the number of 1k blocks of a git object directory
func getGitObjectDirSize(t *testing.T, repoPath string) int64 {
	output := testhelper.MustRunCommand(t, nil, "du", "-s", "-k", filepath.Join(repoPath, "objects"))
	if len(output) < 2 {
		t.Error("invalid output of du -s -k")
	}

	outputSplit := strings.SplitN(string(output), "\t", 2)
	blocks, err := strconv.ParseInt(outputSplit[0], 10, 64)
	require.NoError(t, err)

	return blocks
}

func TestPreFetch(t *testing.T) {
	t.Skip("PreFetch is unsafe https://gitlab.com/gitlab-org/gitaly/issues/1552")

	server, serverSocketPath := runRepoServer(t)
	defer server.Stop()

	client, conn := newRepositoryClient(t, serverSocketPath)
	defer conn.Close()

	ctx, cancel := testhelper.Context()
	defer cancel()

	testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t)
	defer cleanupFn()

	pool, poolRepo := objectpool.NewTestObjectPool(t)
	defer pool.Remove(ctx)

	require.NoError(t, pool.Create(ctx, testRepo))
	require.NoError(t, pool.Link(ctx, testRepo))

	testhelper.MustRunCommand(t, nil, "git", "-C", testRepoPath, "gc")

	forkedRepo, forkRepoPath, forkRepoCleanup := getForkDestination(t)
	defer forkRepoCleanup()

	req := &gitalypb.PreFetchRequest{
		TargetRepository: forkedRepo,
		SourceRepository: testRepo,
		ObjectPool: &gitalypb.ObjectPool{
			Repository: poolRepo,
		},
	}

	_, err := client.PreFetch(ctx, req)
	require.NoError(t, err)

	assert.True(t, getGitObjectDirSize(t, forkRepoPath) < 40)

	// feature is a branch known to exist in the source repository. By looking it up in the target
	// we establish that the target has branches, even though (as we saw above) it has no objects.
	testhelper.MustRunCommand(t, nil, "git", "-C", forkRepoPath, "show-ref", "feature")
}

func TestPreFetchValidationError(t *testing.T) {
	t.Skip("PreFetch is unsafe https://gitlab.com/gitlab-org/gitaly/issues/1552")

	server, serverSocketPath := runRepoServer(t)
	defer server.Stop()

	client, conn := newRepositoryClient(t, serverSocketPath)
	defer conn.Close()

	ctx, cancel := testhelper.Context()
	defer cancel()

	testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
	defer cleanupFn()

	pool, poolRepo := objectpool.NewTestObjectPool(t)
	defer pool.Remove(ctx)

	require.NoError(t, pool.Create(ctx, testRepo))
	require.NoError(t, pool.Link(ctx, testRepo))

	forkedRepo, _, forkRepoCleanup := getForkDestination(t)
	defer forkRepoCleanup()

	badPool, _, cleanupBadPool := testhelper.NewTestRepo(t)
	defer cleanupBadPool()

	badPool.RelativePath = "bad_path"

	testCases := []struct {
		description string
		sourceRepo  *gitalypb.Repository
		targetRepo  *gitalypb.Repository
		objectPool  *gitalypb.Repository
		code        codes.Code
	}{
		{
			description: "source repository nil",
			sourceRepo:  nil,
			targetRepo:  forkedRepo,
			objectPool:  poolRepo,
			code:        codes.InvalidArgument,
		},
		{
			description: "target repository nil",
			sourceRepo:  testRepo,
			targetRepo:  nil,
			objectPool:  poolRepo,
			code:        codes.InvalidArgument,
		},
		{
			description: "source/target repository have different storage",
			sourceRepo:  testRepo,
			targetRepo: &gitalypb.Repository{
				StorageName:  "specialstorage",
				RelativePath: forkedRepo.RelativePath,
				GlRepository: forkedRepo.GlRepository,
			},
			objectPool: poolRepo,
			code:       codes.InvalidArgument,
		},
		{
			description: "bad pool repository",
			sourceRepo:  testRepo,
			targetRepo:  forkedRepo,
			objectPool:  badPool,
			code:        codes.FailedPrecondition,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.description, func(t *testing.T) {
			_, err := client.PreFetch(ctx, &gitalypb.PreFetchRequest{
				TargetRepository: tc.targetRepo,
				SourceRepository: tc.sourceRepo,
				ObjectPool: &gitalypb.ObjectPool{
					Repository: tc.objectPool,
				},
			})
			testhelper.RequireGrpcError(t, err, tc.code)
		})
	}
}

func TestPreFetchDirectoryExists(t *testing.T) {
	t.Skip("PreFetch is unsafe https://gitlab.com/gitlab-org/gitaly/issues/1552")

	server, serverSocketPath := runRepoServer(t)
	defer server.Stop()

	client, conn := newRepositoryClient(t, serverSocketPath)
	defer conn.Close()

	testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
	defer cleanupFn()

	forkedRepo, _, forkRepoCleanup := testhelper.InitBareRepo(t)
	defer forkRepoCleanup()

	ctx, cancel := testhelper.Context()
	defer cancel()

	_, err := client.PreFetch(ctx, &gitalypb.PreFetchRequest{TargetRepository: forkedRepo, SourceRepository: testRepo})
	testhelper.RequireGrpcError(t, err, codes.FailedPrecondition)
}