container_test.go
a27b4b8c
 package docker
 
 import (
4e5ae883
 	"bufio"
a2d7dd1a
 	"fmt"
ca40989e
 	"io"
 	"io/ioutil"
4e5ae883
 	"math/rand"
 	"os"
4fdf11b2
 	"path"
a8ae0aaf
 	"regexp"
fb350e0c
 	"sort"
6de3e8a2
 	"strings"
a27b4b8c
 	"testing"
a2d7dd1a
 	"time"
a27b4b8c
 )
 
fd224ee5
 func TestIDFormat(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
a8ae0aaf
 	defer nuke(runtime)
675f1a80
 	container1, _, err := runtime.Create(
a8ae0aaf
 		&Config{
fd224ee5
 			Image: GetTestImage(runtime).ID,
6ebb2491
 			Cmd:   []string{"/bin/sh", "-c", "echo hello world"},
a8ae0aaf
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
fd224ee5
 	match, err := regexp.Match("^[0-9a-f]{64}$", []byte(container1.ID))
a8ae0aaf
 	if err != nil {
 		t.Fatal(err)
 	}
 	if !match {
fd224ee5
 		t.Fatalf("Invalid container ID: %s", container1.ID)
a8ae0aaf
 	}
 }
 
0ebdca5e
 func TestMultipleAttachRestart(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
0ebdca5e
 	defer nuke(runtime)
080243f0
 	container, hostConfig, _ := mkContainer(
 		runtime,
 		[]string{"_", "/bin/sh", "-c", "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`;  echo hello; done"},
 		t,
0ebdca5e
 	)
 	defer runtime.Destroy(container)
 
 	// Simulate 3 client attaching to the container and stop/restart
 
 	stdout1, err := container.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 	stdout2, err := container.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 	stdout3, err := container.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
4fdf11b2
 	if err := container.Start(hostConfig); err != nil {
0ebdca5e
 		t.Fatal(err)
 	}
 	l1, err := bufio.NewReader(stdout1).ReadString('\n')
 	if err != nil {
 		t.Fatal(err)
 	}
 	if strings.Trim(l1, " \r\n") != "hello" {
 		t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
 	}
 	l2, err := bufio.NewReader(stdout2).ReadString('\n')
 	if err != nil {
 		t.Fatal(err)
 	}
 	if strings.Trim(l2, " \r\n") != "hello" {
 		t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
 	}
 	l3, err := bufio.NewReader(stdout3).ReadString('\n')
 	if err != nil {
 		t.Fatal(err)
 	}
 	if strings.Trim(l3, " \r\n") != "hello" {
 		t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
 	}
 
90602ab6
 	if err := container.Stop(10); err != nil {
0ebdca5e
 		t.Fatal(err)
 	}
 
 	stdout1, err = container.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 	stdout2, err = container.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 	stdout3, err = container.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
4fdf11b2
 	if err := container.Start(hostConfig); err != nil {
0ebdca5e
 		t.Fatal(err)
 	}
c45beabc
 
 	setTimeout(t, "Timeout reading from the process", 3*time.Second, func() {
0ebdca5e
 		l1, err = bufio.NewReader(stdout1).ReadString('\n')
 		if err != nil {
 			t.Fatal(err)
 		}
 		if strings.Trim(l1, " \r\n") != "hello" {
 			t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
 		}
 		l2, err = bufio.NewReader(stdout2).ReadString('\n')
 		if err != nil {
 			t.Fatal(err)
 		}
 		if strings.Trim(l2, " \r\n") != "hello" {
 			t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
 		}
 		l3, err = bufio.NewReader(stdout3).ReadString('\n')
 		if err != nil {
 			t.Fatal(err)
 		}
 		if strings.Trim(l3, " \r\n") != "hello" {
 			t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
 		}
c45beabc
 	})
 	container.Wait()
0ebdca5e
 }
 
97535e5a
 func TestDiff(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
97535e5a
 	defer nuke(runtime)
 	// Create a container and remove a file
080243f0
 	container1, _, _ := mkContainer(runtime, []string{"_", "/bin/rm", "/etc/passwd"}, t)
97535e5a
 	defer runtime.Destroy(container1)
 
46c9c5c8
 	// The changelog should be empty and not fail before run. See #1705
 	c, err := container1.Changes()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if len(c) != 0 {
 		t.Fatalf("Changelog should be empty before run")
 	}
 
97535e5a
 	if err := container1.Run(); err != nil {
 		t.Fatal(err)
 	}
 
 	// Check the changelog
46c9c5c8
 	c, err = container1.Changes()
97535e5a
 	if err != nil {
 		t.Fatal(err)
 	}
 	success := false
 	for _, elem := range c {
 		if elem.Path == "/etc/passwd" && elem.Kind == 2 {
 			success = true
 		}
 	}
 	if !success {
 		t.Fatalf("/etc/passwd as been removed but is not present in the diff")
 	}
 
 	// Commit the container
 	rwTar, err := container1.ExportRw()
 	if err != nil {
 		t.Error(err)
 	}
724e2d6b
 	img, err := runtime.graph.Create(rwTar, container1, "unit test commited image - diff", "", nil)
97535e5a
 	if err != nil {
 		t.Error(err)
 	}
 
 	// Create a new container from the commited image
080243f0
 	container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/etc/passwd"}, t)
97535e5a
 	defer runtime.Destroy(container2)
 
 	if err := container2.Run(); err != nil {
 		t.Fatal(err)
 	}
 
 	// Check the changelog
 	c, err = container2.Changes()
 	if err != nil {
 		t.Fatal(err)
 	}
 	for _, elem := range c {
 		if elem.Path == "/etc/passwd" {
 			t.Fatalf("/etc/passwd should not be present in the diff after commit.")
 		}
 	}
71b1657e
 
9b2a5964
 	// Create a new container
080243f0
 	container3, _, _ := mkContainer(runtime, []string{"_", "rm", "/bin/httpd"}, t)
71b1657e
 	defer runtime.Destroy(container3)
 
 	if err := container3.Run(); err != nil {
 		t.Fatal(err)
 	}
 
 	// Check the changelog
 	c, err = container3.Changes()
 	if err != nil {
 		t.Fatal(err)
 	}
 	success = false
 	for _, elem := range c {
 		if elem.Path == "/bin/httpd" && elem.Kind == 2 {
 			success = true
 		}
 	}
 	if !success {
 		t.Fatalf("/bin/httpd should be present in the diff after commit.")
 	}
97535e5a
 }
 
30d327d3
 func TestCommitAutoRun(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
30d327d3
 	defer nuke(runtime)
080243f0
 	container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
30d327d3
 	defer runtime.Destroy(container1)
 
 	if container1.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	if err := container1.Run(); err != nil {
 		t.Fatal(err)
 	}
 	if container1.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 
 	rwTar, err := container1.ExportRw()
 	if err != nil {
 		t.Error(err)
 	}
 	img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", &Config{Cmd: []string{"cat", "/world"}})
 	if err != nil {
 		t.Error(err)
 	}
 
 	// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
080243f0
 	container2, hostConfig, _ := mkContainer(runtime, []string{img.ID}, t)
30d327d3
 	defer runtime.Destroy(container2)
 	stdout, err := container2.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 	stderr, err := container2.StderrPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
4fdf11b2
 	if err := container2.Start(hostConfig); err != nil {
30d327d3
 		t.Fatal(err)
 	}
 	container2.Wait()
 	output, err := ioutil.ReadAll(stdout)
 	if err != nil {
 		t.Fatal(err)
 	}
 	output2, err := ioutil.ReadAll(stderr)
 	if err != nil {
 		t.Fatal(err)
 	}
 	if err := stdout.Close(); err != nil {
 		t.Fatal(err)
 	}
 	if err := stderr.Close(); err != nil {
 		t.Fatal(err)
 	}
 	if string(output) != "hello\n" {
 		t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
 	}
 }
 
f2dc079c
 func TestCommitRun(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
ff95f2b0
 
080243f0
 	container1, hostConfig, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
b8547f31
 	defer runtime.Destroy(container1)
a27b4b8c
 
f2dc079c
 	if container1.State.Running {
a27b4b8c
 		t.Errorf("Container shouldn't be running")
 	}
f2dc079c
 	if err := container1.Run(); err != nil {
a27b4b8c
 		t.Fatal(err)
 	}
f2dc079c
 	if container1.State.Running {
a27b4b8c
 		t.Errorf("Container shouldn't be running")
 	}
f2dc079c
 
7c57a4cf
 	rwTar, err := container1.ExportRw()
f2dc079c
 	if err != nil {
 		t.Error(err)
 	}
724e2d6b
 	img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", nil)
f2dc079c
 	if err != nil {
 		t.Error(err)
 	}
 
 	// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
080243f0
 	container2, hostConfig, _ := mkContainer(runtime, []string{img.ID, "cat", "/world"}, t)
b8547f31
 	defer runtime.Destroy(container2)
f2dc079c
 	stdout, err := container2.StdoutPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
f2dc079c
 	stderr, err := container2.StderrPipe()
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
4fdf11b2
 	if err := container2.Start(hostConfig); err != nil {
f2dc079c
 		t.Fatal(err)
 	}
 	container2.Wait()
 	output, err := ioutil.ReadAll(stdout)
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
f2dc079c
 	output2, err := ioutil.ReadAll(stderr)
232dbb18
 	if err != nil {
 		t.Fatal(err)
 	}
 	if err := stdout.Close(); err != nil {
 		t.Fatal(err)
 	}
 	if err := stderr.Close(); err != nil {
 		t.Fatal(err)
 	}
f2dc079c
 	if string(output) != "hello\n" {
15b30881
 		t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
a27b4b8c
 	}
d949e280
 }
 
 func TestStart(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
d949e280
 	defer nuke(runtime)
080243f0
 	container, hostConfig, _ := mkContainer(runtime, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
d949e280
 	defer runtime.Destroy(container)
 
2a303dab
 	cStdin, err := container.StdinPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 
4fdf11b2
 	if err := container.Start(hostConfig); err != nil {
d949e280
 		t.Fatal(err)
 	}
 
 	// Give some time to the process to start
 	container.WaitTimeout(500 * time.Millisecond)
 
 	if !container.State.Running {
 		t.Errorf("Container should be running")
 	}
4fdf11b2
 	if err := container.Start(hostConfig); err == nil {
9b2a5964
 		t.Fatalf("A running container should be able to be started")
d949e280
 	}
91b1f9ee
 
9b2a5964
 	// Try to avoid the timeout in destroy. Best effort, don't check error
91b1f9ee
 	cStdin.Close()
20085794
 	container.WaitTimeout(2 * time.Second)
a27b4b8c
 }
 
 func TestRun(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
080243f0
 	container, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
b8547f31
 	defer runtime.Destroy(container)
a27b4b8c
 
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	if err := container.Run(); err != nil {
 		t.Fatal(err)
 	}
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 }
 
 func TestOutput(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(
031f91df
 		&Config{
fd224ee5
 			Image: GetTestImage(runtime).ID,
6ce64e84
 			Cmd:   []string{"echo", "-n", "foobar"},
031f91df
 		},
a27b4b8c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
a27b4b8c
 	output, err := container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if string(output) != "foobar" {
 		t.Error(string(output))
 	}
 }
 
bb22cd49
 func TestKillDifferentUser(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
bb22cd49
 	defer nuke(runtime)
db9d68c3
 
675f1a80
 	container, _, err := runtime.Create(&Config{
db9d68c3
 		Image:     GetTestImage(runtime).ID,
 		Cmd:       []string{"cat"},
 		OpenStdin: true,
 		User:      "daemon",
bb22cd49
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
db9d68c3
 	defer container.stdin.Close()
bb22cd49
 
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
db9d68c3
 	if err := container.Start(&HostConfig{}); err != nil {
bb22cd49
 		t.Fatal(err)
 	}
 
ebba0a60
 	setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
f03c1b8e
 		for !container.State.Running {
 			time.Sleep(10 * time.Millisecond)
 		}
 	})
bb22cd49
 
db9d68c3
 	setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
 		out, _ := container.StdoutPipe()
 		in, _ := container.StdinPipe()
 		if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
 			t.Fatal(err)
 		}
 	})
ebba0a60
 
bb22cd49
 	if err := container.Kill(); err != nil {
 		t.Fatal(err)
 	}
 
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	container.Wait()
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	// Try stopping twice
 	if err := container.Kill(); err != nil {
 		t.Fatal(err)
 	}
 }
 
f9acd605
 // Test that creating a container with a volume doesn't crash. Regression test for #995.
 func TestCreateVolume(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
f9acd605
 	defer nuke(runtime)
 
46a9f29b
 	config, hc, _, err := ParseRun([]string{"-v", "/var/lib/data", GetTestImage(runtime).ID, "echo", "hello", "world"}, nil)
f9acd605
 	if err != nil {
 		t.Fatal(err)
 	}
675f1a80
 	c, _, err := runtime.Create(config)
f9acd605
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(c)
46a9f29b
 	if err := c.Start(hc); err != nil {
f9acd605
 		t.Fatal(err)
 	}
 	c.WaitTimeout(500 * time.Millisecond)
 	c.Wait()
 }
 
24dac228
 func TestKill(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
fb86dcfb
 		Cmd:   []string{"sleep", "2"},
031f91df
 	},
a27b4b8c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
a27b4b8c
 
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
4fdf11b2
 	hostConfig := &HostConfig{}
 	if err := container.Start(hostConfig); err != nil {
a27b4b8c
 		t.Fatal(err)
 	}
bb22cd49
 
 	// Give some time to lxc to spawn the process
 	container.WaitTimeout(500 * time.Millisecond)
 
a27b4b8c
 	if !container.State.Running {
 		t.Errorf("Container should be running")
 	}
24dac228
 	if err := container.Kill(); err != nil {
a27b4b8c
 		t.Fatal(err)
 	}
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	container.Wait()
 	if container.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	// Try stopping twice
24dac228
 	if err := container.Kill(); err != nil {
a27b4b8c
 		t.Fatal(err)
 	}
 }
 
 func TestExitCode(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
a27b4b8c
 
675f1a80
 	trueContainer, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"/bin/true", ""},
