-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_suite.go
More file actions
455 lines (384 loc) · 14.5 KB
/
Copy pathrun_suite.go
File metadata and controls
455 lines (384 loc) · 14.5 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
Copyright The containerd Authors.
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 shimtest
import (
"bytes"
"fmt"
"hash/crc32"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/containerd/containerd/api/events"
taskAPI "github.com/containerd/containerd/api/runtime/task/v3"
tasktypes "github.com/containerd/containerd/api/types/task"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/errdefs"
"github.com/containerd/errdefs/pkg/errgrpc"
"github.com/containerd/ttrpc"
typeurl "github.com/containerd/typeurl/v2"
)
// RunSuite contains the always-run conformance tests: full container
// lifecycle, init exit-code propagation, output-then-exit, and
// event-stream verification. None of these tests are gated by a
// feature key — every shim should pass them.
type RunSuite struct {
cfg Config
}
// NewRunSuite constructs a RunSuite from the given options.
func NewRunSuite(cfg Config) *RunSuite {
return &RunSuite{cfg: cfg}
}
// Run runs every test in the suite as a subtest of t.
func (s *RunSuite) Run(t *testing.T) {
t.Helper()
registerShimLeakCheck(t, s.cfg.ShimBinary)
t.Run("Lifecycle", s.testLifecycle)
t.Run("InitExitCodes", s.testInitExitCodes)
t.Run("OutputThenExit", s.testOutputThenExit)
t.Run("Events", s.testEvents)
t.Run("FastExitInit", s.testFastExitInit)
}
// testLifecycle drives a container through create / start / state /
// kill / wait / delete / shutdown and verifies output appears.
func (s *RunSuite) testLifecycle(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
t.Log("shim binary:", shimBin)
createOCISpec(t, bundleDir, []string{"/bin/forever", "hello"}, s.cfg)
containerID := containerID(t)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "run")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
t.Log("shim started, address:", params.Address)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := newTaskClient(client, params.Version)
var stdoutBuf bytes.Buffer
var stdoutMu sync.Mutex
drainFifoInto(t, ctx, stdoutPath, &stdoutBuf, &stdoutMu)
drainFifo(t, ctx, stderrPath)
t.Log("creating task")
createResp, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts))
if err != nil {
t.Fatal("failed to create task:", err)
}
t.Log("task created, pid:", createResp.Pid)
t.Log("starting task")
startResp, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID})
if err != nil {
t.Fatal("failed to start task:", err)
}
t.Log("task started, pid:", startResp.Pid)
t.Log("checking state")
stateResp, err := tc.State(ctx, &taskAPI.StateRequest{ID: containerID})
if err != nil {
t.Fatal("failed to get state:", err)
}
t.Log("task state:", stateResp.Status)
if stateResp.Status != tasktypes.Status_RUNNING {
t.Fatalf("expected task status RUNNING, got %s", stateResp.Status)
}
t.Log("waiting for output")
deadline := time.After(30 * time.Second)
for {
stdoutMu.Lock()
got := stdoutBuf.String()
stdoutMu.Unlock()
if strings.Contains(got, "hello") {
t.Log("got expected output:", strings.TrimSpace(got))
break
}
select {
case <-deadline:
t.Fatalf("timed out waiting for 'hello' output, got: %q", got)
case <-time.After(100 * time.Millisecond):
}
}
t.Log("killing task")
if _, err := tc.Kill(ctx, &taskAPI.KillRequest{
ID: containerID,
Signal: uint32(syscall.SIGKILL),
All: true,
}); err != nil {
// The process may have exited naturally before Kill is called.
// NotFound means the process is already in a terminal state,
// which is fine - proceed to Wait and Delete.
if !errdefs.IsNotFound(errgrpc.ToNative(err)) {
t.Fatal("failed to kill task:", err)
}
t.Log("task already finished before kill (benign race):", err)
}
t.Log("waiting for task exit")
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID})
if err != nil {
t.Fatal("failed to wait for task:", err)
}
t.Log("task exited, status:", waitResp.ExitStatus)
t.Log("deleting task")
deleteResp, err := tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID})
if err != nil {
t.Fatal("failed to delete task:", err)
}
t.Log("task deleted, pid:", deleteResp.Pid, "exit status:", deleteResp.ExitStatus)
t.Log("shutting down shim")
shutdownTask(ctx, tc, containerID)
}
// testInitExitCodes runs /bin/exit N as the container's init for a
// range of values and verifies the task-level exit status.
func (s *RunSuite) testInitExitCodes(t *testing.T) {
for _, code := range []int{0, 1, 2, 42, 127, 255} {
t.Run(fmt.Sprintf("Exit%d", code), func(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
cid := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/exit", strconv.Itoa(code)}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "run")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, cid, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := newTaskClient(client, params.Version)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, cid, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: cid}); err != nil {
t.Fatal("start failed:", err)
}
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: cid})
if err != nil {
t.Fatal("wait failed:", err)
}
if waitResp.ExitStatus != uint32(code) {
t.Fatalf("expected exit status %d, got %d", code, waitResp.ExitStatus)
}
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: cid})
shutdownTask(ctx, tc, cid)
})
}
}
// testOutputThenExit runs a process that writes 50 lines then exits
// non-zero, and verifies both the exit status and every output line
// are captured by the shim.
func (s *RunSuite) testOutputThenExit(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/tickexit"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "run")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := newTaskClient(client, params.Version)
var stdoutBuf bytes.Buffer
var stdoutMu sync.Mutex
drainFifoInto(t, ctx, stdoutPath, &stdoutBuf, &stdoutMu)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("failed to start task:", err)
}
waitResp, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID})
if err != nil {
t.Fatal("wait failed:", err)
}
t.Log("task exit status:", waitResp.ExitStatus)
const expectedExit = 7
if waitResp.ExitStatus != expectedExit {
t.Fatalf("expected exit status %d, got %d", expectedExit, waitResp.ExitStatus)
}
deadline := time.After(5 * time.Second)
for {
stdoutMu.Lock()
got := stdoutBuf.String()
stdoutMu.Unlock()
if strings.Contains(got, "tick 50\n") {
break
}
select {
case <-deadline:
stdoutMu.Lock()
final := stdoutBuf.String()
stdoutMu.Unlock()
t.Fatalf("timed out waiting for final output, got: %q", final)
case <-time.After(10 * time.Millisecond):
}
}
stdoutMu.Lock()
got := stdoutBuf.String()
stdoutMu.Unlock()
lines := strings.Split(strings.TrimRight(got, "\n"), "\n")
if len(lines) != 50 {
t.Fatalf("expected 50 output lines, got %d: %q", len(lines), got)
}
for i, line := range lines {
want := fmt.Sprintf("tick %d", i+1)
if line != want {
t.Fatalf("line %d: got %q, want %q", i, line, want)
}
}
tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID})
shutdownTask(ctx, tc, containerID)
}
// testEvents verifies that the shim publishes the expected task
// lifecycle events (create, start, exit, delete) to its containerd
// events endpoint during a normal run.
func (s *RunSuite) testEvents(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
ns := uniqueTestNamespace(t, "run")
createOCISpec(t, bundleDir, []string{"/bin/exit", "0"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ctx := namespaces.WithNamespace(t.Context(), ns)
rec := startEventsRecorder(t, bundleDir)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := newTaskClient(client, params.Version)
drainFifo(t, ctx, stdoutPath)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
if _, err := tc.Wait(ctx, &taskAPI.WaitRequest{ID: containerID}); err != nil {
t.Fatal("wait failed:", err)
}
if rec.waitForTopic("/tasks/exit", 2*time.Second) == nil {
t.Fatalf("exit event not published after task wait; received topics: %v", rec.topics())
}
if _, err := tc.Delete(ctx, &taskAPI.DeleteRequest{ID: containerID}); err != nil {
t.Fatal("delete failed:", err)
}
want := []string{
"/tasks/create",
"/tasks/start",
"/tasks/exit",
"/tasks/delete",
}
for _, topic := range want {
if env := rec.waitForTopic(topic, 5*time.Second); env == nil {
t.Fatalf("missing event %q; received topics: %v", topic, rec.topics())
}
}
shutdownTask(ctx, tc, containerID)
got := rec.topics()
t.Log("received topics:", got)
idx := 0
for _, topic := range got {
if idx < len(want) && topic == want[idx] {
idx++
}
}
if idx != len(want) {
t.Fatalf("events out of order or missing: want (in order) %v, got %v", want, got)
}
exitEnv := rec.waitForTopic("/tasks/exit", 0)
if exitEnv == nil {
t.Fatal("exit envelope vanished")
}
if exitEnv.Namespace != ns {
t.Errorf("exit envelope namespace: got %q, want %q", exitEnv.Namespace, ns)
}
var exitEvt events.TaskExit
if err := typeurl.UnmarshalTo(exitEnv.Event, &exitEvt); err != nil {
t.Fatal("unmarshal TaskExit:", err)
}
if exitEvt.ContainerID != containerID {
t.Errorf("exit event container id: got %q, want %q", exitEvt.ContainerID, containerID)
}
if exitEvt.ExitStatus != 0 {
t.Errorf("exit event exit status: got %d, want 0", exitEvt.ExitStatus)
}
}
// testFastExitInit runs /bin/burstexit as the container's init process
// (writes 8 MiB then exits immediately), then tears down the shim via
// Shutdown without calling Delete first. This forces ioShutdown to run
// from service.shutdown → container.shutdown rather than the orderly
// in-VM delete+drain sequence.
//
// The race: service.shutdown → c.shutdown → ioShutdown closes the
// vsock connection while the host copy goroutine may still be blocked
// in io.CopyBuffer's Read, draining bytes sitting in the kernel socket
// receive buffer. A buggy shim closes the connection first, causing
// the goroutine to see "use of closed network connection" and abandon
// the remaining buffered bytes.
//
// A correct shim waits for ioDone (copy goroutines finished) before
// closing the connections, so the full 8 MiB arrives intact.
func (s *RunSuite) testFastExitInit(t *testing.T) {
shimBin, bundleDir, rootfsMounts := shimSetup(t, s.cfg)
containerID := containerID(t)
createOCISpec(t, bundleDir, []string{"/bin/burstexit", strconv.Itoa(burstPayloadSize), "0"}, s.cfg)
stdoutPath, stderrPath := createIOFifos(t, bundleDir)
ns := uniqueTestNamespace(t, "run")
ctx := namespaces.WithNamespace(t.Context(), ns)
params := startShim(t, shimBin, bundleDir, containerID, ns, s.cfg)
conn := connectShim(t, params.Address)
client := ttrpc.NewClient(conn)
defer client.Close()
tc := newTaskClient(client, params.Version)
var stdoutBuf bytes.Buffer
var stdoutMu sync.Mutex
// drainFifoIntoDone closes done when the shim closes the FIFO write
// end, which happens only after ioShutdown completes (i.e. after
// the copy goroutine calls wc.Close). We must not read stdoutBuf
// before that signal.
drainDone := drainFifoIntoDone(t, ctx, stdoutPath, &stdoutBuf, &stdoutMu)
drainFifo(t, ctx, stderrPath)
if _, err := tc.Create(ctx, newCreateTaskRequest(t, containerID, bundleDir, stdoutPath, stderrPath, rootfsMounts)); err != nil {
t.Fatal("create failed:", err)
}
if _, err := tc.Start(ctx, &taskAPI.StartRequest{ID: containerID}); err != nil {
t.Fatal("start failed:", err)
}
// Shut down via Shutdown immediately after Start — without waiting
// for the init to exit and without calling Delete. This triggers
// service.shutdown → c.shutdown → ioShutdown while burstexit is
// still running (writing its 8 MiB burst) or has just exited with
// bytes still buffered in the vsock receive queue. The in-VM
// delete+drain sequence (waitTimeout on the IO copy wg) never runs.
tc.Shutdown(ctx, &taskAPI.ShutdownRequest{ID: containerID})
// Wait for the FIFO reader to reach EOF before inspecting the buffer.
select {
case <-drainDone:
case <-time.After(30 * time.Second):
t.Fatal("timed out waiting for stdout drain after shutdown")
}
stdoutMu.Lock()
got := stdoutBuf.Bytes()
stdoutMu.Unlock()
if len(got) != burstPayloadSize {
t.Fatalf("got %d bytes, want %d (truncated by %d bytes)",
len(got), burstPayloadSize, burstPayloadSize-len(got))
}
wantCRC := burstExpectedCRC32()
gotCRC := crc32.Checksum(got, crc32.MakeTable(crc32.Castagnoli))
if gotCRC != wantCRC {
t.Fatalf("CRC mismatch: got %08x, want %08x (data corrupted)", gotCRC, wantCRC)
}
t.Logf("ok — %d bytes, CRC %08x", len(got), gotCRC)
}