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