| @@ -10,6 +10,7 @@ | ||||
| package lookuptest | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| @@ -20,18 +21,19 @@ import ( | ||||
| 	"github.com/containerd/containerd/mount" | ||||
| 	"github.com/containerd/containerd/testutil" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"github.com/stretchr/testify/require" | ||||
| ) | ||||
|  | ||||
| func testLookup(t *testing.T, fsType string) { | ||||
| 	checkLookup := func(mntPoint, dir string) { | ||||
| 		info, err := mount.Lookup(dir) | ||||
| 		if err != nil { | ||||
| 			t.Fatal(err) | ||||
| 		} | ||||
| 		assert.Equal(t, fsType, info.FSType) | ||||
| 		assert.Equal(t, mntPoint, info.Mountpoint) | ||||
| func checkLookup(t *testing.T, fsType, mntPoint, dir string) { | ||||
| 	info, err := mount.Lookup(dir) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	assert.Equal(t, fsType, info.FSType) | ||||
| 	assert.Equal(t, mntPoint, info.Mountpoint) | ||||
| } | ||||
|  | ||||
| func testLookup(t *testing.T, fsType string) { | ||||
| 	testutil.RequiresRoot(t) | ||||
| 	mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup") | ||||
| 	if err != nil { | ||||
| @@ -56,7 +58,7 @@ func testLookup(t *testing.T, fsType string) { | ||||
| 		cleanupDevice() | ||||
| 	}() | ||||
| 	assert.True(t, strings.HasPrefix(deviceName, "/dev/loop")) | ||||
| 	checkLookup(mnt, mnt) | ||||
| 	checkLookup(t, fsType, mnt, mnt) | ||||
|  | ||||
| 	newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt") | ||||
| 	if err != nil { | ||||
| @@ -70,14 +72,14 @@ func testLookup(t *testing.T, fsType string) { | ||||
| 	defer func() { | ||||
| 		testutil.Unmount(t, newMnt) | ||||
| 	}() | ||||
| 	checkLookup(newMnt, newMnt) | ||||
| 	checkLookup(t, fsType, newMnt, newMnt) | ||||
|  | ||||
| 	subDir := filepath.Join(newMnt, "subDir") | ||||
| 	err = os.MkdirAll(subDir, 0700) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	checkLookup(newMnt, subDir) | ||||
| 	checkLookup(t, fsType, newMnt, subDir) | ||||
| } | ||||
|  | ||||
| func TestLookupWithExt4(t *testing.T) { | ||||
| @@ -87,3 +89,39 @@ func TestLookupWithExt4(t *testing.T) { | ||||
| func TestLookupWithXFS(t *testing.T) { | ||||
| 	testLookup(t, "xfs") | ||||
| } | ||||
|  | ||||
| func TestLookupWithOverlay(t *testing.T) { | ||||
| 	lower, err := ioutil.TempDir("", "containerd-mountinfo-test-lower") | ||||
| 	require.NoError(t, err) | ||||
| 	defer os.RemoveAll(lower) | ||||
|  | ||||
| 	upper, err := ioutil.TempDir("", "containerd-mountinfo-test-upper") | ||||
| 	require.NoError(t, err) | ||||
| 	defer os.RemoveAll(upper) | ||||
|  | ||||
| 	work, err := ioutil.TempDir("", "containerd-mountinfo-test-work") | ||||
| 	require.NoError(t, err) | ||||
| 	defer os.RemoveAll(work) | ||||
|  | ||||
| 	overlay, err := ioutil.TempDir("", "containerd-mountinfo-test-overlay") | ||||
| 	require.NoError(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) | ||||
| 	require.NoError(t, err) | ||||
|  | ||||
| 	testfile := filepath.Join(overlay, "testfile") | ||||
| 	_, err = os.Create(testfile) | ||||
| 	require.NoError(t, err) | ||||
|  | ||||
| 	checkLookup(t, "overlay", overlay, testdir) | ||||
| 	checkLookup(t, "overlay", overlay, testfile) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Lantao Liu
					Lantao Liu