2013-01-19 00:13:39 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-06-11 22:46:23 +00:00
|
|
|
"bytes"
|
2013-04-20 03:46:07 +00:00
|
|
|
"fmt"
|
2013-05-15 01:52:09 +00:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-03-11 12:42:36 +00:00
|
|
|
"io"
|
2013-05-30 13:45:39 +00:00
|
|
|
"log"
|
2013-04-20 03:46:07 +00:00
|
|
|
"net"
|
2013-01-19 00:13:39 +00:00
|
|
|
"os"
|
2013-05-30 13:45:39 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2013-04-02 18:00:14 +00:00
|
|
|
"sync"
|
2013-06-22 02:40:42 +00:00
|
|
|
"syscall"
|
2013-01-19 00:13:39 +00:00
|
|
|
"testing"
|
2013-04-02 17:06:49 +00:00
|
|
|
"time"
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
|
2013-06-24 21:15:52 +00:00
|
|
|
const (
|
2013-07-10 12:55:05 +00:00
|
|
|
unitTestImageName = "docker-test-image"
|
|
|
|
unitTestImageID = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
|
|
|
|
unitTestNetworkBridge = "testdockbr0"
|
|
|
|
unitTestStoreBase = "/var/lib/docker/unit-tests"
|
|
|
|
testDaemonAddr = "127.0.0.1:4270"
|
|
|
|
testDaemonProto = "tcp"
|
2013-06-24 21:15:52 +00:00
|
|
|
)
|
2013-02-27 01:45:46 +00:00
|
|
|
|
2013-06-25 01:22:02 +00:00
|
|
|
var globalRuntime *Runtime
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
func nuke(runtime *Runtime) error {
|
2013-04-02 18:00:14 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, container := range runtime.List() {
|
|
|
|
wg.Add(1)
|
2013-04-03 09:10:50 +00:00
|
|
|
go func(c *Container) {
|
|
|
|
c.Kill()
|
2013-04-03 09:11:34 +00:00
|
|
|
wg.Done()
|
2013-04-03 09:10:50 +00:00
|
|
|
}(container)
|
2013-04-02 18:00:14 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
2013-03-21 07:41:15 +00:00
|
|
|
return os.RemoveAll(runtime.root)
|
2013-03-12 07:08:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 01:22:02 +00:00
|
|
|
func cleanup(runtime *Runtime) error {
|
|
|
|
for _, container := range runtime.List() {
|
|
|
|
container.Kill()
|
|
|
|
runtime.Destroy(container)
|
|
|
|
}
|
|
|
|
images, err := runtime.graph.All()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, image := range images {
|
2013-07-02 22:27:22 +00:00
|
|
|
if image.ID != unitTestImageID {
|
2013-06-25 01:22:02 +00:00
|
|
|
runtime.graph.Delete(image.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-11 12:42:36 +00:00
|
|
|
func layerArchive(tarfile string) (io.Reader, error) {
|
2013-02-27 01:45:46 +00:00
|
|
|
// FIXME: need to close f somewhere
|
|
|
|
f, err := os.Open(tarfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
2013-02-14 23:37:45 +00:00
|
|
|
|
2013-02-13 22:01:44 +00:00
|
|
|
func init() {
|
2013-02-14 23:37:45 +00:00
|
|
|
// Hack to run sys init during unit testing
|
2013-05-15 01:52:09 +00:00
|
|
|
if utils.SelfPath() == "/sbin/init" {
|
2013-02-13 22:01:44 +00:00
|
|
|
SysInit()
|
2013-03-14 09:30:50 +00:00
|
|
|
return
|
2013-02-13 22:01:44 +00:00
|
|
|
}
|
2013-02-14 23:37:45 +00:00
|
|
|
|
2013-06-22 02:40:42 +00:00
|
|
|
if uid := syscall.Geteuid(); uid != 0 {
|
2013-07-04 19:40:14 +00:00
|
|
|
log.Fatal("docker tests need to be run as root")
|
2013-03-15 09:03:41 +00:00
|
|
|
}
|
|
|
|
|
2013-07-08 22:23:04 +00:00
|
|
|
NetworkBridgeIface = unitTestNetworkBridge
|
2013-04-25 00:43:41 +00:00
|
|
|
|
2013-03-14 09:30:50 +00:00
|
|
|
// Make it our Store root
|
2013-04-25 02:01:23 +00:00
|
|
|
runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
|
2013-03-14 09:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-06-25 01:22:02 +00:00
|
|
|
globalRuntime = runtime
|
2013-05-06 09:31:22 +00:00
|
|
|
|
2013-03-14 09:30:50 +00:00
|
|
|
// Create the "Server"
|
|
|
|
srv := &Server{
|
2013-06-17 23:10:00 +00:00
|
|
|
runtime: runtime,
|
|
|
|
enableCors: false,
|
|
|
|
pullingPool: make(map[string]struct{}),
|
|
|
|
pushingPool: make(map[string]struct{}),
|
2013-03-14 09:30:50 +00:00
|
|
|
}
|
2013-07-08 22:23:04 +00:00
|
|
|
// If the unit test is not found, try to download it.
|
|
|
|
if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil || img.ID != unitTestImageID {
|
|
|
|
// Retrieve the Image
|
|
|
|
if err := srv.ImagePull(unitTestImageName, "", os.Stdout, utils.NewStreamFormatter(false), nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-02-14 23:37:45 +00:00
|
|
|
}
|
2013-06-24 21:59:37 +00:00
|
|
|
// Spawn a Daemon
|
|
|
|
go func() {
|
|
|
|
if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2013-06-25 01:22:02 +00:00
|
|
|
|
|
|
|
// Give some time to ListenAndServer to actually start
|
|
|
|
time.Sleep(time.Second)
|
2013-02-13 22:01:44 +00:00
|
|
|
}
|
|
|
|
|
2013-05-25 01:32:21 +00:00
|
|
|
// FIXME: test that ImagePull(json=true) send correct json output
|
|
|
|
|
2013-03-22 00:47:23 +00:00
|
|
|
func GetTestImage(runtime *Runtime) *Image {
|
2013-03-21 07:41:15 +00:00
|
|
|
imgs, err := runtime.graph.All()
|
2013-02-27 01:45:46 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-06-28 00:53:57 +00:00
|
|
|
for i := range imgs {
|
2013-07-02 22:27:22 +00:00
|
|
|
if imgs[i].ID == unitTestImageID {
|
2013-06-28 00:53:57 +00:00
|
|
|
return imgs[i]
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:27:22 +00:00
|
|
|
panic(fmt.Errorf("Test image %v not found", unitTestImageID))
|
2013-02-27 01:45:46 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 00:47:23 +00:00
|
|
|
func TestRuntimeCreate(t *testing.T) {
|
2013-07-12 00:59:25 +00:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
// Make sure we start we 0 containers
|
2013-03-21 07:41:15 +00:00
|
|
|
if len(runtime.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2013-05-07 18:18:13 +00:00
|
|
|
|
|
|
|
builder := NewBuilder(runtime)
|
|
|
|
|
|
|
|
container, err := builder.Create(&Config{
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 19:39:09 +00:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2013-03-23 19:16:58 +00:00
|
|
|
},
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2013-03-21 07:41:15 +00:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Make sure we can find the newly created container with List()
|
2013-03-21 07:41:15 +00:00
|
|
|
if len(runtime.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(runtime.List()))
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the container List() returns is the right one
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.List()[0].ID != container.ID {
|
2013-03-21 07:41:15 +00:00
|
|
|
t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can get the container with Get()
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container.ID) == nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Unable to get newly created container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is the right container
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container.ID) != container {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Get() returned the wrong container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Exists returns it as existing
|
2013-06-04 18:00:22 +00:00
|
|
|
if !runtime.Exists(container.ID) {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Exists() returned false for a newly created container")
|
|
|
|
}
|
2013-05-07 18:18:13 +00:00
|
|
|
|
|
|
|
// Make sure crete with bad parameters returns an error
|
|
|
|
_, err = builder.Create(
|
|
|
|
&Config{
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-07 18:18:13 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Builder.Create should throw an error when Cmd is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = builder.Create(
|
|
|
|
&Config{
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-05-07 18:18:13 +00:00
|
|
|
Cmd: []string{},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Builder.Create should throw an error when Cmd is empty")
|
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDestroy(t *testing.T) {
|
2013-07-12 00:59:25 +00:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-04-24 22:35:28 +00:00
|
|
|
container, err := NewBuilder(runtime).Create(&Config{
|
2013-06-04 18:00:22 +00:00
|
|
|
Image: GetTestImage(runtime).ID,
|
2013-03-23 19:39:09 +00:00
|
|
|
Cmd: []string{"ls", "-al"},
|
2013-03-23 19:16:58 +00:00
|
|
|
},
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Destroy
|
2013-03-21 07:41:15 +00:00
|
|
|
if err := runtime.Destroy(container); err != nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
// Make sure runtime.Exists() behaves correctly
|
|
|
|
if runtime.Exists("test_destroy") {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Exists() returned true")
|
|
|
|
}
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
// Make sure runtime.List() doesn't list the destroyed container
|
|
|
|
if len(runtime.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 container, %v found", len(runtime.List()))
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
// Make sure runtime.Get() refuses to return the unexisting container
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container.ID) != nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
t.Errorf("Unable to get newly created container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the container root directory does not exist anymore
|
2013-03-21 07:25:00 +00:00
|
|
|
_, err = os.Stat(container.root)
|
2013-01-19 00:13:39 +00:00
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Errorf("Container root directory still exists after destroy")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test double destroy
|
2013-03-21 07:41:15 +00:00
|
|
|
if err := runtime.Destroy(container); err == nil {
|
2013-01-19 00:13:39 +00:00
|
|
|
// It should have failed
|
|
|
|
t.Errorf("Double destroy did not fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2013-07-12 00:59:25 +00:00
|
|
|
runtime := mkRuntime(t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-04-24 22:35:28 +00:00
|
|
|
|
2013-07-13 00:56:55 +00:00
|
|
|
container1, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container1)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-07-13 00:56:55 +00:00
|
|
|
container2, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container2)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-07-13 00:56:55 +00:00
|
|
|
container3, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container3)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container1.ID) != container1 {
|
|
|
|
t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.ID), container1)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container2.ID) != container2 {
|
|
|
|
t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.ID), container2)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
if runtime.Get(container3.ID) != container3 {
|
|
|
|
t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.ID), container3)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-01-25 22:09:21 +00:00
|
|
|
|
2013-06-11 22:46:23 +00:00
|
|
|
func startEchoServerContainer(t *testing.T, proto string) (*Runtime, *Container, string) {
|
2013-07-12 00:59:25 +00:00
|
|
|
var err error
|
|
|
|
runtime := mkRuntime(t)
|
2013-06-11 22:46:23 +00:00
|
|
|
port := 5554
|
2013-05-30 13:45:39 +00:00
|
|
|
var container *Container
|
2013-06-11 22:46:23 +00:00
|
|
|
var strPort string
|
2013-05-30 13:45:39 +00:00
|
|
|
for {
|
|
|
|
port += 1
|
2013-06-11 22:46:23 +00:00
|
|
|
strPort = strconv.Itoa(port)
|
|
|
|
var cmd string
|
|
|
|
if proto == "tcp" {
|
|
|
|
cmd = "socat TCP-LISTEN:" + strPort + ",reuseaddr,fork EXEC:/bin/cat"
|
|
|
|
} else if proto == "udp" {
|
|
|
|
cmd = "socat UDP-RECVFROM:" + strPort + ",fork EXEC:/bin/cat"
|
|
|
|
} else {
|
|
|
|
t.Fatal(fmt.Errorf("Unknown protocol %v", proto))
|
|
|
|
}
|
|
|
|
t.Log("Trying port", strPort)
|
|
|
|
container, err = NewBuilder(runtime).Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).ID,
|
|
|
|
Cmd: []string{"sh", "-c", cmd},
|
|
|
|
PortSpecs: []string{fmt.Sprintf("%s/%s", strPort, proto)},
|
|
|
|
})
|
2013-05-30 13:45:39 +00:00
|
|
|
if container != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2013-06-11 22:46:23 +00:00
|
|
|
nuke(runtime)
|
2013-05-30 13:45:39 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-11 22:46:23 +00:00
|
|
|
t.Logf("Port %v already in use", strPort)
|
2013-05-30 13:45:39 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 22:46:23 +00:00
|
|
|
hostConfig := &HostConfig{}
|
|
|
|
if err := container.Start(hostConfig); err != nil {
|
|
|
|
nuke(runtime)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-23 17:28:40 +00:00
|
|
|
|
|
|
|
setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
|
2013-07-02 10:47:37 +00:00
|
|
|
for !container.State.Running {
|
2013-04-23 17:28:40 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-07-02 10:47:37 +00:00
|
|
|
// Even if the state is running, lets give some time to lxc to spawn the process
|
|
|
|
container.WaitTimeout(500 * time.Millisecond)
|
|
|
|
|
2013-06-11 22:46:23 +00:00
|
|
|
strPort = container.NetworkSettings.PortMapping[strings.Title(proto)][strPort]
|
|
|
|
return runtime, container, strPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a container with a TCP port allocated, and test that it can receive connections on localhost
|
|
|
|
func TestAllocateTCPPortLocalhost(t *testing.T) {
|
|
|
|
runtime, container, port := startEchoServerContainer(t, "tcp")
|
|
|
|
defer nuke(runtime)
|
|
|
|
defer container.Kill()
|
|
|
|
|
2013-07-11 01:02:41 +00:00
|
|
|
for i := 0; i != 10; i++ {
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%v", port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2013-06-11 22:46:23 +00:00
|
|
|
|
2013-07-11 01:02:41 +00:00
|
|
|
input := bytes.NewBufferString("well hello there\n")
|
|
|
|
_, err = conn.Write(input.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
read := 0
|
|
|
|
conn.SetReadDeadline(time.Now().Add(3 * time.Second))
|
|
|
|
read, err = conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err, ok := err.(*net.OpError); ok {
|
|
|
|
if err.Err == syscall.ECONNRESET {
|
|
|
|
t.Logf("Connection reset by the proxy, socat is probably not listening yet, trying again in a sec")
|
|
|
|
conn.Close()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err.Timeout() {
|
|
|
|
t.Log("Timeout, trying again")
|
|
|
|
conn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
output := string(buf[:read])
|
|
|
|
if !strings.Contains(output, "well hello there") {
|
|
|
|
t.Fatal(fmt.Errorf("[%v] doesn't contain [well hello there]", output))
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2013-06-11 22:46:23 +00:00
|
|
|
}
|
2013-07-11 01:02:41 +00:00
|
|
|
|
|
|
|
t.Fatal("No reply from the container")
|
2013-06-11 22:46:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-11 01:02:41 +00:00
|
|
|
// Run a container with an UDP port allocated, and test that it can receive connections on localhost
|
2013-06-11 22:46:23 +00:00
|
|
|
func TestAllocateUDPPortLocalhost(t *testing.T) {
|
|
|
|
runtime, container, port := startEchoServerContainer(t, "udp")
|
|
|
|
defer nuke(runtime)
|
|
|
|
defer container.Kill()
|
|
|
|
|
|
|
|
conn, err := net.Dial("udp", fmt.Sprintf("localhost:%v", port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
input := bytes.NewBufferString("well hello there\n")
|
|
|
|
buf := make([]byte, 16)
|
2013-07-11 01:02:41 +00:00
|
|
|
// Try for a minute, for some reason the select in socat may take ages
|
|
|
|
// to return even though everything on the path seems fine (i.e: the
|
|
|
|
// UDPProxy forwards the traffic correctly and you can see the packets
|
|
|
|
// on the interface from within the container).
|
|
|
|
for i := 0; i != 120; i++ {
|
2013-06-11 22:46:23 +00:00
|
|
|
_, err := conn.Write(input.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-11 01:02:41 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
2013-06-11 22:46:23 +00:00
|
|
|
read, err := conn.Read(buf)
|
|
|
|
if err == nil {
|
|
|
|
output := string(buf[:read])
|
|
|
|
if strings.Contains(output, "well hello there") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatal("No reply from the container")
|
2013-04-20 03:46:07 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 22:09:21 +00:00
|
|
|
func TestRestore(t *testing.T) {
|
2013-07-13 00:56:55 +00:00
|
|
|
runtime1 := mkRuntime(t)
|
|
|
|
defer nuke(runtime1)
|
2013-02-27 01:45:46 +00:00
|
|
|
// Create a container with one instance of docker
|
2013-07-13 00:56:55 +00:00
|
|
|
container1, _, _ := mkContainer(runtime1, []string{"_", "ls", "-al"}, t)
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime1.Destroy(container1)
|
2013-04-01 03:14:54 +00:00
|
|
|
|
|
|
|
// Create a second container meant to be killed
|
2013-07-13 00:56:55 +00:00
|
|
|
container2, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/cat"}, t)
|
2013-04-02 14:13:42 +00:00
|
|
|
defer runtime1.Destroy(container2)
|
2013-04-01 03:14:54 +00:00
|
|
|
|
|
|
|
// Start the container non blocking
|
2013-05-13 23:39:54 +00:00
|
|
|
hostConfig := &HostConfig{}
|
|
|
|
if err := container2.Start(hostConfig); err != nil {
|
2013-04-01 03:14:54 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-04-02 14:13:42 +00:00
|
|
|
if !container2.State.Running {
|
2013-06-04 18:00:22 +00:00
|
|
|
t.Fatalf("Container %v should appear as running but isn't", container2.ID)
|
2013-04-02 14:01:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 03:14:54 +00:00
|
|
|
// Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
|
2013-04-02 17:06:49 +00:00
|
|
|
cStdin, _ := container2.StdinPipe()
|
|
|
|
cStdin.Close()
|
2013-04-05 03:32:44 +00:00
|
|
|
if err := container2.WaitTimeout(2 * time.Second); err != nil {
|
2013-04-03 17:48:02 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-02 14:13:42 +00:00
|
|
|
container2.State.Running = true
|
2013-04-02 17:06:49 +00:00
|
|
|
container2.ToDisk()
|
2013-04-01 03:14:54 +00:00
|
|
|
|
|
|
|
if len(runtime1.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
|
2013-01-26 00:20:34 +00:00
|
|
|
}
|
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 14:13:42 +00:00
|
|
|
if !container2.State.Running {
|
2013-06-04 18:00:22 +00:00
|
|
|
t.Fatalf("Container %v should appear as running but isn't", container2.ID)
|
2013-04-02 14:01:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-26 00:20:34 +00:00
|
|
|
// Here are are simulating a docker restart - that is, reloading all containers
|
|
|
|
// from scratch
|
2013-07-13 00:56:55 +00:00
|
|
|
runtime2, err := NewRuntimeFromDirectory(runtime1.root, false)
|
2013-01-25 22:09:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime2)
|
2013-04-01 03:14:54 +00:00
|
|
|
if len(runtime2.List()) != 2 {
|
|
|
|
t.Errorf("Expected 2 container, %v found", len(runtime2.List()))
|
|
|
|
}
|
|
|
|
runningCount := 0
|
|
|
|
for _, c := range runtime2.List() {
|
|
|
|
if c.State.Running {
|
2013-06-04 18:00:22 +00:00
|
|
|
t.Errorf("Running container found: %v (%v)", c.ID, c.Path)
|
2013-04-01 03:14:54 +00:00
|
|
|
runningCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if runningCount != 0 {
|
|
|
|
t.Fatalf("Expected 0 container alive, %d found", runningCount)
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
container3 := runtime2.Get(container1.ID)
|
2013-04-02 14:13:42 +00:00
|
|
|
if container3 == nil {
|
2013-01-26 00:20:34 +00:00
|
|
|
t.Fatal("Unable to Get container")
|
|
|
|
}
|
2013-04-02 14:13:42 +00:00
|
|
|
if err := container3.Run(); err != nil {
|
2013-01-26 00:20:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-05 03:40:42 +00:00
|
|
|
container2.State.Running = false
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|