Skip test if not have Cpu quota or Cpu period

Closes: #13522

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
This commit is contained in:
Qiang Huang 2015-05-28 14:59:58 +08:00
parent 2daede5a9c
commit 34e5b6af19
3 changed files with 36 additions and 13 deletions

View File

@ -5272,6 +5272,7 @@ RUN [ "/hello" ]`, map[string]string{})
} }
func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) { func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
testRequires(c, CpuCfsQuota)
name := "testbuildresourceconstraints" name := "testbuildresourceconstraints"
ctx, err := fakeContext(` ctx, err := fakeContext(`

View File

@ -88,15 +88,13 @@ func (s *DockerSuite) TestRunEchoStdoutWithCPUAndMemoryLimit(c *check.C) {
// "test" should be printed // "test" should be printed
func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) { func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
testRequires(c, CpuCfsQuota)
runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test") runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
out, _, _, err := runCommandWithStdoutStderr(runCmd) out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil { if err != nil {
c.Fatalf("failed to run container: %v, output: %q", err, out) c.Fatalf("failed to run container: %v, output: %q", err, out)
} }
out = strings.TrimSpace(out) out = strings.TrimSpace(out)
if strings.Contains(out, "Your kernel does not support CPU cfs quota") {
c.Skip("Your kernel does not support CPU cfs quota, skip this test")
}
if out != "test" { if out != "test" {
c.Errorf("container should've printed 'test'") c.Errorf("container should've printed 'test'")
} }
@ -105,7 +103,7 @@ func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
if out != "8000" { if out != "8000" {
c.Errorf("setting the CPU CFS quota failed") c.Fatalf("setting the CPU CFS quota failed")
} }
} }
@ -1116,20 +1114,16 @@ func (s *DockerSuite) TestRunProcWritableInPrivilegedContainers(c *check.C) {
} }
func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) { func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
testRequires(c, CpuCfsPeriod)
runCmd := exec.Command(dockerBinary, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true") runCmd := exec.Command(dockerBinary, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true")
out, _, _, err := runCommandWithStdoutStderr(runCmd) if _, err := runCommand(runCmd); err != nil {
if err != nil { c.Fatalf("failed to run container: %v", err)
c.Fatalf("failed to run container: %v, output: %q", err, out)
}
out = strings.TrimSpace(out)
if strings.Contains(out, "Your kernel does not support CPU cfs period") {
c.Skip("Your kernel does not support CPU cfs period, skip this test")
} }
out, err = inspectField("test", "HostConfig.CpuPeriod") out, err := inspectField("test", "HostConfig.CpuPeriod")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
if out != "50000" { if out != "50000" {
c.Errorf("setting the CPU CFS period failed") c.Fatalf("setting the CPU CFS period failed")
} }
} }

View File

@ -7,8 +7,10 @@ import (
"log" "log"
"net/http" "net/http"
"os/exec" "os/exec"
"path"
"strings" "strings"
"github.com/docker/libcontainer/cgroups"
"github.com/go-check/check" "github.com/go-check/check"
) )
@ -96,6 +98,32 @@ var (
}, },
"Test requires underlying root filesystem not be backed by overlay.", "Test requires underlying root filesystem not be backed by overlay.",
} }
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
},
"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
},
"Test requires an environment that supports cgroup cfs quota.",
}
) )
// testRequires checks if the environment satisfies the requirements // testRequires checks if the environment satisfies the requirements