github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/fs/dutils_test.go (about) 1 // Package fs provides mountpath and FQN abstractions and methods to resolve/map stored content 2 /* 3 * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package fs_test 6 7 import ( 8 "os" 9 "testing" 10 11 "github.com/NVIDIA/aistore/cmn" 12 "github.com/NVIDIA/aistore/cmn/cos" 13 "github.com/NVIDIA/aistore/cmn/debug" 14 "github.com/NVIDIA/aistore/fs" 15 "github.com/NVIDIA/aistore/tools/tassert" 16 ) 17 18 // test file for ios/dutils_linux.go 19 // placed here because it requires fs to set up the testing environment 20 21 func TestMountpathSearchValid(t *testing.T) { 22 fs.TestNew(nil) 23 24 mpath := "/tmp/abc" 25 createDirs(mpath) 26 defer removeDirs(mpath) 27 28 oldMPs := setAvailableMountPaths(t, mpath) 29 mi, err := fs.Path2Mpath("/tmp/abc/test") 30 tassert.Errorf(t, err == nil && mi.Path == mpath, "Actual: [%s]. Expected: [%s]", mi.Path, mpath) 31 setAvailableMountPaths(t, oldMPs...) 32 } 33 34 func TestMountpathSearchInvalid(t *testing.T) { 35 fs.TestNew(nil) 36 37 mpath := "/tmp/abc" 38 createDirs(mpath) 39 defer removeDirs(mpath) 40 41 oldMPs := setAvailableMountPaths(t, mpath) 42 mi, err := fs.Path2Mpath("xabc") 43 tassert.Errorf(t, mi == nil, "Expected a nil mountpath info for fqn %q (%v)", "xabc", err) 44 setAvailableMountPaths(t, oldMPs...) 45 } 46 47 func TestMountpathSearchWhenNoAvailable(t *testing.T) { 48 fs.TestNew(nil) 49 oldMPs := setAvailableMountPaths(t, "") 50 mi, err := fs.Path2Mpath("xabc") 51 tassert.Errorf(t, mi == nil, "Expected a nil mountpath info for fqn %q (%v)", "xabc", err) 52 setAvailableMountPaths(t, oldMPs...) 53 } 54 55 func TestSearchWithASuffixToAnotherValue(t *testing.T) { 56 config := cmn.GCO.BeginUpdate() 57 config.TestFSP.Count = 2 58 cmn.GCO.CommitUpdate(config) 59 60 fs.TestNew(nil) 61 dirs := []string{"/tmp/x/z/abc", "/tmp/x/zabc", "/tmp/x/y/abc", "/tmp/x/yabc"} 62 createDirs(dirs...) 63 defer removeDirs(dirs...) 64 65 oldMPs := setAvailableMountPaths(t, "/tmp/x/y", "/tmp/x/z") 66 67 mi, err := fs.Path2Mpath("z/abc") 68 tassert.Errorf(t, err != nil && mi == nil, "Expected a nil mountpath info for fqn %q (%v)", "z/abc", err) 69 70 mi, err = fs.Path2Mpath("/tmp/../tmp/x/z/abc") 71 tassert.Errorf(t, err == nil && mi.Path == "/tmp/x/z", "Actual: [%s]. Expected: [%s] (%v)", 72 mi, "/tmp/x/z", err) 73 74 mi, err = fs.Path2Mpath("/tmp/../tmp/x/y/abc") 75 tassert.Errorf(t, err == nil && mi.Path == "/tmp/x/y", "Actual: [%s]. Expected: [%s] (%v)", 76 mi, "/tmp/x/y", err) 77 setAvailableMountPaths(t, oldMPs...) 78 } 79 80 func TestSimilarCases(t *testing.T) { 81 fs.TestNew(nil) 82 dirs := []string{"/tmp/abc", "/tmp/abx"} 83 createDirs(dirs...) 84 defer removeDirs(dirs...) 85 86 oldMPs := setAvailableMountPaths(t, "/tmp/abc") 87 88 mi, err := fs.Path2Mpath("/tmp/abc/q") 89 mpath := mi.Path 90 tassert.Errorf(t, err == nil && mpath == "/tmp/abc", "Actual: [%s]. Expected: [%s] (%v)", mpath, "/tmp/abc", err) 91 92 mi, err = fs.Path2Mpath("/abx") 93 tassert.Errorf(t, mi == nil, "Expected a nil mountpath info for fqn %q (%v)", "/abx", err) 94 setAvailableMountPaths(t, oldMPs...) 95 } 96 97 func TestSimilarCasesWithRoot(t *testing.T) { 98 fs.TestNew(nil) 99 mpath := "/tmp/abc" 100 createDirs(mpath) 101 defer removeDirs(mpath) 102 103 oldMPs := setAvailableMountPaths(t) 104 // root is an invalid mountpath 105 _, err := fs.Add("/", "daeID") 106 tassert.Errorf(t, err != nil, "Expected failure to add \"/\" mountpath") 107 setAvailableMountPaths(t, oldMPs...) 108 } 109 110 func setAvailableMountPaths(t *testing.T, paths ...string) []string { 111 avail := fs.GetAvail() 112 oldPaths := make([]string, 0, len(avail)) 113 for _, mi := range avail { 114 oldPaths = append(oldPaths, mi.Path) 115 } 116 117 for _, mi := range avail { 118 _, err := fs.Remove(mi.Path) 119 tassert.Errorf(t, err == nil, "%s (%v)", mi, err) 120 debug.AssertNoErr(err) 121 } 122 123 for _, path := range paths { 124 if path == "" { 125 continue 126 } 127 _, err := fs.Add(path, "daeID") 128 if err != nil { 129 tassert.Errorf(t, err == nil, "%s (%v)", path, err) 130 } 131 _ = err 132 } 133 134 return oldPaths 135 } 136 137 func createDirs(dirs ...string) { 138 for _, dir := range dirs { 139 err := cos.CreateDir(dir) 140 debug.AssertNoErr(err) 141 } 142 } 143 144 func removeDirs(dirs ...string) { 145 for _, dir := range dirs { 146 os.RemoveAll(dir) 147 } 148 }