Browse code

Migrate TestAPIUpdateContainer from integration-cli to api tests

This fix migrates TestAPIUpdateContainer from integration-cli to api tests

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/02/01 01:10:41
Showing 1 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"bytes"
5 5
 	"context"
6 6
 	"fmt"
7
+	"io/ioutil"
7 8
 	"strconv"
8 9
 	"strings"
9 10
 	"testing"
... ...
@@ -11,10 +12,67 @@ import (
11 11
 
12 12
 	"github.com/docker/docker/api/types"
13 13
 	"github.com/docker/docker/api/types/container"
14
+	"github.com/docker/docker/api/types/strslice"
15
+	"github.com/docker/docker/client"
14 16
 	"github.com/docker/docker/integration/util/request"
15 17
 	"github.com/docker/docker/pkg/stdcopy"
18
+	"github.com/gotestyourself/gotestyourself/poll"
19
+	"github.com/gotestyourself/gotestyourself/skip"
20
+	"github.com/stretchr/testify/assert"
21
+	"github.com/stretchr/testify/require"
16 22
 )
17 23
 
24
+func TestUpdateMemory(t *testing.T) {
25
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
26
+	skip.If(t, !testEnv.DaemonInfo.MemoryLimit)
27
+	skip.If(t, !testEnv.DaemonInfo.SwapLimit)
28
+
29
+	defer setupTest(t)()
30
+	client := request.NewAPIClient(t)
31
+	ctx := context.Background()
32
+
33
+	c, err := client.ContainerCreate(ctx,
34
+		&container.Config{
35
+			Cmd:   []string{"top"},
36
+			Image: "busybox",
37
+		},
38
+		&container.HostConfig{
39
+			Resources: container.Resources{
40
+				Memory: 200 * 1024 * 1024,
41
+			},
42
+		},
43
+		nil,
44
+		"",
45
+	)
46
+	require.NoError(t, err)
47
+
48
+	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
49
+	require.NoError(t, err)
50
+
51
+	poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
52
+
53
+	_, err = client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{
54
+		Resources: container.Resources{
55
+			Memory:     314572800,
56
+			MemorySwap: 524288000,
57
+		},
58
+	})
59
+	require.NoError(t, err)
60
+
61
+	inspect, err := client.ContainerInspect(ctx, c.ID)
62
+	require.NoError(t, err)
63
+	assert.Equal(t, inspect.HostConfig.Memory, int64(314572800))
64
+	assert.Equal(t, inspect.HostConfig.MemorySwap, int64(524288000))
65
+
66
+	body, err := getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
67
+	require.NoError(t, err)
68
+	assert.Equal(t, strings.TrimSpace(body), "314572800")
69
+
70
+	body, err = getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
71
+	require.NoError(t, err)
72
+	assert.Equal(t, strings.TrimSpace(body), "524288000")
73
+}
74
+
18 75
 func TestUpdateCPUQUota(t *testing.T) {
19 76
 	t.Parallel()
20 77
 
... ...
@@ -106,3 +164,33 @@ func TestUpdateCPUQUota(t *testing.T) {
106 106
 	}
107 107
 
108 108
 }
109
+
110
+func getContainerSysFSValue(ctx context.Context, client client.APIClient, cID string, path string) (string, error) {
111
+	var b bytes.Buffer
112
+
113
+	ex, err := client.ContainerExecCreate(ctx, cID,
114
+		types.ExecConfig{
115
+			AttachStdout: true,
116
+			Cmd:          strslice.StrSlice([]string{"cat", path}),
117
+		},
118
+	)
119
+	if err != nil {
120
+		return "", err
121
+	}
122
+
123
+	resp, err := client.ContainerExecAttach(ctx, ex.ID,
124
+		types.ExecStartCheck{
125
+			Detach: false,
126
+			Tty:    false,
127
+		},
128
+	)
129
+	if err != nil {
130
+		return "", err
131
+	}
132
+
133
+	defer resp.Close()
134
+
135
+	b.Reset()
136
+	_, err = stdcopy.StdCopy(&b, ioutil.Discard, resp.Reader)
137
+	return b.String(), err
138
+}