2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2013-02-14 18:37:45 -05:00
|
|
|
"log"
|
2013-01-18 19:13:39 -05:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-02-14 18:37:45 -05:00
|
|
|
const testLayerPath string = "/var/lib/docker/images/docker-ut"
|
|
|
|
|
2013-02-13 17:01:44 -05:00
|
|
|
func init() {
|
2013-02-14 18:37:45 -05:00
|
|
|
// Hack to run sys init during unit testing
|
2013-02-13 17:01:44 -05:00
|
|
|
if SelfPath() == "/sbin/init" {
|
|
|
|
SysInit()
|
|
|
|
}
|
2013-02-14 18:37:45 -05:00
|
|
|
|
2013-02-15 15:18:36 -05:00
|
|
|
// Make sure the unit test image is there
|
|
|
|
if _, err := os.Stat(testLayerPath); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Fatalf("Unit test base image not found. Please fix the problem by running \"debootstrap --arch=amd64 quantal %v\"", testLayerPath)
|
2013-02-14 18:37:45 -05:00
|
|
|
}
|
2013-02-13 17:01:44 -05:00
|
|
|
}
|
|
|
|
|
2013-01-18 19:13:39 -05:00
|
|
|
func newTestDocker() (*Docker, error) {
|
|
|
|
root, err := ioutil.TempDir("", "docker-test")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
docker, err := NewFromDirectory(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return docker, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we start we 0 containers
|
|
|
|
if len(docker.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 containers, %v found", len(docker.List()))
|
|
|
|
}
|
|
|
|
container, err := docker.Create(
|
|
|
|
"test_create",
|
|
|
|
"ls",
|
|
|
|
[]string{"-al"},
|
2013-02-14 18:37:45 -05:00
|
|
|
[]string{testLayerPath},
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := docker.Destroy(container); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Make sure we can find the newly created container with List()
|
|
|
|
if len(docker.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(docker.List()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the container List() returns is the right one
|
2013-01-21 21:39:52 -05:00
|
|
|
if docker.List()[0].Id != "test_create" {
|
2013-01-18 19:13:39 -05:00
|
|
|
t.Errorf("Unexpected container %v returned by List", docker.List()[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can get the container with Get()
|
|
|
|
if docker.Get("test_create") == nil {
|
|
|
|
t.Errorf("Unable to get newly created container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it is the right container
|
|
|
|
if docker.Get("test_create") != container {
|
|
|
|
t.Errorf("Get() returned the wrong container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Exists returns it as existing
|
|
|
|
if !docker.Exists("test_create") {
|
|
|
|
t.Errorf("Exists() returned false for a newly created container")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDestroy(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container, err := docker.Create(
|
|
|
|
"test_destroy",
|
|
|
|
"ls",
|
|
|
|
[]string{"-al"},
|
2013-02-14 18:37:45 -05:00
|
|
|
[]string{testLayerPath},
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Destroy
|
|
|
|
if err := docker.Destroy(container); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure docker.Exists() behaves correctly
|
|
|
|
if docker.Exists("test_destroy") {
|
|
|
|
t.Errorf("Exists() returned true")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure docker.List() doesn't list the destroyed container
|
|
|
|
if len(docker.List()) != 0 {
|
|
|
|
t.Errorf("Expected 0 container, %v found", len(docker.List()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure docker.Get() refuses to return the unexisting container
|
|
|
|
if docker.Get("test_destroy") != nil {
|
|
|
|
t.Errorf("Unable to get newly created container")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the container root directory does not exist anymore
|
|
|
|
_, err = os.Stat(container.Root)
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Errorf("Container root directory still exists after destroy")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test double destroy
|
|
|
|
if err := docker.Destroy(container); err == nil {
|
|
|
|
// It should have failed
|
|
|
|
t.Errorf("Double destroy did not fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
docker, err := newTestDocker()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container1, err := docker.Create(
|
|
|
|
"test1",
|
|
|
|
"ls",
|
|
|
|
[]string{"-al"},
|
2013-02-14 18:37:45 -05:00
|
|
|
[]string{testLayerPath},
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container1)
|
|
|
|
|
|
|
|
container2, err := docker.Create(
|
|
|
|
"test2",
|
|
|
|
"ls",
|
|
|
|
[]string{"-al"},
|
2013-02-14 18:37:45 -05:00
|
|
|
[]string{testLayerPath},
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container2)
|
|
|
|
|
|
|
|
container3, err := docker.Create(
|
|
|
|
"test3",
|
|
|
|
"ls",
|
|
|
|
[]string{"-al"},
|
2013-02-14 18:37:45 -05:00
|
|
|
[]string{testLayerPath},
|
2013-01-18 19:13:39 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker.Destroy(container3)
|
|
|
|
|
|
|
|
if docker.Get("test1") != container1 {
|
|
|
|
t.Errorf("Get(test1) returned %v while expecting %v", docker.Get("test1"), container1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if docker.Get("test2") != container2 {
|
|
|
|
t.Errorf("Get(test2) returned %v while expecting %v", docker.Get("test2"), container2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if docker.Get("test3") != container3 {
|
|
|
|
t.Errorf("Get(test3) returned %v while expecting %v", docker.Get("test3"), container3)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-01-25 17:09:21 -05:00
|
|
|
|
|
|
|
func TestRestore(t *testing.T) {
|
|
|
|
root, err := ioutil.TempDir("", "docker-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-25 19:20:34 -05:00
|
|
|
|
|
|
|
// Create a container with one instance of docker
|
|
|
|
docker1, err := NewFromDirectory(root)
|
2013-01-25 17:09:21 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-25 19:20:34 -05:00
|
|
|
container1, err := docker1.Create(
|
|
|
|
"restore_test",
|
2013-01-25 17:09:21 -05:00
|
|
|
"ls",
|
|
|
|
[]string{"-al"},
|
2013-02-14 18:37:45 -05:00
|
|
|
[]string{testLayerPath},
|
2013-01-25 17:09:21 -05:00
|
|
|
&Config{},
|
|
|
|
)
|
2013-01-25 19:20:34 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer docker1.Destroy(container1)
|
|
|
|
if len(docker1.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(docker1.List()))
|
|
|
|
}
|
|
|
|
if err := container1.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-01-25 17:09:21 -05:00
|
|
|
}
|
|
|
|
|
2013-01-25 19:20:34 -05:00
|
|
|
// Here are are simulating a docker restart - that is, reloading all containers
|
|
|
|
// from scratch
|
2013-01-25 17:09:21 -05:00
|
|
|
docker2, err := NewFromDirectory(root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(docker2.List()) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(docker2.List()))
|
|
|
|
}
|
2013-01-25 19:20:34 -05:00
|
|
|
container2 := docker2.Get("restore_test")
|
|
|
|
if container2 == nil {
|
|
|
|
t.Fatal("Unable to Get container")
|
|
|
|
}
|
|
|
|
if err := container2.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-01-25 17:09:21 -05:00
|
|
|
}
|