2013-03-22 00:47:23 +00:00
|
|
|
package docker
|
2013-03-18 07:15:35 +00:00
|
|
|
|
|
|
|
import (
|
2013-03-21 05:42:08 +00:00
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2013-04-17 23:35:22 +00:00
|
|
|
"errors"
|
2013-05-15 01:52:09 +00:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-03-21 05:42:08 +00:00
|
|
|
"io"
|
2013-03-18 07:15:35 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
|
|
|
// Root should exist
|
|
|
|
if _, err := os.Stat(graph.Root); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-09-01 03:31:21 +00:00
|
|
|
// Map() should be empty
|
|
|
|
if l, err := graph.Map(); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if len(l) != 0 {
|
2013-09-01 03:31:21 +00:00
|
|
|
t.Fatalf("len(Map()) should return %d, not %d", 0, len(l))
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 23:35:22 +00:00
|
|
|
// Test that Register can be interrupted cleanly without side effects
|
|
|
|
func TestInterruptedRegister(t *testing.T) {
|
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
|
|
|
badArchive, w := io.Pipe() // Use a pipe reader as a fake archive which never yields data
|
|
|
|
image := &Image{
|
2013-06-04 18:00:22 +00:00
|
|
|
ID: GenerateID(),
|
2013-04-17 23:35:22 +00:00
|
|
|
Comment: "testing",
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
2013-07-30 13:13:18 +00:00
|
|
|
go graph.Register(nil, badArchive, image)
|
2013-04-17 23:35:22 +00:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
w.CloseWithError(errors.New("But I'm not a tarball!")) // (Nobody's perfect, darling)
|
2013-06-04 18:00:22 +00:00
|
|
|
if _, err := graph.Get(image.ID); err == nil {
|
2013-04-17 23:35:22 +00:00
|
|
|
t.Fatal("Image should not exist after Register is interrupted")
|
|
|
|
}
|
|
|
|
// Registering the same image again should succeed if the first register was interrupted
|
|
|
|
goodArchive, err := fakeTar()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-30 13:13:18 +00:00
|
|
|
if err := graph.Register(nil, goodArchive, image); err != nil {
|
2013-04-17 23:35:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 07:15:35 +00:00
|
|
|
// FIXME: Do more extensive tests (ex: create multiple, delete, recreate;
|
|
|
|
// create multiple, check the amount of images and paths, etc..)
|
2013-03-22 00:47:23 +00:00
|
|
|
func TestGraphCreate(t *testing.T) {
|
2013-03-18 07:15:35 +00:00
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
2013-03-21 05:42:08 +00:00
|
|
|
archive, err := fakeTar()
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-26 00:02:38 +00:00
|
|
|
image, err := graph.Create(archive, nil, "Testing", "", nil)
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
if err := ValidateID(image.ID); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if image.Comment != "Testing" {
|
|
|
|
t.Fatalf("Wrong comment: should be '%s', not '%s'", "Testing", image.Comment)
|
|
|
|
}
|
2013-04-05 01:38:43 +00:00
|
|
|
if image.DockerVersion != VERSION {
|
|
|
|
t.Fatalf("Wrong docker_version: should be '%s', not '%s'", VERSION, image.DockerVersion)
|
|
|
|
}
|
2013-09-01 03:31:21 +00:00
|
|
|
images, err := graph.Map()
|
|
|
|
if err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if l := len(images); l != 1 {
|
|
|
|
t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
|
|
|
|
}
|
2013-09-01 03:31:21 +00:00
|
|
|
if images[image.ID] == nil {
|
|
|
|
t.Fatalf("Could not find image with id %s", image.ID)
|
|
|
|
}
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegister(t *testing.T) {
|
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
2013-03-21 05:42:08 +00:00
|
|
|
archive, err := fakeTar()
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
image := &Image{
|
2013-06-04 18:00:22 +00:00
|
|
|
ID: GenerateID(),
|
2013-03-18 07:15:35 +00:00
|
|
|
Comment: "testing",
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
2013-07-30 13:13:18 +00:00
|
|
|
err = graph.Register(nil, archive, image)
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-09-01 03:31:21 +00:00
|
|
|
if images, err := graph.Map(); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if l := len(images); l != 1 {
|
|
|
|
t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
if resultImg, err := graph.Get(image.ID); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
2013-06-04 18:00:22 +00:00
|
|
|
if resultImg.ID != image.ID {
|
|
|
|
t.Fatalf("Wrong image ID. Should be '%s', not '%s'", image.ID, resultImg.ID)
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
if resultImg.Comment != image.Comment {
|
|
|
|
t.Fatalf("Wrong image comment. Should be '%s', not '%s'", image.Comment, resultImg.Comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMount(t *testing.T) {
|
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
2013-03-21 05:42:08 +00:00
|
|
|
archive, err := fakeTar()
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-26 00:02:38 +00:00
|
|
|
image, err := graph.Create(archive, nil, "Testing", "", nil)
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-test-graph-mount-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
rootfs := path.Join(tmp, "rootfs")
|
|
|
|
if err := os.MkdirAll(rootfs, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rw := path.Join(tmp, "rw")
|
|
|
|
if err := os.MkdirAll(rw, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := image.Mount(rootfs, rw); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// FIXME: test for mount contents
|
|
|
|
defer func() {
|
|
|
|
if err := Unmount(rootfs); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-04-01 23:02:02 +00:00
|
|
|
// Test that an image can be deleted by its shorthand prefix
|
|
|
|
func TestDeletePrefix(t *testing.T) {
|
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
|
|
|
img := createTestImage(graph, t)
|
2013-06-04 18:00:22 +00:00
|
|
|
if err := graph.Delete(utils.TruncateID(img.ID)); err != nil {
|
2013-04-01 23:02:02 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestImage(graph *Graph, t *testing.T) *Image {
|
|
|
|
archive, err := fakeTar()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-26 00:02:38 +00:00
|
|
|
img, err := graph.Create(archive, nil, "Test image", "", nil)
|
2013-04-01 23:02:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2013-03-19 00:57:18 +00:00
|
|
|
func TestDelete(t *testing.T) {
|
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
2013-03-21 05:42:08 +00:00
|
|
|
archive, err := fakeTar()
|
2013-03-19 00:57:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 0)
|
2013-04-26 00:02:38 +00:00
|
|
|
img, err := graph.Create(archive, nil, "Bla bla", "", nil)
|
2013-03-19 00:57:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 1)
|
2013-06-04 18:00:22 +00:00
|
|
|
if err := graph.Delete(img.ID); err != nil {
|
2013-03-19 00:57:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 0)
|
|
|
|
|
2013-06-19 00:22:32 +00:00
|
|
|
archive, err = fakeTar()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-19 00:57:18 +00:00
|
|
|
// Test 2 create (same name) / 1 delete
|
2013-04-26 00:02:38 +00:00
|
|
|
img1, err := graph.Create(archive, nil, "Testing", "", nil)
|
2013-03-19 00:57:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-19 00:22:32 +00:00
|
|
|
archive, err = fakeTar()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-04-26 00:02:38 +00:00
|
|
|
if _, err = graph.Create(archive, nil, "Testing", "", nil); err != nil {
|
2013-03-19 00:57:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 2)
|
2013-06-04 18:00:22 +00:00
|
|
|
if err := graph.Delete(img1.ID); err != nil {
|
2013-03-19 00:57:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 1)
|
|
|
|
|
|
|
|
// Test delete wrong name
|
|
|
|
if err := graph.Delete("Not_foo"); err == nil {
|
|
|
|
t.Fatalf("Deleting wrong ID should return an error")
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 1)
|
|
|
|
|
2013-06-19 00:22:32 +00:00
|
|
|
archive, err = fakeTar()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-27 19:09:58 +00:00
|
|
|
// Test delete twice (pull -> rm -> pull -> rm)
|
2013-07-30 13:13:18 +00:00
|
|
|
if err := graph.Register(nil, archive, img1); err != nil {
|
2013-03-27 19:09:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-04 18:00:22 +00:00
|
|
|
if err := graph.Delete(img1.ID); err != nil {
|
2013-03-27 19:09:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertNImages(graph, t, 1)
|
2013-03-19 00:57:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 08:24:21 +00:00
|
|
|
func TestByParent(t *testing.T) {
|
|
|
|
archive1, _ := fakeTar()
|
|
|
|
archive2, _ := fakeTar()
|
|
|
|
archive3, _ := fakeTar()
|
|
|
|
|
|
|
|
graph := tempGraph(t)
|
|
|
|
defer os.RemoveAll(graph.Root)
|
|
|
|
parentImage := &Image{
|
|
|
|
ID: GenerateID(),
|
|
|
|
Comment: "parent",
|
|
|
|
Created: time.Now(),
|
|
|
|
Parent: "",
|
|
|
|
}
|
|
|
|
childImage1 := &Image{
|
|
|
|
ID: GenerateID(),
|
|
|
|
Comment: "child1",
|
|
|
|
Created: time.Now(),
|
|
|
|
Parent: parentImage.ID,
|
|
|
|
}
|
|
|
|
childImage2 := &Image{
|
|
|
|
ID: GenerateID(),
|
|
|
|
Comment: "child2",
|
|
|
|
Created: time.Now(),
|
|
|
|
Parent: parentImage.ID,
|
|
|
|
}
|
|
|
|
_ = graph.Register(nil, archive1, parentImage)
|
|
|
|
_ = graph.Register(nil, archive2, childImage1)
|
|
|
|
_ = graph.Register(nil, archive3, childImage2)
|
|
|
|
|
|
|
|
byParent, err := graph.ByParent()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
numChildren := len(byParent[parentImage.ID])
|
|
|
|
if numChildren != 2 {
|
|
|
|
t.Fatalf("Expected 2 children, found %d", numChildren)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 00:57:18 +00:00
|
|
|
func assertNImages(graph *Graph, t *testing.T, n int) {
|
2013-09-01 03:31:21 +00:00
|
|
|
if images, err := graph.Map(); err != nil {
|
2013-03-19 00:57:18 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if actualN := len(images); actualN != n {
|
|
|
|
t.Fatalf("Expected %d images, found %d", n, actualN)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 07:15:35 +00:00
|
|
|
/*
|
|
|
|
* HELPER FUNCTIONS
|
|
|
|
*/
|
|
|
|
|
|
|
|
func tempGraph(t *testing.T) *Graph {
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-graph-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-03-22 00:47:23 +00:00
|
|
|
graph, err := NewGraph(tmp)
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func testArchive(t *testing.T) Archive {
|
2013-03-21 05:42:08 +00:00
|
|
|
archive, err := fakeTar()
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return archive
|
|
|
|
}
|
2013-03-21 05:42:08 +00:00
|
|
|
|
|
|
|
func fakeTar() (io.Reader, error) {
|
|
|
|
content := []byte("Hello world!\n")
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buf)
|
|
|
|
for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
|
|
|
|
hdr := new(tar.Header)
|
|
|
|
hdr.Size = int64(len(content))
|
|
|
|
hdr.Name = name
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tw.Write([]byte(content))
|
|
|
|
}
|
|
|
|
tw.Close()
|
|
|
|
return buf, nil
|
|
|
|
}
|