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

supervisor_test.go « supervisor « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5e071d0c897cbaf73838c541f5bf5e9e5203d16 (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
194
195
196
197
package supervisor

import (
	"context"
	"io/ioutil"
	"net"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"syscall"
	"testing"
	"time"

	log "github.com/sirupsen/logrus"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)

var (
	testDir    string
	testExe    string
	socketPath string
)

func TestMain(m *testing.M) {
	os.Exit(testMain(m))
}

func testMain(m *testing.M) int {
	defer testhelper.MustHaveNoChildProcess()
	cleanup := testhelper.Configure()
	defer cleanup()

	var err error
	testDir, err = ioutil.TempDir("", "gitaly-supervisor-test")
	if err != nil {
		log.Error(err)
		return 1
	}
	defer func() {
		if err := os.RemoveAll(testDir); err != nil {
			log.Error(err)
		}
	}()

	scriptPath, err := filepath.Abs("test-scripts/pid-server.go")
	if err != nil {
		log.Error(err)
		return 1
	}

	testExe = filepath.Join(testDir, "pid-server")
	buildCmd := exec.Command("go", "build", "-o", testExe, scriptPath)
	buildCmd.Dir = filepath.Dir(scriptPath)
	buildCmd.Stderr = os.Stderr
	buildCmd.Stdout = os.Stdout
	if err := buildCmd.Run(); err != nil {
		log.Error(err)
		return 1
	}

	socketPath = filepath.Join(testDir, "socket")

	return m.Run()
}

func TestRespawnAfterCrashWithoutCircuitBreaker(t *testing.T) {
	config, err := NewConfigFromEnv()
	require.NoError(t, err)
	process, err := New(config, t.Name(), nil, []string{testExe}, testDir, 0, nil, nil)
	require.NoError(t, err)
	defer process.Stop()

	attempts := config.CrashThreshold
	require.True(t, attempts > 2, "config.CrashThreshold sanity check")

	pids, err := tryConnect(socketPath, attempts, 1*time.Second)
	require.NoError(t, err)

	require.Equal(t, attempts, len(pids), "number of pids should equal number of attempts")

	previous := 0
	for _, pid := range pids {
		require.True(t, pid > 0, "pid > 0")
		require.NotEqual(t, previous, pid, "pid sanity check")
		previous = pid
	}
}

func TestTooManyCrashes(t *testing.T) {
	config, err := NewConfigFromEnv()
	require.NoError(t, err)
	process, err := New(config, t.Name(), nil, []string{testExe}, testDir, 0, nil, nil)
	require.NoError(t, err)
	defer process.Stop()

	attempts := config.CrashThreshold + 1
	require.True(t, attempts > 2, "config.CrashThreshold sanity check")

	pids, err := tryConnect(socketPath, attempts, 1*time.Second)
	require.Error(t, err, "circuit breaker should cause a connection error / timeout")

	require.Equal(t, config.CrashThreshold, len(pids), "number of pids should equal circuit breaker threshold")
}

func TestSpawnFailure(t *testing.T) {
	config, err := NewConfigFromEnv()
	require.NoError(t, err)
	config.CrashWaitTime = 2 * time.Second

	notFoundExe := filepath.Join(testDir, "not-found")
	require.NoError(t, os.RemoveAll(notFoundExe))
	defer func() { require.NoError(t, os.Remove(notFoundExe)) }()

	process, err := New(config, t.Name(), nil, []string{notFoundExe}, testDir, 0, nil, nil)
	require.NoError(t, err)
	defer process.Stop()

	time.Sleep(1 * time.Second)

	pids, err := tryConnect(socketPath, 1, 1*time.Millisecond)
	require.Error(t, err, "connection must fail because executable cannot be spawned")
	require.Equal(t, 0, len(pids))

	// 'Fix' the spawning problem of our process
	require.NoError(t, os.Symlink(testExe, notFoundExe))

	// After CrashWaitTime, the circuit breaker should have closed
	pids, err = tryConnect(socketPath, 1, config.CrashWaitTime)

	require.NoError(t, err, "process should be accepting connections now")
	require.Equal(t, 1, len(pids), "we should have received the pid of the new process")
	require.True(t, pids[0] > 0, "pid sanity check")
}

func tryConnect(socketPath string, attempts int, timeout time.Duration) (pids []int, err error) {
	ctx, cancel := testhelper.Context(testhelper.ContextWithTimeout(timeout))
	defer cancel()

	for j := 0; j < attempts; j++ {
		var curPid int
		for {
			curPid, err = getPid(ctx, socketPath)
			if err == nil {
				break
			}

			select {
			case <-ctx.Done():
				return pids, ctx.Err()
			case <-time.After(5 * time.Millisecond):
				// sleep
			}
		}
		if err != nil {
			return pids, err
		}

		pids = append(pids, curPid)
		if curPid > 0 {
			syscall.Kill(curPid, syscall.SIGKILL)
		}
	}

	return pids, err
}

func getPid(ctx context.Context, socket string) (int, error) {
	var err error
	var conn net.Conn

	for {
		conn, err = net.DialTimeout("unix", socket, 1*time.Millisecond)
		if err == nil {
			break
		}

		select {
		case <-ctx.Done():
			return 0, ctx.Err()
		case <-time.After(5 * time.Millisecond):
			// sleep
		}
	}
	if err != nil {
		return 0, err
	}
	defer conn.Close()

	response, err := ioutil.ReadAll(conn)
	if err != nil {
		return 0, err
	}

	return strconv.Atoi(string(response))
}