github.com/aaa256/atlantis@v0.0.0-20210707112435-42ee889287a2/cmd/swarm/fs_test.go (about) 1 // Copyright 2018 The go-athereum Authors 2 // This file is part of go-athereum. 3 // 4 // go-athereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-athereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-athereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "bytes" 21 "io" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 "strings" 26 "testing" 27 "time" 28 29 "github.com/athereum/go-athereum/log" 30 colorable "github.com/mattn/go-colorable" 31 ) 32 33 func init() { 34 log.PrintOrigins(true) 35 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 36 } 37 38 type testFile struct { 39 filePath string 40 content string 41 } 42 43 // TestCLISwarmFs is a high-level test of swarmfs 44 func TestCLISwarmFs(t *testing.T) { 45 cluster := newTestCluster(t, 3) 46 defer cluster.Shutdown() 47 48 // create a tmp dir 49 mountPoint, err := ioutil.TempDir("", "swarm-test") 50 log.Debug("swarmfs cli test", "1st mount", mountPoint) 51 if err != nil { 52 t.Fatal(err) 53 } 54 defer os.RemoveAll(mountPoint) 55 56 handlingNode := cluster.Nodes[0] 57 mhash := doUploadEmptyDir(t, handlingNode) 58 log.Debug("swarmfs cli test: mounting first run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 59 60 mount := runSwarm(t, []string{ 61 "fs", 62 "mount", 63 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 64 mhash, 65 mountPoint, 66 }...) 67 mount.ExpectExit() 68 69 filesToAssert := []*testFile{} 70 71 dirPath, err := createDirInDir(mountPoint, "testSubDir") 72 if err != nil { 73 t.Fatal(err) 74 } 75 dirPath2, err := createDirInDir(dirPath, "AnotherTestSubDir") 76 77 dummyContent := "somerandomtestcontentthatshouldbeasserted" 78 dirs := []string{ 79 mountPoint, 80 dirPath, 81 dirPath2, 82 } 83 files := []string{"f1.tmp", "f2.tmp"} 84 for _, d := range dirs { 85 for _, entry := range files { 86 tFile, err := createTestFileInPath(d, entry, dummyContent) 87 if err != nil { 88 t.Fatal(err) 89 } 90 filesToAssert = append(filesToAssert, tFile) 91 } 92 } 93 if len(filesToAssert) != len(dirs)*len(files) { 94 t.Fatalf("should have %d files to assert now, got %d", len(dirs)*len(files), len(filesToAssert)) 95 } 96 hashRegexp := `[a-f\d]{64}` 97 log.Debug("swarmfs cli test: unmounting first run...", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 98 99 unmount := runSwarm(t, []string{ 100 "fs", 101 "unmount", 102 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 103 mountPoint, 104 }...) 105 _, matches := unmount.ExpectRegexp(hashRegexp) 106 unmount.ExpectExit() 107 108 hash := matches[0] 109 if hash == mhash { 110 t.Fatal("this should not be equal") 111 } 112 log.Debug("swarmfs cli test: asserting no files in mount point") 113 114 //check that there's nothing in the mount folder 115 filesInDir, err := ioutil.ReadDir(mountPoint) 116 if err != nil { 117 t.Fatalf("had an error reading the directory: %v", err) 118 } 119 120 if len(filesInDir) != 0 { 121 t.Fatal("there shouldn't be anything here") 122 } 123 124 secondMountPoint, err := ioutil.TempDir("", "swarm-test") 125 log.Debug("swarmfs cli test", "2nd mount point at", secondMountPoint) 126 if err != nil { 127 t.Fatal(err) 128 } 129 defer os.RemoveAll(secondMountPoint) 130 131 log.Debug("swarmfs cli test: remounting at second mount point", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 132 133 //remount, check files 134 newMount := runSwarm(t, []string{ 135 "fs", 136 "mount", 137 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 138 hash, // the latest hash 139 secondMountPoint, 140 }...) 141 142 newMount.ExpectExit() 143 time.Sleep(1 * time.Second) 144 145 filesInDir, err = ioutil.ReadDir(secondMountPoint) 146 if err != nil { 147 t.Fatal(err) 148 } 149 150 if len(filesInDir) == 0 { 151 t.Fatal("there should be somathing here") 152 } 153 154 log.Debug("swarmfs cli test: traversing file tree to see it matches previous mount") 155 156 for _, file := range filesToAssert { 157 file.filePath = strings.Replace(file.filePath, mountPoint, secondMountPoint, -1) 158 fileBytes, err := ioutil.ReadFile(file.filePath) 159 160 if err != nil { 161 t.Fatal(err) 162 } 163 if !bytes.Equal(fileBytes, bytes.NewBufferString(file.content).Bytes()) { 164 t.Fatal("this should be equal") 165 } 166 } 167 168 log.Debug("swarmfs cli test: unmounting second run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 169 170 unmountSec := runSwarm(t, []string{ 171 "fs", 172 "unmount", 173 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 174 secondMountPoint, 175 }...) 176 177 _, matches = unmountSec.ExpectRegexp(hashRegexp) 178 unmountSec.ExpectExit() 179 180 if matches[0] != hash { 181 t.Fatal("these should be equal - no changes made") 182 } 183 } 184 185 func doUploadEmptyDir(t *testing.T, node *testNode) string { 186 // create a tmp dir 187 tmpDir, err := ioutil.TempDir("", "swarm-test") 188 if err != nil { 189 t.Fatal(err) 190 } 191 defer os.RemoveAll(tmpDir) 192 193 hashRegexp := `[a-f\d]{64}` 194 195 flags := []string{ 196 "--bzzapi", node.URL, 197 "--recursive", 198 "up", 199 tmpDir} 200 201 log.Info("swarmfs cli test: uploading dir with 'swarm up'") 202 up := runSwarm(t, flags...) 203 _, matches := up.ExpectRegexp(hashRegexp) 204 up.ExpectExit() 205 hash := matches[0] 206 log.Info("swarmfs cli test: dir uploaded", "hash", hash) 207 return hash 208 } 209 210 func createDirInDir(createInDir string, dirToCreate string) (string, error) { 211 fullpath := filepath.Join(createInDir, dirToCreate) 212 err := os.MkdirAll(fullpath, 0777) 213 if err != nil { 214 return "", err 215 } 216 return fullpath, nil 217 } 218 219 func createTestFileInPath(dir, filename, content string) (*testFile, error) { 220 tFile := &testFile{} 221 filePath := filepath.Join(dir, filename) 222 if file, err := os.Create(filePath); err == nil { 223 tFile.content = content 224 tFile.filePath = filePath 225 226 _, err = io.WriteString(file, content) 227 if err != nil { 228 return nil, err 229 } 230 file.Close() 231 } 232 233 return tFile, nil 234 }