浏览代码

Change the commands unit tests in order to reflect the proper behaviour of CmdRun

Guillaume J. Charmes 12 年之前
父节点
当前提交
cfeed391d7
共有 1 个文件被更改,包括 32 次插入60 次删除
  1. 32 60
      commands_test.go

+ 32 - 60
commands_test.go

@@ -24,7 +24,7 @@ func closeWrap(args ...io.Closer) error {
 	return nil
 	return nil
 }
 }
 
 
-func setTimeout(t *testing.T, msg string, d time.Duration, f func(chan bool)) {
+func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
 	c := make(chan bool)
 	c := make(chan bool)
 
 
 	// Make sure we are not too long
 	// Make sure we are not too long
@@ -32,9 +32,12 @@ func setTimeout(t *testing.T, msg string, d time.Duration, f func(chan bool)) {
 		time.Sleep(d)
 		time.Sleep(d)
 		c <- true
 		c <- true
 	}()
 	}()
-	go f(c)
-	if timeout := <-c; timeout {
-		t.Fatalf("Timeout: %s", msg)
+	go func() {
+		f()
+		c <- false
+	}()
+	if <-c {
+		t.Fatal(msg)
 	}
 	}
 }
 }
 
 
@@ -54,77 +57,46 @@ func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error
 	return nil
 	return nil
 }
 }
 
 
-/*
-// Test the behavior of a client disconnection.
-// We expect a client disconnect to leave the stdin of the container open
-// Therefore a process will keep his stdin open when a client disconnects
-func TestReattachAfterDisconnect(t *testing.T) {
+// Expected behaviour: the process dies when the client disconnects
+func TestRunDisconnect(t *testing.T) {
 	runtime, err := newTestRuntime()
 	runtime, err := newTestRuntime()
 	if err != nil {
 	if err != nil {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}
 	defer nuke(runtime)
 	defer nuke(runtime)
 
 
-	// FIXME: low down the timeout (after #230)
-	setTimeout(t, "TestReattachAfterDisconnect", 12*time.Second, func(timeout chan bool) {
-
-		srv := &Server{runtime: runtime}
-
-		stdin, stdinPipe := io.Pipe()
-		stdout, stdoutPipe := io.Pipe()
-		c1 := make(chan struct{})
-		go func() {
-			if err := srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat"); err == nil {
-				t.Fatal("CmdRun should generate a read/write on closed pipe error. No error found.")
-			}
-			close(c1)
-		}()
-
-		if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
-			t.Fatal(err)
-		}
+	srv := &Server{runtime: runtime}
 
 
-		// Close pipes (simulate disconnect)
-		if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
+	stdin, stdinPipe := io.Pipe()
+	stdout, stdoutPipe := io.Pipe()
+	c1 := make(chan struct{})
+	go func() {
+		if err := srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat"); err != nil {
 			t.Fatal(err)
 			t.Fatal(err)
 		}
 		}
+		close(c1)
+	}()
 
 
-		container := runtime.containers.Back().Value.(*Container)
-
-		// Recreate the pipes
-		stdin, stdinPipe = io.Pipe()
-		stdout, stdoutPipe = io.Pipe()
-
-		// Attach to it
-		c2 := make(chan struct{})
-		go func() {
-			if err := srv.CmdAttach(stdin, stdoutPipe, container.Id); err != nil {
-				t.Fatal(err)
-			}
-			close(c2)
-		}()
-
+	setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
 		if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
 		if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
 			t.Fatal(err)
 			t.Fatal(err)
 		}
 		}
+	})
 
 
-		// Close pipes
-		if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
-			t.Fatal(err)
-		}
+	// Close pipes (simulate disconnect)
+	if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
+		t.Fatal(err)
+	}
 
 
-		// FIXME: when #230 will be finished, send SIGINT instead of SIGTERM
-		//        we expect cat to stay alive so SIGTERM will have no effect
-		//        and Stop will timeout
-		if err := container.Stop(); err != nil {
-			t.Fatal(err)
-		}
-		// Wait for run and attach to finish
+	// as the pipes are close, we expect the process to die,
+	// therefore CmdRun to unblock. Wait for CmdRun
+	setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
 		<-c1
 		<-c1
-		<-c2
-
-		// Finished, no timeout
-		timeout <- false
 	})
 	})
+
+	// Check the status of the container
+	container := runtime.containers.Back().Value.(*Container)
+	if container.State.Running {
+		t.Fatalf("/bin/cat is still running after closing stdin")
+	}
 }
 }
-*/