github.com/ruphin/docker@v1.10.1/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  	memory, err := inspectField(name, "HostConfig.Memory")
    24  	c.Assert(err, check.IsNil)
    25  	if memory != "524288000" {
    26  		c.Fatalf("Got the wrong memory value, we got %d, expected 524288000(500M).", memory)
    27  	}
    28  
    29  	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
    30  	out, _ := dockerCmd(c, "exec", name, "cat", file)
    31  	c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
    32  }
    33  
    34  func (s *DockerSuite) TestUpdateRunningContainerWithRestart(c *check.C) {
    35  	testRequires(c, DaemonIsLinux)
    36  	testRequires(c, memoryLimitSupport)
    37  
    38  	name := "test-update-container"
    39  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
    40  	dockerCmd(c, "update", "-m", "500M", name)
    41  	dockerCmd(c, "restart", name)
    42  
    43  	memory, err := inspectField(name, "HostConfig.Memory")
    44  	c.Assert(err, check.IsNil)
    45  	if memory != "524288000" {
    46  		c.Fatalf("Got the wrong memory value, we got %d, expected 524288000(500M).", memory)
    47  	}
    48  
    49  	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
    50  	out, _ := dockerCmd(c, "exec", name, "cat", file)
    51  	c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
    52  }
    53  
    54  func (s *DockerSuite) TestUpdateStoppedContainer(c *check.C) {
    55  	testRequires(c, DaemonIsLinux)
    56  	testRequires(c, memoryLimitSupport)
    57  
    58  	name := "test-update-container"
    59  	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
    60  	dockerCmd(c, "run", "--name", name, "-m", "300M", "busybox", "cat", file)
    61  	dockerCmd(c, "update", "-m", "500M", name)
    62  
    63  	memory, err := inspectField(name, "HostConfig.Memory")
    64  	c.Assert(err, check.IsNil)
    65  	if memory != "524288000" {
    66  		c.Fatalf("Got the wrong memory value, we got %d, expected 524288000(500M).", memory)
    67  	}
    68  
    69  	out, _ := dockerCmd(c, "start", "-a", name)
    70  	c.Assert(strings.TrimSpace(out), checker.Equals, "524288000")
    71  }
    72  
    73  func (s *DockerSuite) TestUpdatePausedContainer(c *check.C) {
    74  	testRequires(c, DaemonIsLinux)
    75  	testRequires(c, cpuShare)
    76  
    77  	name := "test-update-container"
    78  	dockerCmd(c, "run", "-d", "--name", name, "--cpu-shares", "1000", "busybox", "top")
    79  	dockerCmd(c, "pause", name)
    80  	dockerCmd(c, "update", "--cpu-shares", "500", name)
    81  
    82  	out, err := inspectField(name, "HostConfig.CPUShares")
    83  	c.Assert(err, check.IsNil)
    84  	if out != "500" {
    85  		c.Fatalf("Got the wrong cpu shares value, we got %d, expected 500.", out)
    86  	}
    87  
    88  	dockerCmd(c, "unpause", name)
    89  	file := "/sys/fs/cgroup/cpu/cpu.shares"
    90  	out, _ = dockerCmd(c, "exec", name, "cat", file)
    91  	c.Assert(strings.TrimSpace(out), checker.Equals, "500")
    92  }
    93  
    94  func (s *DockerSuite) TestUpdateWithUntouchedFields(c *check.C) {
    95  	testRequires(c, DaemonIsLinux)
    96  	testRequires(c, memoryLimitSupport)
    97  	testRequires(c, cpuShare)
    98  
    99  	name := "test-update-container"
   100  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "--cpu-shares", "800", "busybox", "top")
   101  	dockerCmd(c, "update", "-m", "500M", name)
   102  
   103  	// Update memory and not touch cpus, `cpuset.cpus` should still have the old value
   104  	out, err := inspectField(name, "HostConfig.CPUShares")
   105  	c.Assert(err, check.IsNil)
   106  	c.Assert(out, check.Equals, "800")
   107  
   108  	file := "/sys/fs/cgroup/cpu/cpu.shares"
   109  	out, _ = dockerCmd(c, "exec", name, "cat", file)
   110  	c.Assert(strings.TrimSpace(out), checker.Equals, "800")
   111  }
   112  
   113  func (s *DockerSuite) TestUpdateContainerInvalidValue(c *check.C) {
   114  	testRequires(c, DaemonIsLinux)
   115  	testRequires(c, memoryLimitSupport)
   116  
   117  	name := "test-update-container"
   118  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
   119  	out, _, err := dockerCmdWithError("update", "-m", "2M", name)
   120  	c.Assert(err, check.NotNil)
   121  	expected := "Minimum memory limit allowed is 4MB"
   122  	c.Assert(out, checker.Contains, expected)
   123  }
   124  
   125  func (s *DockerSuite) TestUpdateContainerWithoutFlags(c *check.C) {
   126  	testRequires(c, DaemonIsLinux)
   127  	testRequires(c, memoryLimitSupport)
   128  
   129  	name := "test-update-container"
   130  	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
   131  	_, _, err := dockerCmdWithError("update", name)
   132  	c.Assert(err, check.NotNil)
   133  }
   134  
   135  func (s *DockerSuite) TestUpdateKernelMemory(c *check.C) {
   136  	testRequires(c, DaemonIsLinux)
   137  	testRequires(c, kernelMemorySupport)
   138  
   139  	name := "test-update-container"
   140  	dockerCmd(c, "run", "-d", "--name", name, "--kernel-memory", "50M", "busybox", "top")
   141  	_, _, err := dockerCmdWithError("update", "--kernel-memory", "100M", name)
   142  	// Update kernel memory to a running container is not allowed.
   143  	c.Assert(err, check.NotNil)
   144  
   145  	out, err := inspectField(name, "HostConfig.KernelMemory")
   146  	c.Assert(err, check.IsNil)
   147  	// Update kernel memory to a running container with failure should not change HostConfig
   148  	if out != "52428800" {
   149  		c.Fatalf("Got the wrong memory value, we got %d, expected 52428800(50M).", out)
   150  	}
   151  
   152  	dockerCmd(c, "stop", name)
   153  	dockerCmd(c, "update", "--kernel-memory", "100M", name)
   154  	dockerCmd(c, "start", name)
   155  
   156  	out, err = inspectField(name, "HostConfig.KernelMemory")
   157  	c.Assert(err, check.IsNil)
   158  	if out != "104857600" {
   159  		c.Fatalf("Got the wrong memory value, we got %d, expected 104857600(100M).", out)
   160  	}
   161  
   162  	file := "/sys/fs/cgroup/memory/memory.kmem.limit_in_bytes"
   163  	out, _ = dockerCmd(c, "exec", name, "cat", file)
   164  	c.Assert(strings.TrimSpace(out), checker.Equals, "104857600")
   165  }
   166  
   167  func (s *DockerSuite) TestUpdateStats(c *check.C) {
   168  	testRequires(c, DaemonIsLinux)
   169  	testRequires(c, memoryLimitSupport)
   170  	testRequires(c, cpuCfsQuota)
   171  	name := "foo"
   172  	dockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
   173  
   174  	c.Assert(waitRun(name), checker.IsNil)
   175  
   176  	getMemLimit := func(id string) uint64 {
   177  		resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
   178  		c.Assert(err, checker.IsNil)
   179  		c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
   180  
   181  		var v *types.Stats
   182  		err = json.NewDecoder(body).Decode(&v)
   183  		c.Assert(err, checker.IsNil)
   184  		body.Close()
   185  
   186  		return v.MemoryStats.Limit
   187  	}
   188  	preMemLimit := getMemLimit(name)
   189  
   190  	dockerCmd(c, "update", "--cpu-quota", "2000", name)
   191  
   192  	curMemLimit := getMemLimit(name)
   193  
   194  	c.Assert(preMemLimit, checker.Equals, curMemLimit)
   195  
   196  }