2013-01-19 00:13:39 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-03-11 12:42:36 +00:00
|
|
|
"io"
|
2013-01-19 00:13:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-03-14 09:30:50 +00:00
|
|
|
"os/exec"
|
2013-03-15 09:03:41 +00:00
|
|
|
"os/user"
|
2013-01-19 00:13:39 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-03-26 00:18:56 +00:00
|
|
|
// FIXME: this is no longer needed
|
2013-02-27 01:45:46 +00:00
|
|
|
const testLayerPath string = "/var/lib/docker/docker-ut.tar"
|
2013-03-26 00:18:56 +00:00
|
|
|
const unitTestImageName string = "docker-ut"
|
2013-03-14 09:30:50 +00:00
|
|
|
|
|
|
|
var unitTestStoreBase string
|
|
|
|
var srv *Server
|
2013-02-27 01:45:46 +00:00
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
func nuke(runtime *Runtime) error {
|
|
|
|
return os.RemoveAll(runtime.root)
|
2013-03-12 07:08:41 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 09:30:50 +00:00
|
|
|
func CopyDirectory(source, dest string) error {
|
|
|
|
if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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-02-13 22:01:44 +00:00
|
|
|
if SelfPath() == "/sbin/init" {
|
|
|
|
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-03-15 09:03:41 +00:00
|
|
|
if usr, err := user.Current(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else if usr.Uid != "0" {
|
|
|
|
panic("docker tests needs to be run as root")
|
|
|
|
}
|
|
|
|
|
2013-03-14 09:30:50 +00:00
|
|
|
// Create a temp directory
|
|
|
|
root, err := ioutil.TempDir("", "docker-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
unitTestStoreBase = root
|
|
|
|
|
|
|
|
// Make it our Store root
|
2013-03-22 00:47:23 +00:00
|
|
|
runtime, err := NewRuntimeFromDirectory(root)
|
2013-03-14 09:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// Create the "Server"
|
|
|
|
srv := &Server{
|
2013-03-21 07:41:15 +00:00
|
|
|
runtime: runtime,
|
2013-03-14 09:30:50 +00:00
|
|
|
}
|
|
|
|
// Retrieve the Image
|
2013-03-26 00:18:56 +00:00
|
|
|
if err := srv.CmdPull(os.Stdin, os.Stdout, unitTestImageName); err != nil {
|
2013-03-14 09:30:50 +00:00
|
|
|
panic(err)
|
2013-02-14 23:37:45 +00:00
|
|
|
}
|
2013-02-13 22:01:44 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
func newTestRuntime() (*Runtime, error) {
|
2013-01-19 00:13:39 +00:00
|
|
|
root, err := ioutil.TempDir("", "docker-test")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-03-14 09:30:50 +00:00
|
|
|
if err := os.Remove(root); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := CopyDirectory(unitTestStoreBase, root); err != nil {
|
|
|
|
panic(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-03-22 00:47:23 +00:00
|
|
|
runtime, err := NewRuntimeFromDirectory(root)
|
2013-01-19 00:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-02-27 01:45:46 +00:00
|
|
|
|
2013-03-21 07:41:15 +00:00
|
|
|
return runtime, nil
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
} else if len(imgs) < 1 {
|
|
|
|
panic("GASP")
|
|
|
|
}
|
|
|
|
return imgs[0]
|
|
|
|
}
|
|
|
|
|
2013-03-22 00:47:23 +00:00
|
|
|
func TestRuntimeCreate(t *testing.T) {
|
2013-03-21 07:41:15 +00:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-19 00:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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-03-23 19:39:09 +00:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
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-03-21 07:41:15 +00:00
|
|
|
if runtime.List()[0].Id != container.Id {
|
|
|
|
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-03-21 07:41:15 +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-03-21 07:41:15 +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-03-21 07:41:15 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDestroy(t *testing.T) {
|
2013-03-21 07:41:15 +00:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-19 00:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-03-23 19:39:09 +00:00
|
|
|
container, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
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
|
|
|
|
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-03-21 07:41:15 +00:00
|
|
|
runtime, err := newTestRuntime()
|
2013-01-19 00:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
defer nuke(runtime)
|
2013-03-23 19:39:09 +00:00
|
|
|
container1, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
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)
|
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container1)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-03-23 19:39:09 +00:00
|
|
|
container2, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
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)
|
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container2)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-03-23 19:39:09 +00:00
|
|
|
container3, err := runtime.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime).Id,
|
|
|
|
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)
|
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime.Destroy(container3)
|
2013-01-19 00:13:39 +00:00
|
|
|
|
2013-03-21 07:41:15 +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-03-21 07:41:15 +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-03-21 07:41:15 +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
|
|
|
|
|
|
|
func TestRestore(t *testing.T) {
|
2013-03-14 09:30:50 +00:00
|
|
|
|
2013-01-25 22:09:21 +00:00
|
|
|
root, err := ioutil.TempDir("", "docker-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-14 09:30:50 +00:00
|
|
|
if err := os.Remove(root); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := CopyDirectory(unitTestStoreBase, root); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-26 00:20:34 +00:00
|
|
|
|
2013-03-22 00:47:23 +00:00
|
|
|
runtime1, err := NewRuntimeFromDirectory(root)
|
2013-01-25 22:09:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-02-27 01:45:46 +00:00
|
|
|
|
|
|
|
// Create a container with one instance of docker
|
2013-03-23 19:39:09 +00:00
|
|
|
container1, err := runtime1.Create(&Config{
|
|
|
|
Image: GetTestImage(runtime1).Id,
|
|
|
|
Cmd: []string{"ls", "-al"},
|
2013-03-23 19:16:58 +00:00
|
|
|
},
|
2013-01-25 22:09:21 +00:00
|
|
|
)
|
2013-01-26 00:20:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
defer runtime1.Destroy(container1)
|
|
|
|
if len(runtime1.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 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-01-26 00:20:34 +00:00
|
|
|
// Here are are simulating a docker restart - that is, reloading all containers
|
|
|
|
// from scratch
|
2013-03-22 00:47:23 +00:00
|
|
|
runtime2, err := NewRuntimeFromDirectory(root)
|
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)
|
|
|
|
if len(runtime2.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(runtime2.List()))
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|
2013-03-21 07:41:15 +00:00
|
|
|
container2 := runtime2.Get(container1.Id)
|
2013-01-26 00:20:34 +00:00
|
|
|
if container2 == nil {
|
|
|
|
t.Fatal("Unable to Get container")
|
|
|
|
}
|
|
|
|
if err := container2.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-25 22:09:21 +00:00
|
|
|
}
|