Browse code

Add a TestRunExit, make sure cmdRun returns after process dies

Guillaume J. Charmes authored on 2013/04/03 01:22:30
Showing 1 changed files
... ...
@@ -80,6 +80,62 @@ func TestRunHostname(t *testing.T) {
80 80
 	}
81 81
 }
82 82
 
83
+func TestRunExit(t *testing.T) {
84
+	runtime, err := newTestRuntime()
85
+	if err != nil {
86
+		t.Fatal(err)
87
+	}
88
+	defer nuke(runtime)
89
+
90
+	srv := &Server{runtime: runtime}
91
+
92
+	stdin, stdinPipe := io.Pipe()
93
+	stdout, stdoutPipe := io.Pipe()
94
+	c1 := make(chan struct{})
95
+	go func() {
96
+		if err := srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat"); err != nil {
97
+			t.Error(err)
98
+		}
99
+		close(c1)
100
+	}()
101
+
102
+	setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
103
+		if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
104
+			t.Fatal(err)
105
+		}
106
+	})
107
+
108
+	container := runtime.List()[0]
109
+
110
+	// Closing /bin/cat stdin, expect it to exit
111
+	p, err := container.StdinPipe()
112
+	if err != nil {
113
+		t.Fatal(err)
114
+	}
115
+	if err := p.Close(); err != nil {
116
+		t.Fatal(err)
117
+	}
118
+
119
+	// as the process exited, CmdRun must finish and unblock. Wait for it
120
+	setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
121
+		<-c1
122
+	})
123
+
124
+	// Make sure that the client has been disconnected
125
+	setTimeout(t, "The client should have been disconnected once the remote process exited.", 2*time.Second, func() {
126
+		if _, err := stdin.Read([]byte{}); err != nil {
127
+			if err != io.EOF {
128
+				t.Fatal(err)
129
+			}
130
+		}
131
+	})
132
+
133
+	// Cleanup pipes
134
+	if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
135
+		t.Fatal(err)
136
+	}
137
+}
138
+
83 139
 // Expected behaviour: the process dies when the client disconnects
84 140
 func TestRunDisconnect(t *testing.T) {
85 141
 	runtime, err := newTestRuntime()