integration/container_test.go
a27b4b8c
 package docker
 
 import (
a2d7dd1a
 	"fmt"
6393c383
 	"github.com/dotcloud/docker/runconfig"
ca40989e
 	"io"
 	"io/ioutil"
4e5ae883
 	"os"
4fdf11b2
 	"path"
6de3e8a2
 	"strings"
a27b4b8c
 	"testing"
a2d7dd1a
 	"time"
a27b4b8c
 )
 
bb22cd49
 func TestKillDifferentUser(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
db9d68c3
 
359b7df5
 	container, _, err := daemon.Create(&runconfig.Config{
 		Image:     GetTestImage(daemon).ID,
db9d68c3
 		Cmd:       []string{"cat"},
 		OpenStdin: true,
 		User:      "daemon",
bb22cd49
 	},
0d292440
 		"",
bb22cd49
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
c001a5af
 	// FIXME @shykes: this seems redundant, but is very old, I'm leaving it in case
 	// there is a side effect I'm not seeing.
 	// defer container.stdin.Close()
bb22cd49
 
33e70864
 	if container.State.IsRunning() {
bb22cd49
 		t.Errorf("Container shouldn't be running")
 	}
31638ab2
 	if err := container.Start(); err != nil {
bb22cd49
 		t.Fatal(err)
 	}
 
ebba0a60
 	setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
33e70864
 		for !container.State.IsRunning() {
f03c1b8e
 			time.Sleep(10 * time.Millisecond)
 		}
 	})
bb22cd49
 
db9d68c3
 	setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
 		out, _ := container.StdoutPipe()
 		in, _ := container.StdinPipe()
ad43d88a
 		if err := assertPipe("hello\n", "hello", out, in, 150); err != nil {
db9d68c3
 			t.Fatal(err)
 		}
 	})
ebba0a60
 
bb22cd49
 	if err := container.Kill(); err != nil {
 		t.Fatal(err)
 	}
 
33e70864
 	if container.State.IsRunning() {
bb22cd49
 		t.Errorf("Container shouldn't be running")
 	}
 	container.Wait()
33e70864
 	if container.State.IsRunning() {
bb22cd49
 		t.Errorf("Container shouldn't be running")
 	}
 	// Try stopping twice
 	if err := container.Kill(); err != nil {
 		t.Fatal(err)
 	}
 }
 
