Browse Source

Fixed a bug preventing proper reattachment to stdin upon container restart

Andrea Luzzardi 12 years ago
parent
commit
0da9ccc18e
2 changed files with 54 additions and 0 deletions
  1. 5 0
      container.go
  2. 49 0
      container_test.go

+ 5 - 0
container.go

@@ -355,6 +355,11 @@ func (container *Container) monitor() {
 		log.Printf("%v: Failed to umount filesystem: %v", container.Id, err)
 	}
 
+	// Re-create a brand new stdin pipe once the container exited
+	if container.Config.OpenStdin {
+		container.stdin, container.stdinPipe = io.Pipe()
+	}
+
 	// Report status back
 	container.State.setStopped(exitCode)
 	container.save()

+ 49 - 0
container_test.go

@@ -221,6 +221,55 @@ func TestRestart(t *testing.T) {
 	}
 }
 
+func TestRestartStdin(t *testing.T) {
+	docker, err := newTestDocker()
+	if err != nil {
+		t.Fatal(err)
+	}
+	container, err := docker.Create(
+		"restart_stdin_test",
+		"cat",
+		[]string{},
+		[]string{"/var/lib/docker/images/ubuntu"},
+		&Config{
+			OpenStdin: true,
+		},
+	)
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer docker.Destroy(container)
+
+	stdin, err := container.StdinPipe()
+	stdout, err := container.StdoutPipe()
+	if err := container.Start(); err != nil {
+		t.Fatal(err)
+	}
+	io.WriteString(stdin, "hello world")
+	stdin.Close()
+	container.Wait()
+	output, err := ioutil.ReadAll(stdout)
+	stdout.Close()
+	if string(output) != "hello world" {
+		t.Fatal(string(output))
+	}
+
+	// Restart and try again
+	stdin, err = container.StdinPipe()
+	stdout, err = container.StdoutPipe()
+	if err := container.Start(); err != nil {
+		t.Fatal(err)
+	}
+	io.WriteString(stdin, "hello world #2")
+	stdin.Close()
+	container.Wait()
+	output, err = ioutil.ReadAll(stdout)
+	stdout.Close()
+	if string(output) != "hello world #2" {
+		t.Fatal(string(output))
+	}
+}
+
 func TestUser(t *testing.T) {
 	docker, err := newTestDocker()
 	if err != nil {