232dbb18
 	})
a27b4b8c
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(trueContainer)
a27b4b8c
 	if err := trueContainer.Run(); err != nil {
 		t.Fatal(err)
 	}
232dbb18
 	if trueContainer.State.ExitCode != 0 {
 		t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
 	}
a27b4b8c
 
675f1a80
 	falseContainer, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"/bin/false", ""},
232dbb18
 	})
a27b4b8c
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(falseContainer)
a27b4b8c
 	if err := falseContainer.Run(); err != nil {
 		t.Fatal(err)
 	}
 	if falseContainer.State.ExitCode != 1 {
232dbb18
 		t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
a27b4b8c
 	}
 }
f958bdba
 
f2c2d953
 func TestRestart(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"echo", "-n", "foobar"},
031f91df
 	},
f2c2d953
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.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) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"cat"},
031f91df
 
 		OpenStdin: true,
 	},
0da9ccc1
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.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)
 	}
4fdf11b2
 	hostConfig := &HostConfig{}
 	if err := container.Start(hostConfig); 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)
 	}
4fdf11b2
 	if err := container.Start(hostConfig); 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
 	}
 }
 
6de3e8a2
 func TestUser(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
6de3e8a2
 
 	// Default user must be root
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"id"},
031f91df
 	},
6de3e8a2
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
6de3e8a2
 	output, err := container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
 		t.Error(string(output))
 	}
 
 	// Set a username
