Move snapshot testsuite to sub package

Prevents imports of snapshot to require importing test packages.

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
Derek McGowan 2017-03-07 09:16:45 -08:00
parent f06db40baf
commit 66bbf5c449
5 changed files with 348 additions and 351 deletions

View file

@ -2,16 +2,8 @@ package snapshot
import (
"context"
"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"
)
// Kind identifies the kind of snapshot.
@ -215,178 +207,3 @@ type Snapshotter interface {
// function will be called.
Walk(ctx context.Context, fn func(context.Context, Info) error) error
}
// SnapshotterSuite runs a test suite on the snapshotter given a factory function.
func SnapshotterSuite(t *testing.T, name string, snapshotterFn func(root string) (Snapshotter, func(), error)) {
t.Run("Basic", makeTest(t, name, snapshotterFn, checkSnapshotterBasic))
t.Run("StatActive", makeTest(t, name, snapshotterFn, checkSnapshotterStatActive))
t.Run("StatComitted", makeTest(t, name, snapshotterFn, checkSnapshotterStatCommitted))
t.Run("TransitivityTest", makeTest(t, name, snapshotterFn, checkSnapshotterTransitivity))
}
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
// work/ -> passed to test functions
// root/ -> passed to snapshotter
//
tmpDir, err := ioutil.TempDir("", "snapshot-suite-"+name+"-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
root := filepath.Join(tmpDir, "root")
if err := os.MkdirAll(root, 0777); err != nil {
t.Fatal(err)
}
snapshotter, cleanup, err := snapshotterFn(root)
if err != nil {
t.Fatal(err)
}
defer cleanup()
work := filepath.Join(tmpDir, "work")
if err := os.MkdirAll(work, 0777); err != nil {
t.Fatal(err)
}
defer testutil.DumpDir(t, tmpDir)
fn(t, snapshotter, work)
}
}
// 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.Fatalf("failure reason: %+v", err)
}
mounts, err := snapshotter.Prepare(ctx, preparing, "")
if err != nil {
t.Fatalf("failure reason: %+v", err)
}
if len(mounts) < 1 {
t.Fatal("expected mounts to have entries")
}
if err := containerd.MountAll(mounts, preparing); err != nil {
t.Fatalf("failure reason: %+v", err)
}
defer testutil.Unmount(t, preparing)
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.Fatalf("failure reason: %+v", err)
}
si, err := snapshotter.Stat(ctx, committed)
if err != nil {
t.Fatalf("failure reason: %+v", err)
}
assert.Equal(t, "", si.Parent)
assert.Equal(t, KindCommitted, si.Kind)
next := filepath.Join(work, "nextlayer")
if err := os.MkdirAll(next, 0777); err != nil {
t.Fatalf("failure reason: %+v", err)
}
mounts, err = snapshotter.Prepare(ctx, next, committed)
if err != nil {
t.Fatalf("failure reason: %+v", err)
}
if err := containerd.MountAll(mounts, next); err != nil {
t.Fatalf("failure reason: %+v", err)
}
defer testutil.Unmount(t, next)
if err := fstest.CheckDirectoryEqualWithApplier(next, initialApplier); err != nil {
t.Fatalf("failure reason: %+v", err)
}
if err := diffApplier.Apply(next); err != nil {
t.Fatalf("failure reason: %+v", err)
}
ni, err := snapshotter.Stat(ctx, next)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, committed, ni.Parent)
assert.Equal(t, KindActive, ni.Kind)
nextCommitted := filepath.Join(work, "committed-next")
if err := snapshotter.Commit(ctx, nextCommitted, next); err != nil {
t.Fatalf("failure reason: %+v", err)
}
si2, err := snapshotter.Stat(ctx, nextCommitted)
if err != nil {
t.Fatalf("failure reason: %+v", err)
}
assert.Equal(t, committed, si2.Parent)
assert.Equal(t, KindCommitted, si2.Kind)
expected := map[string]Info{
si.Name: si,
si2.Name: si2,
}
walked := map[string]Info{} // walk is not ordered
assert.NoError(t, snapshotter.Walk(ctx, func(ctx context.Context, si Info) error {
walked[si.Name] = si
return nil
}))
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)
}
}