github.com/demonoid81/containerd@v1.3.4/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 "reflect" 23 "testing" 24 ) 25 26 func TestLongestCommonPrefix(t *testing.T) { 27 tcases := []struct { 28 in []string 29 expected string 30 }{ 31 {[]string{}, ""}, 32 {[]string{"foo"}, "foo"}, 33 {[]string{"foo", "bar"}, ""}, 34 {[]string{"foo", "foo"}, "foo"}, 35 {[]string{"foo", "foobar"}, "foo"}, 36 {[]string{"foo", "", "foobar"}, ""}, 37 } 38 39 for i, tc := range tcases { 40 if got := longestCommonPrefix(tc.in); got != tc.expected { 41 t.Fatalf("[%d case] expected (%s), but got (%s)", i+1, tc.expected, got) 42 } 43 } 44 } 45 46 func TestCompactLowerdirOption(t *testing.T) { 47 tcases := []struct { 48 opts []string 49 commondir string 50 newopts []string 51 }{ 52 // no lowerdir or only one 53 { 54 []string{"workdir=a"}, 55 "", 56 []string{"workdir=a"}, 57 }, 58 { 59 []string{"workdir=a", "lowerdir=b"}, 60 "", 61 []string{"workdir=a", "lowerdir=b"}, 62 }, 63 64 // >= 2 lowerdir 65 { 66 []string{"lowerdir=/snapshots/1/fs:/snapshots/10/fs"}, 67 "/snapshots/", 68 []string{"lowerdir=1/fs:10/fs"}, 69 }, 70 { 71 []string{"lowerdir=/snapshots/1/fs:/snapshots/10/fs:/snapshots/2/fs"}, 72 "/snapshots/", 73 []string{"lowerdir=1/fs:10/fs:2/fs"}, 74 }, 75 76 // if common dir is / 77 { 78 []string{"lowerdir=/snapshots/1/fs:/other_snapshots/1/fs"}, 79 "", 80 []string{"lowerdir=/snapshots/1/fs:/other_snapshots/1/fs"}, 81 }, 82 } 83 84 for i, tc := range tcases { 85 dir, opts := compactLowerdirOption(tc.opts) 86 if dir != tc.commondir { 87 t.Fatalf("[%d case] expected common dir (%s), but got (%s)", i+1, tc.commondir, dir) 88 } 89 90 if !reflect.DeepEqual(opts, tc.newopts) { 91 t.Fatalf("[%d case] expected options (%v), but got (%v)", i+1, tc.newopts, opts) 92 } 93 } 94 }