-
Notifications
You must be signed in to change notification settings - Fork 1
/
dockerexec_test.go
323 lines (248 loc) · 7.96 KB
/
dockerexec_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
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
package dockerexec_test
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/segevfiner/dockerexec"
)
const testImage = "ubuntu:focal"
var dockerClient *client.Client
func TestMain(m *testing.M) {
var err error
dockerClient, err = client.NewClientWithOpts(client.WithAPIVersionNegotiation(), client.FromEnv)
if err != nil {
panic(err)
}
if _, _, err := dockerClient.ImageInspectWithRaw(context.Background(), testImage); err != nil {
if client.IsErrNotFound(err) {
pullOutput, err := dockerClient.ImagePull(context.Background(), testImage, image.PullOptions{})
if err != nil {
panic(err)
}
defer pullOutput.Close()
err = jsonmessage.DisplayJSONMessagesStream(pullOutput, os.Stderr, 0, false, nil)
if err != nil {
panic(err)
}
} else {
panic(err)
}
}
os.Exit(m.Run())
}
func TestEcho(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!")
output, err := cmd.Output()
require.NoError(t, err)
assert.Equal(t, "Hello, World!\n", string(output))
}
func TestCatStdin(t *testing.T) {
const input = "Line 1\nLine 2"
cmd := dockerexec.Command(dockerClient, testImage, "cat")
cmd.Stdin = strings.NewReader(input)
output, err := cmd.Output()
require.NoError(t, err)
assert.Equal(t, input, string(output))
}
func TestCatFileRace(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "cat")
stdin, err := cmd.StdinPipe()
require.NoError(t, err)
err = cmd.Start()
require.NoError(t, err)
wrote := make(chan bool)
go func() {
defer close(wrote)
fmt.Fprint(stdin, "echo\n")
stdin.Close()
}()
err = cmd.Wait()
require.NoError(t, err)
<-wrote
}
func TestCatGoodAndBadFile(t *testing.T) {
// Testing combined output and error values.
bs, err := dockerexec.Command(dockerClient, testImage, "sh", "-c", "cat /bogus/file.foo; sleep 1; cat /etc/os-release; exit 1").CombinedOutput()
assert.IsType(t, &dockerexec.ExitError{}, err)
sp := strings.SplitN(string(bs), "\n", 2)
assert.Len(t, sp, 2)
errLine, body := sp[0], sp[1]
assert.True(t, strings.HasPrefix(errLine, "cat: /bogus/file.foo: No such file or directory"), errLine)
assert.Contains(t, body, "Ubuntu")
}
func TestNoExistExecutable(t *testing.T) {
// Can't run a non-existent executable
err := dockerexec.Command(dockerClient, testImage, "/no-exist-executable").Run()
assert.Error(t, err)
}
func TestExitStatus(t *testing.T) {
// Test that exit values are returned correctly
err := dockerexec.Command(dockerClient, testImage, "sh", "-c", "exit 42").Run()
require.Error(t, err)
if err, ok := err.(*dockerexec.ExitError); ok {
assert.EqualError(t, err, "exit status 42")
assert.Equal(t, int64(42), err.StatusCode)
} else {
t.Fail()
}
}
func TestExitCode(t *testing.T) {
// Test that exit code are returned correctly
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "exit 42")
_ = cmd.Run()
assert.Equal(t, int64(42), cmd.StatusCode)
cmd = dockerexec.Command(dockerClient, testImage, "sh", "-c", "exit 255")
_ = cmd.Run()
assert.Equal(t, int64(255), cmd.StatusCode)
cmd = dockerexec.Command(dockerClient, testImage, "cat")
_ = cmd.Run()
assert.Equal(t, int64(0), cmd.StatusCode)
// Test when command does not call Run().
cmd = dockerexec.Command(dockerClient, testImage, "cat")
assert.Equal(t, int64(-1), cmd.StatusCode)
}
func TestPipes(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo stderr >&2; cat")
stdin, err := cmd.StdinPipe()
require.NoError(t, err)
stdout, err := cmd.StdoutPipe()
require.NoError(t, err)
stderr, err := cmd.StderrPipe()
require.NoError(t, err)
err = cmd.Start()
require.NoError(t, err)
_, err = stdin.Write([]byte("input\n"))
require.NoError(t, err)
stdin.Close()
outbr := bufio.NewReader(stdout)
errbr := bufio.NewReader(stderr)
line, _, err := errbr.ReadLine()
require.NoError(t, err)
assert.Equal(t, "stderr", string(line))
line, _, err = outbr.ReadLine()
require.NoError(t, err)
assert.Equal(t, "input", string(line))
buf, err := io.ReadAll(outbr)
require.NoError(t, err)
assert.Empty(t, buf)
buf, err = io.ReadAll(errbr)
require.NoError(t, err)
assert.Empty(t, buf)
err = cmd.Wait()
require.NoError(t, err)
}
func TestContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := dockerexec.CommandContext(ctx, dockerClient, testImage, "sleep", "120")
err := cmd.Start()
require.NoError(t, err)
cancel()
err = cmd.Wait()
assert.Error(t, err)
assert.IsType(t, context.Canceled, err)
}
func TestNilContext(t *testing.T) {
assert.Panics(t, func() {
//lint:ignore SA1012 Test for panic
dockerexec.CommandContext(nil, dockerClient, testImage, "cat") //nolint:staticcheck
})
}
func TestStartCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
cmd := dockerexec.CommandContext(ctx, dockerClient, testImage, "sleep", "120")
assert.ErrorIs(t, cmd.Start(), context.Canceled)
}
func TestCmdString(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!")
assert.Equal(t, "sh -c echo Hello, World!", cmd.String())
}
func TestStdoutStartWait(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!")
var stdout bytes.Buffer
cmd.Stdout = &stdout
err := cmd.Start()
require.NoError(t, err)
err = cmd.Wait()
require.NoError(t, err)
assert.Equal(t, int64(0), cmd.StatusCode)
assert.Equal(t, "Hello, World!\n", stdout.String())
}
func TestStderrStartWait(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!; echo stderr >&2")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Start()
require.NoError(t, err)
err = cmd.Wait()
require.NoError(t, err)
assert.Equal(t, int64(0), cmd.StatusCode)
assert.Equal(t, "stderr\n", stderr.String())
}
func TestTtyOutput(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "tty")
cmd.Config.Tty = true
output, err := cmd.Output()
require.NoError(t, err)
assert.Contains(t, string(output), "/dev/pts")
}
func TestTtyCombinedOutput(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "tty")
cmd.Config.Tty = true
output, err := cmd.CombinedOutput()
require.NoError(t, err)
assert.Contains(t, string(output), "/dev/pts")
}
func TestNoTtyAndStderr(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!")
cmd.Config.Tty = true
_, err := cmd.StderrPipe()
require.NoError(t, err)
err = cmd.Run()
assert.EqualError(t, err, "dockerexec: can't set both Config.Tty and Stderr")
}
func TestOutputError(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo stderr >&2; exit 1")
output, err := cmd.Output()
assert.Error(t, err)
assert.Empty(t, output)
if err, ok := err.(*dockerexec.ExitError); ok {
assert.EqualError(t, err, "exit status 1")
assert.Equal(t, int64(1), err.StatusCode)
assert.Equal(t, "stderr\n", string(err.Stderr))
} else {
t.Fail()
}
}
func TestStartTwice(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!")
err := cmd.Start()
require.NoError(t, err)
err = cmd.Start()
assert.EqualError(t, err, "dockerexec: already started")
}
func TestWaitWithoutStart(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!")
err := cmd.Wait()
assert.EqualError(t, err, "dockerexec: not started")
}
func TestWaitTwice(t *testing.T) {
cmd := dockerexec.Command(dockerClient, testImage, "sh", "-c", "echo Hello, World!")
err := cmd.Start()
require.NoError(t, err)
err = cmd.Wait()
require.NoError(t, err)
err = cmd.Wait()
assert.EqualError(t, err, "dockerexec: Wait was already called")
}