-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
common.go
253 lines (219 loc) · 6.41 KB
/
common.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
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
package tests
import (
"bytes"
"context"
"io"
"testing"
"time"
"github.com/containerd/containerd/namespaces"
"github.com/moby/buildkit/cache"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/source"
"github.com/moby/buildkit/worker/base"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
func NewBusyboxSourceSnapshot(ctx context.Context, t *testing.T, w *base.Worker, sm *session.Manager) cache.ImmutableRef {
img, err := source.NewImageIdentifier("docker.io/library/busybox:latest")
require.NoError(t, err)
src, err := w.SourceManager.Resolve(ctx, img, sm, nil)
require.NoError(t, err)
_, _, _, _, err = src.CacheKey(ctx, nil, 0)
require.NoError(t, err)
snap, err := src.Snapshot(ctx, nil)
require.NoError(t, err)
return snap
}
func NewCtx(s string) context.Context {
return namespaces.WithNamespace(context.Background(), s)
}
func TestWorkerExec(t *testing.T, w *base.Worker) {
ctx := NewCtx("buildkit-test")
ctx, cancel := context.WithCancel(ctx)
sm, err := session.NewManager()
require.NoError(t, err)
snap := NewBusyboxSourceSnapshot(ctx, t, w, sm)
root, err := w.CacheMgr.New(ctx, snap, nil)
require.NoError(t, err)
id := identity.NewID()
// verify pid1 exits when stdin sees EOF
ctxTimeout, cancelTimeout := context.WithTimeout(ctx, 5*time.Second)
started := make(chan struct{})
pipeR, pipeW := io.Pipe()
go func() {
select {
case <-ctxTimeout.Done():
t.Error("Unexpected timeout waiting for pid1 to start")
case <-started:
pipeW.Write([]byte("hello"))
pipeW.Close()
}
}()
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
err = w.WorkerOpt.Executor.Run(ctxTimeout, id, execMount(root), nil, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"cat"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin:/sbin:/usr/sbin"},
},
Stdin: pipeR,
Stdout: &nopCloser{stdout},
Stderr: &nopCloser{stderr},
}, started)
cancelTimeout()
t.Logf("Stdout: %s", stdout.String())
t.Logf("Stderr: %s", stderr.String())
require.NoError(t, err)
require.Equal(t, "hello", stdout.String())
require.Empty(t, stderr.String())
// first start pid1 in the background
eg := errgroup.Group{}
started = make(chan struct{})
eg.Go(func() error {
return w.WorkerOpt.Executor.Run(ctx, id, execMount(root), nil, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"sleep", "10"},
Cwd: "/",
Env: []string{"PATH=/bin:/usr/bin:/sbin:/usr/sbin"},
},
}, started)
})
select {
case <-started:
case <-time.After(10 * time.Second):
t.Error("Unexpected timeout waiting for pid1 to start")
}
stdout.Reset()
stderr.Reset()
// verify pid1 is the sleep command via Exec
err = w.WorkerOpt.Executor.Exec(ctx, id, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"ps", "-o", "pid,comm"},
},
Stdout: &nopCloser{stdout},
Stderr: &nopCloser{stderr},
})
t.Logf("Stdout: %s", stdout.String())
t.Logf("Stderr: %s", stderr.String())
require.NoError(t, err)
// verify pid1 is sleep
require.Contains(t, stdout.String(), "1 sleep")
require.Empty(t, stderr.String())
// simulate: echo -n "hello" | cat > /tmp/msg
stdin := bytes.NewReader([]byte("hello"))
stdout.Reset()
stderr.Reset()
err = w.WorkerOpt.Executor.Exec(ctx, id, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"sh", "-c", "cat > /tmp/msg"},
},
Stdin: io.NopCloser(stdin),
Stdout: &nopCloser{stdout},
Stderr: &nopCloser{stderr},
})
require.NoError(t, err)
require.Empty(t, stdout.String())
require.Empty(t, stderr.String())
// verify contents of /tmp/msg
stdout.Reset()
stderr.Reset()
err = w.WorkerOpt.Executor.Exec(ctx, id, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"cat", "/tmp/msg"},
},
Stdout: &nopCloser{stdout},
Stderr: &nopCloser{stderr},
})
t.Logf("Stdout: %s", stdout.String())
t.Logf("Stderr: %s", stderr.String())
require.NoError(t, err)
require.Equal(t, "hello", stdout.String())
require.Empty(t, stderr.String())
// stop pid1
cancel()
err = eg.Wait()
// we expect pid1 to get canceled after we test the exec
require.True(t, errors.Is(err, context.Canceled))
err = snap.Release(ctx)
require.NoError(t, err)
}
func TestWorkerExecFailures(t *testing.T, w *base.Worker) {
ctx := NewCtx("buildkit-test")
sm, err := session.NewManager()
require.NoError(t, err)
snap := NewBusyboxSourceSnapshot(ctx, t, w, sm)
root, err := w.CacheMgr.New(ctx, snap, nil)
require.NoError(t, err)
id := identity.NewID()
// pid1 will start but only long enough for /bin/false to run
eg := errgroup.Group{}
started := make(chan struct{})
eg.Go(func() error {
return w.WorkerOpt.Executor.Run(ctx, id, execMount(root), nil, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"/bin/false"},
Cwd: "/",
},
}, started)
})
select {
case <-started:
case <-time.After(10 * time.Second):
t.Error("Unexpected timeout waiting for pid1 to start")
}
// this should fail since pid1 has already exited
err = w.WorkerOpt.Executor.Exec(ctx, id, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"/bin/true"},
},
})
require.Error(t, err) // pid1 no longer running
err = eg.Wait()
require.Error(t, err) // process returned non-zero exit code: 1
// pid1 will not start, bogus pid1 command
eg = errgroup.Group{}
started = make(chan struct{})
eg.Go(func() error {
return w.WorkerOpt.Executor.Run(ctx, id, execMount(root), nil, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"bogus"},
},
}, started)
})
select {
case <-started:
case <-time.After(10 * time.Second):
t.Error("Unexpected timeout waiting for pid1 to start")
}
// this should fail since pid1 never started
err = w.WorkerOpt.Executor.Exec(ctx, id, executor.ProcessInfo{
Meta: executor.Meta{
Args: []string{"/bin/true"},
},
})
require.Error(t, err) // container has exited with error
err = eg.Wait()
require.Error(t, err) // pid1 did not terminate successfully
err = snap.Release(ctx)
require.NoError(t, err)
}
type nopCloser struct {
io.Writer
}
func (n *nopCloser) Close() error {
return nil
}
func execMount(m cache.Mountable) executor.Mount {
return executor.Mount{Src: &mountable{m: m}}
}
type mountable struct {
m cache.Mountable
}
func (m *mountable) Mount(ctx context.Context, readonly bool) (snapshot.Mountable, error) {
return m.m.Mount(ctx, readonly, nil)
}