changed the fs.store.Register and fs.store.Create, so that it is a little cleaner, and easier to reuse. Also added a unit test

This commit is contained in:
Ken Cochrane 2013-03-13 13:35:34 -07:00
parent fde01381d5
commit f0a65207ab
2 changed files with 51 additions and 22 deletions

View File

@ -160,42 +160,35 @@ func (store *Store) Create(layerData Archive, parent *Image, pth, comment string
if parent != nil {
img.Parent = parent.Id
}
// FIXME: we shouldn't have to pass os.Stderr to AddLayer()...
// FIXME: Archive should contain compression info. For now we only support uncompressed.
_, err := store.layers.AddLayer(img.Id, layerData)
err := store.Register(layerData, img, pth)
return img, err
}
func (store *Store) Register(layerData Archive, image *Image, pth string) error {
image.store = store
_, err := store.layers.AddLayer(image.Id, layerData)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not add layer: %s", err))
return errors.New(fmt.Sprintf("Could not add layer: %s", err))
}
path := &Path{
Path: path.Clean(pth),
Image: img.Id,
Image: image.Id,
}
trans, err := store.orm.Begin()
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not begin transaction:", err))
return errors.New(fmt.Sprintf("Could not begin transaction:", err))
}
if err := trans.Insert(img); err != nil {
return nil, errors.New(fmt.Sprintf("Could not insert image info: %s", err))
if err := trans.Insert(image); err != nil {
return errors.New(fmt.Sprintf("Could not insert image info: %s", err))
}
if err := trans.Insert(path); err != nil {
return nil, errors.New(fmt.Sprintf("Could not insert path info: %s", err))
return errors.New(fmt.Sprintf("Could not insert path info: %s", err))
}
if err := trans.Commit(); err != nil {
return nil, errors.New(fmt.Sprintf("Could not commit transaction: %s", err))
return errors.New(fmt.Sprintf("Could not commit transaction: %s", err))
}
return img, nil
}
func (store *Store) Register(image *Image, pth string) error {
image.store = store
// FIXME: import layer
trans, err := store.orm.Begin()
if err != nil {
return err
}
trans.Insert(image)
trans.Insert(&Path{Path: pth, Image: image.Id})
return trans.Commit()
return nil
}
func (store *Store) Layers() []string {

View File

@ -4,9 +4,11 @@ import (
"errors"
"fmt"
"github.com/dotcloud/docker/fake"
"github.com/dotcloud/docker/future"
"io/ioutil"
"os"
"testing"
"time"
)
func TestInit(t *testing.T) {
@ -52,6 +54,40 @@ func TestCreate(t *testing.T) {
}
}
func TestRegister(t *testing.T) {
store, err := TempStore("testregister")
if err != nil {
t.Fatal(err)
}
defer nuke(store)
archive, err := fake.FakeTar()
if err != nil {
t.Fatal(err)
}
image := &Image{
Id: future.RandomId(),
Comment: "testing",
Created: time.Now().Unix(),
store: store,
}
err = store.Register(archive, image, "foo")
if err != nil {
t.Fatal(err)
}
if images, err := store.Images(); err != nil {
t.Fatal(err)
} else if l := len(images); l != 1 {
t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
}
if images, err := store.List("foo"); err != nil {
t.Fatal(err)
} else if l := len(images); l != 1 {
t.Fatalf("Path foo has wrong number of images (should be %d, not %d)", 1, l)
} else if images[0].Id != image.Id {
t.Fatalf("Imported image should be listed at path foo (%s != %s)", images[0], image)
}
}
func TestTag(t *testing.T) {
store, err := TempStore("testtag")
if err != nil {