snapshotter: add more assertion

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2017-02-15 06:50:43 +00:00
parent a7ef3e5313
commit 750cdc951f
5 changed files with 92 additions and 39 deletions

View file

@ -5,9 +5,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/docker/containerd"
"github.com/docker/containerd/fs/fstest"
"github.com/docker/containerd/testutil"
"github.com/stretchr/testify/assert"
)
@ -225,6 +227,8 @@ func SnapshotterSuite(t *testing.T, name string, snapshotterFn func(root string)
func makeTest(t *testing.T, name string, snapshotterFn func(root string) (Snapshotter, func(), error), fn func(t *testing.T, snapshotter Snapshotter, work string)) func(t *testing.T) {
return func(t *testing.T) {
oldumask := syscall.Umask(0)
defer syscall.Umask(oldumask)
// Make two directories: a snapshotter root and a play area for the tests:
//
// /tmp
@ -261,14 +265,29 @@ func makeTest(t *testing.T, name string, snapshotterFn func(root string) (Snapsh
// checkSnapshotterBasic tests the basic workflow of a snapshot snapshotter.
func checkSnapshotterBasic(t *testing.T, snapshotter Snapshotter, work string) {
ctx := context.TODO()
initialApplier := fstest.Apply(
fstest.CreateFile("/foo", []byte("foo\n"), 0777),
fstest.CreateDir("/a", 0755),
fstest.CreateDir("/a/b", 0755),
fstest.CreateDir("/a/b/c", 0755),
)
diffApplier := fstest.Apply(
fstest.CreateFile("/bar", []byte("bar\n"), 0777),
// also, change content of foo to bar
fstest.CreateFile("/foo", []byte("bar\n"), 0777),
fstest.RemoveAll("/a/b"),
)
preparing := filepath.Join(work, "preparing")
if err := os.MkdirAll(preparing, 0777); err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
mounts, err := snapshotter.Prepare(ctx, preparing, "")
if err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
if len(mounts) < 1 {
@ -276,26 +295,22 @@ func checkSnapshotterBasic(t *testing.T, snapshotter Snapshotter, work string) {
}
if err := containerd.MountAll(mounts, preparing); err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
defer testutil.Unmount(t, preparing)
if err := ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(preparing, "a", "b", "c"), 0755); err != nil {
t.Fatal(err)
if err := initialApplier.Apply(preparing); err != nil {
t.Fatalf("failure reason: %+v", err)
}
committed := filepath.Join(work, "committed")
if err := snapshotter.Commit(ctx, committed, preparing); err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
si, err := snapshotter.Stat(ctx, committed)
if err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
assert.Equal(t, "", si.Parent)
@ -303,29 +318,24 @@ func checkSnapshotterBasic(t *testing.T, snapshotter Snapshotter, work string) {
next := filepath.Join(work, "nextlayer")
if err := os.MkdirAll(next, 0777); err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
mounts, err = snapshotter.Prepare(ctx, next, committed)
if err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
if err := containerd.MountAll(mounts, next); err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
defer testutil.Unmount(t, next)
if err := ioutil.WriteFile(filepath.Join(next, "bar"), []byte("bar\n"), 0777); err != nil {
t.Fatal(err)
if err := fstest.CheckDirectoryEqualWithApplier(next, initialApplier); err != nil {
t.Fatalf("failure reason: %+v", err)
}
// also, change content of foo to bar
if err := ioutil.WriteFile(filepath.Join(next, "foo"), []byte("bar\n"), 0777); err != nil {
t.Fatal(err)
}
if err := os.RemoveAll(filepath.Join(next, "a", "b")); err != nil {
t.Log(err)
if err := diffApplier.Apply(next); err != nil {
t.Fatalf("failure reason: %+v", err)
}
ni, err := snapshotter.Stat(ctx, next)
@ -338,12 +348,12 @@ func checkSnapshotterBasic(t *testing.T, snapshotter Snapshotter, work string) {
nextCommitted := filepath.Join(work, "committed-next")
if err := snapshotter.Commit(ctx, nextCommitted, next); err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
si2, err := snapshotter.Stat(ctx, nextCommitted)
if err != nil {
t.Fatal(err)
t.Fatalf("failure reason: %+v", err)
}
assert.Equal(t, committed, si2.Parent)
@ -360,4 +370,23 @@ func checkSnapshotterBasic(t *testing.T, snapshotter Snapshotter, work string) {
}))
assert.Equal(t, expected, walked)
nextnext := filepath.Join(work, "nextnextlayer")
if err := os.MkdirAll(nextnext, 0777); err != nil {
t.Fatalf("failure reason: %+v", err)
}
mounts, err = snapshotter.View(ctx, nextnext, nextCommitted)
if err != nil {
t.Fatalf("failure reason: %+v", err)
}
if err := containerd.MountAll(mounts, nextnext); err != nil {
t.Fatalf("failure reason: %+v", err)
}
defer testutil.Unmount(t, nextnext)
if err := fstest.CheckDirectoryEqualWithApplier(nextnext,
fstest.Apply(initialApplier, diffApplier)); err != nil {
t.Fatalf("failure reason: %+v", err)
}
}