f2c2d953
 func TestRestart(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
 	container, _, err := daemon.Create(&runconfig.Config{
 		Image: GetTestImage(daemon).ID,
6ce64e84
 		Cmd:   []string{"echo", "-n", "foobar"},
031f91df
 	},
0d292440
 		"",
f2c2d953
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
f2c2d953
 	output, err := container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if string(output) != "foobar" {
 		t.Error(string(output))
 	}
 
 	// Run the container again and check the output
 	output, err = container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if string(output) != "foobar" {
 		t.Error(string(output))
 	}
 }
 
0da9ccc1
 func TestRestartStdin(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
 	container, _, err := daemon.Create(&runconfig.Config{
 		Image: GetTestImage(daemon).ID,
6ce64e84
 		Cmd:   []string{"cat"},
031f91df
 
 		OpenStdin: true,
 	},
0d292440
 		"",
0da9ccc1
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
0da9ccc1
 
 	stdin, err := container.StdinPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
0da9ccc1
 	stdout, err := container.StdoutPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
31638ab2
 	if err := container.Start(); err != nil {
0da9ccc1
 		t.Fatal(err)
 	}
232dbb18
 	if _, err := io.WriteString(stdin, "hello world"); err != nil {
 		t.Fatal(err)
 	}
 	if err := stdin.Close(); err != nil {
 		t.Fatal(err)
 	}
0da9ccc1
 	container.Wait()
 	output, err := ioutil.ReadAll(stdout)
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
 	if err := stdout.Close(); err != nil {
 		t.Fatal(err)
 	}
0da9ccc1
 	if string(output) != "hello world" {
232dbb18
 		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
0da9ccc1
 	}
 
 	// Restart and try again
 	stdin, err = container.StdinPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
0da9ccc1
 	stdout, err = container.StdoutPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
31638ab2
 	if err := container.Start(); err != nil {
0da9ccc1
 		t.Fatal(err)
 	}
232dbb18
 	if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
 		t.Fatal(err)
 	}
 	if err := stdin.Close(); err != nil {
 		t.Fatal(err)
 	}
0da9ccc1
 	container.Wait()
 	output, err = ioutil.ReadAll(stdout)
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
 	if err := stdout.Close(); err != nil {
 		t.Fatal(err)
 	}
0da9ccc1
 	if string(output) != "hello world #2" {
232dbb18
 		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
0da9ccc1
 	}
 }
 
ca40989e
 func TestStdin(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
 	container, _, err := daemon.Create(&runconfig.Config{
 		Image: GetTestImage(daemon).ID,
6ce64e84
 		Cmd:   []string{"cat"},
031f91df
 
 		OpenStdin: true,
 	},
0d292440
 		"",
ca40989e
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
ca40989e
 
 	stdin, err := container.StdinPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
ca40989e
 	stdout, err := container.StdoutPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
31638ab2
 	if err := container.Start(); err != nil {
232dbb18
 		t.Fatal(err)
 	}
ca40989e
 	defer stdin.Close()
 	defer stdout.Close()
232dbb18
 	if _, err := io.WriteString(stdin, "hello world"); err != nil {
 		t.Fatal(err)
 	}
 	if err := stdin.Close(); err != nil {
ca40989e
 		t.Fatal(err)
 	}
 	container.Wait()
 	output, err := ioutil.ReadAll(stdout)
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
ca40989e
 	if string(output) != "hello world" {
232dbb18
 		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
ca40989e
 	}
 }
 
 func TestTty(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
 	container, _, err := daemon.Create(&runconfig.Config{
 		Image: GetTestImage(daemon).ID,
6ce64e84
 		Cmd:   []string{"cat"},
031f91df
 
 		OpenStdin: true,
 	},
0d292440
 		"",
ca40989e
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
ca40989e
 
 	stdin, err := container.StdinPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
ca40989e
 	stdout, err := container.StdoutPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
31638ab2
 	if err := container.Start(); err != nil {
232dbb18
 		t.Fatal(err)
 	}
ca40989e
 	defer stdin.Close()
 	defer stdout.Close()
232dbb18
 	if _, err := io.WriteString(stdin, "hello world"); err != nil {
 		t.Fatal(err)
 	}
 	if err := stdin.Close(); err != nil {
ca40989e
 		t.Fatal(err)
 	}
 	container.Wait()
 	output, err := ioutil.ReadAll(stdout)
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
ca40989e
 	if string(output) != "hello world" {
232dbb18
 		t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
ca40989e
 	}
 }
 
b16ff9f8
 func TestEntrypoint(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
 	container, _, err := daemon.Create(
6393c383
 		&runconfig.Config{
359b7df5
 			Image:      GetTestImage(daemon).ID,
b16ff9f8
 			Entrypoint: []string{"/bin/echo"},
 			Cmd:        []string{"-n", "foobar"},
 		},
0d292440
 		"",
b16ff9f8
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
b16ff9f8
 	output, err := container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if string(output) != "foobar" {
 		t.Error(string(output))
 	}
 }
 
d00fb409
 func TestEntrypointNoCmd(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
 	container, _, err := daemon.Create(
6393c383
 		&runconfig.Config{
359b7df5
 			Image:      GetTestImage(daemon).ID,
d00fb409
 			Entrypoint: []string{"/bin/echo", "foobar"},
 		},
0d292440
 		"",
d00fb409
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
d00fb409
 	output, err := container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if strings.Trim(string(output), "\r\n") != "foobar" {
 		t.Error(string(output))
 	}
 }
 
5583774e
 func BenchmarkRunSequential(b *testing.B) {
359b7df5
 	daemon := mkDaemon(b)
 	defer nuke(daemon)
a2d7dd1a
 	for i := 0; i < b.N; i++ {
359b7df5
 		container, _, err := daemon.Create(&runconfig.Config{
 			Image: GetTestImage(daemon).ID,
6ce64e84
 			Cmd:   []string{"echo", "-n", "foo"},
031f91df
 		},
0d292440
 			"",
a2d7dd1a
 		)
 		if err != nil {
 			b.Fatal(err)
 		}
359b7df5
 		defer daemon.Destroy(container)
a2d7dd1a
 		output, err := container.Output()
 		if err != nil {
 			b.Fatal(err)
 		}
 		if string(output) != "foo" {
15b30881
 			b.Fatalf("Unexpected output: %s", output)
a2d7dd1a
 		}
359b7df5
 		if err := daemon.Destroy(container); err != nil {
a2d7dd1a
 			b.Fatal(err)
 		}
 	}
 }
 
 func BenchmarkRunParallel(b *testing.B) {
359b7df5
 	daemon := mkDaemon(b)
 	defer nuke(daemon)
a2d7dd1a
 
 	var tasks []chan error
 
 	for i := 0; i < b.N; i++ {
 		complete := make(chan error)
 		tasks = append(tasks, complete)
 		go func(i int, complete chan error) {
359b7df5
 			container, _, err := daemon.Create(&runconfig.Config{
 				Image: GetTestImage(daemon).ID,
6ce64e84
 				Cmd:   []string{"echo", "-n", "foo"},
031f91df
 			},
0d292440
 				"",
a2d7dd1a
 			)
 			if err != nil {
 				complete <- err
 				return
 			}
359b7df5
 			defer daemon.Destroy(container)
31638ab2
 			if err := container.Start(); err != nil {
a2d7dd1a
 				complete <- err
 				return
 			}
 			if err := container.WaitTimeout(15 * time.Second); err != nil {
 				complete <- err
 				return
 			}
 			// if string(output) != "foo" {
 			// 	complete <- fmt.Errorf("Unexecpted output: %v", string(output))
 			// }
359b7df5
 			if err := daemon.Destroy(container); err != nil {
a2d7dd1a
 				complete <- err
 				return
 			}
 			complete <- nil
 		}(i, complete)
 	}
 	var errors []error
 	for _, task := range tasks {
 		err := <-task
 		if err != nil {
 			errors = append(errors, err)
 		}
 	}
 	if len(errors) > 0 {
 		b.Fatal(errors)
 	}
 }
4fdf11b2
 
d4e62101
 func tempDir(t *testing.T) string {
5c175357
 	tmpDir, err := ioutil.TempDir("", "docker-test-container")
4fdf11b2
 	if err != nil {
 		t.Fatal(err)
 	}
d4e62101
 	return tmpDir
 }
4fdf11b2
 
9cfbaecf
 // Test for #1737
 func TestCopyVolumeUidGid(t *testing.T) {
661a8a0e
 	eng := NewTestEngine(t)
359b7df5
 	r := mkDaemonFromEngine(eng, t)
661a8a0e
 	defer r.Nuke()
9cfbaecf
 
 	// Add directory not owned by root
ff7b52ab
 	container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test && chown daemon.daemon /hello"}, t)
9cfbaecf
 	defer r.Destroy(container1)
 
33e70864
 	if container1.State.IsRunning() {
9cfbaecf
 		t.Errorf("Container shouldn't be running")
 	}
 	if err := container1.Run(); err != nil {
 		t.Fatal(err)
 	}
33e70864
 	if container1.State.IsRunning() {
9cfbaecf
 		t.Errorf("Container shouldn't be running")
 	}
 
c001a5af
 	img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
9cfbaecf
 	if err != nil {
 		t.Error(err)
 	}
 
 	// Test that the uid and gid is copied from the image to the volume
 	tmpDir1 := tempDir(t)
 	defer os.RemoveAll(tmpDir1)
661a8a0e
 	stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
9cfbaecf
 	if !strings.Contains(stdout1, "daemon daemon") {
 		t.Fatal("Container failed to transfer uid and gid to volume")
 	}
ff7b52ab
 
 	container2, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && chown daemon.daemon /hello"}, t)
 	defer r.Destroy(container1)
 
 	if container2.State.IsRunning() {
 		t.Errorf("Container shouldn't be running")
 	}
 	if err := container2.Run(); err != nil {
 		t.Fatal(err)
 	}
 	if container2.State.IsRunning() {
 		t.Errorf("Container shouldn't be running")
 	}
 
 	img2, err := r.Commit(container2, "", "", "unit test commited image", "", nil)
 	if err != nil {
 		t.Error(err)
 	}
 
 	// Test that the uid and gid is copied from the image to the volume
 	tmpDir2 := tempDir(t)
 	defer os.RemoveAll(tmpDir2)
 	stdout2, _ := runContainer(eng, r, []string{"-v", "/hello", img2.ID, "stat", "-c", "%U %G", "/hello"}, t)
 	if !strings.Contains(stdout2, "daemon daemon") {
 		t.Fatal("Container failed to transfer uid and gid to volume")
 	}
9cfbaecf
 }
 
7a9c7118
 // Test for #1582
 func TestCopyVolumeContent(t *testing.T) {
661a8a0e
 	eng := NewTestEngine(t)
359b7df5
 	r := mkDaemonFromEngine(eng, t)
661a8a0e
 	defer r.Nuke()
7a9c7118
 
 	// Put some content in a directory of a container and commit it
661a8a0e
 	container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
7a9c7118
 	defer r.Destroy(container1)
 
33e70864
 	if container1.State.IsRunning() {
7a9c7118
 		t.Errorf("Container shouldn't be running")
 	}
 	if err := container1.Run(); err != nil {
 		t.Fatal(err)
 	}
33e70864
 	if container1.State.IsRunning() {
7a9c7118
 		t.Errorf("Container shouldn't be running")
 	}
 
c001a5af
 	img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
7a9c7118
 	if err != nil {
 		t.Error(err)
 	}
 
 	// Test that the content is copied from the image to the volume
 	tmpDir1 := tempDir(t)
 	defer os.RemoveAll(tmpDir1)
661a8a0e
 	stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
7a9c7118
 	if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
 		t.Fatal("Container failed to transfer content to volume")
 	}
 }
 
d4e62101
 func TestBindMounts(t *testing.T) {
661a8a0e
 	eng := NewTestEngine(t)
359b7df5
 	r := mkDaemonFromEngine(eng, t)
661a8a0e
 	defer r.Nuke()
 
d4e62101
 	tmpDir := tempDir(t)
 	defer os.RemoveAll(tmpDir)
 	writeFile(path.Join(tmpDir, "touch-me"), "", t)
 
 	// Test reading from a read-only bind mount
661a8a0e
 	stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
d4e62101
 	if !strings.Contains(stdout, "touch-me") {
4fdf11b2
 		t.Fatal("Container failed to read from bind mount")
 	}
d4e62101
 
4fdf11b2
 	// test writing to bind mount
661a8a0e
 	runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
46a9f29b
 	readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
4fdf11b2
 
 	// test mounting to an illegal destination directory
661a8a0e
 	if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
4fdf11b2
 		t.Fatal("Container bind mounted illegal directory")
d4e62101
 	}
0198f8a8
 
 	// test mount a file
 	runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
 	content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
 	if content != "yotta" {
 		t.Fatal("Container failed to write to bind mount file")
 	}
4fdf11b2
 }
5ae8c7a9
 
92cbb7cc
 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
 func TestRestartWithVolumes(t *testing.T) {
359b7df5
 	daemon := mkDaemon(t)
 	defer nuke(daemon)
92cbb7cc
 
359b7df5
 	container, _, err := daemon.Create(&runconfig.Config{
 		Image:   GetTestImage(daemon).ID,
92cbb7cc
 		Cmd:     []string{"echo", "-n", "foobar"},
 		Volumes: map[string]struct{}{"/test": {}},
 	},
0d292440
 		"",
92cbb7cc
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
359b7df5
 	defer daemon.Destroy(container)
92cbb7cc
 
 	for key := range container.Config.Volumes {
 		if key != "/test" {
 			t.Fail()
 		}
 	}
 
 	_, err = container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 
 	expected := container.Volumes["/test"]
 	if expected == "" {
 		t.Fail()
 	}
 	// Run the container again to verify the volume path persists
 	_, err = container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
4fdf11b2
 
92cbb7cc
 	actual := container.Volumes["/test"]
 	if expected != actual {
 		t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
d4e62101
 	}
4fdf11b2
 }