-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_shutdown_test.go
More file actions
225 lines (208 loc) · 5.98 KB
/
Copy pathe2e_shutdown_test.go
File metadata and controls
225 lines (208 loc) · 5.98 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
// 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"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
"time"
)
func TestE2EExitCodePropagation(t *testing.T) {
td := startDaemon(t, `
processes:
- name: failer
command: /bin/sh
args: ["-c", "sleep 1 && exit 42"]
on-failure: shutdown
`)
defer td.kill()
code := td.wait(10 * time.Second)
if code != 42 {
t.Errorf("expected exit code 42, got %d", code)
}
}
func TestE2EGracefulShutdownMultipleServices(t *testing.T) {
td := startDaemon(t, `
processes:
- name: svc1
command: sleep
args: ["300"]
on-failure: shutdown
- name: svc2
command: sleep
args: ["300"]
on-failure: shutdown
- name: svc3
command: sleep
args: ["300"]
on-failure: shutdown
`)
defer td.kill()
// All should be running.
for _, name := range []string{"svc1", "svc2", "svc3"} {
resp := td.sendCommand("status " + name)
if !strings.Contains(resp, "running") {
t.Errorf("expected %s running, got: %s", name, resp)
}
}
// Graceful shutdown should stop all and exit cleanly.
code := td.stop()
if code != 0 {
t.Errorf("expected exit code 0, got %d", code)
}
}
// TestE2EShutdownOrderReverseDep asserts the default `reverse-dep` shutdown
// order: dependents stop before their dependencies. With C `after: [B]` and
// B `after: [A]`, the stop order must be C, B, A.
func TestE2EShutdownOrderReverseDep(t *testing.T) {
dir := t.TempDir()
order := filepath.Join(dir, "shutdown-order")
td := startDaemon(t, fmt.Sprintf(`
shutdown-order: reverse-dep
processes:
- name: a
command: /bin/sh
args: ["-c", "trap 'echo a >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
on-failure: shutdown
on-success: ignore
kill-delay: 5s
- name: b
command: /bin/sh
args: ["-c", "trap 'echo b >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
after: [a]
on-failure: shutdown
on-success: ignore
kill-delay: 5s
- name: c
command: /bin/sh
args: ["-c", "trap 'echo c >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
after: [b]
on-failure: shutdown
on-success: ignore
kill-delay: 5s
`, order))
defer td.kill()
time.Sleep(500 * time.Millisecond)
if code := td.stop(); code != 0 {
t.Errorf("exit code: %d", code)
}
want := []string{"c", "b", "a"}
got := readShutdownOrder(t, order)
if !reflect.DeepEqual(got, want) {
t.Errorf("shutdown order = %v, want %v", got, want)
}
}
// TestE2EShutdownOrderDep asserts `shutdown-order: dep` stops dependencies
// before dependents: A, B, C.
func TestE2EShutdownOrderDep(t *testing.T) {
dir := t.TempDir()
order := filepath.Join(dir, "shutdown-order")
td := startDaemon(t, fmt.Sprintf(`
shutdown-order: dep
processes:
- name: a
command: /bin/sh
args: ["-c", "trap 'echo a >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
on-failure: shutdown
on-success: ignore
kill-delay: 5s
- name: b
command: /bin/sh
args: ["-c", "trap 'echo b >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
after: [a]
on-failure: shutdown
on-success: ignore
kill-delay: 5s
- name: c
command: /bin/sh
args: ["-c", "trap 'echo c >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
after: [b]
on-failure: shutdown
on-success: ignore
kill-delay: 5s
`, order))
defer td.kill()
time.Sleep(500 * time.Millisecond)
if code := td.stop(); code != 0 {
t.Errorf("exit code: %d", code)
}
want := []string{"a", "b", "c"}
got := readShutdownOrder(t, order)
if !reflect.DeepEqual(got, want) {
t.Errorf("shutdown order = %v, want %v", got, want)
}
}
// TestE2EShutdownOrderSimultaneous asserts that `simultaneous` mode signals
// all services together. We can't pin a strict order — only that every
// service got the signal and recorded its exit.
func TestE2EShutdownOrderSimultaneous(t *testing.T) {
dir := t.TempDir()
order := filepath.Join(dir, "shutdown-order")
td := startDaemon(t, fmt.Sprintf(`
shutdown-order: simultaneous
processes:
- name: a
command: /bin/sh
args: ["-c", "trap 'echo a >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
on-failure: shutdown
on-success: ignore
- name: b
command: /bin/sh
args: ["-c", "trap 'echo b >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
after: [a]
on-failure: shutdown
on-success: ignore
- name: c
command: /bin/sh
args: ["-c", "trap 'echo c >> %[1]s; exit 0' TERM; while true; do sleep 0.1; done"]
after: [b]
on-failure: shutdown
on-success: ignore
`, order))
defer td.kill()
time.Sleep(500 * time.Millisecond)
if code := td.stop(); code != 0 {
t.Errorf("exit code: %d", code)
}
got := readShutdownOrder(t, order)
if len(got) != 3 {
t.Fatalf("expected 3 services to record shutdown, got %d (%v)", len(got), got)
}
for _, name := range []string{"a", "b", "c"} {
if !slices.Contains(got, name) {
t.Errorf("missing %s in shutdown record %v", name, got)
}
}
}
// readShutdownOrder reads the trap-recorded shutdown-order file and returns
// the names in the order they were written. Whitespace and blank lines are
// ignored. Used by the shutdown-order e2e tests.
func readShutdownOrder(t *testing.T, path string) []string {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
var out []string
for line := range strings.SplitSeq(string(data), "\n") {
if s := strings.TrimSpace(line); s != "" {
out = append(out, s)
}
}
return out
}