675f1a80
 	container, _, err = runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"id"},
031f91df
 
 		User: "root",
 	},
6de3e8a2
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
6de3e8a2
 	output, err = container.Output()
ac36c986
 	if err != nil || container.State.ExitCode != 0 {
6de3e8a2
 		t.Fatal(err)
 	}
 	if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
 		t.Error(string(output))
 	}
 
 	// Set a UID
675f1a80
 	container, _, err = runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"id"},
031f91df
 
 		User: "0",
 	},
6de3e8a2
 	)
ac36c986
 	if err != nil || container.State.ExitCode != 0 {
6de3e8a2
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
6de3e8a2
 	output, err = container.Output()
ac36c986
 	if err != nil || container.State.ExitCode != 0 {
6de3e8a2
 		t.Fatal(err)
 	}
 	if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
 		t.Error(string(output))
 	}
 
 	// Set a different user by uid
675f1a80
 	container, _, err = runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"id"},
031f91df
 
 		User: "1",
 	},
6de3e8a2
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
6de3e8a2
 	output, err = container.Output()
2ebf3464
 	if err != nil {
6de3e8a2
 		t.Fatal(err)
2ebf3464
 	} else if container.State.ExitCode != 0 {
 		t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output)
6de3e8a2
 	}
 	if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
 		t.Error(string(output))
 	}
 
 	// Set a different user by username
