From c340ca4f5db9ab526532f26718a140654ff45458 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Mon, 24 Aug 2015 11:37:11 +0800 Subject: [PATCH] Add some missing cgroup test require and refactor the require check. Signed-off-by: Lei Jitang --- integration-cli/docker_cli_run_test.go | 73 ----------------- integration-cli/docker_cli_run_unix_test.go | 86 ++++++++++++++++++++ integration-cli/requirements_unix.go | 88 +++++++++++---------- 3 files changed, 133 insertions(+), 114 deletions(-) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 8ff64bba17..19252f78ab 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -31,49 +31,6 @@ func (s *DockerSuite) TestRunEchoStdout(c *check.C) { } } -// "test" should be printed -func (s *DockerSuite) TestRunEchoStdoutWithMemoryLimit(c *check.C) { - out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-m", "16m", "busybox", "echo", "test") - out = strings.Trim(out, "\r\n") - - if expected := "test"; out != expected { - c.Fatalf("container should've printed %q but printed %q", expected, out) - } -} - -// should run without memory swap -func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) { - testRequires(c, NativeExecDriver) - dockerCmd(c, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true") -} - -func (s *DockerSuite) TestRunWithSwappiness(c *check.C) { - dockerCmd(c, "run", "--memory-swappiness", "0", "busybox", "true") -} - -func (s *DockerSuite) TestRunWithSwappinessInvalid(c *check.C) { - out, _, err := dockerCmdWithError("run", "--memory-swappiness", "101", "busybox", "true") - if err == nil { - c.Fatalf("failed. test was able to set invalid value, output: %q", out) - } -} - -// "test" should be printed -func (s *DockerSuite) TestRunEchoStdoutWitCPULimit(c *check.C) { - out, _ := dockerCmd(c, "run", "-c", "1000", "busybox", "echo", "test") - if out != "test\n" { - c.Errorf("container should've printed 'test'") - } -} - -// "test" should be printed -func (s *DockerSuite) TestRunEchoStdoutWithCPUAndMemoryLimit(c *check.C) { - out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-c", "1000", "-m", "16m", "busybox", "echo", "test") - if out != "test\n" { - c.Errorf("container should've printed 'test', got %q instead", out) - } -} - // "test" should be printed func (s *DockerSuite) TestRunEchoNamedContainer(c *check.C) { out, _ := dockerCmd(c, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test") @@ -797,36 +754,6 @@ func (s *DockerSuite) TestRunProcWritableInPrivilegedContainers(c *check.C) { } } -func (s *DockerSuite) TestRunWithCpuset(c *check.C) { - if _, code := dockerCmd(c, "run", "--cpuset", "0", "busybox", "true"); code != 0 { - c.Fatalf("container should run successfully with cpuset of 0") - } -} - -func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) { - if _, code := dockerCmd(c, "run", "--cpuset-cpus", "0", "busybox", "true"); code != 0 { - c.Fatalf("container should run successfully with cpuset-cpus of 0") - } -} - -func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) { - if _, code := dockerCmd(c, "run", "--cpuset-mems", "0", "busybox", "true"); code != 0 { - c.Fatalf("container should run successfully with cpuset-mems of 0") - } -} - -func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) { - if _, code := dockerCmd(c, "run", "--blkio-weight", "300", "busybox", "true"); code != 0 { - c.Fatalf("container should run successfully with blkio-weight of 300") - } -} - -func (s *DockerSuite) TestRunWithBlkioInvalidWeight(c *check.C) { - if _, _, err := dockerCmdWithError("run", "--blkio-weight", "5", "busybox", "true"); err == nil { - c.Fatalf("run with invalid blkio-weight should failed") - } -} - func (s *DockerSuite) TestRunDeviceNumbers(c *check.C) { out, _ := dockerCmd(c, "run", "busybox", "sh", "-c", "ls -l /dev/null") deviceLineFields := strings.Fields(out) diff --git a/integration-cli/docker_cli_run_unix_test.go b/integration-cli/docker_cli_run_unix_test.go index 5c24cdc87a..6215a58057 100644 --- a/integration-cli/docker_cli_run_unix_test.go +++ b/integration-cli/docker_cli_run_unix_test.go @@ -293,6 +293,60 @@ func (s *DockerSuite) TestRunWithKernelMemory(c *check.C) { } } +// "test" should be printed +func (s *DockerSuite) TestRunEchoStdoutWitCPUShares(c *check.C) { + testRequires(c, cpuShare) + out, _ := dockerCmd(c, "run", "-c", "1000", "busybox", "echo", "test") + if out != "test\n" { + c.Errorf("container should've printed 'test'") + } +} + +// "test" should be printed +func (s *DockerSuite) TestRunEchoStdoutWithCPUSharesAndMemoryLimit(c *check.C) { + testRequires(c, cpuShare) + testRequires(c, memoryLimitSupport) + out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-c", "1000", "-m", "16m", "busybox", "echo", "test") + if out != "test\n" { + c.Errorf("container should've printed 'test', got %q instead", out) + } +} + +func (s *DockerSuite) TestRunWithCpuset(c *check.C) { + testRequires(c, cgroupCpuset) + if _, code := dockerCmd(c, "run", "--cpuset", "0", "busybox", "true"); code != 0 { + c.Fatalf("container should run successfully with cpuset of 0") + } +} + +func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) { + testRequires(c, cgroupCpuset) + if _, code := dockerCmd(c, "run", "--cpuset-cpus", "0", "busybox", "true"); code != 0 { + c.Fatalf("container should run successfully with cpuset-cpus of 0") + } +} + +func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) { + testRequires(c, cgroupCpuset) + if _, code := dockerCmd(c, "run", "--cpuset-mems", "0", "busybox", "true"); code != 0 { + c.Fatalf("container should run successfully with cpuset-mems of 0") + } +} + +func (s *DockerSuite) TestRunWithBlkioWeight(c *check.C) { + testRequires(c, blkioWeight) + if _, code := dockerCmd(c, "run", "--blkio-weight", "300", "busybox", "true"); code != 0 { + c.Fatalf("container should run successfully with blkio-weight of 300") + } +} + +func (s *DockerSuite) TestRunWithBlkioInvalidWeight(c *check.C) { + testRequires(c, blkioWeight) + if _, _, err := dockerCmdWithError("run", "--blkio-weight", "5", "busybox", "true"); err == nil { + c.Fatalf("run with invalid blkio-weight should failed") + } +} + func (s *DockerSuite) TestRunOOMExitCode(c *check.C) { testRequires(c, oomControl) errChan := make(chan error) @@ -424,3 +478,35 @@ func (s *DockerSuite) TestTwoContainersInNetHost(c *check.C) { dockerCmd(c, "stop", "first") dockerCmd(c, "stop", "second") } + +// "test" should be printed +func (s *DockerSuite) TestRunEchoStdoutWithMemoryLimit(c *check.C) { + testRequires(c, memoryLimitSupport) + out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-m", "16m", "busybox", "echo", "test") + out = strings.Trim(out, "\r\n") + + if expected := "test"; out != expected { + c.Fatalf("container should've printed %q but printed %q", expected, out) + } +} + +// should run without memory swap +func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) { + testRequires(c, NativeExecDriver) + testRequires(c, memoryLimitSupport) + testRequires(c, swapMemorySupport) + dockerCmd(c, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true") +} + +func (s *DockerSuite) TestRunWithSwappiness(c *check.C) { + testRequires(c, memorySwappinessSupport) + dockerCmd(c, "run", "--memory-swappiness", "0", "busybox", "true") +} + +func (s *DockerSuite) TestRunWithSwappinessInvalid(c *check.C) { + testRequires(c, memorySwappinessSupport) + out, _, err := dockerCmdWithError("run", "--memory-swappiness", "101", "busybox", "true") + if err == nil { + c.Fatalf("failed. test was able to set invalid value, output: %q", out) + } +} diff --git a/integration-cli/requirements_unix.go b/integration-cli/requirements_unix.go index f11675ef23..aee5207fa9 100644 --- a/integration-cli/requirements_unix.go +++ b/integration-cli/requirements_unix.go @@ -3,68 +3,74 @@ package main import ( - "io/ioutil" - "path" - - "github.com/opencontainers/runc/libcontainer/cgroups" + "github.com/docker/docker/pkg/sysinfo" ) var ( + // SysInfo stores information about which features a kernel supports. + SysInfo *sysinfo.SysInfo cpuCfsPeriod = testRequirement{ func() bool { - cgroupCPUMountpoint, err := cgroups.FindCgroupMountpoint("cpu") - if err != nil { - return false - } - if _, err := ioutil.ReadFile(path.Join(cgroupCPUMountpoint, "cpu.cfs_period_us")); err != nil { - return false - } - return true + return SysInfo.CPUCfsPeriod }, "Test requires an environment that supports cgroup cfs period.", } cpuCfsQuota = testRequirement{ func() bool { - cgroupCPUMountpoint, err := cgroups.FindCgroupMountpoint("cpu") - if err != nil { - return false - } - if _, err := ioutil.ReadFile(path.Join(cgroupCPUMountpoint, "cpu.cfs_quota_us")); err != nil { - return false - } - return true + return SysInfo.CPUCfsQuota }, "Test requires an environment that supports cgroup cfs quota.", } + cpuShare = testRequirement{ + func() bool { + return SysInfo.CPUShares + }, + "Test requires an environment that supports cgroup cpu shares.", + } oomControl = testRequirement{ func() bool { - cgroupMemoryMountpoint, err := cgroups.FindCgroupMountpoint("memory") - if err != nil { - return false - } - if _, err := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes")); err != nil { - return false - } - - if _, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.oom_control")); err != nil { - return false - } - return true - + return SysInfo.OomKillDisable }, "Test requires Oom control enabled.", } kernelMemorySupport = testRequirement{ func() bool { - cgroupMemoryMountpoint, err := cgroups.FindCgroupMountpoint("memory") - if err != nil { - return false - } - if _, err := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.kmem.limit_in_bytes")); err != nil { - return false - } - return true + return SysInfo.KernelMemory }, "Test requires an environment that supports cgroup kernel memory.", } + memoryLimitSupport = testRequirement{ + func() bool { + return SysInfo.MemoryLimit + }, + "Test requires an environment that supports cgroup memory limit.", + } + swapMemorySupport = testRequirement{ + func() bool { + return SysInfo.SwapLimit + }, + "Test requires an environment that supports cgroup swap memory limit.", + } + memorySwappinessSupport = testRequirement{ + func() bool { + return SysInfo.MemorySwappiness + }, + "Test requires an environment that supports cgroup memory swappiness.", + } + blkioWeight = testRequirement{ + func() bool { + return SysInfo.BlkioWeight + }, + "Test requires an environment that supports blkio weight.", + } + cgroupCpuset = testRequirement{ + func() bool { + return SysInfo.Cpuset + }, + "Test requires an environment that supports cgroup cpuset.", + } ) + +func init() { + SysInfo = sysinfo.New(true) +}