runtime_test.go
a27b4b8c
 package docker
 
 import (
911925b5
 	"fmt"
80f6b458
 	"github.com/dotcloud/docker/rcli"
97a82094
 	"io"
a27b4b8c
 	"io/ioutil"
911925b5
 	"net"
a27b4b8c
 	"os"
de753d5a
 	"os/exec"
2839a590
 	"os/user"
8f9e4542
 	"sync"
a27b4b8c
 	"testing"
791ca6fd
 	"time"
a27b4b8c
 )
 
d01b5894
 const unitTestImageName string = "docker-ut"
de753d5a
 
ca6cd5b5
 const unitTestStoreBase string = "/var/lib/docker/unit-tests"
2ebf3464
 
b8547f31
 func nuke(runtime *Runtime) error {
8f9e4542
 	var wg sync.WaitGroup
 	for _, container := range runtime.List() {
 		wg.Add(1)
1fc55c2b
 		go func(c *Container) {
 			c.Kill()
cab31fd5
 			wg.Done()
1fc55c2b
 		}(container)
8f9e4542
 	}
 	wg.Wait()
b8547f31
 	return os.RemoveAll(runtime.root)
e0b09d49
 }
 
de753d5a
 func CopyDirectory(source, dest string) error {
 	if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
 		return err
 	}
 	return nil
 }
 
97a82094
 func layerArchive(tarfile string) (io.Reader, error) {
2ebf3464
 	// FIXME: need to close f somewhere
 	f, err := os.Open(tarfile)
 	if err != nil {
 		return nil, err
 	}
 	return f, nil
 }
dd84ba34
 
58a22942
 func init() {
dd84ba34
 	// Hack to run sys init during unit testing
58a22942
 	if SelfPath() == "/sbin/init" {
 		SysInit()
de753d5a
 		return
58a22942
 	}
dd84ba34
 
2839a590
 	if usr, err := user.Current(); err != nil {
 		panic(err)
 	} else if usr.Uid != "0" {
 		panic("docker tests needs to be run as root")
 	}
 
ee298d14
 	NetworkBridgeIface = "testdockbr0"
 
de753d5a
 	// Make it our Store root
50144aeb
 	runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
de753d5a
 	if err != nil {
 		panic(err)
 	}
 	// Create the "Server"
 	srv := &Server{
b8547f31
 		runtime: runtime,
de753d5a
 	}
 	// Retrieve the Image
80f6b458
 	if err := srv.CmdPull(os.Stdin, rcli.NewDockerLocalConn(os.Stdout), unitTestImageName); err != nil {
de753d5a
 		panic(err)
dd84ba34
 	}
58a22942
 }
 
b8547f31
 func newTestRuntime() (*Runtime, error) {
a27b4b8c
 	root, err := ioutil.TempDir("", "docker-test")
 	if err != nil {
 		return nil, err
 	}
de753d5a
 	if err := os.Remove(root); err != nil {
 		return nil, err
 	}
 	if err := CopyDirectory(unitTestStoreBase, root); err != nil {
 		return nil, err
 	}
 
50144aeb
 	runtime, err := NewRuntimeFromDirectory(root, false)
a27b4b8c
 	if err != nil {
 		return nil, err
 	}
9042535f
 	runtime.UpdateCapabilities(true)
b8547f31
 	return runtime, nil
a27b4b8c
 }
 
ef711962
 func GetTestImage(runtime *Runtime) *Image {
b8547f31
 	imgs, err := runtime.graph.All()
2ebf3464
 	if err != nil {
 		panic(err)
 	} else if len(imgs) < 1 {
 		panic("GASP")
 	}
 	return imgs[0]
 }
 
