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>
This commit is contained in:
Kir Kolyshkin 2018-02-15 11:59:53 -08:00
parent 8a7d6143fc
commit 0f9da07b56
1 changed files with 19 additions and 44 deletions

View File

@ -1,21 +1,15 @@
package container // import "github.com/docker/docker/integration/container" package container // import "github.com/docker/docker/integration/container"
import ( import (
"bytes"
"context" "context"
"io/ioutil"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container" containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/integration/internal/request" "github.com/docker/docker/integration/internal/request"
"github.com/docker/docker/pkg/stdcopy"
"github.com/gotestyourself/gotestyourself/poll" "github.com/gotestyourself/gotestyourself/poll"
"github.com/gotestyourself/gotestyourself/skip" "github.com/gotestyourself/gotestyourself/skip"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -39,26 +33,37 @@ func TestUpdateMemory(t *testing.T) {
poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond)) poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
const (
setMemory int64 = 314572800
setMemorySwap = 524288000
)
_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{ _, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
Resources: containertypes.Resources{ Resources: containertypes.Resources{
Memory: 314572800, Memory: setMemory,
MemorySwap: 524288000, MemorySwap: setMemorySwap,
}, },
}) })
require.NoError(t, err) require.NoError(t, err)
inspect, err := client.ContainerInspect(ctx, cID) inspect, err := client.ContainerInspect(ctx, cID)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, inspect.HostConfig.Memory, int64(314572800)) assert.Equal(t, setMemory, inspect.HostConfig.Memory)
assert.Equal(t, inspect.HostConfig.MemorySwap, int64(524288000)) assert.Equal(t, setMemorySwap, inspect.HostConfig.MemorySwap)
body, err := getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.limit_in_bytes") res, err := container.Exec(ctx, client, cID,
[]string{"cat", "/sys/fs/cgroup/memory/memory.limit_in_bytes"})
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, strings.TrimSpace(body), "314572800") require.Empty(t, res.Stderr())
require.Equal(t, 0, res.ExitCode)
assert.Equal(t, strconv.FormatInt(setMemory, 10), strings.TrimSpace(res.Stdout()))
body, err = getContainerSysFSValue(ctx, client, cID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes") res, err = container.Exec(ctx, client, cID,
[]string{"cat", "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"})
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, strings.TrimSpace(body), "524288000") require.Empty(t, res.Stderr())
require.Equal(t, 0, res.ExitCode)
assert.Equal(t, strconv.FormatInt(setMemorySwap, 10), strings.TrimSpace(res.Stdout()))
} }
func TestUpdateCPUQUota(t *testing.T) { func TestUpdateCPUQUota(t *testing.T) {
@ -100,33 +105,3 @@ func TestUpdateCPUQUota(t *testing.T) {
assert.Equal(t, strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout())) assert.Equal(t, strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout()))
} }
} }
func getContainerSysFSValue(ctx context.Context, client client.APIClient, cID string, path string) (string, error) {
var b bytes.Buffer
ex, err := client.ContainerExecCreate(ctx, cID,
types.ExecConfig{
AttachStdout: true,
Cmd: strslice.StrSlice([]string{"cat", path}),
},
)
if err != nil {
return "", err
}
resp, err := client.ContainerExecAttach(ctx, ex.ID,
types.ExecStartCheck{
Detach: false,
Tty: false,
},
)
if err != nil {
return "", err
}
defer resp.Close()
b.Reset()
_, err = stdcopy.StdCopy(&b, ioutil.Discard, resp.Reader)
return b.String(), err
}