675f1a80
 	container, _, err = runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"id"},
031f91df
 
 		User: "daemon",
 	},
6de3e8a2
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
6de3e8a2
 	output, err = container.Output()
ac36c986
 	if err != nil || container.State.ExitCode != 0 {
6de3e8a2
 		t.Fatal(err)
 	}
 	if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
 		t.Error(string(output))
 	}
e41507bd
 
 	// Test an wrong username
675f1a80
 	container, _, err = runtime.Create(&Config{
e41507bd
 		Image: GetTestImage(runtime).ID,
 		Cmd:   []string{"id"},
 
9b2a5964
 		User: "unknownuser",
e41507bd
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 	output, err = container.Output()
 	if container.State.ExitCode == 0 {
 		t.Fatal("Starting container with wrong uid should fail but it passed.")
 	}
6de3e8a2
 }
 
f958bdba
 func TestMultipleContainers(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
f958bdba
 
675f1a80
 	container1, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
fb86dcfb
 		Cmd:   []string{"sleep", "2"},
031f91df
 	},
f958bdba
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container1)
f958bdba
 
675f1a80
 	container2, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
fb86dcfb
 		Cmd:   []string{"sleep", "2"},
031f91df
 	},
f958bdba
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container2)
f958bdba
 
 	// Start both containers
