github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/swarm/fs_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:31</date> 10 //</624342605685067776> 11 12 13 // 14 15 package main 16 17 import ( 18 "bytes" 19 "io" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "strings" 24 "testing" 25 "time" 26 27 "github.com/ethereum/go-ethereum/log" 28 colorable "github.com/mattn/go-colorable" 29 ) 30 31 func init() { 32 log.PrintOrigins(true) 33 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 34 } 35 36 type testFile struct { 37 filePath string 38 content string 39 } 40 41 // 42 // 43 // 44 // 45 // 46 // 47 func TestCLISwarmFs(t *testing.T) { 48 cluster := newTestCluster(t, 3) 49 defer cluster.Shutdown() 50 51 // 52 mountPoint, err := ioutil.TempDir("", "swarm-test") 53 log.Debug("swarmfs cli test", "1st mount", mountPoint) 54 if err != nil { 55 t.Fatal(err) 56 } 57 defer os.RemoveAll(mountPoint) 58 59 handlingNode := cluster.Nodes[0] 60 mhash := doUploadEmptyDir(t, handlingNode) 61 log.Debug("swarmfs cli test: mounting first run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 62 63 mount := runSwarm(t, []string{ 64 "fs", 65 "mount", 66 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 67 mhash, 68 mountPoint, 69 }...) 70 mount.ExpectExit() 71 72 filesToAssert := []*testFile{} 73 74 dirPath, err := createDirInDir(mountPoint, "testSubDir") 75 if err != nil { 76 t.Fatal(err) 77 } 78 dirPath2, err := createDirInDir(dirPath, "AnotherTestSubDir") 79 80 dummyContent := "somerandomtestcontentthatshouldbeasserted" 81 dirs := []string{ 82 mountPoint, 83 dirPath, 84 dirPath2, 85 } 86 files := []string{"f1.tmp", "f2.tmp"} 87 for _, d := range dirs { 88 for _, entry := range files { 89 tFile, err := createTestFileInPath(d, entry, dummyContent) 90 if err != nil { 91 t.Fatal(err) 92 } 93 filesToAssert = append(filesToAssert, tFile) 94 } 95 } 96 if len(filesToAssert) != len(dirs)*len(files) { 97 t.Fatalf("should have %d files to assert now, got %d", len(dirs)*len(files), len(filesToAssert)) 98 } 99 hashRegexp := `[a-f\d]{64}` 100 log.Debug("swarmfs cli test: unmounting first run...", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 101 102 unmount := runSwarm(t, []string{ 103 "fs", 104 "unmount", 105 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 106 mountPoint, 107 }...) 108 _, matches := unmount.ExpectRegexp(hashRegexp) 109 unmount.ExpectExit() 110 111 hash := matches[0] 112 if hash == mhash { 113 t.Fatal("this should not be equal") 114 } 115 log.Debug("swarmfs cli test: asserting no files in mount point") 116 117 // 118 filesInDir, err := ioutil.ReadDir(mountPoint) 119 if err != nil { 120 t.Fatalf("had an error reading the directory: %v", err) 121 } 122 123 if len(filesInDir) != 0 { 124 t.Fatal("there shouldn't be anything here") 125 } 126 127 secondMountPoint, err := ioutil.TempDir("", "swarm-test") 128 log.Debug("swarmfs cli test", "2nd mount point at", secondMountPoint) 129 if err != nil { 130 t.Fatal(err) 131 } 132 defer os.RemoveAll(secondMountPoint) 133 134 log.Debug("swarmfs cli test: remounting at second mount point", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 135 136 // 137 newMount := runSwarm(t, []string{ 138 "fs", 139 "mount", 140 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 141 hash, //最新散列 142 secondMountPoint, 143 }...) 144 145 newMount.ExpectExit() 146 time.Sleep(1 * time.Second) 147 148 filesInDir, err = ioutil.ReadDir(secondMountPoint) 149 if err != nil { 150 t.Fatal(err) 151 } 152 153 if len(filesInDir) == 0 { 154 t.Fatal("there should be something here") 155 } 156 157 log.Debug("swarmfs cli test: traversing file tree to see it matches previous mount") 158 159 for _, file := range filesToAssert { 160 file.filePath = strings.Replace(file.filePath, mountPoint, secondMountPoint, -1) 161 fileBytes, err := ioutil.ReadFile(file.filePath) 162 163 if err != nil { 164 t.Fatal(err) 165 } 166 if !bytes.Equal(fileBytes, bytes.NewBufferString(file.content).Bytes()) { 167 t.Fatal("this should be equal") 168 } 169 } 170 171 log.Debug("swarmfs cli test: unmounting second run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 172 173 unmountSec := runSwarm(t, []string{ 174 "fs", 175 "unmount", 176 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 177 secondMountPoint, 178 }...) 179 180 _, matches = unmountSec.ExpectRegexp(hashRegexp) 181 unmountSec.ExpectExit() 182 183 if matches[0] != hash { 184 t.Fatal("these should be equal - no changes made") 185 } 186 } 187 188 func doUploadEmptyDir(t *testing.T, node *testNode) string { 189 //创建tmp dir 190 tmpDir, err := ioutil.TempDir("", "swarm-test") 191 if err != nil { 192 t.Fatal(err) 193 } 194 defer os.RemoveAll(tmpDir) 195 196 hashRegexp := `[a-f\d]{64}` 197 198 flags := []string{ 199 "--bzzapi", node.URL, 200 "--recursive", 201 "up", 202 tmpDir} 203 204 log.Info("swarmfs cli test: uploading dir with 'swarm up'") 205 up := runSwarm(t, flags...) 206 _, matches := up.ExpectRegexp(hashRegexp) 207 up.ExpectExit() 208 hash := matches[0] 209 log.Info("swarmfs cli test: dir uploaded", "hash", hash) 210 return hash 211 } 212 213 func createDirInDir(createInDir string, dirToCreate string) (string, error) { 214 fullpath := filepath.Join(createInDir, dirToCreate) 215 err := os.MkdirAll(fullpath, 0777) 216 if err != nil { 217 return "", err 218 } 219 return fullpath, nil 220 } 221 222 func createTestFileInPath(dir, filename, content string) (*testFile, error) { 223 tFile := &testFile{} 224 filePath := filepath.Join(dir, filename) 225 if file, err := os.Create(filePath); err == nil { 226 tFile.content = content 227 tFile.filePath = filePath 228 229 _, err = io.WriteString(file, content) 230 if err != nil { 231 return nil, err 232 } 233 file.Close() 234 } 235 236 return tFile, nil 237 } 238