forked from xplorfin/docker-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
61 lines (47 loc) · 1.38 KB
/
container_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
package docker
import (
"testing"
"github.com/brianvoe/gofakeit/v5"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
. "github.com/stretchr/testify/assert"
)
type TestClient struct {
Client
}
func (c TestClient) CreateTestContainer(network string) (id string, err error) {
created, err := c.CreateContainer(&container.Config{
Image: "docker.io/library/alpine",
Tty: false,
}, &container.HostConfig{
NetworkMode: container.NetworkMode(network),
}, nil, nil, "")
if err != nil {
return id, err
}
err = c.ContainerStart(c.Ctx, created.ID, types.ContainerStartOptions{})
if err != nil {
return id, err
}
c.PrintContainerLogs(created.ID)
return created.ID, err
}
func TestContainerLifecycle(t *testing.T) {
client := TestClient{NewDockerClient()}
// we run this twice to ensure teardown occurred correctly
containerNetworkName := gofakeit.Word()
containerOutput := gofakeit.Word()
networkID, err := client.CreateNetwork(containerNetworkName)
Nil(t, err)
id, err := client.CreateTestContainer(networkID)
Nil(t, err)
res, err := client.Exec(id, []string{"echo", containerOutput})
Nil(t, err)
if res.ExitCode != 0 && res.StdErr != "" && res.StdOut != containerOutput {
t.Errorf("expeceted containerOutput to equal %s", "hi")
}
// TODO verify
_, err = client.ContainerStatus(id)
Nil(t, err)
Nil(t, client.TeardownSession())
}