github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_cli_update_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/docker/docker/pkg/integration/checker"
    11  	"github.com/docker/engine-api/types"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func (s *DockerSuite) TestUpdateRunningContainer(c *check.C) {
    16  	testRequires(c, DaemonIsLinux)
    17  	testRequires(c, memoryLimitSupport)
    18  
    19  	name := "test-update-container"
    20  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
    21  	dockerCmd(c, "update", "-m", "500M", name)
    22  
    23  	c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "524288000")
    24  
    25  	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
    26  	out, _ := dockerCmd(c, "exec", name, "cat", file)
    27  	c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
    28  }
    29  
    30  func (s *DockerSuite) TestUpdateRunningContainerWithRestart(c *check.C) {
    31  	testRequires(c, DaemonIsLinux)
    32  	testRequires(c, memoryLimitSupport)
    33  
    34  	name := "test-update-container"
    35  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
    36  	dockerCmd(c, "update", "-m", "500M", name)
    37  	dockerCmd(c, "restart", name)
    38  
    39  	c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "524288000")
    40  
    41  	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
    42  	out, _ := dockerCmd(c, "exec", name, "cat", file)
    43  	c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
    44  }
    45  
    46  func (s *DockerSuite) TestUpdateStoppedContainer(c *check.C) {
    47  	testRequires(c, DaemonIsLinux)
    48  	testRequires(c, memoryLimitSupport)
    49  
    50  	name := "test-update-container"
    51  	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
    52  	dockerCmd(c, "run", "--name", name, "-m", "300M", "busybox", "cat", file)
    53  	dockerCmd(c, "update", "-m", "500M", name)
    54  
    55  	c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "524288000")
    56  
    57  	out, _ := dockerCmd(c, "start", "-a", name)
    58  	c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
    59  }
    60  
    61  func (s *DockerSuite) TestUpdatePausedContainer(c *check.C) {
    62  	testRequires(c, DaemonIsLinux)
    63  	testRequires(c, cpuShare)
    64  
    65  	name := "test-update-container"
    66  	dockerCmd(c, "run", "-d", "--name", name, "--cpu-shares", "1000", "busybox", "top")
    67  	dockerCmd(c, "pause", name)
    68  	dockerCmd(c, "update", "--cpu-shares", "500", name)
    69  
    70  	c.Assert(inspectField(c, name, "HostConfig.CPUShares"), checker.Equals, "500")
    71  
    72  	dockerCmd(c, "unpause", name)
    73  	file := "/sys/fs/cgroup/cpu/cpu.shares"
    74  	out, _ := dockerCmd(c, "exec", name, "cat", file)
    75  	c.Assert(strings.TrimSpace(out), checker.Equals, "500")
    76  }
    77  
    78  func (s *DockerSuite) TestUpdateWithUntouchedFields(c *check.C) {
    79  	testRequires(c, DaemonIsLinux)
    80  	testRequires(c, memoryLimitSupport)
    81  	testRequires(c, cpuShare)
    82  
    83  	name := "test-update-container"
    84  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "--cpu-shares", "800", "busybox", "top")
    85  	dockerCmd(c, "update", "-m", "500M", name)
    86  
    87  	// Update memory and not touch cpus, `cpuset.cpus` should still have the old value
    88  	out := inspectField(c, name, "HostConfig.CPUShares")
    89  	c.Assert(out, check.Equals, "800")
    90  
    91  	file := "/sys/fs/cgroup/cpu/cpu.shares"
    92  	out, _ = dockerCmd(c, "exec", name, "cat", file)
    93  	c.Assert(strings.TrimSpace(out), checker.Equals, "800")
    94  }
    95  
    96  func (s *DockerSuite) TestUpdateContainerInvalidValue(c *check.C) {
    97  	testRequires(c, DaemonIsLinux)
    98  	testRequires(c, memoryLimitSupport)
    99  
   100  	name := "test-update-container"
   101  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
   102  	out, _, err := dockerCmdWithError("update", "-m", "2M", name)
   103  	c.Assert(err, check.NotNil)
   104  	expected := "Minimum memory limit allowed is 4MB"
   105  	c.Assert(out, checker.Contains, expected)
   106  }
   107  
   108  func (s *DockerSuite) TestUpdateContainerWithoutFlags(c *check.C) {
   109  	testRequires(c, DaemonIsLinux)
   110  	testRequires(c, memoryLimitSupport)
   111  
   112  	name := "test-update-container"
   113  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
   114  	_, _, err := dockerCmdWithError("update", name)
   115  	c.Assert(err, check.NotNil)
   116  }
   117  
   118  func (s *DockerSuite) TestUpdateKernelMemory(c *check.C) {
   119  	testRequires(c, DaemonIsLinux, kernelMemorySupport)
   120  
   121  	name := "test-update-container"
   122  	dockerCmd(c, "run", "-d", "--name", name, "--kernel-memory", "50M", "busybox", "top")
   123  	_, _, err := dockerCmdWithError("update", "--kernel-memory", "100M", name)
   124  	// Update kernel memory to a running container is not allowed.
   125  	c.Assert(err, check.NotNil)
   126  
   127  	// Update kernel memory to a running container with failure should not change HostConfig
   128  	c.Assert(inspectField(c, name, "HostConfig.KernelMemory"), checker.Equals, "52428800")
   129  
   130  	dockerCmd(c, "pause", name)
   131  	_, _, err = dockerCmdWithError("update", "--kernel-memory", "100M", name)
   132  	c.Assert(err, check.NotNil)
   133  	c.Assert(inspectField(c, name, "HostConfig.KernelMemory"), checker.Equals, "52428800")
   134  	dockerCmd(c, "unpause", name)
   135  
   136  	dockerCmd(c, "stop", name)
   137  	dockerCmd(c, "update", "--kernel-memory", "100M", name)
   138  	dockerCmd(c, "start", name)
   139  
   140  	c.Assert(inspectField(c, name, "HostConfig.KernelMemory"), checker.Equals, "104857600")
   141  
   142  	file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
   143  	out, _ := dockerCmd(c, "exec", name, "cat", file)
   144  	c.Assert(strings.TrimSpace(out), checker.Equals, "104857600")
   145  }
   146  
   147  func (s *DockerSuite) TestUpdateSwapMemoryOnly(c *check.C) {
   148  	testRequires(c, DaemonIsLinux)
   149  	testRequires(c, memoryLimitSupport)
   150  	testRequires(c, swapMemorySupport)
   151  
   152  	name := "test-update-container"
   153  	dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
   154  	dockerCmd(c, "update", "--memory-swap", "600M", name)
   155  
   156  	c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "629145600")
   157  
   158  	file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
   159  	out, _ := dockerCmd(c, "exec", name, "cat", file)
   160  	c.Assert(strings.TrimSpace(out), checker.Equals, "629145600")
   161  }
   162  
   163  func (s *DockerSuite) TestUpdateInvalidSwapMemory(c *check.C) {
   164  	testRequires(c, DaemonIsLinux)
   165  	testRequires(c, memoryLimitSupport)
   166  	testRequires(c, swapMemorySupport)
   167  
   168  	name := "test-update-container"
   169  	dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
   170  	_, _, err := dockerCmdWithError("update", "--memory-swap", "200M", name)
   171  	// Update invalid swap memory should fail.
   172  	// This will pass docker config validation, but failed at kernel validation
   173  	c.Assert(err, check.NotNil)
   174  
   175  	// Update invalid swap memory with failure should not change HostConfig
   176  	c.Assert(inspectField(c, name, "HostConfig.Memory"), checker.Equals, "314572800")
   177  	c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "524288000")
   178  
   179  	dockerCmd(c, "update", "--memory-swap", "600M", name)
   180  
   181  	c.Assert(inspectField(c, name, "HostConfig.MemorySwap"), checker.Equals, "629145600")
   182  
   183  	file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
   184  	out, _ := dockerCmd(c, "exec", name, "cat", file)
   185  	c.Assert(strings.TrimSpace(out), checker.Equals, "629145600")
   186  }
   187  
   188  func (s *DockerSuite) TestUpdateStats(c *check.C) {
   189  	testRequires(c, DaemonIsLinux)
   190  	testRequires(c, memoryLimitSupport)
   191  	testRequires(c, cpuCfsQuota)
   192  	name := "foo"
   193  	dockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
   194  
   195  	c.Assert(waitRun(name), checker.IsNil)
   196  
   197  	getMemLimit := func(id string) uint64 {
   198  		resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
   199  		c.Assert(err, checker.IsNil)
   200  		c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
   201  
   202  		var v *types.Stats
   203  		err = json.NewDecoder(body).Decode(&v)
   204  		c.Assert(err, checker.IsNil)
   205  		body.Close()
   206  
   207  		return v.MemoryStats.Limit
   208  	}
   209  	preMemLimit := getMemLimit(name)
   210  
   211  	dockerCmd(c, "update", "--cpu-quota", "2000", name)
   212  
   213  	curMemLimit := getMemLimit(name)
   214  
   215  	c.Assert(preMemLimit, checker.Equals, curMemLimit)
   216  
   217  }