2013-01-19 04:13:39 +04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-03-12 06:15:29 +04:00
|
|
|
"bufio"
|
2013-01-28 23:51:51 +04:00
|
|
|
"fmt"
|
2013-11-14 10:10:20 +04:00
|
|
|
"github.com/dotcloud/docker"
|
2013-10-25 05:59:59 +04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-01-30 03:16:45 +04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-03-12 06:15:29 +04:00
|
|
|
"os"
|
2013-05-14 03:39:54 +04:00
|
|
|
"path"
|
2013-03-27 03:54:13 +04:00
|
|
|
"regexp"
|
2013-03-07 21:25:41 +04:00
|
|
|
"sort"
|
2013-02-14 05:24:35 +04:00
|
|
|
"strings"
|
2013-01-19 04:13:39 +04:00
|
|
|
"testing"
|
2013-01-28 23:51:51 +04:00
|
|
|
"time"
|
2013-01-19 04:13:39 +04:00
|
|
|
)
|
|
|
|
|
2013-06-04 22:00:22 +04:00
|
|
|
func TestIDFormat(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-27 03:54:13 +04:00
|
|
|
defer nuke(runtime)
|
2013-10-05 06:25:15 +04:00
|
|
|
container1, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-04-23 22:25:16 +04:00
|
|
|
Cmd: []string{"/bin/sh", "-c", "echo hello world"},
|
2013-03-27 03:54:13 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-03-27 03:54:13 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-04 22:00:22 +04:00
|
|
|
match, err := regexp.Match("^[0-9a-f]{64}$", []byte(container1.ID))
|
2013-03-27 03:54:13 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !match {
|
2013-06-04 22:00:22 +04:00
|
|
|
t.Fatalf("Invalid container ID: %s", container1.ID)
|
2013-03-27 03:54:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 21:56:49 +04:00
|
|
|
func TestMultipleAttachRestart(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-29 21:56:49 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-15 00:33:15 +04:00
|
|
|
container, _, _ := mkContainer(
|
2013-07-13 04:56:55 +04:00
|
|
|
runtime,
|
|
|
|
[]string{"_", "/bin/sh", "-c", "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
|
|
|
|
t,
|
2013-03-29 21:56:49 +04:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-03-29 21:56:49 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-04-18 18:03:50 +04:00
|
|
|
if err := container.Stop(10); err != nil {
|
2013-03-29 21:56:49 +04:00
|
|
|
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)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-03-29 21:56:49 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-23 22:22:30 +04:00
|
|
|
|
|
|
|
setTimeout(t, "Timeout reading from the process", 3*time.Second, func() {
|
2013-03-29 21:56:49 +04:00
|
|
|
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)
|
|
|
|
}
|
2013-04-23 22:22:30 +04:00
|
|
|
})
|
|
|
|
container.Wait()
|
2013-03-29 21:56:49 +04:00
|
|
|
}
|
|
|
|
|
2013-04-23 04:51:09 +04:00
|
|
|
func TestDiff(t *testing.T) {
|
2013-11-14 10:10:20 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-04-23 04:51:09 +04:00
|
|
|
defer nuke(runtime)
|
|
|
|
// Create a container and remove a file
|
2013-11-15 00:33:15 +04:00
|
|
|
container1, _, _ := mkContainer(runtime, []string{"_", "/bin/rm", "/etc/passwd"}, t)
|
2013-04-23 04:51:09 +04:00
|
|
|
defer runtime.Destroy(container1)
|
|
|
|
|
2013-08-30 23:45:01 +04:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2013-04-23 04:51:09 +04:00
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the changelog
|
2013-08-30 23:45:01 +04:00
|
|
|
c, err = container1.Changes()
|
2013-04-23 04:51:09 +04:00
|
|
|
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
|
2013-11-14 10:10:20 +04:00
|
|
|
img, err := runtime.Commit(container1, "", "", "unit test commited image - diff", "", nil)
|
2013-04-23 04:51:09 +04:00
|
|
|
if err != nil {
|
2013-11-09 02:54:20 +04:00
|
|
|
t.Fatal(err)
|
2013-04-23 04:51:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new container from the commited image
|
2013-11-15 00:33:15 +04:00
|
|
|
container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/etc/passwd"}, t)
|
2013-04-23 04:51:09 +04:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 21:02:57 +04:00
|
|
|
|
2013-08-12 21:53:06 +04:00
|
|
|
// Create a new container
|
2013-11-15 00:33:15 +04:00
|
|
|
container3, _, _ := mkContainer(runtime, []string{"_", "rm", "/bin/httpd"}, t)
|
2013-06-03 21:02:57 +04:00
|
|
|
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.")
|
|
|
|
}
|
2013-04-23 04:51:09 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:03:13 +04:00
|
|
|
func TestCommitAutoRun(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-04-26 04:03:13 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-15 00:33:15 +04:00
|
|
|
container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
|
2013-04-26 04:03:13 +04:00
|
|
|
defer runtime.Destroy(container1)
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-04-26 04:03:13 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-04-26 04:03:13 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
img, err := runtime.Commit(container1, "", "", "unit test commited image", "", &docker.Config{Cmd: []string{"cat", "/world"}})
|
2013-04-26 04:03:13 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
2013-11-15 00:33:15 +04:00
|
|
|
container2, _, _ := mkContainer(runtime, []string{img.ID}, t)
|
2013-04-26 04:03:13 +04:00
|
|
|
defer runtime.Destroy(container2)
|
|
|
|
stdout, err := container2.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
stderr, err := container2.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container2.Start(); err != nil {
|
2013-04-26 04:03:13 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 16:35:47 +04:00
|
|
|
func TestCommitRun(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-04-25 02:35:28 +04:00
|
|
|
|
2013-11-15 00:33:15 +04:00
|
|
|
container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container1)
|
2013-01-19 04:13:39 +04:00
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
2013-03-12 16:35:47 +04:00
|
|
|
if err := container1.Run(); err != nil {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
2013-03-12 16:35:47 +04:00
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
img, err := runtime.Commit(container1, "", "", "unit test commited image", "", nil)
|
2013-03-12 16:35:47 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
2013-11-15 00:33:15 +04:00
|
|
|
container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/world"}, t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container2)
|
2013-03-12 16:35:47 +04:00
|
|
|
stdout, err := container2.StdoutPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 16:35:47 +04:00
|
|
|
stderr, err := container2.StderrPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container2.Start(); err != nil {
|
2013-03-12 16:35:47 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container2.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 16:35:47 +04:00
|
|
|
output2, err := ioutil.ReadAll(stderr)
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdout.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stderr.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 16:35:47 +04:00
|
|
|
if string(output) != "hello\n" {
|
2013-03-30 11:22:56 +04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
|
2013-01-19 04:13:39 +04:00
|
|
|
}
|
2013-04-01 01:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStart(t *testing.T) {
|
2013-12-05 04:31:09 +04:00
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
container, _, _ := mkContainer(runtime, []string{"-i", "_", "/bin/cat"}, t)
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
cStdin, err := container.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give some time to the process to start
|
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
|
|
|
if !container.State.IsRunning() {
|
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
|
|
|
if err := container.Start(); err == nil {
|
|
|
|
t.Fatalf("A running container should be able to be started")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to avoid the timeout in destroy. Best effort, don't check error
|
|
|
|
cStdin.Close()
|
|
|
|
container.WaitTimeout(2 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCpuShares(t *testing.T) {
|
2013-11-06 19:57:43 +04:00
|
|
|
_, err1 := os.Stat("/sys/fs/cgroup/cpuacct,cpu")
|
|
|
|
_, err2 := os.Stat("/sys/fs/cgroup/cpu,cpuacct")
|
|
|
|
if err1 == nil || err2 == nil {
|
|
|
|
t.Skip("Fixme. Setting cpu cgroup shares doesn't work in dind on a Fedora host. The lxc utils are confused by the cpu,cpuacct mount.")
|
|
|
|
}
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-04-01 01:15:10 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-15 00:33:15 +04:00
|
|
|
container, _, _ := mkContainer(runtime, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
|
2013-04-01 01:15:10 +04:00
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
2013-05-10 06:19:24 +04:00
|
|
|
cStdin, err := container.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-04-01 01:15:10 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give some time to the process to start
|
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if !container.State.IsRunning() {
|
2013-04-01 01:15:10 +04:00
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err == nil {
|
2013-08-12 21:53:06 +04:00
|
|
|
t.Fatalf("A running container should be able to be started")
|
2013-04-01 01:15:10 +04:00
|
|
|
}
|
2013-04-01 09:42:10 +04:00
|
|
|
|
2013-08-12 21:53:06 +04:00
|
|
|
// Try to avoid the timeout in destroy. Best effort, don't check error
|
2013-04-01 09:42:10 +04:00
|
|
|
cStdin.Close()
|
2013-04-05 13:01:38 +04:00
|
|
|
container.WaitTimeout(2 * time.Second)
|
2013-01-19 04:13:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-15 00:33:15 +04:00
|
|
|
container, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-19 04:13:39 +04:00
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
if err := container.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutput(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-10-05 06:25:15 +04:00
|
|
|
container, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"echo", "-n", "foobar"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-19 04:13:39 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-19 04:13:39 +04:00
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "foobar" {
|
2013-11-22 00:21:03 +04:00
|
|
|
t.Fatalf("%s != %s", string(output), "foobar")
|
2013-01-19 04:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 12:53:00 +04:00
|
|
|
func TestContainerNetwork(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
2013-10-17 04:08:14 +04:00
|
|
|
container, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-09-18 12:53:00 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"ping", "-c", "1", "127.0.0.1"},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-09-18 12:53:00 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
if err := container.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if code := container.State.GetExitCode(); code != 0 {
|
|
|
|
t.Fatalf("Unexpected ping 127.0.0.1 exit code %d (expected 0)", code)
|
2013-09-18 12:53:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 03:21:19 +04:00
|
|
|
func TestKillDifferentUser(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-04-12 03:21:19 +04:00
|
|
|
defer nuke(runtime)
|
2013-08-09 21:50:58 +04:00
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-08-09 21:50:58 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
User: "daemon",
|
2013-04-12 03:21:19 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-04-12 03:21:19 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
2013-11-14 10:10:20 +04:00
|
|
|
// 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()
|
2013-04-12 03:21:19 +04:00
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-04-12 03:21:19 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-04-12 03:21:19 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:37 +04:00
|
|
|
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
|
2013-11-22 00:21:03 +04:00
|
|
|
for !container.State.IsRunning() {
|
2013-07-02 04:19:39 +04:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
})
|
2013-04-12 03:21:19 +04:00
|
|
|
|
2013-08-09 21:50:58 +04:00
|
|
|
setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
|
|
|
|
out, _ := container.StdoutPipe()
|
|
|
|
in, _ := container.StdinPipe()
|
2013-11-29 04:12:45 +04:00
|
|
|
if err := assertPipe("hello\n", "hello", out, in, 150); err != nil {
|
2013-08-09 21:50:58 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
2013-07-02 14:47:37 +04:00
|
|
|
|
2013-04-12 03:21:19 +04:00
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-04-12 03:21:19 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
container.Wait()
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-04-12 03:21:19 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
// Try stopping twice
|
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 00:17:52 +04:00
|
|
|
// Test that creating a container with a volume doesn't crash. Regression test for #995.
|
|
|
|
func TestCreateVolume(t *testing.T) {
|
2013-11-14 10:10:20 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-06-23 00:17:52 +04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
config, hc, _, err := docker.ParseRun([]string{"-v", "/var/lib/data", unitTestImageID, "echo", "hello", "world"}, nil)
|
2013-06-23 00:17:52 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
jobCreate := eng.Job("create")
|
|
|
|
if err := jobCreate.ImportEnv(config); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var id string
|
2013-11-20 11:37:03 +04:00
|
|
|
jobCreate.Stdout.AddString(&id)
|
2013-11-14 10:10:20 +04:00
|
|
|
if err := jobCreate.Run(); err != nil {
|
2013-06-23 00:17:52 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
jobStart := eng.Job("start", id)
|
|
|
|
if err := jobStart.ImportEnv(hc); err != nil {
|
2013-06-23 00:17:52 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
if err := jobStart.Run(); err != nil {
|
2013-06-23 00:17:52 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
// FIXME: this hack can be removed once Wait is a job
|
|
|
|
c := runtime.Get(id)
|
|
|
|
if c == nil {
|
|
|
|
t.Fatalf("Couldn't retrieve container %s from runtime", id)
|
|
|
|
}
|
2013-06-23 00:17:52 +04:00
|
|
|
c.WaitTimeout(500 * time.Millisecond)
|
|
|
|
c.Wait()
|
|
|
|
}
|
|
|
|
|
2013-01-22 06:03:23 +04:00
|
|
|
func TestKill(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-06-25 18:52:10 +04:00
|
|
|
Cmd: []string{"sleep", "2"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-19 04:13:39 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-19 04:13:39 +04:00
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-12 03:21:19 +04:00
|
|
|
|
|
|
|
// Give some time to lxc to spawn the process
|
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if !container.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
2013-01-22 06:03:23 +04:00
|
|
|
if err := container.Kill(); err != nil {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
container.Wait()
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.IsRunning() {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
// Try stopping twice
|
2013-01-22 06:03:23 +04:00
|
|
|
if err := container.Kill(); err != nil {
|
2013-01-19 04:13:39 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExitCode(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-01-19 04:13:39 +04:00
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
trueContainer, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-11-08 21:08:18 +04:00
|
|
|
Cmd: []string{"/bin/true"},
|
2013-10-29 03:58:59 +04:00
|
|
|
}, "")
|
2013-01-19 04:13:39 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(trueContainer)
|
2013-01-19 04:13:39 +04:00
|
|
|
if err := trueContainer.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if code := trueContainer.State.GetExitCode(); code != 0 {
|
|
|
|
t.Fatalf("Unexpected exit code %d (expected 0)", code)
|
2013-03-29 18:44:58 +04:00
|
|
|
}
|
2013-01-19 04:13:39 +04:00
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
falseContainer, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-11-08 21:08:18 +04:00
|
|
|
Cmd: []string{"/bin/false"},
|
2013-10-29 03:58:59 +04:00
|
|
|
}, "")
|
2013-01-19 04:13:39 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(falseContainer)
|
2013-01-19 04:13:39 +04:00
|
|
|
if err := falseContainer.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if code := falseContainer.State.GetExitCode(); code != 1 {
|
|
|
|
t.Fatalf("Unexpected exit code %d (expected 1)", code)
|
2013-01-19 04:13:39 +04:00
|
|
|
}
|
|
|
|
}
|
2013-01-23 05:30:09 +04:00
|
|
|
|
2013-02-14 06:17:42 +04:00
|
|
|
func TestRestart(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"echo", "-n", "foobar"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-02-14 06:17:42 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-14 06:17:42 +04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 07:05:57 +04:00
|
|
|
func TestRestartStdin(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 23:16:58 +04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-02-14 07:05:57 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-14 07:05:57 +04:00
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-14 07:05:57 +04:00
|
|
|
stdout, err := container.StdoutPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-02-14 07:05:57 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-29 18:44:58 +04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-14 07:05:57 +04:00
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdout.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-14 07:05:57 +04:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 18:44:58 +04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-02-14 07:05:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restart and try again
|
|
|
|
stdin, err = container.StdinPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-14 07:05:57 +04:00
|
|
|
stdout, err = container.StdoutPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-02-14 07:05:57 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-29 18:44:58 +04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-14 07:05:57 +04:00
|
|
|
container.Wait()
|
|
|
|
output, err = ioutil.ReadAll(stdout)
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdout.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-14 07:05:57 +04:00
|
|
|
if string(output) != "hello world #2" {
|
2013-03-29 18:44:58 +04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
|
2013-02-14 07:05:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 05:24:35 +04:00
|
|
|
func TestUser(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-02-14 05:24:35 +04:00
|
|
|
|
|
|
|
// Default user must be root
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"id"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-02-14 05:24:35 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-14 05:24:35 +04:00
|
|
|
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
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err = runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"id"},
|
2013-03-23 23:16:58 +04:00
|
|
|
|
|
|
|
User: "root",
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-02-14 05:24:35 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-14 05:24:35 +04:00
|
|
|
output, err = container.Output()
|
2013-11-22 00:21:03 +04:00
|
|
|
if code := container.State.GetExitCode(); err != nil || code != 0 {
|
2013-02-14 05:24:35 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a UID
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err = runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"id"},
|
2013-03-23 23:16:58 +04:00
|
|
|
|
|
|
|
User: "0",
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-02-14 05:24:35 +04:00
|
|
|
)
|
2013-11-22 00:21:03 +04:00
|
|
|
if code := container.State.GetExitCode(); err != nil || code != 0 {
|
2013-02-14 05:24:35 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-14 05:24:35 +04:00
|
|
|
output, err = container.Output()
|
2013-11-22 00:21:03 +04:00
|
|
|
if code := container.State.GetExitCode(); err != nil || code != 0 {
|
2013-02-14 05:24:35 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a different user by uid
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err = runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"id"},
|
2013-03-23 23:16:58 +04:00
|
|
|
|
|
|
|
User: "1",
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-02-14 05:24:35 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-14 05:24:35 +04:00
|
|
|
output, err = container.Output()
|
2013-02-27 05:45:46 +04:00
|
|
|
if err != nil {
|
2013-02-14 05:24:35 +04:00
|
|
|
t.Fatal(err)
|
2013-11-22 00:21:03 +04:00
|
|
|
} else if code := container.State.GetExitCode(); code != 0 {
|
|
|
|
t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", code, output)
|
2013-02-14 05:24:35 +04:00
|
|
|
}
|
|
|
|
if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a different user by username
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err = runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"id"},
|
2013-03-23 23:16:58 +04:00
|
|
|
|
|
|
|
User: "daemon",
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-02-14 05:24:35 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-14 05:24:35 +04:00
|
|
|
output, err = container.Output()
|
2013-11-22 00:21:03 +04:00
|
|
|
if code := container.State.GetExitCode(); err != nil || code != 0 {
|
2013-02-14 05:24:35 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
2013-07-02 04:08:42 +04:00
|
|
|
|
|
|
|
// Test an wrong username
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err = runtime.Create(&docker.Config{
|
2013-07-02 04:08:42 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"id"},
|
|
|
|
|
2013-08-12 21:53:06 +04:00
|
|
|
User: "unknownuser",
|
2013-07-02 04:08:42 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-07-02 04:08:42 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
output, err = container.Output()
|
2013-11-22 00:21:03 +04:00
|
|
|
if container.State.GetExitCode() == 0 {
|
2013-07-02 04:08:42 +04:00
|
|
|
t.Fatal("Starting container with wrong uid should fail but it passed.")
|
|
|
|
}
|
2013-02-14 05:24:35 +04:00
|
|
|
}
|
|
|
|
|
2013-01-23 05:30:09 +04:00
|
|
|
func TestMultipleContainers(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-01-23 05:30:09 +04:00
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
container1, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-06-25 18:52:10 +04:00
|
|
|
Cmd: []string{"sleep", "2"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-23 05:30:09 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container1)
|
2013-01-23 05:30:09 +04:00
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
container2, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-06-25 18:52:10 +04:00
|
|
|
Cmd: []string{"sleep", "2"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-23 05:30:09 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container2)
|
2013-01-23 05:30:09 +04:00
|
|
|
|
|
|
|
// Start both containers
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container1.Start(); err != nil {
|
2013-01-23 05:30:09 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container2.Start(); err != nil {
|
2013-01-23 05:30:09 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-04-12 03:21:19 +04:00
|
|
|
// Make sure they are running before trying to kill them
|
|
|
|
container1.WaitTimeout(250 * time.Millisecond)
|
|
|
|
container2.WaitTimeout(250 * time.Millisecond)
|
|
|
|
|
2013-01-23 05:30:09 +04:00
|
|
|
// If we are here, both containers should be running
|
2013-11-22 00:21:03 +04:00
|
|
|
if !container1.State.IsRunning() {
|
2013-01-23 05:30:09 +04:00
|
|
|
t.Fatal("Container not running")
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if !container2.State.IsRunning() {
|
2013-01-23 05:30:09 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 23:51:51 +04:00
|
|
|
|
2013-01-30 03:16:45 +04:00
|
|
|
func TestStdin(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 23:16:58 +04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-30 03:16:45 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-30 03:16:45 +04:00
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-30 03:16:45 +04:00
|
|
|
stdout, err := container.StdoutPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-03-29 18:44:58 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-30 03:16:45 +04:00
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
2013-03-29 18:44:58 +04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
2013-01-30 03:16:45 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-30 03:16:45 +04:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 18:44:58 +04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-01-30 03:16:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTty(t *testing.T) {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 23:16:58 +04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-30 03:16:45 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-30 03:16:45 +04:00
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-30 03:16:45 +04:00
|
|
|
stdout, err := container.StdoutPipe()
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-03-29 18:44:58 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-30 03:16:45 +04:00
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
2013-03-29 18:44:58 +04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
2013-01-30 03:16:45 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 18:44:58 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-30 03:16:45 +04:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 18:44:58 +04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-01-30 03:16:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 21:25:41 +04:00
|
|
|
func TestEnv(t *testing.T) {
|
2013-08-14 04:31:04 +04:00
|
|
|
os.Setenv("TRUE", "false")
|
2013-08-15 04:14:30 +04:00
|
|
|
os.Setenv("TRICKY", "tri\ncky\n")
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-11-14 10:10:20 +04:00
|
|
|
config, _, _, err := docker.ParseRun([]string{"-e=FALSE=true", "-e=TRUE", "-e=TRICKY", GetTestImage(runtime).ID, "env"}, nil)
|
2013-08-14 04:31:04 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container, _, err := runtime.Create(config, "")
|
2013-03-07 21:25:41 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-03-29 18:44:58 +04:00
|
|
|
|
2013-03-07 21:25:41 +04:00
|
|
|
stdout, err := container.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer stdout.Close()
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-03-07 21:25:41 +04:00
|
|
|
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=/",
|
2013-07-22 20:26:05 +04:00
|
|
|
"container=lxc",
|
2013-10-25 05:59:59 +04:00
|
|
|
"HOSTNAME=" + utils.TruncateID(container.ID),
|
2013-08-14 04:31:04 +04:00
|
|
|
"FALSE=true",
|
|
|
|
"TRUE=false",
|
2013-08-15 04:14:30 +04:00
|
|
|
"TRICKY=tri",
|
|
|
|
"cky",
|
|
|
|
"",
|
2013-03-07 21:25:41 +04:00
|
|
|
}
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 06:20:05 +04:00
|
|
|
func TestEntrypoint(t *testing.T) {
|
2013-07-12 04:59:25 +04:00
|
|
|
runtime := mkRuntime(t)
|
2013-06-25 06:20:05 +04:00
|
|
|
defer nuke(runtime)
|
2013-10-05 06:25:15 +04:00
|
|
|
container, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-06-25 06:20:05 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Entrypoint: []string{"/bin/echo"},
|
|
|
|
Cmd: []string{"-n", "foobar"},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-06-25 06:20:05 +04:00
|
|
|
)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 18:03:42 +04:00
|
|
|
func TestEntrypointNoCmd(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
2013-10-05 06:25:15 +04:00
|
|
|
container, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-08-05 18:03:42 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Entrypoint: []string{"/bin/echo", "foobar"},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-08-05 18:03:42 +04:00
|
|
|
)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 23:51:51 +04:00
|
|
|
func BenchmarkRunSequencial(b *testing.B) {
|
2013-07-12 04:59:25 +04:00
|
|
|
runtime := mkRuntime(b)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-01-28 23:51:51 +04:00
|
|
|
for i := 0; i < b.N; i++ {
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"echo", "-n", "foo"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-28 23:51:51 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-28 23:51:51 +04:00
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "foo" {
|
2013-03-30 11:22:56 +04:00
|
|
|
b.Fatalf("Unexpected output: %s", output)
|
2013-01-28 23:51:51 +04:00
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-28 23:51:51 +04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRunParallel(b *testing.B) {
|
2013-07-12 04:59:25 +04:00
|
|
|
runtime := mkRuntime(b)
|
2013-03-21 11:41:15 +04:00
|
|
|
defer nuke(runtime)
|
2013-01-28 23:51:51 +04:00
|
|
|
|
|
|
|
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) {
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-06-04 22:00:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 23:39:09 +04:00
|
|
|
Cmd: []string{"echo", "-n", "foo"},
|
2013-03-23 23:16:58 +04:00
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-01-28 23:51:51 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
complete <- err
|
|
|
|
return
|
|
|
|
}
|
2013-03-21 11:41:15 +04:00
|
|
|
defer runtime.Destroy(container)
|
2013-11-01 01:58:43 +04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-01-28 23:51:51 +04:00
|
|
|
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))
|
|
|
|
// }
|
2013-03-21 11:41:15 +04:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-28 23:51:51 +04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2013-05-14 03:39:54 +04:00
|
|
|
|
2013-06-25 10:30:48 +04:00
|
|
|
func tempDir(t *testing.T) string {
|
2013-10-17 00:44:15 +04:00
|
|
|
tmpDir, err := ioutil.TempDir("", "docker-test-container")
|
2013-05-14 03:39:54 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-25 10:30:48 +04:00
|
|
|
return tmpDir
|
|
|
|
}
|
2013-05-14 03:39:54 +04:00
|
|
|
|
2013-08-30 04:22:08 +04:00
|
|
|
// Test for #1737
|
|
|
|
func TestCopyVolumeUidGid(t *testing.T) {
|
2013-11-15 00:33:15 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
r := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer r.Nuke()
|
2013-08-30 04:22:08 +04:00
|
|
|
|
|
|
|
// Add directory not owned by root
|
2013-11-15 00:33:15 +04:00
|
|
|
container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
|
2013-08-30 04:22:08 +04:00
|
|
|
defer r.Destroy(container1)
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-08-30 04:22:08 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-08-30 04:22:08 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
|
2013-08-30 04:22:08 +04:00
|
|
|
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)
|
2013-11-15 00:33:15 +04:00
|
|
|
stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
|
2013-08-30 04:22:08 +04:00
|
|
|
if !strings.Contains(stdout1, "daemon daemon") {
|
|
|
|
t.Fatal("Container failed to transfer uid and gid to volume")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 04:29:35 +04:00
|
|
|
// Test for #1582
|
|
|
|
func TestCopyVolumeContent(t *testing.T) {
|
2013-11-15 00:33:15 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
r := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer r.Nuke()
|
2013-08-30 04:29:35 +04:00
|
|
|
|
|
|
|
// Put some content in a directory of a container and commit it
|
2013-11-15 00:33:15 +04:00
|
|
|
container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
|
2013-08-30 04:29:35 +04:00
|
|
|
defer r.Destroy(container1)
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-08-30 04:29:35 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
if container1.State.IsRunning() {
|
2013-08-30 04:29:35 +04:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
|
2013-08-30 04:29:35 +04:00
|
|
|
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)
|
2013-11-15 00:33:15 +04:00
|
|
|
stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
|
2013-08-30 04:29:35 +04:00
|
|
|
if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
|
|
|
|
t.Fatal("Container failed to transfer content to volume")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 10:30:48 +04:00
|
|
|
func TestBindMounts(t *testing.T) {
|
2013-11-15 00:33:15 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
r := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer r.Nuke()
|
|
|
|
|
2013-06-25 10:30:48 +04:00
|
|
|
tmpDir := tempDir(t)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
writeFile(path.Join(tmpDir, "touch-me"), "", t)
|
|
|
|
|
|
|
|
// Test reading from a read-only bind mount
|
2013-11-15 00:33:15 +04:00
|
|
|
stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
|
2013-06-25 10:30:48 +04:00
|
|
|
if !strings.Contains(stdout, "touch-me") {
|
2013-05-14 03:39:54 +04:00
|
|
|
t.Fatal("Container failed to read from bind mount")
|
|
|
|
}
|
2013-06-25 10:30:48 +04:00
|
|
|
|
2013-05-14 03:39:54 +04:00
|
|
|
// test writing to bind mount
|
2013-11-15 00:33:15 +04:00
|
|
|
runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
|
2013-06-26 23:04:55 +04:00
|
|
|
readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
|
2013-05-14 03:39:54 +04:00
|
|
|
|
|
|
|
// test mounting to an illegal destination directory
|
2013-11-15 00:33:15 +04:00
|
|
|
if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
|
2013-05-14 03:39:54 +04:00
|
|
|
t.Fatal("Container bind mounted illegal directory")
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
2013-11-20 03:27:01 +04:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
2013-05-14 03:39:54 +04:00
|
|
|
}
|
2013-07-15 07:23:20 +04:00
|
|
|
|
2013-10-14 00:58:54 +04:00
|
|
|
// Test that -volumes-from supports both read-only mounts
|
|
|
|
func TestFromVolumesInReadonlyMode(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
container, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-11-08 05:46:22 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/echo", "-n", "foobar"},
|
|
|
|
Volumes: map[string]struct{}{"/test": {}},
|
2013-10-14 00:58:54 +04:00
|
|
|
},
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
container2, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-10-14 00:58:54 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/echo", "-n", "foobar"},
|
|
|
|
VolumesFrom: container.ID + ":ro",
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
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"] {
|
2013-11-08 05:46:22 +04:00
|
|
|
t.Logf("container volumes do not match: %s | %s ",
|
2013-10-14 00:58:54 +04:00
|
|
|
container.Volumes["/test"],
|
|
|
|
container2.Volumes["/test"])
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, exists := container2.VolumesRW["/test"]
|
|
|
|
if !exists {
|
|
|
|
t.Logf("container2 is missing '/test' volume: %s", container2.VolumesRW)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
if container2.VolumesRW["/test"] != false {
|
|
|
|
t.Log("'/test' volume mounted in read-write mode, expected read-only")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 07:23:20 +04:00
|
|
|
// 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)
|
2013-10-05 06:25:15 +04:00
|
|
|
container, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-07-15 07:23:20 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/echo", "-n", "foobar"},
|
|
|
|
Volumes: map[string]struct{}{"/test": {}},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-07-15 07:23:20 +04:00
|
|
|
)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2013-10-05 06:25:15 +04:00
|
|
|
container2, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-07-15 07:23:20 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/echo", "-n", "foobar"},
|
|
|
|
VolumesFrom: container.ID,
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-07-15 07:23:20 +04:00
|
|
|
)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2013-07-15 04:26:57 +04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-07-15 04:26:57 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"echo", "-n", "foobar"},
|
|
|
|
Volumes: map[string]struct{}{"/test": {}},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-07-15 04:26:57 +04:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
2013-05-14 03:39:54 +04:00
|
|
|
|
2013-07-15 04:26:57 +04:00
|
|
|
actual := container.Volumes["/test"]
|
|
|
|
if expected != actual {
|
|
|
|
t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
2013-05-14 03:39:54 +04:00
|
|
|
}
|
2013-07-22 04:11:47 +04:00
|
|
|
|
2013-08-10 08:55:23 +04:00
|
|
|
// Test for #1351
|
|
|
|
func TestVolumesFromWithVolumes(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-08-10 08:55:23 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
|
|
|
|
Volumes: map[string]struct{}{"/test": {}},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-08-10 08:55:23 +04:00
|
|
|
)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2013-10-05 06:25:15 +04:00
|
|
|
container2, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-08-10 08:55:23 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"cat", "/test/foo"},
|
|
|
|
VolumesFrom: container.ID,
|
|
|
|
Volumes: map[string]struct{}{"/test": {}},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-08-10 08:55:23 +04:00
|
|
|
)
|
|
|
|
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()
|
|
|
|
}
|
2013-08-10 10:37:57 +04:00
|
|
|
|
|
|
|
// Ensure it restarts successfully
|
|
|
|
_, err = container2.Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-08-10 08:55:23 +04:00
|
|
|
}
|
|
|
|
|
2013-07-22 04:11:47 +04:00
|
|
|
func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
|
2013-11-14 10:10:20 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
2013-07-22 04:11:47 +04:00
|
|
|
defer nuke(runtime)
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
config, hc, _, err := docker.ParseRun([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show"}, nil)
|
2013-07-22 04:11:47 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
|
|
|
|
jobCreate := eng.Job("create")
|
|
|
|
if err := jobCreate.ImportEnv(config); err != nil {
|
2013-07-22 04:11:47 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
var id string
|
2013-11-20 11:37:03 +04:00
|
|
|
jobCreate.Stdout.AddString(&id)
|
2013-11-14 10:10:20 +04:00
|
|
|
if err := jobCreate.Run(); err != nil {
|
2013-07-22 04:11:47 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
// FIXME: this hack can be removed once Wait is a job
|
|
|
|
c := runtime.Get(id)
|
|
|
|
if c == nil {
|
|
|
|
t.Fatalf("Couldn't retrieve container %s from runtime", id)
|
|
|
|
}
|
2013-07-22 04:11:47 +04:00
|
|
|
stdout, err := c.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
|
|
|
|
jobStart := eng.Job("start", id)
|
|
|
|
if err := jobStart.ImportEnv(hc); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := jobStart.Run(); err != nil {
|
2013-07-22 04:11:47 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
|
2013-07-22 04:11:47 +04:00
|
|
|
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 {
|
2013-08-07 05:31:05 +04:00
|
|
|
t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
|
2013-07-22 04:11:47 +04:00
|
|
|
}
|
2013-08-07 05:31:05 +04:00
|
|
|
if !strings.HasSuffix(interfaces[0], ": lo") {
|
|
|
|
t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
|
2013-07-22 04:11:47 +04:00
|
|
|
}
|
|
|
|
}
|
2013-08-10 02:53:02 +04:00
|
|
|
|
|
|
|
func TestPrivilegedCanMknod(t *testing.T) {
|
2013-11-15 00:33:15 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer runtime.Nuke()
|
|
|
|
if output, _ := runContainer(eng, runtime, []string{"-privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
|
2013-08-10 02:53:02 +04:00
|
|
|
t.Fatal("Could not mknod into privileged container")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrivilegedCanMount(t *testing.T) {
|
2013-11-15 00:33:15 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer runtime.Nuke()
|
|
|
|
if output, _ := runContainer(eng, runtime, []string{"-privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
|
2013-08-10 02:53:02 +04:00
|
|
|
t.Fatal("Could not mount into privileged container")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrivilegedCannotMknod(t *testing.T) {
|
2013-11-15 00:33:15 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer runtime.Nuke()
|
|
|
|
if output, _ := runContainer(eng, runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
|
2013-08-10 02:53:02 +04:00
|
|
|
t.Fatal("Could mknod into secure container")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrivilegedCannotMount(t *testing.T) {
|
2013-11-15 00:33:15 +04:00
|
|
|
eng := NewTestEngine(t)
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
|
|
|
defer runtime.Nuke()
|
|
|
|
if output, _ := runContainer(eng, runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
|
2013-08-10 02:53:02 +04:00
|
|
|
t.Fatal("Could mount into secure container")
|
|
|
|
}
|
|
|
|
}
|
2013-09-15 00:09:15 +04:00
|
|
|
|
|
|
|
func TestMultipleVolumesFrom(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
container, _, err := runtime.Create(&docker.Config{
|
2013-09-15 00:09:15 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
|
|
|
|
Volumes: map[string]struct{}{"/test": {}},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-09-15 00:09:15 +04:00
|
|
|
)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2013-10-05 06:25:15 +04:00
|
|
|
container2, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-09-15 00:09:15 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"sh", "-c", "echo -n bar > /other/foo"},
|
|
|
|
Volumes: map[string]struct{}{"/other": {}},
|
|
|
|
},
|
2013-10-29 03:58:59 +04:00
|
|
|
"",
|
2013-09-15 00:09:15 +04:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2013-10-05 06:25:15 +04:00
|
|
|
container3, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-09-15 00:09:15 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"/bin/echo", "-n", "foobar"},
|
|
|
|
VolumesFrom: strings.Join([]string{container.ID, container2.ID}, ","),
|
2013-10-29 03:58:59 +04:00
|
|
|
}, "")
|
2013-09-15 00:09:15 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container3)
|
|
|
|
|
|
|
|
if _, err := container3.Output(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if container3.Volumes["/test"] != container.Volumes["/test"] {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if container3.Volumes["/other"] != container2.Volumes["/other"] {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2013-10-26 17:51:56 +04:00
|
|
|
|
|
|
|
func TestRestartGhost(t *testing.T) {
|
|
|
|
runtime := mkRuntime(t)
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
2013-10-27 17:04:57 +04:00
|
|
|
container, _, err := runtime.Create(
|
2013-11-14 10:10:20 +04:00
|
|
|
&docker.Config{
|
2013-10-27 17:04:57 +04:00
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
|
|
|
|
Volumes: map[string]struct{}{"/test": {}},
|
|
|
|
},
|
|
|
|
"",
|
2013-10-26 17:51:56 +04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
|
2013-10-26 17:51:56 +04:00
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
container.State.SetGhost(true)
|
2013-10-26 17:51:56 +04:00
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
_, err = container.Output()
|
2013-10-26 17:51:56 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-10-27 17:04:57 +04:00
|
|
|
}
|