mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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:
parent
fde01381d5
commit
f0a65207ab
2 changed files with 51 additions and 22 deletions
37
fs/store.go
37
fs/store.go
|
@ -160,42 +160,35 @@ func (store *Store) Create(layerData Archive, parent *Image, pth, comment string
|
||||||
if parent != nil {
|
if parent != nil {
|
||||||
img.Parent = parent.Id
|
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.
|
// 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 {
|
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{
|
||||||
Path: path.Clean(pth),
|
Path: path.Clean(pth),
|
||||||
Image: img.Id,
|
Image: image.Id,
|
||||||
}
|
}
|
||||||
trans, err := store.orm.Begin()
|
trans, err := store.orm.Begin()
|
||||||
if err != nil {
|
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 {
|
if err := trans.Insert(image); err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Could not insert image info: %s", err))
|
return errors.New(fmt.Sprintf("Could not insert image info: %s", err))
|
||||||
}
|
}
|
||||||
if err := trans.Insert(path); err != nil {
|
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 {
|
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
|
return 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()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *Store) Layers() []string {
|
func (store *Store) Layers() []string {
|
||||||
|
|
|
@ -4,9 +4,11 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/fake"
|
"github.com/dotcloud/docker/fake"
|
||||||
|
"github.com/dotcloud/docker/future"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInit(t *testing.T) {
|
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) {
|
func TestTag(t *testing.T) {
|
||||||
store, err := TempStore("testtag")
|
store, err := TempStore("testtag")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue