Selaa lähdekoodia

Skip test if not have Cpu quota or Cpu period

Closes: #13522

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
Qiang Huang 10 vuotta sitten
vanhempi
commit
34e5b6af19

+ 1 - 0
integration-cli/docker_cli_build_test.go

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

+ 7 - 13
integration-cli/docker_cli_run_test.go

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

+ 28 - 0
integration-cli/requirements.go

@@ -7,8 +7,10 @@ import (
 	"log"
 	"net/http"
 	"os/exec"
+	"path"
 	"strings"
 
+	"github.com/docker/libcontainer/cgroups"
 	"github.com/go-check/check"
 )
 
@@ -96,6 +98,32 @@ var (
 		},
 		"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