-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish_test.go
More file actions
351 lines (304 loc) · 12.6 KB
/
Copy pathpublish_test.go
File metadata and controls
351 lines (304 loc) · 12.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
//go:build docker
package container
import (
"errors"
"os/exec"
"strings"
"testing"
"time"
)
// The contract: the destination changes only on success, and it changes
// atomically whatever the two entry types are.
//
// rename(2) cannot replace a file with a directory (or the reverse) at all, and
// refuses a non-empty directory target - which is why publishing goes through a
// swap rather than a delete-then-rename, and why every combination is covered
// rather than just the common one.
func TestPublishReplacesEveryCombinationOfTypes(t *testing.T) {
cases := []struct {
name string
seed string
wantAtDest string
wantMissing string
}{
{
name: "a directory replaces an existing file",
seed: `mkdir -p /work/src && echo new > /work/src/f && echo original > /work/dest`,
wantAtDest: "dest/f",
},
{
name: "a directory replaces an existing directory, without merging into it",
seed: `mkdir -p /work/src /work/dest && echo new > /work/src/f && echo old > /work/dest/keep`,
wantAtDest: "dest/f",
wantMissing: "dest/keep",
},
{
name: "a file replaces an existing directory",
seed: `mkdir -p /work/dest && echo new > /work/src && echo old > /work/dest/keep`,
wantAtDest: "dest",
},
{
name: "a new destination is one rename",
seed: `mkdir -p /work/src && echo new > /work/src/f`,
wantAtDest: "dest/f",
},
}
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, testCase.seed)
staging := "/work/.flux-op-" + operationID
result := fluxOp(t, volume, "",
append(baseArgs("--discard-staging", staging, "/work/dest", "--"),
"cp", "-a", "-T", "/work/src", staging)...)
if result.exit != 0 {
t.Fatalf("exit %d:\n%s", result.exit, result.output)
}
if !exists(volume, testCase.wantAtDest) {
t.Errorf("%s is not there\n%s", testCase.wantAtDest, tree(volume))
}
if testCase.wantMissing != "" && exists(volume, testCase.wantMissing) {
t.Errorf("%s survived - the destination was merged into, not replaced\n%s",
testCase.wantMissing, tree(volume))
}
requireNoArtefacts(t, volume)
})
}
}
func TestAFailedCommandLeavesTheDestinationAndReclaimsStaging(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `echo original > /work/dest`)
staging := "/work/.flux-op-" + operationID
result := fluxOp(t, volume, "", append(baseArgs("--discard-staging", staging, "/work/dest", "--"), "false")...)
if result.exit == 0 {
t.Fatalf("a failing command reported success:\n%s", result.output)
}
if got := contents(t, volume, "dest"); got != "original" {
t.Errorf("destination holds %q, want original", got)
}
requireNoArtefacts(t, volume)
}
// Checked against what actually landed rather than against what an archive
// claims about itself: those numbers are written by whoever built it, so a bomb
// simply lies.
func TestAResultOverTheCeilingIsRefusedAndReclaimed(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/src && head -c 2000 /dev/zero > /work/src/big`)
staging := "/work/.flux-op-" + operationID
result := fluxOp(t, volume, "",
append(baseArgs("--discard-staging", "--max-bytes", "1000", staging, "/work/dest", "--"),
"cp", "-a", "-T", "/work/src", staging)...)
if result.exit != 3 {
t.Fatalf("exit %d, want 3:\n%s", result.exit, result.output)
}
if exists(volume, "dest") {
t.Error("the destination was published despite the refusal")
}
requireNoArtefacts(t, volume)
}
// An archive that carries a symlink and then writes through it reaches wherever
// the link points. Inside this container that is nowhere useful, but the result
// is published onto a volume that other code paths - and other nodes, through
// sync - do read.
func TestAResultContainingALinkIsRefused(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/src && ln -s /etc/shadow /work/src/link`)
staging := "/work/.flux-op-" + operationID
result := fluxOp(t, volume, "",
append(baseArgs("--discard-staging", "--no-links", staging, "/work/dest", "--"),
"cp", "-a", "-T", "/work/src", staging)...)
if result.exit != 4 {
t.Fatalf("exit %d, want 4:\n%s", result.exit, result.output)
}
if exists(volume, "dest") {
t.Error("the destination was published despite the refusal")
}
requireNoArtefacts(t, volume)
}
// A move and a rename have NO command: the caller's source already IS the
// result, so publishing it is the whole operation. A usage check that demanded a
// command rejected every move, and nothing noticed for a whole branch.
func TestAMovePublishesWithNoCommandAtAll(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/photos && echo hi > /work/photos/f`)
result := fluxOp(t, volume, "", baseArgs("/work/photos", "/work/out", "--")...)
if result.exit != 0 {
t.Fatalf("exit %d:\n%s", result.exit, result.output)
}
if !exists(volume, "out/f") {
t.Errorf("the move did not publish\n%s", tree(volume))
}
if exists(volume, "photos") {
t.Error("the source survived the move")
}
requireNoArtefacts(t, volume)
}
func TestAMoveOverAnExistingDestinationSwapsItAsideAndCleansUp(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/photos && echo new > /work/photos/f && echo old > /work/out`)
result := fluxOp(t, volume, "", baseArgs("/work/photos", "/work/out", "--")...)
if result.exit != 0 {
t.Fatalf("exit %d:\n%s", result.exit, result.output)
}
if got := contents(t, volume, "out/f"); got != "new" {
t.Errorf("destination holds %q, want new", got)
}
requireNoArtefacts(t, volume)
}
// Staging is only ever discarded when the caller says it owns it. A move's
// operand is the user's own data, and discarding it on a failure would destroy
// the only copy.
func TestAFailureNeverDiscardsAnOperandTheCallerOwns(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/photos && echo precious > /work/photos/f`)
result := fluxOp(t, volume, "",
baseArgs("--max-bytes", "1", "/work/photos", "/work/dest", "--")...)
if result.exit != 3 {
t.Fatalf("exit %d, want 3:\n%s", result.exit, result.output)
}
if got := contents(t, volume, "photos/f"); got != "precious" {
t.Errorf("the caller's own data holds %q, want precious", got)
}
}
// The state the marker exists for: the caller's previous data has been moved
// aside and the replacement never arrived. Reproduced by publishing a staging
// path that does not exist, so the second rename fails exactly where a crash
// would land.
func TestAnInterruptedPublishLeavesTheDataAndAMarkerThatPlacesIt(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/x/y/out && echo precious > /work/x/y/out/2024`)
// A directory published over its own parent, which is a move a user can ask
// for. The first rename carries the staging path away inside the
// destination, so the second finds nothing at it and the publish stops
// between the two - the state a crash in that window leaves, without having
// to kill the process to reach it.
result := fluxOp(t, volume, "", baseArgs("/work/x/y/out/2024", "/work/x/y/out", "--")...)
if result.exit == 0 {
t.Fatalf("publishing a directory over its own parent succeeded:\n%s", result.output)
}
displaced := ".flux-old-" + operationID
if got := contents(t, volume, displaced+"/2024"); got != "precious" {
t.Errorf("displaced data holds %q, want precious\n%s", got, tree(volume))
}
if exists(volume, "x/y/out") {
t.Error("the destination is still there, so this is not the interrupted state")
}
// Relative, so nothing that reads it can be sent off the volume by following
// an absolute path. At the volume root, which is the one directory the sweep
// reads - not beside the destination, wherever the caller kept it.
marker := contents(t, volume, displaced+".dest")
lines := strings.Split(marker, "\n")
if len(lines) != 2 {
t.Fatalf("marker holds %q, want a destination and an identity", marker)
}
if lines[0] != "x/y/out" {
t.Errorf("marker names %q, want x/y/out", lines[0])
}
// The identity is read from the displaced copy: rename preserved it, which
// is the property that lets a sweep tell the published object from one the
// app owner put at the same path.
if want := identityOf(t, volume, displaced+"/2024"); lines[1] != want {
t.Errorf("marker identity is %q, want %q\n%s", lines[1], want, tree(volume))
}
}
// Nothing is displaced over an operation that cannot be carried out, so there is
// nothing for a sweep to put back afterwards.
func TestAMissingStagingPathFailsBeforeAnythingMoves(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/x/y && echo precious > /work/x/y/out`)
result := fluxOp(t, volume, "", baseArgs("/work/a/b/photos", "/work/x/y/out", "--")...)
if result.exit == 0 {
t.Fatalf("publishing a staging path that does not exist succeeded:\n%s", result.output)
}
if got := contents(t, volume, "x/y/out"); got != "precious" {
t.Errorf("destination holds %q, want precious\n%s", got, tree(volume))
}
if exists(volume, ".flux-old-"+operationID) || exists(volume, ".flux-old-"+operationID+".dest") {
t.Errorf("an operation that never started left artefacts behind\n%s", tree(volume))
}
}
// tar -C and unzip -d both need the directory to exist already. A file copy must
// NOT ask for it: cp -T refuses to overwrite a directory with a non-directory.
func TestStagingIsCreatedForCommandsThatNeedIt(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `mkdir -p /work/src && echo x > /work/src/f && tar -cf /work/a.tar -C /work src`)
staging := "/work/.flux-op-" + operationID
result := fluxOp(t, volume, "",
append(baseArgs("--discard-staging", "--mkdir", staging, "/work/out", "--"),
"tar", "-xf", "/work/a.tar", "-C", staging)...)
if result.exit != 0 {
t.Fatalf("exit %d:\n%s", result.exit, result.output)
}
if got := contents(t, volume, "out/src/f"); got != "x" {
t.Errorf("extracted content is %q", got)
}
}
// Cancelling an operation has to reach the command, not just the process
// supervising it.
//
// A container stop delivers SIGTERM to PID 1 only, so an unforwarded signal
// leaves the command writing into a staging directory nobody will publish. And
// an untrapped one kills the supervisor outright, so its cleanup never runs and
// the space stays spent on a volume the caller pays for until the next boot
// sweep.
//
// Driven with `docker stop`, which is what FluxOS issues - not by signalling a
// process on this side of the container.
func TestACancelledOperationStopsItsCommandAndReclaimsStaging(t *testing.T) {
volume := volumeDir(t)
seed(t, volume, `echo original > /work/dest`)
name := "flux-op-cancel-" + strings.ReplaceAll(t.Name(), "/", "-")
staging := "/work/.flux-op-" + operationID
argv := append([]string{"run", "--name", name}, executorConfig(volume)...)
argv = append(argv, image(), "flux-op")
argv = append(argv, baseArgs("--discard-staging", "--mkdir", staging, "/work/dest", "--", "sleep", "30")...)
cmd := exec.Command("docker", argv...)
if err := cmd.Start(); err != nil {
t.Fatalf("could not start the container: %v", err)
}
t.Cleanup(func() { exec.Command("docker", "rm", "-f", name).Run() })
// Wait for the operation to actually be under way, so the stop cannot
// arrive before there is anything to stop.
deadline := time.Now().Add(30 * time.Second)
for !exists(volume, ".flux-op-"+operationID) {
if time.Now().After(deadline) {
t.Fatal("the operation never created its staging directory")
}
time.Sleep(100 * time.Millisecond)
}
if out, err := exec.Command("docker", "stop", "--time", "15", name).CombinedOutput(); err != nil {
t.Fatalf("could not stop the container: %v\n%s", err, out)
}
err := cmd.Wait()
var exitErr *exec.ExitError
if err == nil {
t.Fatal("a cancelled operation reported success")
}
if !errors.As(err, &exitErr) {
t.Fatalf("unexpected failure: %v", err)
}
if exitErr.ExitCode() != 143 {
t.Errorf("exit %d, want 143 - which is what tells a cancelled operation from a failed one", exitErr.ExitCode())
}
if got := contents(t, volume, "dest"); got != "original" {
t.Errorf("destination holds %q, want original", got)
}
requireNoArtefacts(t, volume)
}
func TestTheIdentifierAndVolumeRootAreRequired(t *testing.T) {
volume := volumeDir(t)
cases := [][]string{
{"/work/.flux-op-1", "/work/dest", "--", "true"},
{"--id", operationID, "/work/.flux-op-1", "/work/dest", "--", "true"},
{"--root", "/work", "/work/.flux-op-1", "/work/dest", "--", "true"},
}
for _, argv := range cases {
result := fluxOp(t, volume, "", argv...)
if result.exit != 2 {
t.Errorf("exit %d for %v, want 2:\n%s", result.exit, argv, result.output)
}
if exists(volume, "dest") {
t.Error("a refused invocation touched the destination")
}
}
}