-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_control_test.go
More file actions
404 lines (347 loc) · 10.6 KB
/
Copy pathe2e_control_test.go
File metadata and controls
404 lines (347 loc) · 10.6 KB
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
// Copyright 2026 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestE2EControlStartStopRestart(t *testing.T) {
td := startDaemon(t, `
processes:
- name: keeper
command: sleep
args: ["300"]
on-failure: shutdown
- name: svc
command: sleep
args: ["300"]
on-failure: ignore
on-success: ignore
`)
defer td.kill()
// Verify running.
resp := td.sendCommand("status svc")
if !strings.Contains(resp, "running") {
t.Fatalf("expected running, got: %s", resp)
}
// Stop via control.
resp = td.sendCommand("stop svc")
if strings.Contains(resp, "error") {
t.Fatalf("stop failed: %s", resp)
}
time.Sleep(500 * time.Millisecond)
resp = td.sendCommand("status svc")
if !strings.Contains(resp, "stopped") {
t.Fatalf("expected stopped after stop, got: %s", resp)
}
// Start via control.
resp = td.sendCommand("start svc")
if strings.Contains(resp, "error") {
t.Fatalf("start failed: %s", resp)
}
time.Sleep(500 * time.Millisecond)
resp = td.sendCommand("status svc")
if !strings.Contains(resp, "running") {
t.Fatalf("expected running after start, got: %s", resp)
}
// Restart via control.
resp = td.sendCommand("restart svc")
if strings.Contains(resp, "error") {
t.Fatalf("restart failed: %s", resp)
}
time.Sleep(1 * time.Second)
resp = td.sendCommand("status svc")
if !strings.Contains(resp, "running") {
t.Fatalf("expected running after restart, got: %s", resp)
}
td.stop()
}
// Regression: a control-socket restart of a service whose on-success defaults
// to shutdown must restart the service in place. Pre-fix, the reap loop saw
// the signal-death exit, collapsed effectiveCode to 0 via the WasStopped
// rule, then dispatched the default OnSuccess=Shutdown — racing the pending
// restart and (often) winning, taking the daemon down. The reap-loop ECHILD
// path is also exercised here: with svc as the only managed process, Wait4
// returns ECHILD between the old pid being reaped and the new pid being
// registered, and the loop must treat that as transient while a restart is
// in flight.
func TestE2EControlRestartDefaultOnSuccess(t *testing.T) {
td := startDaemon(t, `
processes:
- name: svc
command: sleep
args: ["300"]
kill-delay: 1s
`)
defer td.kill()
resp := td.sendCommand("restart svc")
if strings.Contains(resp, "error") {
t.Fatalf("restart failed: %s", resp)
}
time.Sleep(1500 * time.Millisecond)
if !td.daemonAlive() {
t.Fatalf("daemon exited unexpectedly after restart")
}
resp = td.sendCommand("status svc")
if !strings.Contains(resp, "running") {
t.Fatalf("expected running after restart, got: %s", resp)
}
// A control-socket restart counts as one `restarts +1` and must NOT bump
// `exits` or `ok`/`fail` — operator-initiated restarts are accounted as a
// single restart event rather than exit-and-start.
resp = td.sendCommand("status")
if !strings.Contains(resp, "restarts=1") {
t.Errorf("expected restarts=1, got: %s", resp)
}
if !strings.Contains(resp, "exits=0") {
t.Errorf("expected exits=0 after a restart-only cycle, got: %s", resp)
}
td.stop()
}
// Regression: stopping the only running service must NOT take gopherd down.
// Pre-fix, the reap loop hit Wait4 ECHILD with no children and broke out,
// exiting the daemon. As a live supervisor it must idle instead, so the
// service can be started again.
func TestE2EControlStopLastServiceStaysAlive(t *testing.T) {
td := startDaemon(t, `
processes:
- name: svc
command: sleep
args: ["300"]
on-failure: ignore
on-success: ignore
`)
defer td.kill()
resp := td.sendCommand("stop svc")
if strings.Contains(resp, "error") {
t.Fatalf("stop failed: %s", resp)
}
time.Sleep(500 * time.Millisecond)
if !td.daemonAlive() {
t.Fatalf("daemon exited after stopping the last service; expected it to idle")
}
resp = td.sendCommand("status svc")
if !strings.Contains(resp, "stopped") {
t.Fatalf("expected stopped, got: %s", resp)
}
// The idling daemon must still accept a start and bring the service back.
resp = td.sendCommand("start svc")
if strings.Contains(resp, "error") {
t.Fatalf("start failed: %s", resp)
}
time.Sleep(500 * time.Millisecond)
resp = td.sendCommand("status svc")
if !strings.Contains(resp, "running") {
t.Fatalf("expected running after restart from idle, got: %s", resp)
}
td.stop()
}
func TestE2EControlStartDisabled(t *testing.T) {
td := startDaemon(t, `
processes:
- name: keeper
command: sleep
args: ["300"]
on-failure: shutdown
- name: lazy
command: sleep
args: ["300"]
startup: disabled
on-failure: ignore
on-success: ignore
`)
defer td.kill()
// lazy should be disabled initially.
resp := td.sendCommand("status lazy")
if !strings.Contains(resp, "disabled") {
t.Fatalf("expected lazy disabled, got: %s", resp)
}
// Start it via control socket.
resp = td.sendCommand("start lazy")
if strings.Contains(resp, "error") {
t.Fatalf("start lazy failed: %s", resp)
}
time.Sleep(500 * time.Millisecond)
resp = td.sendCommand("status lazy")
if !strings.Contains(resp, "running") {
t.Fatalf("expected lazy running after start, got: %s", resp)
}
td.stop()
}
func TestE2EControlUnknownService(t *testing.T) {
td := startDaemon(t, `
processes:
- name: app
command: sleep
args: ["300"]
on-failure: shutdown
`)
defer td.kill()
resp := td.sendCommand("status nonexistent")
if !strings.Contains(resp, "error") {
t.Fatalf("expected error for unknown service, got: %s", resp)
}
resp = td.sendCommand("start nonexistent")
if !strings.Contains(resp, "error") {
t.Fatalf("expected error for unknown service, got: %s", resp)
}
td.stop()
}
func TestE2EControlSignal(t *testing.T) {
td := startDaemon(t, `
processes:
- name: trapper
command: /bin/sh
args: ["-c", "trap 'echo got-usr1' USR1; while true; do sleep 1; done"]
on-failure: shutdown
on-success: shutdown
`)
defer td.kill()
time.Sleep(500 * time.Millisecond)
// Send USR1 signal via control.
resp := td.sendCommand("signal trapper SIGUSR1")
if strings.Contains(resp, "error") {
t.Fatalf("signal failed: %s", resp)
}
if !strings.Contains(resp, "sent") {
t.Errorf("expected 'sent' in response, got: %s", resp)
}
td.stop()
}
func TestE2EControlLogs(t *testing.T) {
td := startDaemon(t, `
log-capture: true
processes:
- name: talker
command: /bin/sh
args: ["-c", "echo hello-from-talker && sleep 300"]
on-failure: shutdown
`)
defer td.kill()
time.Sleep(1 * time.Second)
// Get recent logs.
resp := td.sendCommand("logs talker")
if !strings.Contains(resp, "hello-from-talker") {
t.Errorf("expected 'hello-from-talker' in logs, got: %s", resp)
}
td.stop()
}
func TestE2EControlLogsCaptureDisabled(t *testing.T) {
td := startDaemon(t, `
processes:
- name: talker
command: /bin/sh
args: ["-c", "echo hello-from-talker && sleep 300"]
on-failure: shutdown
`)
defer td.kill()
time.Sleep(500 * time.Millisecond)
// Capture defaults to off: logs must fail with a clear reason, not silence.
resp := td.sendCommand("logs talker")
if !strings.Contains(resp, "log capture disabled") {
t.Errorf("expected 'log capture disabled' error, got: %s", resp)
}
td.stop()
}
// TestE2ECLIExitCodeOnError pins that the CLI's *exit status* reflects failure,
// not just its stderr. Every script, healthcheck and CI step around
// `gopherd <cmd>` branches on the code, so printing an error and exiting 0
// stops all of them detecting failure.
func TestE2ECLIExitCodeOnError(t *testing.T) {
td := startDaemon(t, `
processes:
- name: app
command: sleep
args: ["300"]
on-success: ignore
on-failure: ignore
`)
defer td.kill()
td.WaitRunning("app", 10*time.Second)
run := func(args ...string) (int, string) {
t.Helper()
cmd := exec.Command(testBinary, args...)
cmd.Env = append(os.Environ(), "GOPHERD_SOCKET="+td.SocketPath())
out, err := cmd.CombinedOutput()
if err == nil {
return 0, string(out)
}
if ee, ok := errors.AsType[*exec.ExitError](err); ok {
return ee.ExitCode(), string(out)
}
t.Fatalf("run %v: %v", args, err)
return -1, ""
}
// A command the daemon answers with an error must exit non-zero.
for _, args := range [][]string{
{"status", "nosuchservice"},
{"start", "nosuchservice"},
{"nosuchservice", "restart"},
} {
code, out := run(args...)
if code == 0 {
t.Errorf("`gopherd %v` exited 0 despite failing; output: %s", args, out)
}
}
// The positive control: a command that succeeds still exits 0.
if code, out := run("status", "app"); code != 0 {
t.Errorf("`gopherd status app` exited %d, want 0; output: %s", code, out)
}
}
// TestE2ECLIHandlesLongLogLines pins that the client can read a log line longer
// than bufio's 64 KiB default, which stack traces and single-line JSON logs
// routinely exceed. The failure is not truncation but
// `bufio.Scanner: token too long`: the CLI gives up mid-stream, losing the rest
// of the output just as a service dumps a panic.
func TestE2ECLIHandlesLongLogLines(t *testing.T) {
const lineLen = 200 * 1024 // comfortably over bufio's 64 KiB default
td := startDaemon(t, fmt.Sprintf(`
processes:
- name: chatty
command: /bin/sh
args: ["-c", "awk 'BEGIN{s=sprintf(\"%%%dd\", 0); gsub(/ /, \"x\", s); print s}'; sleep 300"]
log-capture: true
on-success: ignore
on-failure: ignore
`, lineLen))
defer td.kill()
td.WaitRunning("chatty", 10*time.Second)
time.Sleep(500 * time.Millisecond) // let the line reach the ring
cmd := exec.Command(testBinary, "logs", "chatty")
cmd.Env = append(os.Environ(), "GOPHERD_SOCKET="+td.SocketPath())
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("gopherd logs failed: %v\n%s", err, out)
}
if strings.Contains(string(out), "token too long") {
t.Fatalf("the client could not read a %d-byte line: %s", lineLen, out)
}
longest := 0
for l := range strings.SplitSeq(string(out), "\n") {
if len(l) > longest {
longest = len(l)
}
}
if longest < lineLen {
t.Errorf("longest line received = %d bytes, want at least %d; the client's "+
"scanner buffer must be raised above bufio's 64 KiB default",
longest, lineLen)
}
td.stop()
}