github.com/containerd/Containerd@v1.4.13/mount/mount_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 "reflect" 28 "testing" 29 30 "github.com/containerd/continuity/testutil" 31 ) 32 33 func TestLongestCommonPrefix(t *testing.T) { 34 tcases := []struct { 35 in []string 36 expected string 37 }{ 38 {[]string{}, ""}, 39 {[]string{"foo"}, "foo"}, 40 {[]string{"foo", "bar"}, ""}, 41 {[]string{"foo", "foo"}, "foo"}, 42 {[]string{"foo", "foobar"}, "foo"}, 43 {[]string{"foo", "", "foobar"}, ""}, 44 } 45 46 for i, tc := range tcases { 47 if got := longestCommonPrefix(tc.in); got != tc.expected { 48 t.Fatalf("[%d case] expected (%s), but got (%s)", i+1, tc.expected, got) 49 } 50 } 51 } 52 53 func TestCompactLowerdirOption(t *testing.T) { 54 tcases := []struct { 55 opts []string 56 commondir string 57 newopts []string 58 }{ 59 // no lowerdir or only one 60 { 61 []string{"workdir=a"}, 62 "", 63 []string{"workdir=a"}, 64 }, 65 { 66 []string{"workdir=a", "lowerdir=b"}, 67 "", 68 []string{"workdir=a", "lowerdir=b"}, 69 }, 70 71 // >= 2 lowerdir 72 { 73 []string{"lowerdir=/snapshots/1/fs:/snapshots/10/fs"}, 74 "/snapshots/", 75 []string{"lowerdir=1/fs:10/fs"}, 76 }, 77 { 78 []string{"lowerdir=/snapshots/1/fs:/snapshots/10/fs:/snapshots/2/fs"}, 79 "/snapshots/", 80 []string{"lowerdir=1/fs:10/fs:2/fs"}, 81 }, 82 83 // if common dir is / 84 { 85 []string{"lowerdir=/snapshots/1/fs:/other_snapshots/1/fs"}, 86 "", 87 []string{"lowerdir=/snapshots/1/fs:/other_snapshots/1/fs"}, 88 }, 89 } 90 91 for i, tc := range tcases { 92 dir, opts := compactLowerdirOption(tc.opts) 93 if dir != tc.commondir { 94 t.Fatalf("[%d case] expected common dir (%s), but got (%s)", i+1, tc.commondir, dir) 95 } 96 97 if !reflect.DeepEqual(opts, tc.newopts) { 98 t.Fatalf("[%d case] expected options (%v), but got (%v)", i+1, tc.newopts, opts) 99 } 100 } 101 } 102 103 func TestFUSEHelper(t *testing.T) { 104 testutil.RequiresRoot(t) 105 const fuseoverlayfsBinary = "fuse-overlayfs" 106 _, err := exec.LookPath(fuseoverlayfsBinary) 107 if err != nil { 108 t.Skip("fuse-overlayfs not installed") 109 } 110 td, err := ioutil.TempDir("", "fuse") 111 if err != nil { 112 t.Fatal(err) 113 } 114 defer func() { 115 if err := os.RemoveAll(td); err != nil { 116 t.Fatal(err) 117 } 118 }() 119 120 for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} { 121 if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil { 122 t.Fatal(err) 123 } 124 } 125 126 opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", filepath.Join(td, "lower2"), filepath.Join(td, "lower1"), filepath.Join(td, "upper"), filepath.Join(td, "work")) 127 m := Mount{ 128 Type: "fuse3." + fuseoverlayfsBinary, 129 Source: "overlay", 130 Options: []string{opts}, 131 } 132 dest := filepath.Join(td, "merged") 133 if err := m.Mount(dest); err != nil { 134 t.Fatal(err) 135 } 136 if err := UnmountAll(dest, 0); err != nil { 137 t.Fatal(err) 138 } 139 }