Getting started with mountpoints

This commit is contained in:
Solomon Hykes 2013-02-15 21:51:36 -08:00
Родитель aab32e1012
Коммит 1531848ca6
2 изменённых файлов: 76 добавлений и 0 удалений

Просмотреть файл

@ -29,6 +29,7 @@ func New(root string) (*Store, error) {
orm := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}} orm := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
orm.AddTableWithName(Image{}, "images").SetKeys(false, "Id") orm.AddTableWithName(Image{}, "images").SetKeys(false, "Id")
orm.AddTableWithName(Path{}, "paths").SetKeys(false, "Path", "Image") orm.AddTableWithName(Path{}, "paths").SetKeys(false, "Path", "Image")
orm.AddTableWithName(Mountpoint{}, "mountpoints").SetKeys(false, "Root")
if err := orm.CreateTables(); err != nil { if err := orm.CreateTables(); err != nil {
return nil, err return nil, err
} }
@ -144,6 +145,32 @@ func (image *Image) Copy(pth string) (*Image, error) {
return image, nil return image, nil
} }
type Mountpoint struct {
Image string
Root string
Rw string
}
func (image *Image) Mountpoint(root, rw string) (*Mountpoint, error) {
mountpoint := &Mountpoint{Root: path.Clean(root), Rw: path.Clean(rw), Image: image.Id}
if err := image.store.orm.Insert(mountpoint); err != nil {
return nil, err
}
return mountpoint, nil
}
func (image *Image) Mountpoints() ([]*Mountpoint, error) {
var mountpoints []*Mountpoint
res, err := image.store.orm.Select(Mountpoint{}, "select * from mountpoints where Image=?", image.Id)
if err != nil {
return nil, err
}
for _, mp := range res {
mountpoints = append(mountpoints, mp.(*Mountpoint))
}
return mountpoints, nil
}
type Path struct { type Path struct {
Path string Path string

Просмотреть файл

@ -113,6 +113,55 @@ func TestCopySameName(t *testing.T) {
} }
} }
func TestMountPoint(t *testing.T) {
store, err := TempStore("test-mountpoint")
if err != nil {
t.Fatal(err)
}
defer nuke(store)
archive, err := fake.FakeTar()
if err != nil {
t.Fatal(err)
}
image, err := store.Create(archive, nil, "foo", "Testing")
if err != nil {
t.Fatal(err)
}
mountpoint, err := image.Mountpoint("/tmp/a", "/tmp/b")
if err != nil {
t.Fatal(err)
}
if mountpoint.Root != "/tmp/a" {
t.Fatal("Wrong mountpoint root (should be %s, not %s)", "/tmp/a", mountpoint.Root)
}
if mountpoint.Rw!= "/tmp/b" {
t.Fatal("Wrong mountpoint root (should be %s, not %s)", "/tmp/b", mountpoint.Rw)
}
}
func TestMountpointDuplicateRoot(t *testing.T) {
store, err := TempStore("test-mountpoint")
if err != nil {
t.Fatal(err)
}
defer nuke(store)
archive, err := fake.FakeTar()
if err != nil {
t.Fatal(err)
}
image, err := store.Create(archive, nil, "foo", "Testing")
if err != nil {
t.Fatal(err)
}
_, err = image.Mountpoint("/tmp/a", "/tmp/b")
if err != nil {
t.Fatal(err)
}
if _, err = image.Mountpoint("/tmp/a", "/tmp/foobar"); err == nil {
t.Fatal("Duplicate mountpoint root should fail")
}
}
/* /*
func TestMount(t *testing.T) { func TestMount(t *testing.T) {
store, err := TempStore() store, err := TempStore()