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:
parent
4028add553
commit
502734116d
@ -20,7 +20,10 @@ func testSupportsDType(t *testing.T, expected bool, mkfs ...string) {
|
||||
}
|
||||
defer os.RemoveAll(mnt)
|
||||
|
||||
deviceName, cleanupDevice := testutil.NewLoopback(t, 100<<20) // 100 MB
|
||||
deviceName, cleanupDevice, err := testutil.NewLoopback(100 << 20) // 100 MB
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out, err := exec.Command(mkfs[0], append(mkfs[1:], deviceName)...).CombinedOutput(); err != nil {
|
||||
// not fatal
|
||||
t.Skipf("could not mkfs (%v) %s: %v (out: %q)", mkfs, deviceName, err, string(out))
|
||||
|
@ -29,7 +29,10 @@ func testLookup(t *testing.T, fsType string) {
|
||||
}
|
||||
defer os.RemoveAll(mnt)
|
||||
|
||||
deviceName, cleanupDevice := testutil.NewLoopback(t, 100<<20) // 100 MB
|
||||
deviceName, cleanupDevice, err := testutil.NewLoopback(100 << 20) // 100 MB
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out, err := exec.Command("mkfs", "-t", fsType, deviceName).CombinedOutput(); err != nil {
|
||||
// not fatal
|
||||
t.Skipf("could not mkfs (%s) %s: %v (out: %q)", fsType, deviceName, err, string(out))
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -7,19 +7,21 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// NewLoopback creates a loopback device, and returns its device name (/dev/loopX), and its clean-up function.
|
||||
func NewLoopback(t *testing.T, size int64) (string, func()) {
|
||||
func NewLoopback(size int64) (string, func() error, error) {
|
||||
// create temporary file for the disk image
|
||||
file, err := ioutil.TempFile("", "containerd-test-loopback")
|
||||
if err != nil {
|
||||
t.Fatalf("could not create temporary file for loopback: %v", err)
|
||||
return "", nil, errors.Wrap(err, "could not create temporary file for loopback")
|
||||
}
|
||||
|
||||
if err := file.Truncate(size); err != nil {
|
||||
t.Fatal(err)
|
||||
return "", nil, errors.Wrap(err, "failed to resize temp file")
|
||||
}
|
||||
file.Close()
|
||||
|
||||
@ -27,27 +29,28 @@ func NewLoopback(t *testing.T, size int64) (string, func()) {
|
||||
losetup := exec.Command("losetup", "--find", "--show", file.Name())
|
||||
p, err := losetup.Output()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
return "", nil, errors.Wrap(err, "loopback setup failed")
|
||||
}
|
||||
|
||||
deviceName := strings.TrimSpace(string(p))
|
||||
t.Logf("Created loop device %s (using %s)", deviceName, file.Name())
|
||||
logrus.Debugf("Created loop device %s (using %s)", deviceName, file.Name())
|
||||
|
||||
cleanup := func() {
|
||||
cleanup := func() error {
|
||||
// detach device
|
||||
t.Logf("Removing loop device %s", deviceName)
|
||||
logrus.Debugf("Removing loop device %s", deviceName)
|
||||
losetup := exec.Command("losetup", "--detach", deviceName)
|
||||
err := losetup.Run()
|
||||
if err != nil {
|
||||
t.Error("Could not remove loop device", deviceName, err)
|
||||
return errors.Wrapf(err, "Could not remove loop device %s", deviceName)
|
||||
}
|
||||
|
||||
// remove file
|
||||
t.Logf("Removing temporary file %s", file.Name())
|
||||
logrus.Debugf("Removing temporary file %s", file.Name())
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
t.Error(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return deviceName, cleanup
|
||||
return deviceName, cleanup, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user