-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_test.go
More file actions
262 lines (225 loc) · 6.44 KB
/
container_test.go
File metadata and controls
262 lines (225 loc) · 6.44 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
package container_test
import (
"archive/tar"
"bytes"
"context"
"crypto/rand"
"flag"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/relab/container"
"github.com/relab/container/build"
"github.com/relab/container/network"
)
const containerTestTag = "container-test"
// TestMain builds the test image once before running tests that depend on it.
func TestMain(m *testing.M) {
// Parse flags early so testing.Verbose() works in buildTestImage.
flag.Parse()
if err := buildTestImage(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to build test image: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
func TestPing(t *testing.T) {
c, err := container.NewContainer()
if err != nil {
t.Fatalf("Failed to create container client: %v", err)
}
if err := c.Ping(t.Context()); err != nil {
t.Fatalf("Failed to ping Docker daemon: %v", err)
}
t.Log("Ping successful")
}
func TestNetworkCreateAndNetworkRemove(t *testing.T) {
c, err := container.NewContainer()
if err != nil {
t.Fatalf("Failed to create container client: %v", err)
}
resp, err := c.NetworkCreate(t.Context(), network.CreateOptions{
Name: "container-" + rand.Text()[:8],
Driver: "bridge",
})
if err != nil {
t.Fatalf("Failed to create network: %v", err)
}
t.Cleanup(func() {
// cannot use t.Context() here, since it may be canceled before cleanup runs
if err := c.NetworkRemove(context.Background(), resp.ID); err != nil {
t.Errorf("Failed to remove network: %v", err)
}
t.Logf("Network removed: %s", resp.ID)
})
t.Logf("Network created: %s", resp.ID)
}
func TestContainerCreateAndStartAndInspectAndStop(t *testing.T) {
c, err := container.NewContainer()
if err != nil {
t.Fatalf("Failed to create container client: %v", err)
}
net, err := c.NetworkCreate(t.Context(), network.CreateOptions{
Name: "container-" + rand.Text()[:8],
Driver: "bridge",
})
if err != nil {
t.Fatalf("Failed to create network: %v", err)
}
t.Logf("Network created: %s", net.ID)
resp, err := c.ContainerCreate(context.Background(), &container.Config{
Env: []string{"AUTHORIZED_KEYS=xyz"},
Image: containerTestTag,
ExposedPorts: container.PortSet{
"22/tcp": struct{}{},
},
}, &container.HostConfig{
PortBindings: container.PortMap{"22/tcp": {{}}}, // map ssh port to ephemeral port
AutoRemove: true,
}, nil, "")
if err != nil {
t.Fatalf("Failed to create container: %v", err)
}
t.Cleanup(func() {
timeout := 1 // seconds to wait before forcefully killing the container
opts := container.StopOptions{Timeout: &timeout}
// cannot use t.Context() here, since it may be canceled before cleanup runs
ctx := context.Background()
if err := c.ContainerStop(ctx, resp.ID, opts); err != nil {
t.Errorf("Failed to stop container '%s': %v", resp.ID, err)
} else {
t.Logf("Container stopped: %s", resp.ID)
}
if err := c.NetworkDisconnect(ctx, net.ID, resp.ID, true); err != nil {
t.Errorf("Failed to disconnect container from network '%s': %v", net.ID, err)
} else {
t.Logf("Container disconnected from network: %s", net.ID)
}
if err := c.NetworkRemove(ctx, net.ID); err != nil {
t.Errorf("Failed to remove network: %v", err)
} else {
t.Logf("Network removed: %s", net.ID)
}
})
t.Logf("Container created: %s", resp.ID)
err = c.ContainerStart(t.Context(), resp.ID)
if err != nil {
t.Fatalf("Failed to start container: %v", err)
}
t.Logf("Container started: %s", resp.ID)
insp, err := c.ContainerInspect(t.Context(), resp.ID)
if err != nil {
t.Fatalf("Failed to inspect container: %v", err)
}
t.Logf("Container inspected: %+v", insp)
name := strings.TrimPrefix(insp.Name, "/")
err = c.NetworkConnect(context.Background(), net.ID, resp.ID, &network.EndpointSettings{
Aliases: []string{name},
})
if err != nil {
t.Fatal(err)
}
t.Logf("Container name: %s", name)
t.Logf("Container network settings: %+v", insp.NetworkSettings.Ports)
}
func TestBuild(t *testing.T) {
resp, err := startImageBuild(t.Context(), []string{containerTestTag})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := resp.Close(); err != nil {
t.Error(err)
}
})
if err := build.ConsumeStream(resp, t.Output()); err != nil {
t.Error(err)
}
}
// startImageBuild initializes a Docker image build and returns the build output stream.
// The caller owns the returned ReadCloser and must Close it after consuming the stream.
func startImageBuild(ctx context.Context, tags []string) (io.ReadCloser, error) {
c, err := container.NewContainer()
if err != nil {
return nil, fmt.Errorf("create container failed: %w", err)
}
buildCtx, err := prepareBuildContext()
if err != nil {
return nil, fmt.Errorf("prepare build context failed: %w", err)
}
return c.ImageBuild(ctx, buildCtx, build.ImageBuildOptions{
Dockerfile: "Dockerfile",
Tags: tags,
})
}
// buildTestImage builds the container image needed for tests.
func buildTestImage() error {
resp, err := startImageBuild(context.Background(), []string{containerTestTag})
if err != nil {
return err
}
defer func() { _ = resp.Close() }()
// Capture logs. If tests are verbose (-v), also echo to stderr live.
var buf bytes.Buffer
out := io.Writer(&buf)
if testing.Verbose() {
out = io.MultiWriter(&buf, os.Stderr)
}
if err := build.ConsumeStream(resp, out); err != nil {
// Ensure logs are visible on failure even if logging not enabled.
if !testing.Verbose() {
_, _ = io.Copy(os.Stderr, &buf)
}
return err
}
return nil
}
var (
dockerfile = `FROM alpine:latest
RUN apk add --no-cache openssh lsb-release && \
ssh-keygen -A && \
mkdir -p /root/.ssh && \
chmod 700 /root/.ssh
ADD entrypoint.sh /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh", "sleep", "infinity" ]
`
entrypoint = `#!/bin/sh
mkdir "$HOME/.ssh"
echo "$AUTHORIZED_KEYS" > "$HOME/.ssh/authorized_keys"
/usr/sbin/sshd
exec "$@"
`
)
func prepareBuildContext() (r io.ReadCloser, err error) {
var buf bytes.Buffer
tarWriter := tar.NewWriter(&buf)
err = tarWriter.WriteHeader(&tar.Header{
Name: "Dockerfile",
Size: int64(len(dockerfile)),
Mode: 0o644,
Format: tar.FormatUSTAR,
})
if err != nil {
return nil, err
}
_, err = tarWriter.Write([]byte(dockerfile))
if err != nil {
return nil, err
}
err = tarWriter.WriteHeader(&tar.Header{
Name: "entrypoint.sh",
Size: int64(len(entrypoint)),
Mode: 0o755,
Format: tar.FormatUSTAR,
})
if err != nil {
return nil, err
}
_, err = tarWriter.Write([]byte(entrypoint))
if err != nil {
return nil, err
}
return io.NopCloser(&buf), nil
}