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
|
package repository
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v15/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v15/internal/helper"
"gitlab.com/gitlab-org/gitaly/v15/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
"google.golang.org/grpc/codes"
)
func TestSetFullPath(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
cfg, client := setupRepositoryServiceWithoutRepo(t)
t.Run("missing repository", func(t *testing.T) {
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: nil,
Path: "my/repo",
})
require.Nil(t, response)
require.Error(t, err)
require.Contains(t, err.Error(), "empty Repository")
})
t.Run("missing path", func(t *testing.T) {
repo, _ := gittest.CreateRepository(ctx, t, cfg)
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: repo,
Path: "",
})
require.Nil(t, response)
testhelper.RequireGrpcError(t, helper.ErrInvalidArgumentf("no path provided"), err)
})
t.Run("invalid storage", func(t *testing.T) {
repo, _ := gittest.CreateRepository(ctx, t, cfg)
repo.StorageName = ""
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: repo,
Path: "my/repo",
})
require.Nil(t, response)
// We can't assert a concrete error given that they're different when running with
// Praefect or without Praefect.
require.Error(t, err)
})
t.Run("nonexistent repo", func(t *testing.T) {
repo := &gitalypb.Repository{
RelativePath: "/path/to/repo.git",
StorageName: cfg.Storages[0].Name,
}
repoPath, err := config.NewLocator(cfg).GetPath(repo)
require.NoError(t, err)
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: repo,
Path: "my/repo",
})
require.Nil(t, response)
expectedErr := fmt.Sprintf("rpc error: code = NotFound desc = setting config: GetRepoPath: not a git repository: %q", repoPath)
if testhelper.IsPraefectEnabled() {
expectedErr = `rpc error: code = NotFound desc = mutator call: route repository mutator: get repository id: repository "default"/"/path/to/repo.git" not found`
}
require.EqualError(t, err, expectedErr)
})
t.Run("normal repo", func(t *testing.T) {
repo, repoPath := gittest.CreateRepository(ctx, t, cfg)
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: repo,
Path: "foo/bar",
})
require.NoError(t, err)
testhelper.ProtoEqual(t, &gitalypb.SetFullPathResponse{}, response)
fullPath := gittest.Exec(t, cfg, "-C", repoPath, "config", fullPathKey)
require.Equal(t, "foo/bar", text.ChompBytes(fullPath))
})
t.Run("missing config", func(t *testing.T) {
repo, repoPath := gittest.CreateRepository(ctx, t, cfg)
configPath := filepath.Join(repoPath, "config")
require.NoError(t, os.Remove(configPath))
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: repo,
Path: "foo/bar",
})
require.NoError(t, err)
testhelper.ProtoEqual(t, &gitalypb.SetFullPathResponse{}, response)
fullPath := gittest.Exec(t, cfg, "-C", repoPath, "config", fullPathKey)
require.Equal(t, "foo/bar", text.ChompBytes(fullPath))
})
t.Run("multiple times", func(t *testing.T) {
repo, repoPath := gittest.CreateRepository(ctx, t, cfg)
for i := 0; i < 5; i++ {
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: repo,
Path: fmt.Sprintf("foo/%d", i),
})
require.NoError(t, err)
testhelper.ProtoEqual(t, &gitalypb.SetFullPathResponse{}, response)
}
fullPath := gittest.Exec(t, cfg, "-C", repoPath, "config", "--get-all", fullPathKey)
require.Equal(t, "foo/4", text.ChompBytes(fullPath))
})
t.Run("multiple preexisting paths", func(t *testing.T) {
repo, repoPath := gittest.CreateRepository(ctx, t, cfg)
for i := 0; i < 5; i++ {
gittest.Exec(t, cfg, "-C", repoPath, "config", "--add", fullPathKey, fmt.Sprintf("foo/%d", i))
}
response, err := client.SetFullPath(ctx, &gitalypb.SetFullPathRequest{
Repository: repo,
Path: "replace",
})
require.NoError(t, err)
testhelper.ProtoEqual(t, &gitalypb.SetFullPathResponse{}, response)
fullPath := gittest.Exec(t, cfg, "-C", repoPath, "config", "--get-all", fullPathKey)
require.Equal(t, "replace", text.ChompBytes(fullPath))
})
}
func TestFullPath(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
cfg, client := setupRepositoryServiceWithoutRepo(t)
t.Run("missing repository", func(t *testing.T) {
response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{
Repository: nil,
})
require.Nil(t, response)
require.Error(t, err)
require.Contains(t, err.Error(), "empty Repository")
testhelper.RequireGrpcCode(t, err, codes.InvalidArgument)
})
t.Run("nonexistent repository", func(t *testing.T) {
repo := &gitalypb.Repository{
RelativePath: "/path/to/repo.git",
StorageName: cfg.Storages[0].Name,
}
repoPath, err := config.NewLocator(cfg).GetPath(repo)
require.NoError(t, err)
response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{
Repository: repo,
})
require.Nil(t, response)
expectedErr := fmt.Sprintf("rpc error: code = NotFound desc = fetch config: GetRepoPath: not a git repository: %q", repoPath)
if testhelper.IsPraefectEnabled() {
expectedErr = `rpc error: code = NotFound desc = accessor call: route repository accessor: consistent storages: repository "default"/"/path/to/repo.git" not found`
}
require.EqualError(t, err, expectedErr)
testhelper.RequireGrpcCode(t, err, codes.NotFound)
})
t.Run("missing config", func(t *testing.T) {
repo, repoPath := gittest.CreateRepository(ctx, t, cfg)
configPath := filepath.Join(repoPath, "config")
require.NoError(t, os.Remove(configPath))
response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{
Repository: repo,
})
require.Nil(t, response)
require.EqualError(t, err, "rpc error: code = Internal desc = fetch config: exit status 1")
testhelper.RequireGrpcCode(t, err, codes.Internal)
})
t.Run("existing config", func(t *testing.T) {
repo, repoPath := gittest.CreateRepository(ctx, t, cfg)
gittest.Exec(t, cfg, "-C", repoPath, "config", "--add", fullPathKey, "foo/bar")
response, err := client.FullPath(ctx, &gitalypb.FullPathRequest{
Repository: repo,
})
require.NoError(t, err)
testhelper.ProtoEqual(t, &gitalypb.FullPathResponse{Path: "foo/bar"}, response)
})
}
|