Ver código fonte

Improve TestGetContainersTop so it does not rely on sleep

Guillaume J. Charmes 12 anos atrás
pai
commit
ceb33818cd
1 arquivos alterados com 31 adições e 9 exclusões
  1. 31 9
      api_test.go

+ 31 - 9
api_test.go

@@ -461,26 +461,48 @@ func TestGetContainersTop(t *testing.T) {
 
 	container, err := builder.Create(
 		&Config{
-			Image: GetTestImage(runtime).ID,
-			Cmd:   []string{"/bin/sh", "-c", "sleep 2"},
+			Image:     GetTestImage(runtime).ID,
+			Cmd:       []string{"/bin/sh", "-c", "cat"},
+			OpenStdin: true,
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
+	defer func() {
+		// Make sure the process dies before destorying runtime
+		container.stdin.Close()
+		container.WaitTimeout(2 * time.Second)
+	}()
+
 	hostConfig := &HostConfig{}
 	if err := container.Start(hostConfig); err != nil {
 		t.Fatal(err)
 	}
 
-	// Give some time to the process to start
-	container.WaitTimeout(500 * time.Millisecond)
+	setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() {
+		for {
+			if container.State.Running {
+				break
+			}
+			time.Sleep(10 * time.Millisecond)
+		}
+	})
 
 	if !container.State.Running {
-		t.Errorf("Container should be running")
+		t.Fatalf("Container should be running")
 	}
 
+	// Make sure sh spawn up cat
+	setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
+		in, _ := container.StdinPipe()
+		out, _ := container.StdoutPipe()
+		if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
+			t.Fatal(err)
+		}
+	})
+
 	r := httptest.NewRecorder()
 	req, err := http.NewRequest("GET", "/"+container.ID+"/top?ps_args=u", bytes.NewReader([]byte{}))
 	if err != nil {
@@ -504,11 +526,11 @@ func TestGetContainersTop(t *testing.T) {
 	if len(procs.Processes) != 2 {
 		t.Fatalf("Expected 2 processes, found %d.", len(procs.Processes))
 	}
-	if procs.Processes[0][10] != "/bin/sh" && procs.Processes[0][10] != "sleep" {
-		t.Fatalf("Expected `sleep` or `/bin/sh`, found %s.", procs.Processes[0][10])
+	if procs.Processes[0][10] != "/bin/sh" && procs.Processes[0][10] != "cat" {
+		t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[0][10])
 	}
-	if procs.Processes[1][10] != "/bin/sh" && procs.Processes[1][10] != "sleep" {
-		t.Fatalf("Expected `sleep` or `/bin/sh`, found %s.", procs.Processes[1][10])
+	if procs.Processes[1][10] != "/bin/sh" && procs.Processes[1][10] != "cat" {
+		t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[1][10])
 	}
 }