-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainerdstdout_test.go
137 lines (113 loc) · 3 KB
/
containerdstdout_test.go
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
package containerdstdout_test
import (
"bytes"
"context"
"fmt"
"io"
"strconv"
"sync"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/opencontainers/runtime-spec/specs-go"
)
// This needs to be run as root so it can contact the containerd service.
// Ensure you have ginkgo and go in your $PATH, and then run:
// `ginkgo -untilItFails`
const containerdSocket = "/run/containerd/containerd.sock"
var _ = Describe("Stdouttest", func() {
var (
ctx context.Context
client *containerd.Client
image containerd.Image
container containerd.Container
task containerd.Task
)
BeforeEach(func() {
var err error
client, err = containerd.New(containerdSocket)
Expect(err).NotTo(HaveOccurred())
ctx = namespaces.WithNamespace(context.Background(), "example")
image, err = client.Pull(ctx, "docker.io/library/busybox:latest", containerd.WithPullUnpack)
Expect(err).NotTo(HaveOccurred())
container, err = client.NewContainer(
ctx,
"example",
containerd.WithNewSnapshot("busybox-snapshot", image),
containerd.WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("sleep", "600")),
)
Expect(err).NotTo(HaveOccurred())
task, err = container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
Expect(err).NotTo(HaveOccurred())
err = task.Start(ctx)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
task.Delete(ctx, containerd.WithProcessKill)
container.Delete(ctx, containerd.WithSnapshotCleanup)
client.Close()
})
It("captures stdout consistently", func() {
var wg sync.WaitGroup
count := 10
wg.Add(count)
ec := make(chan error, count)
for i := 0; i < count; i++ {
go runProcess(ctx, i, &wg, ec, task)
}
wg.Wait()
errs := []error{}
outer:
for {
select {
case err := <-ec:
errs = append(errs, err)
default:
break outer
}
}
Expect(errs).To(BeEmpty())
})
})
func runProcess(ctx context.Context, i int, wg *sync.WaitGroup, ec chan error, task containerd.Task) {
defer wg.Done()
stdout := gbytes.NewBuffer()
process, err := task.Exec(ctx, "say-hello-"+strconv.Itoa(i), &specs.Process{
Args: []string{"/bin/echo", "hi stdout"},
Cwd: "/",
}, cio.NewCreator(
cio.WithStreams(nil, stdout, io.Discard),
))
if err != nil {
ec <- err
return
}
defer process.Delete(ctx, containerd.WithProcessKill)
processExit, err := process.Wait(ctx)
if err != nil {
ec <- err
return
}
err = process.Start(ctx)
if err != nil {
ec <- err
return
}
exitStatus := <-processExit
if exitStatus.Error() != nil {
ec <- exitStatus.Error()
return
}
if exitStatus.ExitCode() != 0 {
ec <- fmt.Errorf("exit code: %d", exitStatus.ExitCode())
return
}
if !bytes.Contains(stdout.Contents(), []byte("hi stdout")) {
ec <- fmt.Errorf("expected 'hi stdout', got: %q", string(stdout.Contents()))
return
}
}