4fdf11b2
 	hostConfig := &HostConfig{}
 	if err := container1.Start(hostConfig); err != nil {
f958bdba
 		t.Fatal(err)
 	}
4fdf11b2
 	if err := container2.Start(hostConfig); err != nil {
f958bdba
 		t.Fatal(err)
 	}
 
bb22cd49
 	// Make sure they are running before trying to kill them
 	container1.WaitTimeout(250 * time.Millisecond)
 	container2.WaitTimeout(250 * time.Millisecond)
 
f958bdba
 	// If we are here, both containers should be running
 	if !container1.State.Running {
 		t.Fatal("Container not running")
 	}
 	if !container2.State.Running {
 		t.Fatal("Container not running")
 	}
 
 	// Kill them
 	if err := container1.Kill(); err != nil {
 		t.Fatal(err)
 	}
 
 	if err := container2.Kill(); err != nil {
 		t.Fatal(err)
 	}
 }
a2d7dd1a
 
ca40989e
 func TestStdin(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"cat"},
031f91df
 
 		OpenStdin: true,
 	},
ca40989e
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.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)
 	}
4fdf11b2
 	hostConfig := &HostConfig{}
 	if err := container.Start(hostConfig); 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) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"cat"},
031f91df
 
 		OpenStdin: true,
 	},
ca40989e
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.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)
 	}
4fdf11b2
 	hostConfig := &HostConfig{}
 	if err := container.Start(hostConfig); 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
 	}
 }
 
fb350e0c
 func TestEnv(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
f03c1b8e
 		Cmd:   []string{"env"},
031f91df
 	},
fb350e0c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
232dbb18
 
fb350e0c
 	stdout, err := container.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer stdout.Close()
4fdf11b2
 	hostConfig := &HostConfig{}
 	if err := container.Start(hostConfig); err != nil {
fb350e0c
 		t.Fatal(err)
 	}
 	container.Wait()
 	output, err := ioutil.ReadAll(stdout)
 	if err != nil {
 		t.Fatal(err)
 	}
 	actualEnv := strings.Split(string(output), "\n")
 	if actualEnv[len(actualEnv)-1] == "" {
 		actualEnv = actualEnv[:len(actualEnv)-1]
 	}
 	sort.Strings(actualEnv)
 	goodEnv := []string{
 		"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
 		"HOME=/",
5c1af383
 		"container=lxc",
dde8f74c
 		"HOSTNAME=" + container.ShortID(),
fb350e0c
 	}
 	sort.Strings(goodEnv)
 	if len(goodEnv) != len(actualEnv) {
 		t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
 	}
 	for i := range goodEnv {
 		if actualEnv[i] != goodEnv[i] {
 			t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
 		}
 	}
 }
 
