github.com/containerd/Containerd@v1.4.13/mount/lookup_linux_test.go (about) 1 // +build linux 2 3 /* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package mount 20 21 import ( 22 "fmt" 23 "io/ioutil" 24 "os" 25 "os/exec" 26 "path/filepath" 27 "strings" 28 "testing" 29 30 // containerd/pkg/testutil has circular dependency on this mount pkg. 31 // so we use continuity/testutil instead. 32 "github.com/containerd/continuity/testutil" 33 "github.com/containerd/continuity/testutil/loopback" 34 "gotest.tools/v3/assert" 35 ) 36 37 func checkLookup(t *testing.T, fsType, mntPoint, dir string) { 38 t.Helper() 39 info, err := Lookup(dir) 40 assert.NilError(t, err) 41 assert.Equal(t, fsType, info.FSType) 42 assert.Equal(t, mntPoint, info.Mountpoint) 43 } 44 45 func testLookup(t *testing.T, fsType string) { 46 testutil.RequiresRoot(t) 47 mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup") 48 if err != nil { 49 t.Fatal(err) 50 } 51 defer os.RemoveAll(mnt) 52 53 loop, err := loopback.New(100 << 20) // 100 MB 54 if err != nil { 55 t.Fatal(err) 56 } 57 if out, err := exec.Command("mkfs", "-t", fsType, loop.Device).CombinedOutput(); err != nil { 58 // not fatal 59 loop.Close() 60 t.Skipf("could not mkfs (%s) %s: %v (out: %q)", fsType, loop.Device, err, string(out)) 61 } 62 if out, err := exec.Command("mount", loop.Device, mnt).CombinedOutput(); err != nil { 63 // not fatal 64 loop.Close() 65 t.Skipf("could not mount %s: %v (out: %q)", loop.Device, err, string(out)) 66 } 67 defer func() { 68 testutil.Unmount(t, mnt) 69 loop.Close() 70 }() 71 assert.Check(t, strings.HasPrefix(loop.Device, "/dev/loop")) 72 checkLookup(t, fsType, mnt, mnt) 73 74 newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt") 75 if err != nil { 76 t.Fatal(err) 77 } 78 defer os.RemoveAll(newMnt) 79 80 if out, err := exec.Command("mount", "--bind", mnt, newMnt).CombinedOutput(); err != nil { 81 t.Fatalf("could not mount %s to %s: %v (out: %q)", mnt, newMnt, err, string(out)) 82 } 83 defer func() { 84 testutil.Unmount(t, newMnt) 85 }() 86 checkLookup(t, fsType, newMnt, newMnt) 87 88 subDir := filepath.Join(newMnt, "subDir") 89 err = os.MkdirAll(subDir, 0700) 90 if err != nil { 91 t.Fatal(err) 92 } 93 checkLookup(t, fsType, newMnt, subDir) 94 } 95 96 func TestLookupWithExt4(t *testing.T) { 97 testLookup(t, "ext4") 98 } 99 100 func TestLookupWithXFS(t *testing.T) { 101 testLookup(t, "xfs") 102 } 103 104 func TestLookupWithOverlay(t *testing.T) { 105 lower, err := ioutil.TempDir("", "containerd-mountinfo-test-lower") 106 assert.NilError(t, err) 107 defer os.RemoveAll(lower) 108 109 upper, err := ioutil.TempDir("", "containerd-mountinfo-test-upper") 110 assert.NilError(t, err) 111 defer os.RemoveAll(upper) 112 113 work, err := ioutil.TempDir("", "containerd-mountinfo-test-work") 114 assert.NilError(t, err) 115 defer os.RemoveAll(work) 116 117 overlay, err := ioutil.TempDir("", "containerd-mountinfo-test-overlay") 118 assert.NilError(t, err) 119 defer os.RemoveAll(overlay) 120 121 if out, err := exec.Command("mount", "-t", "overlay", "overlay", "-o", fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", 122 lower, upper, work), overlay).CombinedOutput(); err != nil { 123 // not fatal 124 t.Skipf("could not mount overlay: %v (out: %q)", err, string(out)) 125 } 126 defer testutil.Unmount(t, overlay) 127 128 testdir := filepath.Join(overlay, "testdir") 129 err = os.Mkdir(testdir, 0777) 130 assert.NilError(t, err) 131 132 testfile := filepath.Join(overlay, "testfile") 133 _, err = os.Create(testfile) 134 assert.NilError(t, err) 135 136 checkLookup(t, "overlay", overlay, testdir) 137 checkLookup(t, "overlay", overlay, testfile) 138 }