ef711962
 func TestRuntimeCreate(t *testing.T) {
b8547f31
 	runtime, err := newTestRuntime()
a27b4b8c
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer nuke(runtime)
a27b4b8c
 
 	// Make sure we start we 0 containers
b8547f31
 	if len(runtime.List()) != 0 {
 		t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
a27b4b8c
 	}
6ce64e84
 	container, err := runtime.Create(&Config{
 		Image: GetTestImage(runtime).Id,
 		Cmd:   []string{"ls", "-al"},
031f91df
 	},
a27b4b8c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 
 	defer func() {
b8547f31
 		if err := runtime.Destroy(container); err != nil {
a27b4b8c
 			t.Error(err)
 		}
 	}()
 
 	// Make sure we can find the newly created container with List()
b8547f31
 	if len(runtime.List()) != 1 {
 		t.Errorf("Expected 1 container, %v found", len(runtime.List()))
a27b4b8c
 	}
 
 	// Make sure the container List() returns is the right one
b8547f31
 	if runtime.List()[0].Id != container.Id {
 		t.Errorf("Unexpected container %v returned by List", runtime.List()[0])
a27b4b8c
 	}
 
 	// Make sure we can get the container with Get()
b8547f31
 	if runtime.Get(container.Id) == nil {
a27b4b8c
 		t.Errorf("Unable to get newly created container")
 	}
 
 	// Make sure it is the right container
b8547f31
 	if runtime.Get(container.Id) != container {
a27b4b8c
 		t.Errorf("Get() returned the wrong container")
 	}
 
 	// Make sure Exists returns it as existing
b8547f31
 	if !runtime.Exists(container.Id) {
a27b4b8c
 		t.Errorf("Exists() returned false for a newly created container")
 	}
 }
 
 func TestDestroy(t *testing.T) {
b8547f31
 	runtime, err := newTestRuntime()
a27b4b8c
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer nuke(runtime)
6ce64e84
 	container, err := runtime.Create(&Config{
 		Image: GetTestImage(runtime).Id,
 		Cmd:   []string{"ls", "-al"},
031f91df
 	},
a27b4b8c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	// Destroy
b8547f31
 	if err := runtime.Destroy(container); err != nil {
a27b4b8c
 		t.Error(err)
 	}
 
b8547f31
 	// Make sure runtime.Exists() behaves correctly
 	if runtime.Exists("test_destroy") {
a27b4b8c
 		t.Errorf("Exists() returned true")
 	}
 
b8547f31
 	// 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()))
a27b4b8c
 	}
 
b8547f31
 	// Make sure runtime.Get() refuses to return the unexisting container
 	if runtime.Get(container.Id) != nil {
a27b4b8c
 		t.Errorf("Unable to get newly created container")
 	}
 
 	// Make sure the container root directory does not exist anymore
7c57a4cf
 	_, err = os.Stat(container.root)
a27b4b8c
 	if err == nil || !os.IsNotExist(err) {
 		t.Errorf("Container root directory still exists after destroy")
 	}
 
 	// Test double destroy
b8547f31
 	if err := runtime.Destroy(container); err == nil {
a27b4b8c
 		// It should have failed
 		t.Errorf("Double destroy did not fail")
 	}
 }
 
 func TestGet(t *testing.T) {
b8547f31
 	runtime, err := newTestRuntime()
a27b4b8c
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer nuke(runtime)
6ce64e84
 	container1, err := runtime.Create(&Config{
 		Image: GetTestImage(runtime).Id,
 		Cmd:   []string{"ls", "-al"},
031f91df
 	},
a27b4b8c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container1)
a27b4b8c
 
6ce64e84
 	container2, err := runtime.Create(&Config{
 		Image: GetTestImage(runtime).Id,
 		Cmd:   []string{"ls", "-al"},
031f91df
 	},
a27b4b8c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container2)
a27b4b8c
 
6ce64e84
 	container3, err := runtime.Create(&Config{
 		Image: GetTestImage(runtime).Id,
 		Cmd:   []string{"ls", "-al"},
031f91df
 	},
a27b4b8c
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime.Destroy(container3)
a27b4b8c
 
b8547f31
 	if runtime.Get(container1.Id) != container1 {
 		t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.Id), container1)
a27b4b8c
 	}
 
b8547f31
 	if runtime.Get(container2.Id) != container2 {
 		t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.Id), container2)
a27b4b8c
 	}
 
b8547f31
 	if runtime.Get(container3.Id) != container3 {
 		t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.Id), container3)
a27b4b8c
 	}
 
 }
2193b0c9
 
911925b5
 // Run a container with a TCP port allocated, and test that it can receive connections on localhost
 func TestAllocatePortLocalhost(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
 	}
 	container, err := runtime.Create(&Config{
 		Image:     GetTestImage(runtime).Id,
 		Cmd:       []string{"sh", "-c", "echo well hello there | nc -l -p 5555"},
 		PortSpecs: []string{"5555"},
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	if err := container.Start(); err != nil {
 		t.Fatal(err)
 	}
 	defer container.Kill()
5a02c9ba
 
 	setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
 		for {
 			if container.State.Running {
 				break
 			}
 			time.Sleep(10 * time.Millisecond)
 		}
 	})
 
