Browse code

integration/TestUpdateMemory: simplify

1. Use integration/internal/exec, removing the getContainerSysFSValue().

2. Avoid repeating magic numbers, use a variable for those.

3. Fix order of arguments to assert.Equal (first "expected", then "actual").

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

Kir Kolyshkin authored on 2018/02/16 04:59:53
Showing 1 changed files
... ...
@@ -1,21 +1,15 @@
1 1
 package container // import "github.com/docker/docker/integration/container"
2 2
 
3 3
 import (
4
-	"bytes"
5 4
 	"context"
6
-	"io/ioutil"
7 5
 	"strconv"
8 6
 	"strings"
9 7
 	"testing"
10 8
 	"time"
11 9
 
12
-	"github.com/docker/docker/api/types"
13 10
 	containertypes "github.com/docker/docker/api/types/container"
14
-	"github.com/docker/docker/api/types/strslice"
15
-	"github.com/docker/docker/client"
16 11
 	"github.com/docker/docker/integration/internal/container"
17 12
 	"github.com/docker/docker/integration/internal/request"
18
-	"github.com/docker/docker/pkg/stdcopy"
19 13
 	"github.com/gotestyourself/gotestyourself/poll"
20 14
 	"github.com/gotestyourself/gotestyourself/skip"
21 15
 	"github.com/stretchr/testify/assert"
... ...
@@ -39,26 +33,37 @@ func TestUpdateMemory(t *testing.T) {
39 39
 
40 40
 	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
41 41
 
42
+	const (
43
+		setMemory     int64 = 314572800
44
+		setMemorySwap       = 524288000
45
+	)
46
+
42 47
 	_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
43 48
 		Resources: containertypes.Resources{
44
-			Memory:     314572800,
45
-			MemorySwap: 524288000,
49
+			Memory:     setMemory,
50
+			MemorySwap: setMemorySwap,
46 51
 		},
47 52
 	})
48 53
 	require.NoError(t, err)
49 54
 
50 55
 	inspect, err := client.ContainerInspect(ctx, cID)
51 56
 	require.NoError(t, err)
52
-	assert.Equal(t, inspect.HostConfig.Memory, int64(314572800))
53
-	assert.Equal(t, inspect.HostConfig.MemorySwap, int64(524288000))
57
+	assert.Equal(t, setMemory, inspect.HostConfig.Memory)
58
+	assert.Equal(t, setMemorySwap, inspect.HostConfig.MemorySwap)
54 59
 
55
-	body, err := getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
60
+	res, err := container.Exec(ctx, client, cID,
61
+		[]string{"cat", "/sys/fs/cgroup/memory/memory.limit_in_bytes"})
56 62
 	require.NoError(t, err)
57
-	assert.Equal(t, strings.TrimSpace(body), "314572800")
63
+	require.Empty(t, res.Stderr())
64
+	require.Equal(t, 0, res.ExitCode)
65
+	assert.Equal(t, strconv.FormatInt(setMemory, 10), strings.TrimSpace(res.Stdout()))
58 66
 
59
-	body, err = getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
67
+	res, err = container.Exec(ctx, client, cID,
68
+		[]string{"cat", "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"})
60 69
 	require.NoError(t, err)
61
-	assert.Equal(t, strings.TrimSpace(body), "524288000")
70
+	require.Empty(t, res.Stderr())
71
+	require.Equal(t, 0, res.ExitCode)
72
+	assert.Equal(t, strconv.FormatInt(setMemorySwap, 10), strings.TrimSpace(res.Stdout()))
62 73
 }
63 74
 
64 75
 func TestUpdateCPUQUota(t *testing.T) {
... ...
@@ -100,33 +105,3 @@ func TestUpdateCPUQUota(t *testing.T) {
100 100
 		assert.Equal(t, strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout()))
101 101
 	}
102 102
 }
103
-
104
-func getContainerSysFSValue(ctx context.Context, client client.APIClient, cID string, path string) (string, error) {
105
-	var b bytes.Buffer
106
-
107
-	ex, err := client.ContainerExecCreate(ctx, cID,
108
-		types.ExecConfig{
109
-			AttachStdout: true,
110
-			Cmd:          strslice.StrSlice([]string{"cat", path}),
111
-		},
112
-	)
113
-	if err != nil {
114
-		return "", err
115
-	}
116
-
117
-	resp, err := client.ContainerExecAttach(ctx, ex.ID,
118
-		types.ExecStartCheck{
119
-			Detach: false,
120
-			Tty:    false,
121
-		},
122
-	)
123
-	if err != nil {
124
-		return "", err
125
-	}
126
-
127
-	defer resp.Close()
128
-
129
-	b.Reset()
130
-	_, err = stdcopy.StdCopy(&b, ioutil.Discard, resp.Reader)
131
-	return b.String(), err
132
-}