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