Browse code

Optimize slow bottleneck test of DockerSuite.TestRunUnshareProc.

This fix tries to improve the time to run TestRunUnshareProc
in #19425.
In this fix goroutines are used to run test cases in parallel to
prevent the test from taking a long time to run.
As the majority of the execution time in the tests is from
multiple executions of 'docker run' and each of which takes
several seconds, parallel executions improve the test time.
Since each 'docker run' is independent, the purpose of the
test is not altered in this fix.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/03/04 14:15:41
Showing 1 changed files
... ...
@@ -3002,29 +3002,51 @@ func (s *DockerSuite) TestRunUnshareProc(c *check.C) {
3002 3002
 	// Not applicable on Windows as uses Unix specific functionality
3003 3003
 	testRequires(c, Apparmor, DaemonIsLinux, NotUserNamespace)
3004 3004
 
3005
-	name := "acidburn"
3006
-	out, _, err := dockerCmdWithError("run", "--name", name, "--security-opt", "seccomp:unconfined", "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "--mount-proc=/proc", "mount")
3007
-	if err == nil ||
3008
-		!(strings.Contains(strings.ToLower(out), "permission denied") ||
3009
-			strings.Contains(strings.ToLower(out), "operation not permitted")) {
3010
-		c.Fatalf("unshare with --mount-proc should have failed with 'permission denied' or 'operation not permitted', got: %s, %v", out, err)
3011
-	}
3005
+	// In this test goroutines are used to run test cases in parallel to prevent the test from taking a long time to run.
3006
+	errChan := make(chan error)
3012 3007
 
3013
-	name = "cereal"
3014
-	out, _, err = dockerCmdWithError("run", "--name", name, "--security-opt", "seccomp:unconfined", "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc")
3015
-	if err == nil ||
3016
-		!(strings.Contains(strings.ToLower(out), "mount: cannot mount none") ||
3017
-			strings.Contains(strings.ToLower(out), "permission denied")) {
3018
-		c.Fatalf("unshare and mount of /proc should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v", out, err)
3019
-	}
3008
+	go func() {
3009
+		name := "acidburn"
3010
+		out, _, err := dockerCmdWithError("run", "--name", name, "--security-opt", "seccomp:unconfined", "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "--mount-proc=/proc", "mount")
3011
+		if err == nil ||
3012
+			!(strings.Contains(strings.ToLower(out), "permission denied") ||
3013
+				strings.Contains(strings.ToLower(out), "operation not permitted")) {
3014
+			errChan <- fmt.Errorf("unshare with --mount-proc should have failed with 'permission denied' or 'operation not permitted', got: %s, %v", out, err)
3015
+		} else {
3016
+			errChan <- nil
3017
+		}
3018
+	}()
3019
+
3020
+	go func() {
3021
+		name := "cereal"
3022
+		out, _, err := dockerCmdWithError("run", "--name", name, "--security-opt", "seccomp:unconfined", "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc")
3023
+		if err == nil ||
3024
+			!(strings.Contains(strings.ToLower(out), "mount: cannot mount none") ||
3025
+				strings.Contains(strings.ToLower(out), "permission denied")) {
3026
+			errChan <- fmt.Errorf("unshare and mount of /proc should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v", out, err)
3027
+		} else {
3028
+			errChan <- nil
3029
+		}
3030
+	}()
3020 3031
 
3021 3032
 	/* Ensure still fails if running privileged with the default policy */
3022
-	name = "crashoverride"
3023
-	out, _, err = dockerCmdWithError("run", "--privileged", "--security-opt", "seccomp:unconfined", "--security-opt", "apparmor:docker-default", "--name", name, "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc")
3024
-	if err == nil ||
3025
-		!(strings.Contains(strings.ToLower(out), "mount: cannot mount none") ||
3026
-			strings.Contains(strings.ToLower(out), "permission denied")) {
3027
-		c.Fatalf("privileged unshare with apparmor should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v", out, err)
3033
+	go func() {
3034
+		name := "crashoverride"
3035
+		out, _, err := dockerCmdWithError("run", "--privileged", "--security-opt", "seccomp:unconfined", "--security-opt", "apparmor:docker-default", "--name", name, "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc")
3036
+		if err == nil ||
3037
+			!(strings.Contains(strings.ToLower(out), "mount: cannot mount none") ||
3038
+				strings.Contains(strings.ToLower(out), "permission denied")) {
3039
+			errChan <- fmt.Errorf("privileged unshare with apparmor should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v", out, err)
3040
+		} else {
3041
+			errChan <- nil
3042
+		}
3043
+	}()
3044
+
3045
+	for i := 0; i < 3; i++ {
3046
+		err := <-errChan
3047
+		if err != nil {
3048
+			c.Fatal(err)
3049
+		}
3028 3050
 	}
3029 3051
 }
3030 3052