2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-03-12 08:35:47 -04:00
|
|
|
"./fs"
|
2013-03-11 22:15:29 -04:00
|
|
|
"bufio"
|
2013-01-28 14:51:51 -05:00
|
|
|
"fmt"
|
2013-01-29 18:16:45 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-03-11 22:15:29 -04:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
2013-03-07 12:25:41 -05:00
|
|
|
"sort"
|
2013-02-13 20:24:35 -05:00
|
|
|
"strings"
|
2013-01-18 19:13:39 -05:00
|
|
|
"testing"
|
2013-01-28 14:51:51 -05:00
|
|
|
"time"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2013-03-12 08:35:47 -04:00
|
|
|
func TestCommitRun(t *testing.T) {
|
2013-01-18 19:13:39 -05:00
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-03-12 08:35:47 -04:00
|
|
|
container1, err := docker.Create(
|
|
|
|
"precommit_test",
|
|
|
|
"/bin/sh",
|
|
|
|
[]string{"-c", "echo hello > /world"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{
|
2013-03-11 22:25:02 -04:00
|
|
|
Memory: 33554432,
|
2013-01-18 19:13:39 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 08:35:47 -04:00
|
|
|
defer docker.Destroy(container1)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2013-03-12 08:35:47 -04:00
|
|
|
if container1.State.Running {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
2013-03-12 08:35:47 -04:00
|
|
|
if err := container1.Run(); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 08:35:47 -04:00
|
|
|
if container1.State.Running {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
2013-03-12 08:35:47 -04:00
|
|
|
|
|
|
|
// FIXME: freeze the container before copying it to avoid data corruption?
|
|
|
|
rwTar, err := fs.Tar(container1.Mountpoint.Rw, fs.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// Create a new image from the container's base layers + a new layer from container changes
|
|
|
|
parentImg, err := docker.Store.Get(container1.Image)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := docker.Store.Create(rwTar, parentImg, "test_commitrun", "unit test commited image")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
|
|
|
|
|
|
|
container2, err := docker.Create(
|
|
|
|
"postcommit_test",
|
|
|
|
"cat",
|
|
|
|
[]string{"/world"},
|
|
|
|
img,
|
|
|
|
&Config{
|
2013-03-12 11:33:21 -04:00
|
|
|
Memory: 33554432,
|
2013-03-12 08:36:37 -04:00
|
|
|
},
|
|
|
|
)
|
2013-03-12 08:35:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container2)
|
|
|
|
|
|
|
|
stdout, err := container2.StdoutPipe()
|
|
|
|
stderr, err := container2.StderrPipe()
|
|
|
|
if err := container2.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container2.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
|
|
|
output2, err := ioutil.ReadAll(stderr)
|
|
|
|
stdout.Close()
|
|
|
|
stderr.Close()
|
|
|
|
if string(output) != "hello\n" {
|
|
|
|
t.Fatalf("\nout: %s\nerr: %s\n", string(output), string(output2))
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-18 19:13:39 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"run_test",
|
|
|
|
"ls",
|
|
|
|
[]string{"-al"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{
|
2013-03-11 22:25:02 -04:00
|
|
|
Memory: 33554432,
|
2013-01-18 19:13:39 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-18 19:13:39 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"output_test",
|
|
|
|
"echo",
|
|
|
|
[]string{"-n", "foobar"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "foobar" {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 21:03:23 -05:00
|
|
|
func TestKill(t *testing.T) {
|
2013-01-18 19:13:39 -05:00
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-18 19:13:39 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"stop_test",
|
2013-01-21 21:03:23 -05:00
|
|
|
"cat",
|
|
|
|
[]string{"/dev/zero"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
|
|
|
|
if container.State.Running {
|
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !container.State.Running {
|
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
2013-01-21 21:03:23 -05:00
|
|
|
if err := container.Kill(); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
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
|
2013-01-21 21:03:23 -05:00
|
|
|
if err := container.Kill(); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExitCode(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
trueContainer, err := docker.Create(
|
|
|
|
"exit_test_1",
|
|
|
|
"/bin/true",
|
|
|
|
[]string{""},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(trueContainer)
|
|
|
|
if err := trueContainer.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
falseContainer, err := docker.Create(
|
|
|
|
"exit_test_2",
|
|
|
|
"/bin/false",
|
|
|
|
[]string{""},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(falseContainer)
|
|
|
|
if err := falseContainer.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if trueContainer.State.ExitCode != 0 {
|
|
|
|
t.Errorf("Unexpected exit code %v", trueContainer.State.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if falseContainer.State.ExitCode != 1 {
|
|
|
|
t.Errorf("Unexpected exit code %v", falseContainer.State.ExitCode)
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 20:30:09 -05:00
|
|
|
|
2013-02-13 21:17:42 -05:00
|
|
|
func TestRestart(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-02-13 21:17:42 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"restart_test",
|
|
|
|
"echo",
|
|
|
|
[]string{"-n", "foobar"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-02-13 21:17:42 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
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-13 22:05:57 -05:00
|
|
|
func TestRestartStdin(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-02-13 22:05:57 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"restart_stdin_test",
|
|
|
|
"cat",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-02-13 22:05:57 -05:00
|
|
|
&Config{
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
|
|
|
stdout, err := container.StdoutPipe()
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
io.WriteString(stdin, "hello world")
|
|
|
|
stdin.Close()
|
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
|
|
|
stdout.Close()
|
|
|
|
if string(output) != "hello world" {
|
|
|
|
t.Fatal(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart and try again
|
|
|
|
stdin, err = container.StdinPipe()
|
|
|
|
stdout, err = container.StdoutPipe()
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
io.WriteString(stdin, "hello world #2")
|
|
|
|
stdin.Close()
|
|
|
|
container.Wait()
|
|
|
|
output, err = ioutil.ReadAll(stdout)
|
|
|
|
stdout.Close()
|
|
|
|
if string(output) != "hello world #2" {
|
|
|
|
t.Fatal(string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:24:35 -05:00
|
|
|
func TestUser(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-02-13 20:24:35 -05:00
|
|
|
|
|
|
|
// Default user must be root
|
|
|
|
container, err := docker.Create(
|
|
|
|
"user_default",
|
|
|
|
"id",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-02-13 20:24:35 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
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
|
|
|
|
container, err = docker.Create(
|
|
|
|
"user_root",
|
|
|
|
"id",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-02-13 20:24:35 -05:00
|
|
|
&Config{
|
|
|
|
User: "root",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
output, err = container.Output()
|
2013-02-15 15:17:58 -05:00
|
|
|
if err != nil || container.State.ExitCode != 0 {
|
2013-02-13 20:24:35 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a UID
|
|
|
|
container, err = docker.Create(
|
|
|
|
"user_uid0",
|
|
|
|
"id",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-02-13 20:24:35 -05:00
|
|
|
&Config{
|
|
|
|
User: "0",
|
|
|
|
},
|
|
|
|
)
|
2013-02-15 15:17:58 -05:00
|
|
|
if err != nil || container.State.ExitCode != 0 {
|
2013-02-13 20:24:35 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
output, err = container.Output()
|
2013-02-15 15:17:58 -05:00
|
|
|
if err != nil || container.State.ExitCode != 0 {
|
2013-02-13 20:24:35 -05: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
|
|
|
|
container, err = docker.Create(
|
|
|
|
"user_uid1",
|
|
|
|
"id",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-02-13 20:24:35 -05:00
|
|
|
&Config{
|
|
|
|
User: "1",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
output, err = container.Output()
|
2013-02-26 20:45:46 -05:00
|
|
|
if err != nil {
|
2013-02-13 20:24:35 -05:00
|
|
|
t.Fatal(err)
|
2013-02-26 20:45:46 -05:00
|
|
|
} else if container.State.ExitCode != 0 {
|
|
|
|
t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output)
|
2013-02-13 20:24:35 -05:00
|
|
|
}
|
|
|
|
if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a different user by username
|
|
|
|
container, err = docker.Create(
|
|
|
|
"user_daemon",
|
|
|
|
"id",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-02-13 20:24:35 -05:00
|
|
|
&Config{
|
|
|
|
User: "daemon",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
output, err = container.Output()
|
2013-02-15 15:17:58 -05:00
|
|
|
if err != nil || container.State.ExitCode != 0 {
|
2013-02-13 20:24:35 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 20:30:09 -05:00
|
|
|
func TestMultipleContainers(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-22 20:30:09 -05:00
|
|
|
|
|
|
|
container1, err := docker.Create(
|
|
|
|
"container1",
|
|
|
|
"cat",
|
|
|
|
[]string{"/dev/zero"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-22 20:30:09 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container1)
|
|
|
|
|
|
|
|
container2, err := docker.Create(
|
|
|
|
"container2",
|
|
|
|
"cat",
|
|
|
|
[]string{"/dev/zero"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-22 20:30:09 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container2)
|
|
|
|
|
|
|
|
// Start both containers
|
|
|
|
if err := container1.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := container2.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2013-01-28 14:51:51 -05:00
|
|
|
|
2013-01-29 18:16:45 -05:00
|
|
|
func TestStdin(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-29 18:16:45 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"stdin_test",
|
|
|
|
"cat",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-29 18:16:45 -05:00
|
|
|
&Config{
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
|
|
|
stdout, err := container.StdoutPipe()
|
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
io.WriteString(stdin, "hello world")
|
|
|
|
stdin.Close()
|
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
|
|
|
if string(output) != "hello world" {
|
|
|
|
t.Fatal(string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTty(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-29 18:16:45 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"tty_test",
|
|
|
|
"cat",
|
|
|
|
[]string{},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-29 18:16:45 -05:00
|
|
|
&Config{
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
|
|
|
stdout, err := container.StdoutPipe()
|
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
io.WriteString(stdin, "hello world")
|
|
|
|
stdin.Close()
|
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
|
|
|
if string(output) != "hello world" {
|
|
|
|
t.Fatal(string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 12:25:41 -05:00
|
|
|
func TestEnv(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-03-07 12:25:41 -05:00
|
|
|
container, err := docker.Create(
|
|
|
|
"env_test",
|
|
|
|
"/usr/bin/env",
|
|
|
|
[]string{},
|
2013-03-11 08:42:36 -04:00
|
|
|
GetTestImage(docker),
|
2013-03-07 12:25:41 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
stdout, err := container.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
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=/",
|
|
|
|
}
|
|
|
|
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-03-11 22:15:29 -04:00
|
|
|
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) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 11:33:21 -04:00
|
|
|
defer nuke(docker)
|
2013-03-11 22:25:02 -04:00
|
|
|
// Memory is allocated randomly for testing
|
2013-03-11 22:15:29 -04:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
2013-03-11 22:25:02 -04:00
|
|
|
memMin := 33554432
|
|
|
|
memMax := 536870912
|
|
|
|
mem := memMin + rand.Intn(memMax-memMin)
|
2013-03-11 22:15:29 -04:00
|
|
|
container, err := docker.Create(
|
|
|
|
"config_test",
|
|
|
|
"/bin/true",
|
|
|
|
[]string{},
|
2013-03-12 11:33:21 -04:00
|
|
|
GetTestImage(docker),
|
2013-03-11 22:15:29 -04:00
|
|
|
&Config{
|
|
|
|
Hostname: "foobar",
|
2013-03-11 22:25:02 -04:00
|
|
|
Memory: int64(mem),
|
2013-03-11 22:15:29 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
container.generateLXCConfig()
|
|
|
|
grepFile(t, container.lxcConfigPath, "lxc.utsname = foobar")
|
|
|
|
grepFile(t, container.lxcConfigPath,
|
2013-03-11 22:25:02 -04:00
|
|
|
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
|
2013-03-11 22:15:29 -04:00
|
|
|
grepFile(t, container.lxcConfigPath,
|
2013-03-11 22:25:02 -04:00
|
|
|
fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
|
2013-03-11 22:15:29 -04:00
|
|
|
}
|
|
|
|
|
2013-01-28 14:51:51 -05:00
|
|
|
func BenchmarkRunSequencial(b *testing.B) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-28 14:51:51 -05:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
container, err := docker.Create(
|
|
|
|
fmt.Sprintf("bench_%v", i),
|
|
|
|
"echo",
|
|
|
|
[]string{"-n", "foo"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-28 14:51:51 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "foo" {
|
|
|
|
b.Fatalf("Unexecpted output: %v", string(output))
|
|
|
|
}
|
|
|
|
if err := docker.Destroy(container); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRunParallel(b *testing.B) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2013-03-12 03:08:41 -04:00
|
|
|
defer nuke(docker)
|
2013-01-28 14:51:51 -05: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) {
|
|
|
|
container, err := docker.Create(
|
|
|
|
fmt.Sprintf("bench_%v", i),
|
|
|
|
"echo",
|
|
|
|
[]string{"-n", "foo"},
|
2013-02-26 20:45:46 -05:00
|
|
|
GetTestImage(docker),
|
2013-01-28 14:51:51 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
complete <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container)
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
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))
|
|
|
|
// }
|
|
|
|
if err := docker.Destroy(container); err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|