911925b5
 	conn, err := net.Dial("tcp",
 		fmt.Sprintf(
 			"localhost:%s", container.NetworkSettings.PortMapping["5555"],
 		),
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer conn.Close()
 	output, err := ioutil.ReadAll(conn)
 	if err != nil {
 		t.Fatal(err)
 	}
 	if string(output) != "well hello there\n" {
 		t.Fatalf("Received wrong output from network connection: should be '%s', not '%s'",
 			"well hello there\n",
 			string(output),
 		)
 	}
5a02c9ba
 	container.Wait()
911925b5
 }
 
2193b0c9
 func TestRestore(t *testing.T) {
de753d5a
 
2193b0c9
 	root, err := ioutil.TempDir("", "docker-test")
 	if err != nil {
 		t.Fatal(err)
 	}
de753d5a
 	if err := os.Remove(root); err != nil {
 		t.Fatal(err)
 	}
 	if err := CopyDirectory(unitTestStoreBase, root); err != nil {
 		t.Fatal(err)
 	}
6513a1d9
 
50144aeb
 	runtime1, err := NewRuntimeFromDirectory(root, false)
2193b0c9
 	if err != nil {
 		t.Fatal(err)
 	}
2ebf3464
 
 	// Create a container with one instance of docker
6ce64e84
 	container1, err := runtime1.Create(&Config{
 		Image: GetTestImage(runtime1).Id,
 		Cmd:   []string{"ls", "-al"},
031f91df
 	},
2193b0c9
 	)
6513a1d9
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer runtime1.Destroy(container1)
8c36e692
 
 	// Create a second container meant to be killed
02c211a0
 	container2, err := runtime1.Create(&Config{
8c36e692
 		Image:     GetTestImage(runtime1).Id,
 		Cmd:       []string{"/bin/cat"},
 		OpenStdin: true,
 	},
 	)
 	if err != nil {
 		t.Fatal(err)
 	}
02c211a0
 	defer runtime1.Destroy(container2)
8c36e692
 
 	// Start the container non blocking
02c211a0
 	if err := container2.Start(); err != nil {
8c36e692
 		t.Fatal(err)
 	}
 
02c211a0
 	if !container2.State.Running {
 		t.Fatalf("Container %v should appear as running but isn't", container2.Id)
c780ff5a
 	}
 
8c36e692
 	// Simulate a crash/manual quit of dockerd: process dies, states stays 'Running'
791ca6fd
 	cStdin, _ := container2.StdinPipe()
 	cStdin.Close()
bae6f958
 	if err := container2.WaitTimeout(2 * time.Second); err != nil {
ad0183e4
 		t.Fatal(err)
 	}
02c211a0
 	container2.State.Running = true
791ca6fd
 	container2.ToDisk()
8c36e692
 
 	if len(runtime1.List()) != 2 {
 		t.Errorf("Expected 2 container, %v found", len(runtime1.List()))
6513a1d9
 	}
 	if err := container1.Run(); err != nil {
 		t.Fatal(err)
2193b0c9
 	}
 
02c211a0
 	if !container2.State.Running {
 		t.Fatalf("Container %v should appear as running but isn't", container2.Id)
c780ff5a
 	}
 
6513a1d9
 	// Here are are simulating a docker restart - that is, reloading all containers
 	// from scratch
50144aeb
 	runtime2, err := NewRuntimeFromDirectory(root, false)
2193b0c9
 	if err != nil {
 		t.Fatal(err)
 	}
b8547f31
 	defer nuke(runtime2)
8c36e692
 	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 {
c780ff5a
 			t.Errorf("Running container found: %v (%v)", c.Id, c.Path)
8c36e692
 			runningCount++
 		}
 	}
 	if runningCount != 0 {
 		t.Fatalf("Expected 0 container alive, %d found", runningCount)
2193b0c9
 	}
02c211a0
 	container3 := runtime2.Get(container1.Id)
 	if container3 == nil {
6513a1d9
 		t.Fatal("Unable to Get container")
 	}
02c211a0
 	if err := container3.Run(); err != nil {
6513a1d9
 		t.Fatal(err)
 	}
b76b329e
 	container2.State.Running = false
2193b0c9
 }