Browse code

Merge pull request #13529 from hqhq/hq_skip_cgroup_test

Skip test if not have Cpu quota or Cpu period

Jessie Frazelle authored on 2015/05/29 02:57:32
Showing 3 changed files
... ...
@@ -5272,6 +5272,7 @@ RUN [ "/hello" ]`, map[string]string{})
5272 5272
 }
5273 5273
 
5274 5274
 func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
5275
+	testRequires(c, CpuCfsQuota)
5275 5276
 	name := "testbuildresourceconstraints"
5276 5277
 
5277 5278
 	ctx, err := fakeContext(`
... ...
@@ -88,15 +88,13 @@ func (s *DockerSuite) TestRunEchoStdoutWithCPUAndMemoryLimit(c *check.C) {
88 88
 
89 89
 // "test" should be printed
90 90
 func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
91
+	testRequires(c, CpuCfsQuota)
91 92
 	runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
92 93
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
93 94
 	if err != nil {
94 95
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
95 96
 	}
96 97
 	out = strings.TrimSpace(out)
97
-	if strings.Contains(out, "Your kernel does not support CPU cfs quota") {
98
-		c.Skip("Your kernel does not support CPU cfs quota, skip this test")
99
-	}
100 98
 	if out != "test" {
101 99
 		c.Errorf("container should've printed 'test'")
102 100
 	}
... ...
@@ -105,7 +103,7 @@ func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
105 105
 	c.Assert(err, check.IsNil)
106 106
 
107 107
 	if out != "8000" {
108
-		c.Errorf("setting the CPU CFS quota failed")
108
+		c.Fatalf("setting the CPU CFS quota failed")
109 109
 	}
110 110
 }
111 111
 
... ...
@@ -1116,20 +1114,16 @@ func (s *DockerSuite) TestRunProcWritableInPrivilegedContainers(c *check.C) {
1116 1116
 }
1117 1117
 
1118 1118
 func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
1119
+	testRequires(c, CpuCfsPeriod)
1119 1120
 	runCmd := exec.Command(dockerBinary, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true")
1120
-	out, _, _, err := runCommandWithStdoutStderr(runCmd)
1121
-	if err != nil {
1122
-		c.Fatalf("failed to run container: %v, output: %q", err, out)
1123
-	}
1124
-	out = strings.TrimSpace(out)
1125
-	if strings.Contains(out, "Your kernel does not support CPU cfs period") {
1126
-		c.Skip("Your kernel does not support CPU cfs period, skip this test")
1121
+	if _, err := runCommand(runCmd); err != nil {
1122
+		c.Fatalf("failed to run container: %v", err)
1127 1123
 	}
1128 1124
 
1129
-	out, err = inspectField("test", "HostConfig.CpuPeriod")
1125
+	out, err := inspectField("test", "HostConfig.CpuPeriod")
1130 1126
 	c.Assert(err, check.IsNil)
1131 1127
 	if out != "50000" {
1132
-		c.Errorf("setting the CPU CFS period failed")
1128
+		c.Fatalf("setting the CPU CFS period failed")
1133 1129
 	}
1134 1130
 }
1135 1131
 
... ...
@@ -7,8 +7,10 @@ import (
7 7
 	"log"
8 8
 	"net/http"
9 9
 	"os/exec"
10
+	"path"
10 11
 	"strings"
11 12
 
13
+	"github.com/docker/libcontainer/cgroups"
12 14
 	"github.com/go-check/check"
13 15
 )
14 16
 
... ...
@@ -96,6 +98,32 @@ var (
96 96
 		},
97 97
 		"Test requires underlying root filesystem not be backed by overlay.",
98 98
 	}
99
+	CpuCfsPeriod = TestRequirement{
100
+		func() bool {
101
+			cgroupCpuMountpoint, err := cgroups.FindCgroupMountpoint("cpu")
102
+			if err != nil {
103
+				return false
104
+			}
105
+			if _, err := ioutil.ReadFile(path.Join(cgroupCpuMountpoint, "cpu.cfs_period_us")); err != nil {
106
+				return false
107
+			}
108
+			return true
109
+		},
110
+		"Test requires an environment that supports cgroup cfs period.",
111
+	}
112
+	CpuCfsQuota = TestRequirement{
113
+		func() bool {
114
+			cgroupCpuMountpoint, err := cgroups.FindCgroupMountpoint("cpu")
115
+			if err != nil {
116
+				return false
117
+			}
118
+			if _, err := ioutil.ReadFile(path.Join(cgroupCpuMountpoint, "cpu.cfs_quota_us")); err != nil {
119
+				return false
120
+			}
121
+			return true
122
+		},
123
+		"Test requires an environment that supports cgroup cfs quota.",
124
+	}
99 125
 )
100 126
 
101 127
 // testRequires checks if the environment satisfies the requirements