containerd/mount/lookup_test/lookup_linux_test.go
Daniel Nephin ef48a0268e Migrate to gotestyourself/assert
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
2018-02-12 12:26:26 -05:00

126 lines
3.5 KiB
Go

// +build linux
// FIXME: we can't put this test to the mount package:
// import cycle not allowed in test
// package github.com/containerd/containerd/mount (test)
// imports github.com/containerd/containerd/testutil
// imports github.com/containerd/containerd/mount
//
// NOTE: we can't have this as lookup_test (compilation fails)
package lookuptest
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/testutil"
"github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp"
)
func checkLookup(t *testing.T, fsType, mntPoint, dir string) {
info, err := mount.Lookup(dir)
assert.NilError(t, err)
assert.Check(t, is.Equal(fsType, info.FSType))
assert.Check(t, is.Equal(mntPoint, info.Mountpoint))
}
func testLookup(t *testing.T, fsType string) {
testutil.RequiresRoot(t)
mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(mnt)
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))
}
if out, err := exec.Command("mount", deviceName, mnt).CombinedOutput(); err != nil {
// not fatal
t.Skipf("could not mount %s: %v (out: %q)", deviceName, err, string(out))
}
defer func() {
testutil.Unmount(t, mnt)
cleanupDevice()
}()
assert.Check(t, strings.HasPrefix(deviceName, "/dev/loop"))
checkLookup(t, fsType, mnt, mnt)
newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(newMnt)
if out, err := exec.Command("mount", "--bind", mnt, newMnt).CombinedOutput(); err != nil {
t.Fatalf("could not mount %s to %s: %v (out: %q)", mnt, newMnt, err, string(out))
}
defer func() {
testutil.Unmount(t, newMnt)
}()
checkLookup(t, fsType, newMnt, newMnt)
subDir := filepath.Join(newMnt, "subDir")
err = os.MkdirAll(subDir, 0700)
if err != nil {
t.Fatal(err)
}
checkLookup(t, fsType, newMnt, subDir)
}
func TestLookupWithExt4(t *testing.T) {
testLookup(t, "ext4")
}
func TestLookupWithXFS(t *testing.T) {
testLookup(t, "xfs")
}
func TestLookupWithOverlay(t *testing.T) {
lower, err := ioutil.TempDir("", "containerd-mountinfo-test-lower")
assert.NilError(t, err)
defer os.RemoveAll(lower)
upper, err := ioutil.TempDir("", "containerd-mountinfo-test-upper")
assert.NilError(t, err)
defer os.RemoveAll(upper)
work, err := ioutil.TempDir("", "containerd-mountinfo-test-work")
assert.NilError(t, err)
defer os.RemoveAll(work)
overlay, err := ioutil.TempDir("", "containerd-mountinfo-test-overlay")
assert.NilError(t, err)
defer os.RemoveAll(overlay)
if out, err := exec.Command("mount", "-t", "overlay", "overlay", "-o", fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s",
lower, upper, work), overlay).CombinedOutput(); err != nil {
// not fatal
t.Skipf("could not mount overlay: %v (out: %q)", err, string(out))
}
defer testutil.Unmount(t, overlay)
testdir := filepath.Join(overlay, "testdir")
err = os.Mkdir(testdir, 0777)
assert.NilError(t, err)
testfile := filepath.Join(overlay, "testfile")
_, err = os.Create(testfile)
assert.NilError(t, err)
checkLookup(t, "overlay", overlay, testdir)
checkLookup(t, "overlay", overlay, testfile)
}