b16ff9f8
 func TestEntrypoint(t *testing.T) {
6bdb6f22
 	runtime := mkRuntime(t)
b16ff9f8
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(
b16ff9f8
 		&Config{
 			Image:      GetTestImage(runtime).ID,
 			Entrypoint: []string{"/bin/echo"},
 			Cmd:        []string{"-n", "foobar"},
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 	output, err := container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if string(output) != "foobar" {
 		t.Error(string(output))
 	}
 }
 
d00fb409
 func TestEntrypointNoCmd(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(
d00fb409
 		&Config{
 			Image:      GetTestImage(runtime).ID,
 			Entrypoint: []string{"/bin/echo", "foobar"},
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 	output, err := container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if strings.Trim(string(output), "\r\n") != "foobar" {
 		t.Error(string(output))
 	}
 }
 
4e5ae883
 func grepFile(t *testing.T, path string, pattern string) {
 	f, err := os.Open(path)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer f.Close()
 	r := bufio.NewReader(f)
 	var (
 		line string
 	)
 	err = nil
 	for err == nil {
 		line, err = r.ReadString('\n')
 		if strings.Contains(line, pattern) == true {
 			return
 		}
 	}
 	t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
 }
 
 func TestLXCConfig(t *testing.T) {
d4e62101
 	runtime := mkRuntime(t)
b8547f31
 	defer nuke(runtime)
94896183
 	// Memory is allocated randomly for testing
4e5ae883
 	rand.Seed(time.Now().UTC().UnixNano())
94896183
 	memMin := 33554432
 	memMax := 536870912
 	mem := memMin + rand.Intn(memMax-memMin)
efd9becb
 	// CPU shares as well
 	cpuMin := 100
 	cpuMax := 10000
 	cpu := cpuMin + rand.Intn(cpuMax-cpuMin)
675f1a80
 	container, _, err := runtime.Create(&Config{
fd224ee5
 		Image: GetTestImage(runtime).ID,
6ce64e84
 		Cmd:   []string{"/bin/true"},
031f91df
 
efd9becb
 		Hostname:  "foobar",
 		Memory:    int64(mem),
 		CpuShares: int64(cpu),
031f91df
 	},
4e5ae883
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container)
551092f9
 	container.generateLXCConfig(nil)
7c57a4cf
 	grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
 	grepFile(t, container.lxcConfigPath(),
94896183
 		fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
7c57a4cf
 	grepFile(t, container.lxcConfigPath(),
94896183
 		fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
4e5ae883
 }
 
551092f9
 func TestCustomLxcConfig(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(&Config{
551092f9
 		Image: GetTestImage(runtime).ID,
 		Cmd:   []string{"/bin/true"},
 
 		Hostname: "foobar",
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 	hostConfig := &HostConfig{LxcConf: []KeyValuePair{
 		{
 			Key:   "lxc.utsname",
 			Value: "docker",
 		},
 		{
 			Key:   "lxc.cgroup.cpuset.cpus",
 			Value: "0,1",
 		},
 	}}
 
 	container.generateLXCConfig(hostConfig)
 	grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
 	grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
 }
 
a2d7dd1a
 func BenchmarkRunSequencial(b *testing.B) {
6bdb6f22
 	runtime := mkRuntime(b)
b8547f31
 	defer nuke(runtime)
a2d7dd1a
 	for i := 0; i < b.N; i++ {
675f1a80
 		container, _, err := runtime.Create(&Config{
fd224ee5
 			Image: GetTestImage(runtime).ID,
6ce64e84
 			Cmd:   []string{"echo", "-n", "foo"},
031f91df
 		},
a2d7dd1a
 		)
 		if err != nil {
 			b.Fatal(err)
 		}
b8547f31
 		defer runtime.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
 		}
b8547f31
 		if err := runtime.Destroy(container); err != nil {
a2d7dd1a
 			b.Fatal(err)
 		}
 	}
 }
 
 func BenchmarkRunParallel(b *testing.B) {
6bdb6f22
 	runtime := mkRuntime(b)
b8547f31
 	defer nuke(runtime)
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) {
675f1a80
 			container, _, err := runtime.Create(&Config{
fd224ee5
 				Image: GetTestImage(runtime).ID,
6ce64e84
 				Cmd:   []string{"echo", "-n", "foo"},
031f91df
 			},
a2d7dd1a
 			)
 			if err != nil {
 				complete <- err
 				return
 			}
b8547f31
 			defer runtime.Destroy(container)
4fdf11b2
 			hostConfig := &HostConfig{}
 			if err := container.Start(hostConfig); 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))
 			// }
b8547f31
 			if err := runtime.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 {
4fdf11b2
 	tmpDir, err := ioutil.TempDir("", "docker-test")
 	if err != nil {
 		t.Fatal(err)
 	}
d4e62101
 	return tmpDir
 }
4fdf11b2
 
9cfbaecf
 // Test for #1737
 func TestCopyVolumeUidGid(t *testing.T) {
 	r := mkRuntime(t)
 	defer nuke(r)
 
 	// Add directory not owned by root
b45e280e
 	container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
9cfbaecf
 	defer r.Destroy(container1)
 
 	if container1.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	if err := container1.Run(); err != nil {
 		t.Fatal(err)
 	}
 	if container1.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 
 	rwTar, err := container1.ExportRw()
 	if err != nil {
 		t.Error(err)
 	}
 	img, err := r.graph.Create(rwTar, container1, "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
 	tmpDir1 := tempDir(t)
 	defer os.RemoveAll(tmpDir1)
b45e280e
 	stdout1, _ := runContainer(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")
 	}
 }
 
7a9c7118
 // Test for #1582
 func TestCopyVolumeContent(t *testing.T) {
 	r := mkRuntime(t)
 	defer nuke(r)
 
 	// Put some content in a directory of a container and commit it
 	container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
 	defer r.Destroy(container1)
 
 	if container1.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 	if err := container1.Run(); err != nil {
 		t.Fatal(err)
 	}
 	if container1.State.Running {
 		t.Errorf("Container shouldn't be running")
 	}
 
 	rwTar, err := container1.ExportRw()
 	if err != nil {
 		t.Error(err)
 	}
 	img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
 	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)
b45e280e
 	stdout1, _ := runContainer(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) {
 	r := mkRuntime(t)
 	defer nuke(r)
 	tmpDir := tempDir(t)
 	defer os.RemoveAll(tmpDir)
 	writeFile(path.Join(tmpDir, "touch-me"), "", t)
 
 	// Test reading from a read-only bind mount
eefbadd2
 	stdout, _ := runContainer(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
eefbadd2
 	runContainer(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
68b09cbe
 	if _, err := runContainer(r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
4fdf11b2
 		t.Fatal("Container bind mounted illegal directory")
d4e62101
 	}
4fdf11b2
 }
5ae8c7a9
 
 // Test that VolumesRW values are copied to the new container.  Regression test for #1201
 func TestVolumesFromReadonlyMount(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
675f1a80
 	container, _, err := runtime.Create(
5ae8c7a9
 		&Config{
 			Image:   GetTestImage(runtime).ID,
 			Cmd:     []string{"/bin/echo", "-n", "foobar"},
 			Volumes: map[string]struct{}{"/test": {}},
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 	_, err = container.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 	if !container.VolumesRW["/test"] {
 		t.Fail()
 	}
 
675f1a80
 	container2, _, err := runtime.Create(
5ae8c7a9
 		&Config{
 			Image:       GetTestImage(runtime).ID,
 			Cmd:         []string{"/bin/echo", "-n", "foobar"},
 			VolumesFrom: container.ID,
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container2)
 
 	_, err = container2.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 
 	if container.Volumes["/test"] != container2.Volumes["/test"] {
 		t.Fail()
 	}
 
 	actual, exists := container2.VolumesRW["/test"]
 	if !exists {
 		t.Fail()
 	}
 
 	if container.VolumesRW["/test"] != actual {
 		t.Fail()
 	}
 }
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) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 
675f1a80
 	container, _, err := runtime.Create(&Config{
92cbb7cc
 		Image:   GetTestImage(runtime).ID,
 		Cmd:     []string{"echo", "-n", "foobar"},
 		Volumes: map[string]struct{}{"/test": {}},
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 
 	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
 }
3342bdb3
 
3bd73a96
 // Test for #1351
 func TestVolumesFromWithVolumes(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 
675f1a80
 	container, _, err := runtime.Create(&Config{
3bd73a96
 		Image:   GetTestImage(runtime).ID,
 		Cmd:     []string{"sh", "-c", "echo -n bar > /test/foo"},
 		Volumes: map[string]struct{}{"/test": {}},
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 
 	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()
 	}
 
675f1a80
 	container2, _, err := runtime.Create(
3bd73a96
 		&Config{
 			Image:       GetTestImage(runtime).ID,
 			Cmd:         []string{"cat", "/test/foo"},
 			VolumesFrom: container.ID,
 			Volumes:     map[string]struct{}{"/test": {}},
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container2)
 
 	output, err := container2.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
 
 	if string(output) != "bar" {
 		t.Fail()
 	}
 
 	if container.Volumes["/test"] != container2.Volumes["/test"] {
 		t.Fail()
 	}
57b49efc
 
 	// Ensure it restarts successfully
 	_, err = container2.Output()
 	if err != nil {
 		t.Fatal(err)
 	}
3bd73a96
 }
 
3342bdb3
 func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 
 	config, hc, _, err := ParseRun([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show"}, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
675f1a80
 	c, _, err := runtime.Create(config)
3342bdb3
 	if err != nil {
 		t.Fatal(err)
 	}
 	stdout, err := c.StdoutPipe()
 	if err != nil {
 		t.Fatal(err)
 	}
 
 	defer runtime.Destroy(c)
 	if err := c.Start(hc); err != nil {
 		t.Fatal(err)
 	}
 	c.WaitTimeout(500 * time.Millisecond)
 	c.Wait()
 	output, err := ioutil.ReadAll(stdout)
 	if err != nil {
 		t.Fatal(err)
 	}
 
 	interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
 	if len(interfaces) != 1 {
84790aaf
 		t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
3342bdb3
 	}
84790aaf
 	if !strings.HasSuffix(interfaces[0], ": lo") {
 		t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
3342bdb3
 	}
 
 }
280901e5
 
 func TestPrivilegedCanMknod(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 	if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
 		t.Fatal("Could not mknod into privileged container")
 	}
 }
 
 func TestPrivilegedCanMount(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 	if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
 		t.Fatal("Could not mount into privileged container")
 	}
 }
 
 func TestPrivilegedCannotMknod(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 	if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
 		t.Fatal("Could mknod into secure container")
 	}
 }
 
 func TestPrivilegedCannotMount(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 	if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
 		t.Fatal("Could mount into secure container")
 	}
 }
b79bd4e8
 
 func TestMultipleVolumesFrom(t *testing.T) {
 	runtime := mkRuntime(t)
 	defer nuke(runtime)
 
7a3d4b1d
 	container, _, err := runtime.Create(&Config{
b79bd4e8
 		Image:   GetTestImage(runtime).ID,
 		Cmd:     []string{"sh", "-c", "echo -n bar > /test/foo"},
 		Volumes: map[string]struct{}{"/test": {}},
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 
 	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()
 	}
 
7a3d4b1d
 	container2, _, err := runtime.Create(
b79bd4e8
 		&Config{
 			Image:   GetTestImage(runtime).ID,
 			Cmd:     []string{"sh", "-c", "echo -n bar > /other/foo"},
 			Volumes: map[string]struct{}{"/other": {}},
 		},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container2)
 
 	for key := range container2.Config.Volumes {
 		if key != "/other" {
 			t.FailNow()
 		}
 	}
 	if _, err := container2.Output(); err != nil {
 		t.Fatal(err)
 	}
 
7a3d4b1d
 	container3, _, err := runtime.Create(
b79bd4e8
 		&Config{
 			Image:       GetTestImage(runtime).ID,
 			Cmd:         []string{"/bin/echo", "-n", "foobar"},
 			VolumesFrom: strings.Join([]string{container.ID, container2.ID}, ","),
 		})
 
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container3)
 
 	if _, err := container3.Output(); err != nil {
 		t.Fatal(err)
 	}
 
 	t.Log(container3.Volumes)
 	if container3.Volumes["/test"] != container.Volumes["/test"] {
 		t.Fail()
 	}
 	if container3.Volumes["/other"] != container2.Volumes["/other"] {
 		t.Fail()
 	}
 }