Merge pull request #414 from samuelkarp/snapshot-tests

snapshot: Fix leaked mounts and temp directories
This commit is contained in:
Stephen Day 2017-01-11 17:19:39 -08:00 committed by GitHub
commit 9a740a0a42
4 changed files with 36 additions and 24 deletions

View file

@ -53,11 +53,7 @@ func TestBtrfs(t *testing.T) {
if err := containerd.MountAll(mounts...); err != nil { if err := containerd.MountAll(mounts...); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer func(mounts []containerd.Mount) { defer unmountAll(t, mounts)
for _, mount := range mounts {
unmount(t, mount.Target)
}
}(mounts)
// write in some data // write in some data
if err := ioutil.WriteFile(filepath.Join(mounts[0].Target, "foo"), []byte("content"), 0777); err != nil { if err := ioutil.WriteFile(filepath.Join(mounts[0].Target, "foo"), []byte("content"), 0777); err != nil {
@ -93,11 +89,7 @@ func TestBtrfs(t *testing.T) {
if err := containerd.MountAll(mounts...); err != nil { if err := containerd.MountAll(mounts...); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer func(mounts []containerd.Mount) { defer unmountAll(t, mounts)
for _, mount := range mounts {
unmount(t, mount.Target)
}
}(mounts)
// TODO(stevvooe): Verify contents of "foo" // TODO(stevvooe): Verify contents of "foo"
if err := ioutil.WriteFile(filepath.Join(mounts[0].Target, "bar"), []byte("content"), 0777); err != nil { if err := ioutil.WriteFile(filepath.Join(mounts[0].Target, "bar"), []byte("content"), 0777); err != nil {
@ -214,13 +206,3 @@ func removeBtrfsLoopbackDevice(t *testing.T, device *testDevice) {
t.Error(err) t.Error(err)
} }
} }
func unmount(t *testing.T, mountPoint string) {
t.Log("unmount", mountPoint)
umount := exec.Command("umount", mountPoint)
err := umount.Run()
if err != nil {
t.Error("Could not umount", mountPoint, err)
}
}

24
snapshot/common_test.go Normal file
View file

@ -0,0 +1,24 @@
package snapshot
import (
"os/exec"
"testing"
"github.com/docker/containerd"
)
func unmountAll(t *testing.T, mounts []containerd.Mount) {
for _, mount := range mounts {
unmount(t, mount.Target)
}
}
func unmount(t *testing.T, mountPoint string) {
t.Log("unmount", mountPoint)
umount := exec.Command("umount", mountPoint)
err := umount.Run()
if err != nil {
t.Error("Could not umount", mountPoint, err)
}
}

View file

@ -18,7 +18,13 @@ func TestSnapshotManagerBasic(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// defer os.RemoveAll(tmpDir) defer func() {
t.Log("Removing", tmpDir)
err := os.RemoveAll(tmpDir)
if err != nil {
t.Error(err)
}
}()
root := filepath.Join(tmpDir, "root") root := filepath.Join(tmpDir, "root")
@ -52,6 +58,7 @@ func TestSnapshotManagerBasic(t *testing.T) {
if err := containerd.MountAll(mounts...); err != nil { if err := containerd.MountAll(mounts...); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer unmountAll(t, mounts)
if err := ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil { if err := ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
t.Fatal(err) t.Fatal(err)
@ -59,8 +66,6 @@ func TestSnapshotManagerBasic(t *testing.T) {
os.MkdirAll(preparing+"/a/b/c", 0755) os.MkdirAll(preparing+"/a/b/c", 0755)
// defer os.Remove(filepath.Join(tmpDir, "foo"))
committed := filepath.Join(lm.root, "committed") committed := filepath.Join(lm.root, "committed")
if err := lm.Commit(committed, preparing); err != nil { if err := lm.Commit(committed, preparing); err != nil {
@ -83,6 +88,7 @@ func TestSnapshotManagerBasic(t *testing.T) {
if err := containerd.MountAll(mounts...); err != nil { if err := containerd.MountAll(mounts...); err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer unmountAll(t, mounts)
for _, mount := range mounts { for _, mount := range mounts {
if !strings.HasPrefix(mount.Target, next) { if !strings.HasPrefix(mount.Target, next) {

View file

@ -15,7 +15,7 @@ func TestSnapshotNaiveBasic(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
t.Log(tmpDir) t.Log(tmpDir)
root := filepath.Join(tmpDir, "root") root := filepath.Join(tmpDir, "root")