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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
package config
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func configFileReader(content string) io.Reader {
return bytes.NewReader([]byte(content))
}
func TestLoadClearPrevConfig(t *testing.T) {
Config = config{SocketPath: "/tmp"}
err := Load(&bytes.Buffer{})
assert.NoError(t, err)
assert.Empty(t, Config.SocketPath)
}
func TestLoadBrokenConfig(t *testing.T) {
tmpFile := configFileReader(`path = "/tmp"\nname="foo"`)
err := Load(tmpFile)
assert.Error(t, err)
assert.Equal(t, config{}, Config)
}
func TestLoadEmptyConfig(t *testing.T) {
tmpFile := configFileReader(``)
err := Load(tmpFile)
assert.NoError(t, err)
assert.Equal(t, config{}, Config)
}
func TestLoadStorage(t *testing.T) {
tmpFile := configFileReader(`[[storage]]
name = "default"
path = "/tmp"`)
err := Load(tmpFile)
assert.NoError(t, err)
if assert.Equal(t, 1, len(Config.Storages), "Expected one (1) storage") {
assert.Equal(t, config{
Storages: []Storage{
{Name: "default", Path: "/tmp"},
},
}, Config)
}
}
func TestLoadMultiStorage(t *testing.T) {
tmpFile := configFileReader(`[[storage]]
name="default"
path="/tmp/repos1"
[[storage]]
name="other"
path="/tmp/repos2"`)
err := Load(tmpFile)
assert.NoError(t, err)
if assert.Equal(t, 2, len(Config.Storages), "Expected one (1) storage") {
assert.Equal(t, config{
Storages: []Storage{
{Name: "default", Path: "/tmp/repos1"},
{Name: "other", Path: "/tmp/repos2"},
},
}, Config)
}
}
func TestLoadPrometheus(t *testing.T) {
tmpFile := configFileReader(`prometheus_listen_addr=":9236"`)
err := Load(tmpFile)
assert.NoError(t, err)
assert.Equal(t, ":9236", Config.PrometheusListenAddr)
}
func TestLoadSocketPath(t *testing.T) {
tmpFile := configFileReader(`socket_path="/tmp/gitaly.sock"`)
err := Load(tmpFile)
assert.NoError(t, err)
assert.Equal(t, "/tmp/gitaly.sock", Config.SocketPath)
}
func TestLoadListenAddr(t *testing.T) {
tmpFile := configFileReader(`listen_addr=":8080"`)
err := Load(tmpFile)
assert.NoError(t, err)
assert.Equal(t, ":8080", Config.ListenAddr)
}
func tempEnv(key, value string) func() {
temp := os.Getenv(key)
os.Setenv(key, value)
return func() {
os.Setenv(key, temp)
}
}
func TestLoadOverrideEnvironment(t *testing.T) {
// Test that this works since we still want this to work
tempEnv1 := tempEnv("GITALY_SOCKET_PATH", "/tmp/gitaly2.sock")
defer tempEnv1()
tempEnv2 := tempEnv("GITALY_LISTEN_ADDR", ":8081")
defer tempEnv2()
tempEnv3 := tempEnv("GITALY_PROMETHEUS_LISTEN_ADDR", ":9237")
defer tempEnv3()
tmpFile := configFileReader(`socket_path = "/tmp/gitaly.sock"
listen_addr = ":8080"
prometheus_listen_addr = ":9236"`)
err := Load(tmpFile)
assert.NoError(t, err)
assert.Equal(t, ":9237", Config.PrometheusListenAddr)
assert.Equal(t, "/tmp/gitaly2.sock", Config.SocketPath)
assert.Equal(t, ":8081", Config.ListenAddr)
}
func TestLoadOnlyEnvironment(t *testing.T) {
// Test that this works since we still want this to work
os.Setenv("GITALY_SOCKET_PATH", "/tmp/gitaly2.sock")
os.Setenv("GITALY_LISTEN_ADDR", ":8081")
os.Setenv("GITALY_PROMETHEUS_LISTEN_ADDR", ":9237")
err := Load(&bytes.Buffer{})
assert.NoError(t, err)
assert.Equal(t, ":9237", Config.PrometheusListenAddr)
assert.Equal(t, "/tmp/gitaly2.sock", Config.SocketPath)
assert.Equal(t, ":8081", Config.ListenAddr)
}
func TestValidateStorages(t *testing.T) {
defer func(oldStorages []Storage) {
Config.Storages = oldStorages
}(Config.Storages)
repositories, err := filepath.Abs("testdata/repositories")
require.NoError(t, err)
repositories2, err := filepath.Abs("testdata/repositories2")
require.NoError(t, err)
invalidDir := path.Join(repositories, t.Name())
testCases := []struct {
desc string
storages []Storage
invalid bool
}{
{
desc: "just 1 storage",
storages: []Storage{
{Name: "default", Path: repositories},
},
},
{
desc: "multiple storages",
storages: []Storage{
{Name: "default", Path: repositories},
{Name: "other", Path: repositories2},
},
},
{
desc: "multiple storages pointing to same directory",
storages: []Storage{
{Name: "default", Path: repositories},
{Name: "other", Path: repositories},
{Name: "third", Path: repositories},
},
},
{
desc: "nested paths 1",
storages: []Storage{
{Name: "default", Path: "/home/git/repositories"},
{Name: "other", Path: "/home/git/repositories"},
{Name: "third", Path: "/home/git/repositories/third"},
},
invalid: true,
},
{
desc: "nested paths 2",
storages: []Storage{
{Name: "default", Path: "/home/git/repositories/default"},
{Name: "other", Path: "/home/git/repositories"},
{Name: "third", Path: "/home/git/repositories"},
},
invalid: true,
},
{
desc: "duplicate definition",
storages: []Storage{
{Name: "default", Path: repositories},
{Name: "default", Path: repositories},
},
invalid: true,
},
{
desc: "re-definition",
storages: []Storage{
{Name: "default", Path: repositories},
{Name: "default", Path: repositories2},
},
invalid: true,
},
{
desc: "empty name",
storages: []Storage{
{Name: "", Path: repositories},
},
invalid: true,
},
{
desc: "empty path",
storages: []Storage{
{Name: "default", Path: ""},
},
invalid: true,
},
{
desc: "non existing directory",
storages: []Storage{
{Name: "default", Path: repositories},
{Name: "nope", Path: invalidDir},
},
invalid: true,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
Config.Storages = tc.storages
err := validateStorages()
if tc.invalid {
assert.Error(t, err, "%+v", tc.storages)
return
}
assert.NoError(t, err, "%+v", tc.storages)
})
}
}
func TestStoragePath(t *testing.T) {
defer func(oldStorages []Storage) {
Config.Storages = oldStorages
}(Config.Storages)
Config.Storages = []Storage{
{Name: "default", Path: "/home/git/repositories1"},
{Name: "other", Path: "/home/git/repositories2"},
{Name: "third", Path: "/home/git/repositories3"},
}
testCases := []struct {
in, out string
ok bool
}{
{in: "default", out: "/home/git/repositories1", ok: true},
{in: "third", out: "/home/git/repositories3", ok: true},
{in: "", ok: false},
{in: "foobar", ok: false},
}
for _, tc := range testCases {
out, ok := StoragePath(tc.in)
if !assert.Equal(t, tc.ok, ok, "%+v", tc) {
continue
}
assert.Equal(t, tc.out, out, "%+v", tc)
}
}
func TestLoadGit(t *testing.T) {
tmpFile := configFileReader(`[git]
bin_path = "/my/git/path"`)
err := Load(tmpFile)
assert.NoError(t, err)
assert.Equal(t, "/my/git/path", Config.Git.BinPath)
}
func TestSetGitPath(t *testing.T) {
defer func(oldGitSettings Git) {
Config.Git = oldGitSettings
}(Config.Git)
resolvedGitPath, err := exec.LookPath("git")
if err != nil {
t.Fatal(err)
}
testCases := []struct {
desc string
gitBinPath string
expected string
}{
{
desc: "With a Git Path set through the settings",
gitBinPath: "/path/to/myGit",
expected: "/path/to/myGit",
},
{
desc: "When a git path hasn't been set",
gitBinPath: "",
expected: resolvedGitPath,
},
}
for _, tc := range testCases {
Config.Git.BinPath = tc.gitBinPath
SetGitPath()
assert.Equal(t, tc.expected, Config.Git.BinPath, tc.desc)
}
}
func TestValidateShellPath(t *testing.T) {
defer func(oldShellSettings GitlabShell) {
Config.GitlabShell = oldShellSettings
}(Config.GitlabShell)
tmpDir, err := ioutil.TempDir("", "gitaly-tests-")
require.NoError(t, err)
require.NoError(t, os.MkdirAll(path.Join(tmpDir, "bin"), 0755))
tmpFile := path.Join(tmpDir, "my-file")
defer os.RemoveAll(tmpDir)
fp, err := os.Create(tmpFile)
require.NoError(t, err)
require.NoError(t, fp.Close())
testCases := []struct {
desc string
path string
shouldErr bool
}{
{
desc: "When no Shell Path set",
path: "",
shouldErr: true,
},
{
desc: "When Shell Path set to non-existing path",
path: "/non/existing/path",
shouldErr: true,
},
{
desc: "When Shell Path set to non-dir path",
path: tmpFile,
shouldErr: true,
},
{
desc: "When Shell Path set to a valid directory",
path: tmpDir,
shouldErr: false,
},
}
for _, tc := range testCases {
t.Log(tc.desc)
Config.GitlabShell.Dir = tc.path
err = validateShell()
if tc.shouldErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
}
}
func TestConfigureRuby(t *testing.T) {
defer func(oldRuby Ruby) {
Config.Ruby = oldRuby
}(Config.Ruby)
tmpDir, err := ioutil.TempDir("", "gitaly-test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
tmpFile := path.Join(tmpDir, "file")
require.NoError(t, ioutil.WriteFile(tmpFile, nil, 0644))
testCases := []struct {
dir string
ok bool
desc string
}{
{dir: "", desc: "empty"},
{dir: "/does/not/exist", desc: "does not exist"},
{dir: tmpFile, desc: "exists but is not a directory"},
{dir: tmpDir, ok: true, desc: "ok"},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
Config.Ruby = Ruby{Dir: tc.dir}
err := ConfigureRuby()
if tc.ok {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
func TestConfigureRubyNumWorkers(t *testing.T) {
defer func(oldRuby Ruby) {
Config.Ruby = oldRuby
}(Config.Ruby)
testCases := []struct {
in, out int
}{
{in: -1, out: 2},
{in: 0, out: 2},
{in: 1, out: 2},
{in: 2, out: 2},
{in: 3, out: 3},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%+v", tc), func(t *testing.T) {
Config.Ruby = Ruby{Dir: "/", NumWorkers: tc.in}
require.NoError(t, ConfigureRuby())
require.Equal(t, tc.out, Config.Ruby.NumWorkers)
})
}
}
func TestValidateListeners(t *testing.T) {
defer func(cfg config) {
Config = cfg
}(Config)
testCases := []struct {
desc string
config
ok bool
}{
{desc: "empty"},
{desc: "socket only", config: config{SocketPath: "/foo/bar"}, ok: true},
{desc: "tcp only", config: config{ListenAddr: "a.b.c.d:1234"}, ok: true},
{desc: "both socket and tcp", config: config{SocketPath: "/foo/bar", ListenAddr: "a.b.c.d:1234"}, ok: true},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
Config = tc.config
err := validateListeners()
if tc.ok {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
|