From 1531848ca6ead7c46497a625feba1224825e9011 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 15 Feb 2013 21:51:36 -0800 Subject: [PATCH] Getting started with mountpoints --- fs/store.go | 27 ++++++++++++++++++++++++++ fs/store_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/fs/store.go b/fs/store.go index 02bfd47e8f..4ba23ae7dd 100644 --- a/fs/store.go +++ b/fs/store.go @@ -29,6 +29,7 @@ func New(root string) (*Store, error) { orm := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}} orm.AddTableWithName(Image{}, "images").SetKeys(false, "Id") orm.AddTableWithName(Path{}, "paths").SetKeys(false, "Path", "Image") + orm.AddTableWithName(Mountpoint{}, "mountpoints").SetKeys(false, "Root") if err := orm.CreateTables(); err != nil { return nil, err } @@ -144,6 +145,32 @@ func (image *Image) Copy(pth string) (*Image, error) { 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 { Path string diff --git a/fs/store_test.go b/fs/store_test.go index 940396227d..89198f94d5 100644 --- a/fs/store_test.go +++ b/fs/store_test.go @@ -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) { store, err := TempStore()