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
|
package main
import (
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/config"
)
// TestStolenPid tests for regressions in https://gitlab.com/gitlab-org/gitaly/issues/1661
func TestStolenPid(t *testing.T) {
defer func(oldValue string) {
os.Setenv(config.EnvPidFile, oldValue)
}(os.Getenv(config.EnvPidFile))
pidFile, err := ioutil.TempFile("", "pidfile")
require.NoError(t, err)
defer os.Remove(pidFile.Name())
os.Setenv(config.EnvPidFile, pidFile.Name())
cmd := exec.Command("tail", "-f")
require.NoError(t, cmd.Start())
defer cmd.Process.Kill()
_, err = pidFile.WriteString(strconv.Itoa(cmd.Process.Pid))
require.NoError(t, err)
require.NoError(t, pidFile.Close())
gitaly, err := findGitaly()
require.NoError(t, err)
require.Nil(t, gitaly)
}
func TestExistingGitaly(t *testing.T) {
defer func(oldValue string) {
os.Setenv(config.EnvPidFile, oldValue)
}(os.Getenv(config.EnvPidFile))
tmpDir, err := ioutil.TempDir("", "gitaly-pid")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
pidFile := path.Join(tmpDir, "gitaly.pid")
fakeGitaly := path.Join(tmpDir, "gitaly")
require.NoError(t, buildFakeGitaly(t, fakeGitaly), "Can't build a fake gitaly binary")
os.Setenv(config.EnvPidFile, pidFile)
cmd := exec.Command(fakeGitaly, "-f")
require.NoError(t, cmd.Start())
defer cmd.Process.Kill()
require.NoError(t, ioutil.WriteFile(pidFile, []byte(strconv.Itoa(cmd.Process.Pid)), 0644))
gitaly, err := findGitaly()
require.NoError(t, err)
require.NotNil(t, gitaly)
require.Equal(t, cmd.Process.Pid, gitaly.Pid)
gitaly.Kill()
}
func buildFakeGitaly(t *testing.T, path string) error {
tail := exec.Command("tail", "-f")
require.NoError(t, tail.Start())
defer tail.Process.Kill()
tailPath, err := procPath(tail.Process.Pid)
require.NoError(t, err)
tail.Process.Kill()
src, err := os.Open(tailPath)
require.NoError(t, err)
defer src.Close()
out, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0744)
require.NoError(t, err)
defer out.Close()
if _, err := io.Copy(out, src); err != nil {
return err
}
return out.Sync()
}
|