Просмотр исходного кода

Merge pull request #36166 from yongtang/01312018-TestAPIUpdateContainer

Migrate TestAPIUpdateContainer from integration-cli to api tests
Vincent Demeester 7 лет назад
Родитель
Сommit
5772c4b8a2

+ 0 - 45
integration-cli/docker_api_update_unix_test.go

@@ -1,45 +0,0 @@
-// +build !windows
-
-package main
-
-import (
-	"strings"
-
-	"github.com/docker/docker/api/types/container"
-	"github.com/docker/docker/client"
-	"github.com/docker/docker/integration-cli/checker"
-	"github.com/go-check/check"
-	"golang.org/x/net/context"
-)
-
-func (s *DockerSuite) TestAPIUpdateContainer(c *check.C) {
-	testRequires(c, DaemonIsLinux)
-	testRequires(c, memoryLimitSupport)
-	testRequires(c, swapMemorySupport)
-
-	name := "apiUpdateContainer"
-	updateConfig := container.UpdateConfig{
-		Resources: container.Resources{
-			Memory:     314572800,
-			MemorySwap: 524288000,
-		},
-	}
-	dockerCmd(c, "run", "-d", "--name", name, "-m", "200M", "busybox", "top")
-	cli, err := client.NewEnvClient()
-	c.Assert(err, check.IsNil)
-	defer cli.Close()
-
-	_, err = cli.ContainerUpdate(context.Background(), name, updateConfig)
-
-	c.Assert(err, check.IsNil)
-
-	c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "314572800")
-	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
-	out, _ := dockerCmd(c, "exec", name, "cat", file)
-	c.Assert(strings.TrimSpace(out), checker.Equals, "314572800")
-
-	c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "524288000")
-	file = "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
-	out, _ = dockerCmd(c, "exec", name, "cat", file)
-	c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
-}

+ 88 - 0
integration/container/update_linux_test.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"context"
 	"fmt"
+	"io/ioutil"
 	"strconv"
 	"strings"
 	"testing"
@@ -11,10 +12,67 @@ import (
 
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types/container"
+	"github.com/docker/docker/api/types/strslice"
+	"github.com/docker/docker/client"
 	"github.com/docker/docker/integration/util/request"
 	"github.com/docker/docker/pkg/stdcopy"
+	"github.com/gotestyourself/gotestyourself/poll"
+	"github.com/gotestyourself/gotestyourself/skip"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 )
 
+func TestUpdateMemory(t *testing.T) {
+	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
+	skip.If(t, !testEnv.DaemonInfo.MemoryLimit)
+	skip.If(t, !testEnv.DaemonInfo.SwapLimit)
+
+	defer setupTest(t)()
+	client := request.NewAPIClient(t)
+	ctx := context.Background()
+
+	c, err := client.ContainerCreate(ctx,
+		&container.Config{
+			Cmd:   []string{"top"},
+			Image: "busybox",
+		},
+		&container.HostConfig{
+			Resources: container.Resources{
+				Memory: 200 * 1024 * 1024,
+			},
+		},
+		nil,
+		"",
+	)
+	require.NoError(t, err)
+
+	err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
+	require.NoError(t, err)
+
+	poll.WaitOn(t, containerIsInState(ctx, client, c.ID, "running"), poll.WithDelay(100*time.Millisecond))
+
+	_, err = client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{
+		Resources: container.Resources{
+			Memory:     314572800,
+			MemorySwap: 524288000,
+		},
+	})
+	require.NoError(t, err)
+
+	inspect, err := client.ContainerInspect(ctx, c.ID)
+	require.NoError(t, err)
+	assert.Equal(t, inspect.HostConfig.Memory, int64(314572800))
+	assert.Equal(t, inspect.HostConfig.MemorySwap, int64(524288000))
+
+	body, err := getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.limit_in_bytes")
+	require.NoError(t, err)
+	assert.Equal(t, strings.TrimSpace(body), "314572800")
+
+	body, err = getContainerSysFSValue(ctx, client, c.ID, "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
+	require.NoError(t, err)
+	assert.Equal(t, strings.TrimSpace(body), "524288000")
+}
+
 func TestUpdateCPUQUota(t *testing.T) {
 	t.Parallel()
 
@@ -106,3 +164,33 @@ func TestUpdateCPUQUota(t *testing.T) {
 	}
 
 }
+
+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
+}