Browse code

Rewrite TestRunMutableNetworkFiles to avoid races

Signed-off-by: Alexander Morozov <lk4d4@docker.com>

Alexander Morozov authored on 2015/01/14 07:14:36
Showing 1 changed files
... ...
@@ -2007,7 +2007,7 @@ func TestRunMutableNetworkFiles(t *testing.T) {
2007 2007
 	for _, fn := range []string{"resolv.conf", "hosts"} {
2008 2008
 		deleteAllContainers()
2009 2009
 
2010
-		content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s; while true; do sleep 1; done", fn)))
2010
+		content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
2011 2011
 		if err != nil {
2012 2012
 			t.Fatal(err)
2013 2013
 		}
... ...
@@ -2016,16 +2016,16 @@ func TestRunMutableNetworkFiles(t *testing.T) {
2016 2016
 			t.Fatal("Content was not what was modified in the container", string(content))
2017 2017
 		}
2018 2018
 
2019
-		out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "sh", "-c", fmt.Sprintf("while true; do cat /etc/%s; sleep 1; done", fn)))
2019
+		out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "top"))
2020 2020
 		if err != nil {
2021 2021
 			t.Fatal(err)
2022 2022
 		}
2023 2023
 
2024 2024
 		contID := strings.TrimSpace(out)
2025 2025
 
2026
-		resolvConfPath := containerStorageFile(contID, fn)
2026
+		netFilePath := containerStorageFile(contID, fn)
2027 2027
 
2028
-		f, err := os.OpenFile(resolvConfPath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
2028
+		f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
2029 2029
 		if err != nil {
2030 2030
 			t.Fatal(err)
2031 2031
 		}
... ...
@@ -2044,19 +2044,14 @@ func TestRunMutableNetworkFiles(t *testing.T) {
2044 2044
 			f.Close()
2045 2045
 			t.Fatal(err)
2046 2046
 		}
2047
-
2048 2047
 		f.Close()
2049 2048
 
2050
-		time.Sleep(2 * time.Second) // don't race sleep
2051
-
2052
-		out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "logs", "c2"))
2049
+		res, err := exec.Command(dockerBinary, "exec", contID, "cat", "/etc/"+fn).CombinedOutput()
2053 2050
 		if err != nil {
2054
-			t.Fatal(err)
2051
+			t.Fatalf("Output: %s, error: %s", res, err)
2055 2052
 		}
2056
-
2057
-		lines := strings.Split(out, "\n")
2058
-		if strings.TrimSpace(lines[len(lines)-2]) != "success2" {
2059
-			t.Fatalf("Did not find the correct output in /etc/%s: %s %#v", fn, out, lines)
2053
+		if string(res) != "success2\n" {
2054
+			t.Fatalf("Expected content of %s: %q, got: %q", fn, "success2\n", res)
2060 2055
 		}
2061 2056
 	}
2062 2057
 	logDone("run - mutable network files")