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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
package repository
import (
"fmt"
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/git"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config/auth"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/transaction"
"gitlab.com/gitlab-org/gitaly/v16/internal/grpc/metadata"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/v16/internal/praefect/praefectutil"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testserver"
"gitlab.com/gitlab-org/gitaly/v16/internal/transaction/txinfo"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"golang.org/x/sys/unix"
)
func TestCreateRepository_missingAuth(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
cfg := testcfg.Build(t, testcfg.WithBase(config.Cfg{Auth: auth.Config{Token: "some"}}))
_, serverSocketPath := runRepositoryService(t, cfg)
client := newRepositoryClient(t, config.Cfg{Auth: auth.Config{Token: ""}}, serverSocketPath)
_, err := client.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{
Repository: &gitalypb.Repository{
StorageName: cfg.Storages[0].Name,
RelativePath: gittest.NewRepositoryName(t),
},
})
testhelper.RequireGrpcError(t, structerr.NewUnauthenticated("authentication required"), err)
}
func TestCreateRepository_successful(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
cfg, client := setupRepositoryServiceWithoutRepo(t)
repo := &gitalypb.Repository{
StorageName: cfg.Storages[0].Name,
RelativePath: gittest.NewRepositoryName(t),
}
_, err := client.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{
Repository: repo,
})
require.NoError(t, err)
repoDir := filepath.Join(cfg.Storages[0].Path, gittest.GetReplicaPath(t, ctx, cfg, repo))
require.NoError(t, unix.Access(repoDir, unix.R_OK))
require.NoError(t, unix.Access(repoDir, unix.W_OK))
require.NoError(t, unix.Access(repoDir, unix.X_OK))
for _, dir := range []string{repoDir, filepath.Join(repoDir, "refs")} {
fi, err := os.Stat(dir)
require.NoError(t, err)
require.True(t, fi.IsDir(), "%q must be a directory", fi.Name())
require.NoError(t, unix.Access(dir, unix.R_OK))
require.NoError(t, unix.Access(dir, unix.W_OK))
require.NoError(t, unix.Access(dir, unix.X_OK))
}
symRef := testhelper.MustReadFile(t, path.Join(repoDir, "HEAD"))
require.Equal(t, symRef, []byte(fmt.Sprintf("ref: %s\n", git.DefaultRef)))
}
func TestCreateRepository_withDefaultBranch(t *testing.T) {
t.Parallel()
cfg, client := setupRepositoryServiceWithoutRepo(t)
ctx := testhelper.Context(t)
for _, tc := range []struct {
desc string
defaultBranch string
expected string
expectedErrString string
}{
{
desc: "valid default branch",
defaultBranch: "develop",
expected: "refs/heads/develop",
},
{
desc: "empty branch name",
defaultBranch: "",
expected: "refs/heads/main",
},
{
desc: "invalid branch name",
defaultBranch: "./.lock",
expected: "refs/heads/main",
expectedErrString: `creating repository: exit status 128, stderr: "fatal: invalid initial branch name: './.lock'\n"`,
},
} {
t.Run(tc.desc, func(t *testing.T) {
repo := &gitalypb.Repository{StorageName: cfg.Storages[0].Name, RelativePath: gittest.NewRepositoryName(t)}
req := &gitalypb.CreateRepositoryRequest{Repository: repo, DefaultBranch: []byte(tc.defaultBranch)}
_, err := client.CreateRepository(ctx, req)
if tc.expectedErrString != "" {
require.Contains(t, err.Error(), tc.expectedErrString)
} else {
require.NoError(t, err)
repoPath := filepath.Join(cfg.Storages[0].Path, gittest.GetReplicaPath(t, ctx, cfg, repo))
symRef := text.ChompBytes(gittest.Exec(
t,
cfg,
"-C", repoPath,
"symbolic-ref", "HEAD"))
require.Equal(t, tc.expected, symRef)
}
})
}
}
func TestCreateRepository_withObjectFormat(t *testing.T) {
t.Parallel()
cfg, client := setupRepositoryServiceWithoutRepo(t)
ctx := testhelper.Context(t)
gitCmdFactory := gittest.NewCommandFactory(t, cfg)
for _, tc := range []struct {
desc string
objectFormat gitalypb.ObjectFormat
expectedResponse *gitalypb.CreateRepositoryResponse
expectedObjectHash git.ObjectHash
expectedErr error
}{
{
desc: "unspecified object format",
objectFormat: gitalypb.ObjectFormat_OBJECT_FORMAT_UNSPECIFIED,
expectedResponse: &gitalypb.CreateRepositoryResponse{},
expectedObjectHash: git.ObjectHashSHA1,
},
{
desc: "SHA1",
objectFormat: gitalypb.ObjectFormat_OBJECT_FORMAT_SHA1,
expectedResponse: &gitalypb.CreateRepositoryResponse{},
expectedObjectHash: git.ObjectHashSHA1,
},
{
desc: "SHA256",
objectFormat: gitalypb.ObjectFormat_OBJECT_FORMAT_SHA256,
expectedResponse: &gitalypb.CreateRepositoryResponse{},
expectedObjectHash: git.ObjectHashSHA256,
},
{
desc: "invalid object format",
objectFormat: 3,
expectedErr: structerr.NewInvalidArgument("unknown object format: \"3\""),
},
} {
t.Run(tc.desc, func(t *testing.T) {
repoProto := &gitalypb.Repository{
StorageName: cfg.Storages[0].Name,
RelativePath: gittest.NewRepositoryName(t),
}
response, err := client.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{
Repository: repoProto,
ObjectFormat: tc.objectFormat,
})
testhelper.RequireGrpcError(t, tc.expectedErr, err)
testhelper.ProtoEqual(t, tc.expectedResponse, response)
if err != nil {
return
}
// If the repository was created we can check whether the object format of
// the created repository matches our expectations.
objectHash, err := git.DetectObjectHash(ctx, gitCmdFactory, gittest.RewrittenRepository(t, ctx, cfg, repoProto))
require.NoError(t, err)
require.Equal(t, tc.expectedObjectHash.Format, objectHash.Format)
})
}
}
func TestCreateRepository_invalidArguments(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
cfg, client := setupRepositoryServiceWithoutRepo(t)
preexistingRepo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
// This creates the first repository on the server. As this test can run with
// Praefect in front of it, we'll use the next replica path Praefect will assign in
// order to ensure this repository creation conflicts even with Praefect in front of
// it.
RelativePath: praefectutil.DeriveReplicaPath(1),
})
for _, tc := range []struct {
desc string
repo *gitalypb.Repository
expectedErr error
}{
{
desc: "missing repository",
repo: nil,
expectedErr: structerr.NewInvalidArgument(testhelper.GitalyOrPraefect(
"empty Repository",
"repo scoped: empty Repository",
)),
},
{
desc: "invalid storage",
repo: &gitalypb.Repository{
StorageName: "does not exist",
RelativePath: "foobar.git",
},
expectedErr: structerr.NewInvalidArgument(testhelper.GitalyOrPraefect(
`creating repository: locate repository: GetStorageByName: no such storage: "does not exist"`,
"repo scoped: invalid Repository",
)),
},
{
desc: "preexisting repository",
repo: preexistingRepo,
expectedErr: structerr.NewAlreadyExists(testhelper.GitalyOrPraefect(
"creating repository: repository exists already",
"route repository creation: reserve repository id: repository already exists",
)),
},
} {
t.Run(tc.desc, func(t *testing.T) {
_, err := client.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{Repository: tc.repo})
require.Error(t, err)
testhelper.RequireGrpcError(t, tc.expectedErr, err)
})
}
}
func TestCreateRepository_transactional(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
txManager := transaction.NewTrackingManager()
cfg, client := setupRepositoryServiceWithoutRepo(t, testserver.WithTransactionManager(txManager))
ctx, err := txinfo.InjectTransaction(ctx, 1, "node", true)
require.NoError(t, err)
ctx = metadata.IncomingToOutgoing(ctx)
repo := &gitalypb.Repository{
StorageName: cfg.Storages[0].Name,
RelativePath: "repo.git",
}
_, err = client.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{Repository: repo})
require.NoError(t, err)
require.DirExists(t, filepath.Join(cfg.Storages[0].Path, gittest.GetReplicaPath(t, ctx, cfg, repo)))
require.Equal(t, 2, len(txManager.Votes()), "expected transactional vote")
}
|