github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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 84 dummyContent := "somerandomtestcontentthatshouldbeasserted" 85 dirs := []string{ 86 mountPoint, 87 dirPath, 88 dirPath2, 89 } 90 files := []string{"f1.tmp", "f2.tmp"} 91 for _, d := range dirs { 92 for _, entry := range files { 93 tFile, err := createTestFileInPath(d, entry, dummyContent) 94 if err != nil { 95 t.Fatal(err) 96 } 97 filesToAssert = append(filesToAssert, tFile) 98 } 99 } 100 if len(filesToAssert) != len(dirs)*len(files) { 101 t.Fatalf("should have %d files to assert now, got %d", len(dirs)*len(files), len(filesToAssert)) 102 } 103 hashRegexp := `[a-f\d]{64}` 104 log.Debug("swarmfs cli test: unmounting first run...", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 105 106 unmount := runSwarm(t, []string{ 107 "fs", 108 "unmount", 109 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 110 mountPoint, 111 }...) 112 _, matches := unmount.ExpectRegexp(hashRegexp) 113 unmount.ExpectExit() 114 115 hash := matches[0] 116 if hash == mhash { 117 t.Fatal("this should not be equal") 118 } 119 log.Debug("swarmfs cli test: asserting no files in mount point") 120 121 //check that there's nothing in the mount folder 122 filesInDir, err := ioutil.ReadDir(mountPoint) 123 if err != nil { 124 t.Fatalf("had an error reading the directory: %v", err) 125 } 126 127 if len(filesInDir) != 0 { 128 t.Fatal("there shouldn't be anything here") 129 } 130 131 secondMountPoint, err := ioutil.TempDir("", "swarm-test") 132 log.Debug("swarmfs cli test", "2nd mount point at", secondMountPoint) 133 if err != nil { 134 t.Fatal(err) 135 } 136 defer os.RemoveAll(secondMountPoint) 137 138 log.Debug("swarmfs cli test: remounting at second mount point", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 139 140 //remount, check files 141 newMount := runSwarm(t, []string{ 142 "fs", 143 "mount", 144 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 145 hash, // the latest hash 146 secondMountPoint, 147 }...) 148 149 newMount.ExpectExit() 150 time.Sleep(1 * time.Second) 151 152 filesInDir, err = ioutil.ReadDir(secondMountPoint) 153 if err != nil { 154 t.Fatal(err) 155 } 156 157 if len(filesInDir) == 0 { 158 t.Fatal("there should be something here") 159 } 160 161 log.Debug("swarmfs cli test: traversing file tree to see it matches previous mount") 162 163 for _, file := range filesToAssert { 164 file.filePath = strings.Replace(file.filePath, mountPoint, secondMountPoint, -1) 165 fileBytes, err := ioutil.ReadFile(file.filePath) 166 167 if err != nil { 168 t.Fatal(err) 169 } 170 if !bytes.Equal(fileBytes, bytes.NewBufferString(file.content).Bytes()) { 171 t.Fatal("this should be equal") 172 } 173 } 174 175 log.Debug("swarmfs cli test: unmounting second run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 176 177 unmountSec := runSwarm(t, []string{ 178 "fs", 179 "unmount", 180 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 181 secondMountPoint, 182 }...) 183 184 _, matches = unmountSec.ExpectRegexp(hashRegexp) 185 unmountSec.ExpectExit() 186 187 if matches[0] != hash { 188 t.Fatal("these should be equal - no changes made") 189 } 190 } 191 192 func doUploadEmptyDir(t *testing.T, node *testNode) string { 193 // create a tmp dir 194 tmpDir, err := ioutil.TempDir("", "swarm-test") 195 if err != nil { 196 t.Fatal(err) 197 } 198 defer os.RemoveAll(tmpDir) 199 200 hashRegexp := `[a-f\d]{64}` 201 202 flags := []string{ 203 "--bzzapi", node.URL, 204 "--recursive", 205 "up", 206 tmpDir} 207 208 log.Info("swarmfs cli test: uploading dir with 'swarm up'") 209 up := runSwarm(t, flags...) 210 _, matches := up.ExpectRegexp(hashRegexp) 211 up.ExpectExit() 212 hash := matches[0] 213 log.Info("swarmfs cli test: dir uploaded", "hash", hash) 214 return hash 215 } 216 217 func createDirInDir(createInDir string, dirToCreate string) (string, error) { 218 fullpath := filepath.Join(createInDir, dirToCreate) 219 err := os.MkdirAll(fullpath, 0777) 220 if err != nil { 221 return "", err 222 } 223 return fullpath, nil 224 } 225 226 func createTestFileInPath(dir, filename, content string) (*testFile, error) { 227 tFile := &testFile{} 228 filePath := filepath.Join(dir, filename) 229 if file, err := os.Create(filePath); err == nil { 230 tFile.content = content 231 tFile.filePath = filePath 232 233 _, err = io.WriteString(file, content) 234 if err != nil { 235 return nil, err 236 } 237 file.Close() 238 } 239 240 return tFile, nil 241 }