Update loopback to return error

Avoid calling testing function in creation closure since the
context may no longer be valid.

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
Derek McGowan
2017-08-14 20:52:01 -07:00
parent 4028add553
commit 502734116d
4 changed files with 42 additions and 22 deletions

View File

@@ -15,30 +15,41 @@ import (
"github.com/containerd/containerd/snapshot"
"github.com/containerd/containerd/snapshot/testsuite"
"github.com/containerd/containerd/testutil"
"github.com/pkg/errors"
)
func boltSnapshotter(t *testing.T) func(context.Context, string) (snapshot.Snapshotter, func(), error) {
mkbtrfs, err := exec.LookPath("mkfs.btrfs")
if err != nil {
t.Skipf("could not find mkfs.btrfs: %v", err)
}
// TODO: Check for btrfs in /proc/module and skip if not loaded
return func(ctx context.Context, root string) (snapshot.Snapshotter, func(), error) {
deviceName, cleanupDevice := testutil.NewLoopback(t, 100<<20) // 100 MB
deviceName, cleanupDevice, err := testutil.NewLoopback(100 << 20) // 100 MB
if err != nil {
return nil, nil, err
}
if out, err := exec.Command("mkfs.btrfs", deviceName).CombinedOutput(); err != nil {
// not fatal
t.Skipf("could not mkfs.btrfs %s: %v (out: %q)", deviceName, err, string(out))
if out, err := exec.Command(mkbtrfs, deviceName).CombinedOutput(); err != nil {
return nil, nil, errors.Wrapf(err, "failed to make btrfs filesystem (out: %q)", out)
}
if out, err := exec.Command("mount", deviceName, root).CombinedOutput(); err != nil {
// not fatal
t.Skipf("could not mount %s: %v (out: %q)", deviceName, err, string(out))
return nil, nil, errors.Wrapf(err, "failed to mount device %s (out: %q)", deviceName, out)
}
snapshotter, err := NewSnapshotter(root)
if err != nil {
t.Fatal(err)
return nil, nil, errors.Wrap(err, "failed to create new snapshotter")
}
return snapshotter, func() {
testutil.Unmount(t, root)
cleanupDevice()
if err := cleanupDevice(); err != nil {
t.Errorf("Device cleanup failed: %v", err)
}
}, nil
}
}