2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
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-26 19:54:13 -04:00
|
|
|
"regexp"
|
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-26 19:54:13 -04:00
|
|
|
func TestIdFormat(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
container1, err := runtime.Create(
|
|
|
|
&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"/bin/sh", "-c", "echo hello world"},
|
|
|
|
Memory: 33554432,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
match, err := regexp.Match("^[0-9a-f]{64}$", []byte(container1.Id))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
t.Fatalf("Invalid container ID: %s", container1.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 13:56:49 -04:00
|
|
|
func TestMultipleAttachRestart(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
container, err := runtime.Create(
|
|
|
|
&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"/bin/sh", "-c",
|
|
|
|
"i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
|
|
|
|
Memory: 33554432,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
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 10:03:50 -04:00
|
|
|
if err := container.Stop(10); err != nil {
|
2013-03-29 13: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)
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
timeout := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
timeout <- false
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
timeout <- true
|
|
|
|
}()
|
|
|
|
if <-timeout {
|
|
|
|
t.Fatalf("Timeout reading from the process")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 08:35:47 -04:00
|
|
|
func TestCommitRun(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
container1, err := runtime.Create(
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{
|
2013-03-23 15:39:09 -04:00
|
|
|
Image: GetTestImage(runtime).Id,
|
2013-03-23 15:16:58 -04:00
|
|
|
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
|
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-21 03:41:15 -04:00
|
|
|
defer runtime.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
|
|
|
|
2013-03-21 03:25:00 -04:00
|
|
|
rwTar, err := container1.ExportRw()
|
2013-03-12 08:35:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-04-17 22:58:17 -04:00
|
|
|
img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "")
|
2013-03-12 08: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-03-21 03:41:15 -04:00
|
|
|
container2, err := runtime.Create(
|
2013-03-12 08:35:47 -04:00
|
|
|
&Config{
|
2013-03-23 15:39:09 -04:00
|
|
|
Image: img.Id,
|
2013-03-12 11:33:21 -04:00
|
|
|
Memory: 33554432,
|
2013-03-23 15:16:58 -04:00
|
|
|
Cmd: []string{"cat", "/world"},
|
2013-03-12 08:36:37 -04:00
|
|
|
},
|
|
|
|
)
|
2013-03-12 08:35:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container2)
|
2013-03-12 08:35:47 -04:00
|
|
|
stdout, err := container2.StdoutPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 08:35:47 -04:00
|
|
|
stderr, err := container2.StderrPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 08:35:47 -04:00
|
|
|
if err := container2.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container2.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-12 08:35:47 -04:00
|
|
|
output2, err := ioutil.ReadAll(stderr)
|
2013-03-29 10: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 08:35:47 -04:00
|
|
|
if string(output) != "hello\n" {
|
2013-03-30 03:22:56 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2013-03-31 17:15:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStart(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
container, err := runtime.Create(
|
|
|
|
&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Memory: 33554432,
|
|
|
|
Cmd: []string{"/bin/cat"},
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
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.Running {
|
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
|
|
|
if err := container.Start(); err == nil {
|
|
|
|
t.Fatalf("A running containter should be able to be started")
|
|
|
|
}
|
2013-04-01 01:42:10 -04:00
|
|
|
|
|
|
|
// Try to avoid the timeoout in destroy. Best effort, don't check error
|
|
|
|
cStdin, _ := container.StdinPipe()
|
|
|
|
cStdin.Close()
|
2013-04-05 05:01:38 -04:00
|
|
|
container.WaitTimeout(2 * time.Second)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
container, err := runtime.Create(
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{
|
2013-03-23 15:39:09 -04:00
|
|
|
Image: GetTestImage(runtime).Id,
|
2013-03-11 22:25:02 -04:00
|
|
|
Memory: 33554432,
|
2013-03-23 15:16:58 -04:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2013-01-18 19:13:39 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
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) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
|
|
|
container, err := runtime.Create(
|
2013-03-23 15:16:58 -04:00
|
|
|
&Config{
|
2013-03-23 15:39:09 -04:00
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"echo", "-n", "foobar"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-18 19:13:39 -05:00
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "foobar" {
|
|
|
|
t.Error(string(output))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-11 19:21:19 -04:00
|
|
|
func TestKillDifferentUser(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"tail", "-f", "/etc/resolv.conf"},
|
|
|
|
User: "daemon",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
|
|
|
|
|
|
|
if container.State.Running {
|
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give some time to lxc to spawn the process (setuid might take some time)
|
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
|
|
|
if !container.State.Running {
|
|
|
|
t.Errorf("Container should be running")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.State.Running {
|
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
container.Wait()
|
|
|
|
if container.State.Running {
|
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
// Try stopping twice
|
|
|
|
if err := container.Kill(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 21:03:23 -05:00
|
|
|
func TestKill(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"cat", "/dev/zero"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
if container.State.Running {
|
|
|
|
t.Errorf("Container shouldn't be running")
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-11 19:21:19 -04:00
|
|
|
|
|
|
|
// Give some time to lxc to spawn the process
|
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
2013-01-18 19:13:39 -05:00
|
|
|
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) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2013-03-23 15:39:09 -04:00
|
|
|
trueContainer, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"/bin/true", ""},
|
2013-03-29 10:44:58 -04:00
|
|
|
})
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(trueContainer)
|
2013-01-18 19:13:39 -05:00
|
|
|
if err := trueContainer.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-29 10:44:58 -04:00
|
|
|
if trueContainer.State.ExitCode != 0 {
|
|
|
|
t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
|
|
|
|
}
|
2013-01-18 19:13:39 -05:00
|
|
|
|
2013-03-23 15:39:09 -04:00
|
|
|
falseContainer, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"/bin/false", ""},
|
2013-03-29 10:44:58 -04:00
|
|
|
})
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(falseContainer)
|
2013-01-18 19:13:39 -05:00
|
|
|
if err := falseContainer.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if falseContainer.State.ExitCode != 1 {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-22 20:30:09 -05:00
|
|
|
|
2013-02-13 21:17:42 -05:00
|
|
|
func TestRestart(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-02-13 21:17:42 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"echo", "-n", "foobar"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-02-13 21:17:42 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-13 21:17:42 -05: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-13 22:05:57 -05:00
|
|
|
func TestRestartStdin(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-02-13 22:05:57 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-02-13 22:05:57 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-13 22:05:57 -05:00
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
stdout, err := container.StdoutPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-29 10: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-13 22:05:57 -05:00
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdout.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-02-13 22:05:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restart and try again
|
|
|
|
stdin, err = container.StdinPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
stdout, err = container.StdoutPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-29 10: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-13 22:05:57 -05:00
|
|
|
container.Wait()
|
|
|
|
output, err = ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdout.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-13 22:05:57 -05:00
|
|
|
if string(output) != "hello world #2" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
|
2013-02-13 22:05:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:24:35 -05:00
|
|
|
func TestUser(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-02-13 20:24:35 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-02-13 20:24:35 -05:00
|
|
|
|
|
|
|
// Default user must be root
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"id"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-02-13 20:24:35 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-13 20:24:35 -05: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-03-23 15:39:09 -04:00
|
|
|
container, err = runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"id"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
User: "root",
|
|
|
|
},
|
2013-02-13 20:24:35 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-13 20:24:35 -05:00
|
|
|
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
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err = runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"id"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
User: "0",
|
|
|
|
},
|
2013-02-13 20:24:35 -05:00
|
|
|
)
|
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)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-13 20:24:35 -05:00
|
|
|
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
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err = runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"id"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
User: "1",
|
|
|
|
},
|
2013-02-13 20:24:35 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-13 20:24:35 -05:00
|
|
|
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
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err = runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"id"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
User: "daemon",
|
|
|
|
},
|
2013-02-13 20:24:35 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-02-13 20:24:35 -05:00
|
|
|
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) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-22 20:30:09 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-01-22 20:30:09 -05:00
|
|
|
|
2013-03-23 15:39:09 -04:00
|
|
|
container1, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"cat", "/dev/zero"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-01-22 20:30:09 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container1)
|
2013-01-22 20:30:09 -05:00
|
|
|
|
2013-03-23 15:39:09 -04:00
|
|
|
container2, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"cat", "/dev/zero"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-01-22 20:30:09 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container2)
|
2013-01-22 20:30:09 -05:00
|
|
|
|
|
|
|
// Start both containers
|
|
|
|
if err := container1.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := container2.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-04-11 19: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-22 20:30:09 -05:00
|
|
|
// 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) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-29 18:16:45 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-01-29 18:16:45 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-29 18:16:45 -05:00
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
stdout, err := container.StdoutPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
2013-03-29 10:44:58 -04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
2013-01-29 18:16:45 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-01-29 18:16:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTty(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-29 18:16:45 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"cat"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
OpenStdin: true,
|
|
|
|
},
|
2013-01-29 18:16:45 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-29 18:16:45 -05:00
|
|
|
|
|
|
|
stdin, err := container.StdinPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
stdout, err := container.StdoutPipe()
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
defer stdin.Close()
|
|
|
|
defer stdout.Close()
|
2013-03-29 10:44:58 -04:00
|
|
|
if _, err := io.WriteString(stdin, "hello world"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := stdin.Close(); err != nil {
|
2013-01-29 18:16:45 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container.Wait()
|
|
|
|
output, err := ioutil.ReadAll(stdout)
|
2013-03-29 10:44:58 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-29 18:16:45 -05:00
|
|
|
if string(output) != "hello world" {
|
2013-03-29 10:44:58 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
|
2013-01-29 18:16:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 12:25:41 -05:00
|
|
|
func TestEnv(t *testing.T) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-03-07 12:25:41 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"/usr/bin/env"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-03-07 12:25:41 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-03-29 10:44:58 -04:00
|
|
|
|
2013-03-07 12:25:41 -05:00
|
|
|
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) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-03-11 22:15:29 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
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-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"/bin/true"},
|
2013-03-23 15:16:58 -04:00
|
|
|
|
|
|
|
Hostname: "foobar",
|
|
|
|
Memory: int64(mem),
|
|
|
|
},
|
2013-03-11 22:15:29 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-03-11 22:15:29 -04:00
|
|
|
container.generateLXCConfig()
|
2013-03-21 03:25:00 -04:00
|
|
|
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-21 03:25:00 -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) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-28 14:51:51 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
2013-01-28 14:51:51 -05:00
|
|
|
for i := 0; i < b.N; i++ {
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"echo", "-n", "foo"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-01-28 14:51:51 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-28 14:51:51 -05:00
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "foo" {
|
2013-03-30 03:22:56 -04:00
|
|
|
b.Fatalf("Unexpected output: %s", output)
|
2013-01-28 14:51:51 -05:00
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-28 14:51:51 -05:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRunParallel(b *testing.B) {
|
2013-03-21 03:41:15 -04:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-28 14:51:51 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer nuke(runtime)
|
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) {
|
2013-03-23 15:39:09 -04:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
Cmd: []string{"echo", "-n", "foo"},
|
2013-03-23 15:16:58 -04:00
|
|
|
},
|
2013-01-28 14:51:51 -05:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
complete <- err
|
|
|
|
return
|
|
|
|
}
|
2013-03-21 03:41:15 -04:00
|
|
|
defer runtime.Destroy(container)
|
2013-01-28 14:51:51 -05:00
|
|
|
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))
|
|
|
|
// }
|
2013-03-21 03:41:15 -04:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-28 14:51:51 -05: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)
|
|
|
|
}
|
|
|
|
}
|