-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_oneshot_test.go
More file actions
286 lines (250 loc) · 7.58 KB
/
Copy pathe2e_oneshot_test.go
File metadata and controls
286 lines (250 loc) · 7.58 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
// 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 (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
)
func TestE2EOneshotBeforeDependents(t *testing.T) {
dir := t.TempDir()
marker := filepath.Join(dir, "init-done")
td := startDaemon(t, fmt.Sprintf(`
processes:
- name: init
command: /bin/sh
args: ["-c", "echo done > %s"]
startup: oneshot
- name: app
command: sleep
args: ["300"]
after: [init]
on-failure: shutdown
`, marker))
defer td.kill()
// Verify the oneshot ran (marker file exists).
data, err := os.ReadFile(marker)
if err != nil {
t.Fatalf("oneshot marker not created: %v", err)
}
if !strings.Contains(string(data), "done") {
t.Errorf("unexpected marker content: %s", data)
}
// app should be running.
resp := td.sendCommand("status app")
if !strings.Contains(resp, "running") {
t.Fatalf("expected app running, got: %s", resp)
}
td.stop()
}
func TestE2EOneshotFailure(t *testing.T) {
// A failing oneshot should cause the daemon to exit (fatal).
dir := t.TempDir()
cfgPath := filepath.Join(dir, "gopherd.yml")
sockPath := filepath.Join(dir, "gopherd.sock")
config := fmt.Sprintf(`no-logo: true
control:
socket: %s
processes:
- name: bad-init
command: /bin/sh
args: ["-c", "exit 1"]
startup: oneshot
- name: app
command: sleep
args: ["300"]
after: [bad-init]
`, sockPath)
os.WriteFile(cfgPath, []byte(config), 0o644)
cmd := exec.Command(testBinary)
cmd.Env = append(os.Environ(), "GOPHERD_CONFIG="+cfgPath, "GOPHERD_SOCKET="+sockPath)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err := cmd.Start()
if err != nil {
t.Fatalf("start: %v", err)
}
defer func() {
cmd.Process.Signal(syscall.SIGKILL)
cmd.Wait()
}()
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
select {
case err := <-done:
if err == nil {
t.Fatal("expected non-zero exit for failed oneshot")
}
case <-time.After(10 * time.Second):
t.Fatal("daemon did not exit after failed oneshot")
}
}
// TestE2EOneshotExitCodeMap verifies that exit-code-map is applied to oneshots
// that run during the startup sequence, just like the reap loop applies it to
// long-running services. A oneshot exiting 17 with exit-code-map {17: 0} must
// be treated as success: the daemon survives startup and dependents proceed.
func TestE2EOneshotExitCodeMap(t *testing.T) {
// startDaemon waits for the control socket; if the remapped-to-0 oneshot
// were still treated as a failure it would fatal the daemon during startup
// and the socket would never come up, failing this call.
td := startDaemon(t, `
processes:
- name: migrate
command: /bin/sh
args: ["-c", "exit 17"]
startup: oneshot
on-failure: shutdown
exit-code-map:
17: 0
- name: app
command: sleep
args: ["300"]
after: [migrate]
on-failure: shutdown
`)
defer td.kill()
// The dependent only starts if the oneshot was treated as completed.
resp := td.sendCommand("status app")
if !strings.Contains(resp, "running") {
t.Fatalf("expected app running after remapped oneshot, got: %s", resp)
}
td.stop()
}
// TestE2EOneshotStartupTimeout verifies that a oneshot which hangs past its
// startup-timeout is killed and treated as a fatal startup failure. Without
// this, a wedged init step would block the daemon indefinitely before any
// dependent service could be started.
func TestE2EOneshotStartupTimeout(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "gopherd.yml")
sockPath := filepath.Join(dir, "gopherd.sock")
config := fmt.Sprintf(`no-logo: true
control:
socket: %s
processes:
- name: hung-init
command: sleep
args: ["300"]
startup: oneshot
startup-timeout: 500ms
- name: app
command: sleep
args: ["300"]
after: [hung-init]
`, sockPath)
os.WriteFile(cfgPath, []byte(config), 0o644)
cmd := exec.Command(testBinary)
cmd.Env = append(os.Environ(), "GOPHERD_CONFIG="+cfgPath, "GOPHERD_SOCKET="+sockPath)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
start := time.Now()
if err := cmd.Start(); err != nil {
t.Fatalf("start: %v", err)
}
defer func() {
cmd.Process.Signal(syscall.SIGKILL)
cmd.Wait()
}()
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
select {
case err := <-done:
if err == nil {
t.Fatal("expected non-zero exit for timed-out oneshot")
}
// Generous upper bound: the timeout is 500ms; allow startup overhead
// but ensure we did not wait anywhere near the sleep 300 horizon.
if elapsed := time.Since(start); elapsed > 5*time.Second {
t.Errorf("daemon took %s to exit after 500ms timeout — startup-timeout did not fire", elapsed)
}
case <-time.After(10 * time.Second):
t.Fatal("daemon did not exit after oneshot startup-timeout")
}
}
// Regression: oneshots that complete during startup must appear in
// `gopherd status`. Pre-fix, runLayerOneshots called svc.Start() directly,
// bypassing the metrics map, so a oneshot was invisible in stats until
// something else (a check-failure restart, a control restart) re-entered
// startService.
func TestE2EOneshotInStats(t *testing.T) {
td := startDaemon(t, `
processes:
- name: init-step
command: /bin/sh
args: ["-c", "true"]
startup: oneshot
- name: keeper
command: sleep
args: ["300"]
after: [init-step]
on-failure: shutdown
`)
defer td.kill()
time.Sleep(300 * time.Millisecond)
resp := td.sendCommand("status")
if !strings.Contains(resp, "init-step") {
t.Errorf("expected init-step in stats, got: %s", resp)
}
td.stop()
}
// TestE2EControlStartOneshot verifies a oneshot can be re-triggered via the
// control socket after startup and that the daemon stays alive afterward —
// the reap loop's oneshot branch must take ActionIgnore on a successful
// post-startup exit rather than dispatching the default OnSuccess=Shutdown.
func TestE2EControlStartOneshot(t *testing.T) {
dir := t.TempDir()
marker := filepath.Join(dir, "ran")
td := startDaemon(t, fmt.Sprintf(`
processes:
- name: task
command: /bin/sh
args: ["-c", "echo run >> %s"]
startup: oneshot
- name: keeper
command: sleep
args: ["300"]
after: [task]
on-failure: shutdown
`, marker))
defer td.kill()
// Oneshot ran once during startup.
data, err := os.ReadFile(marker)
if err != nil {
t.Fatalf("marker not created during startup: %v", err)
}
if got := strings.Count(string(data), "run"); got != 1 {
t.Fatalf("expected 1 startup run, got %d (%q)", got, data)
}
// Re-trigger the oneshot via the control socket. This exercises the
// post-startup oneshot path in the reap loop.
resp := td.sendCommand("start task")
if strings.Contains(resp, "error") {
t.Fatalf("start failed: %s", resp)
}
time.Sleep(500 * time.Millisecond)
if !td.daemonAlive() {
t.Fatalf("daemon exited unexpectedly after control-triggered oneshot")
}
data, err = os.ReadFile(marker)
if err != nil {
t.Fatalf("read marker after second run: %v", err)
}
if got := strings.Count(string(data), "run"); got != 2 {
t.Errorf("expected 2 runs after control start, got %d (%q)", got, data)
}
td.stop()
}