Repository navigation
Expand file tree
/
Copy pathboot_test.go
More file actions
272 lines (247 loc) · 7.66 KB
/
Copy pathboot_test.go
File metadata and controls
272 lines (247 loc) · 7.66 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
package main
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
type bootLoop struct {
dir string
bin string
prev string
}
func newBootLoop(t *testing.T) *bootLoop {
t.Helper()
dir := t.TempDir()
for _, sub := range []string{"bin", "tmp", "stubs"} {
if err := os.Mkdir(filepath.Join(dir, sub), 0o755); err != nil {
t.Fatal(err)
}
}
return &bootLoop{
dir: dir,
bin: filepath.Join(dir, "bin", "overdub"),
prev: filepath.Join(dir, "bin", "overdub.prev"),
}
}
func (b *bootLoop) trial() string { return filepath.Join(b.dir, "bin", ".overdub-trial") }
func (b *bootLoop) failed() string { return filepath.Join(b.dir, "bin", ".overdub-failed") }
func (b *bootLoop) stub(t *testing.T, label string, limit int, extra string) string {
t.Helper()
path := filepath.Join(b.dir, "stubs", label)
script := "#!/bin/sh\n" +
"echo \"" + label + " $OVERDUB_ROLLBACK\" >> " + b.dir + "/runs\n" +
extra + "\n" +
"if [ \"$(wc -l < " + b.dir + "/runs)\" -ge " + strconv.Itoa(limit) + " ]; then rm -f " + b.bin + "; fi\n" +
"exit 1\n"
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
return path
}
func (b *bootLoop) place(t *testing.T, stub, at string) {
t.Helper()
data, err := os.ReadFile(stub)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(at, data, 0o755); err != nil {
t.Fatal(err)
}
}
func (b *bootLoop) write(t *testing.T, path, text string) {
t.Helper()
if err := os.WriteFile(path, []byte(text), 0o644); err != nil {
t.Fatal(err)
}
}
func (b *bootLoop) run(t *testing.T) (runs []string, log string) {
t.Helper()
raw, err := os.ReadFile("deploy/overdub.sh")
if err != nil {
t.Fatal(err)
}
script := string(raw)
for _, r := range []struct{ old, new string }{
{"/data/local/bin", b.dir + "/bin"},
{"/data/local/tmp", b.dir + "/tmp"},
{"sleep 5", ":"},
{"\nNAME=\n", "\nNAME=kitchen\n"},
} {
if !strings.Contains(script, r.old) {
t.Fatalf("deploy/overdub.sh no longer says %q, which this test rewrites", r.old)
}
script = strings.ReplaceAll(script, r.old, r.new)
}
path := filepath.Join(b.dir, "overdub.sh")
b.write(t, path, script)
if out, err := exec.Command("sh", path).CombinedOutput(); err != nil {
t.Fatalf("the boot script failed: %v\n%s", err, out)
}
data, _ := os.ReadFile(filepath.Join(b.dir, "runs"))
logged, _ := os.ReadFile(filepath.Join(b.dir, "tmp", "overdub.log"))
return strings.Fields(strings.ReplaceAll(string(data), " 1\n", "\n")), string(logged)
}
func checkRuns(t *testing.T, runs []string, want ...string) {
t.Helper()
if strings.Join(runs, " ") != strings.Join(want, " ") {
t.Errorf("the loop ran %v, want %v", runs, want)
}
}
func TestAnUpdateThatKeepsExitingRunsThreeTimesThenTheLoopGoesBack(t *testing.T) {
b := newBootLoop(t)
b.place(t, b.stub(t, "new", 5, ""), b.bin)
b.place(t, b.stub(t, "old", 5, ""), b.prev)
b.write(t, b.trial(), "v2\n")
runs, log := b.run(t)
checkRuns(t, runs, "new", "new", "new", "old", "old")
for _, path := range []string{b.trial(), b.prev} {
if _, err := os.Stat(path); err == nil {
t.Errorf("%s outlived the rollback", filepath.Base(path))
}
}
if !strings.Contains(log, "the update to v2 exited 3 times; back to the binary it replaced") {
t.Errorf("the log does not say the loop went back:\n%s", log)
}
if data, _ := os.ReadFile(b.failed()); string(data) != "v2\n" {
t.Errorf("the failed-update record holds %q, want the version that failed", data)
}
}
func TestEveryStartSeesTheRollbackFlag(t *testing.T) {
b := newBootLoop(t)
b.place(t, b.stub(t, "new", 2, ""), b.bin)
raw, _ := os.ReadFile("deploy/overdub.sh")
if !strings.Contains(string(raw), "OVERDUB_ROLLBACK=1 \"$BIN\"") {
t.Fatal("the loop does not start the daemon with OVERDUB_ROLLBACK=1")
}
b.run(t)
data, _ := os.ReadFile(filepath.Join(b.dir, "runs"))
if got := string(data); got != "new 1\nnew 1\n" {
t.Errorf("the daemon saw %q, want OVERDUB_ROLLBACK=1 on every start", got)
}
}
func TestANewMarkerStartsANewCount(t *testing.T) {
b := newBootLoop(t)
v3 := b.stub(t, "v3", 6, "")
install := "if [ ! -f " + b.dir + "/v2-ran ]; then touch " + b.dir + "/v2-ran; rm -f " + b.trial() +
"; ln " + b.bin + " " + b.prev + "; echo v3 > " + b.trial() + "; cp " + v3 + " " + b.bin + ".new; mv " +
b.bin + ".new " + b.bin + "; fi"
b.place(t, b.stub(t, "v2", 6, install), b.bin)
b.write(t, b.trial(), "v2\n")
runs, _ := b.run(t)
checkRuns(t, runs, "v2", "v3", "v3", "v3", "v2", "v2")
}
func TestAMissingPreviousBinaryIsLoggedAndTheUpdateKept(t *testing.T) {
b := newBootLoop(t)
b.place(t, b.stub(t, "new", 5, ""), b.bin)
b.write(t, b.trial(), "v2\n")
runs, log := b.run(t)
checkRuns(t, runs, "new", "new", "new", "new", "new")
if !strings.Contains(log, "the update to v2 exited 3 times, and") || !strings.Contains(log, "is missing; keeping it") {
t.Errorf("the log does not say the previous binary is missing:\n%s", log)
}
if _, err := os.Stat(b.failed()); err == nil {
t.Error("the failed-update record is there after the loop kept the update")
}
}
func TestWithNoTrialTheLoopNeverGoesBack(t *testing.T) {
b := newBootLoop(t)
b.place(t, b.stub(t, "new", 5, ""), b.bin)
b.place(t, b.stub(t, "old", 5, ""), b.prev)
runs, _ := b.run(t)
checkRuns(t, runs, "new", "new", "new", "new", "new")
}
func newTrial(t *testing.T, marker string) trial {
t.Helper()
dir := t.TempDir()
tr := trial{
marker: filepath.Join(dir, "trial"),
prev: filepath.Join(dir, "overdub.prev"),
failed: filepath.Join(dir, "failed"),
}
for path, text := range map[string]string{
tr.marker: marker, tr.prev: "old binary", tr.failed: "v2\n",
} {
if err := os.WriteFile(path, []byte(text), 0o600); err != nil {
t.Fatal(err)
}
}
return tr
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func served() <-chan struct{} {
c := make(chan struct{})
close(c)
return c
}
func TestASettledTrialRemovesTheMarkerAndThePreviousFiles(t *testing.T) {
tr := newTrial(t, "v2\n")
tr.settle("v2", served(), time.Millisecond)
for _, path := range []string{tr.marker, tr.prev, tr.failed} {
if exists(path) {
t.Errorf("%s outlived a trial that passed", filepath.Base(path))
}
}
}
func TestATrialWaitsForHomeAssistant(t *testing.T) {
tr := newTrial(t, "v2\n")
ready := make(chan struct{})
done := make(chan struct{})
go func() {
tr.settle("v2", ready, time.Millisecond)
close(done)
}()
time.Sleep(50 * time.Millisecond)
if !exists(tr.marker) || !exists(tr.prev) {
t.Fatal("the trial passed before Home Assistant was served")
}
close(ready)
<-done
if exists(tr.marker) {
t.Error("the trial did not pass once Home Assistant was served")
}
}
func TestATrialLeavesANewerMarkerAlone(t *testing.T) {
tr := newTrial(t, "v2\n")
done := make(chan struct{})
go func() {
tr.settle("v2", served(), 200*time.Millisecond)
close(done)
}()
time.Sleep(50 * time.Millisecond)
os.WriteFile(tr.marker, []byte("v3\n"), 0o600)
<-done
if data, _ := os.ReadFile(tr.marker); string(data) != "v3\n" {
t.Errorf("the marker holds %q; the newer update's trial was ended", data)
}
if !exists(tr.prev) {
t.Error("the newer update's previous binary was removed")
}
}
func TestABinaryUnderAnotherVersionsTrialForgetsIt(t *testing.T) {
tr := newTrial(t, "v3\n")
done := make(chan struct{})
go func() {
tr.settle("v2", make(chan struct{}), time.Hour)
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("a binary under another version's trial waited for that trial")
}
for _, path := range []string{tr.marker, tr.prev} {
if exists(path) {
t.Errorf("%s outlived an install that stopped partway", filepath.Base(path))
}
}
if !exists(tr.failed) {
t.Error("forgetting the stale trial removed the failed-update record")
}
}