Browse code

test case to check « exec » works as expected

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>

Nicolas De Loof authored on 2017/12/03 18:56:50
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,60 @@
0
+package container
1
+
2
+import (
3
+	"context"
4
+	"io/ioutil"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/api/types"
8
+	"github.com/docker/docker/api/types/container"
9
+	"github.com/docker/docker/api/types/network"
10
+	"github.com/docker/docker/api/types/strslice"
11
+	"github.com/docker/docker/integration/util/request"
12
+	"github.com/stretchr/testify/require"
13
+)
14
+
15
+func TestExec(t *testing.T) {
16
+	defer setupTest(t)()
17
+	ctx := context.Background()
18
+	client := request.NewAPIClient(t)
19
+
20
+	container, err := client.ContainerCreate(ctx,
21
+		&container.Config{
22
+			Image:      "busybox",
23
+			Tty:        true,
24
+			WorkingDir: "/root",
25
+			Cmd:        strslice.StrSlice([]string{"top"}),
26
+		},
27
+		&container.HostConfig{},
28
+		&network.NetworkingConfig{},
29
+		"foo",
30
+	)
31
+	require.NoError(t, err)
32
+	err = client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{})
33
+	require.NoError(t, err)
34
+
35
+	id, err := client.ContainerExecCreate(ctx, container.ID,
36
+		types.ExecConfig{
37
+			WorkingDir:   "/tmp",
38
+			Env:          strslice.StrSlice([]string{"FOO=BAR"}),
39
+			AttachStdout: true,
40
+			Cmd:          strslice.StrSlice([]string{"sh", "-c", "env"}),
41
+		},
42
+	)
43
+	require.NoError(t, err)
44
+
45
+	resp, err := client.ContainerExecAttach(ctx, id.ID,
46
+		types.ExecStartCheck{
47
+			Detach: false,
48
+			Tty:    false,
49
+		},
50
+	)
51
+	require.NoError(t, err)
52
+	defer resp.Close()
53
+	r, err := ioutil.ReadAll(resp.Reader)
54
+	require.NoError(t, err)
55
+	out := string(r)
56
+	require.NoError(t, err)
57
+	require.Contains(t, out, "PWD=/tmp", "exec command not running in expected /tmp working directory")
58
+	require.Contains(t, out, "FOO=BAR", "exec command not running with expected environment variable FOO")
59
+}