github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/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 19:16:33</date> 10 //</624450070959362048> 11 12 13 // 14 15 package main 16 17 import ( 18 "bytes" 19 "fmt" 20 "io" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "strings" 25 "testing" 26 "time" 27 28 "github.com/ethereum/go-ethereum/cmd/utils" 29 "github.com/ethereum/go-ethereum/log" 30 ) 31 32 type testFile struct { 33 filePath string 34 content string 35 } 36 37 //如果是最基本的fs命令,即list,则测试cliswarmfsdefaultpcpath 38 //可以在默认情况下找到并正确连接到正在运行的群节点 39 //IPCPath。 40 func TestCLISwarmFsDefaultIPCPath(t *testing.T) { 41 cluster := newTestCluster(t, 1) 42 defer cluster.Shutdown() 43 44 handlingNode := cluster.Nodes[0] 45 list := runSwarm(t, []string{ 46 "--datadir", handlingNode.Dir, 47 "fs", 48 "list", 49 }...) 50 51 list.WaitExit() 52 if list.Err != nil { 53 t.Fatal(list.Err) 54 } 55 } 56 57 // 58 // 59 //此测试在Travis for MacOS上失败,因为此可执行文件退出时代码为1。 60 //日志中没有任何日志消息: 61 ///library/filesystems/osxfuse.fs/contents/resources/load_osxfuse。 62 //这就是这个文件不是建立在达尔文体系结构之上的原因。 63 func TestCLISwarmFs(t *testing.T) { 64 cluster := newTestCluster(t, 3) 65 defer cluster.Shutdown() 66 67 //创建tmp dir 68 mountPoint, err := ioutil.TempDir("", "swarm-test") 69 log.Debug("swarmfs cli test", "1st mount", mountPoint) 70 if err != nil { 71 t.Fatal(err) 72 } 73 defer os.RemoveAll(mountPoint) 74 75 handlingNode := cluster.Nodes[0] 76 mhash := doUploadEmptyDir(t, handlingNode) 77 log.Debug("swarmfs cli test: mounting first run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 78 79 mount := runSwarm(t, []string{ 80 fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 81 "fs", 82 "mount", 83 mhash, 84 mountPoint, 85 }...) 86 mount.ExpectExit() 87 88 filesToAssert := []*testFile{} 89 90 dirPath, err := createDirInDir(mountPoint, "testSubDir") 91 if err != nil { 92 t.Fatal(err) 93 } 94 dirPath2, err := createDirInDir(dirPath, "AnotherTestSubDir") 95 if err != nil { 96 t.Fatal(err) 97 } 98 99 dummyContent := "somerandomtestcontentthatshouldbeasserted" 100 dirs := []string{ 101 mountPoint, 102 dirPath, 103 dirPath2, 104 } 105 files := []string{"f1.tmp", "f2.tmp"} 106 for _, d := range dirs { 107 for _, entry := range files { 108 tFile, err := createTestFileInPath(d, entry, dummyContent) 109 if err != nil { 110 t.Fatal(err) 111 } 112 filesToAssert = append(filesToAssert, tFile) 113 } 114 } 115 if len(filesToAssert) != len(dirs)*len(files) { 116 t.Fatalf("should have %d files to assert now, got %d", len(dirs)*len(files), len(filesToAssert)) 117 } 118 hashRegexp := `[a-f\d]{64}` 119 log.Debug("swarmfs cli test: unmounting first run...", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 120 121 unmount := runSwarm(t, []string{ 122 fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 123 "fs", 124 "unmount", 125 mountPoint, 126 }...) 127 _, matches := unmount.ExpectRegexp(hashRegexp) 128 unmount.ExpectExit() 129 130 hash := matches[0] 131 if hash == mhash { 132 t.Fatal("this should not be equal") 133 } 134 log.Debug("swarmfs cli test: asserting no files in mount point") 135 136 // 137 filesInDir, err := ioutil.ReadDir(mountPoint) 138 if err != nil { 139 t.Fatalf("had an error reading the directory: %v", err) 140 } 141 142 if len(filesInDir) != 0 { 143 t.Fatal("there shouldn't be anything here") 144 } 145 146 secondMountPoint, err := ioutil.TempDir("", "swarm-test") 147 log.Debug("swarmfs cli test", "2nd mount point at", secondMountPoint) 148 if err != nil { 149 t.Fatal(err) 150 } 151 defer os.RemoveAll(secondMountPoint) 152 153 log.Debug("swarmfs cli test: remounting at second mount point", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 154 155 //重新安装,检查文件 156 newMount := runSwarm(t, []string{ 157 fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 158 "fs", 159 "mount", 160 hash, //最新散列 161 secondMountPoint, 162 }...) 163 164 newMount.ExpectExit() 165 time.Sleep(1 * time.Second) 166 167 filesInDir, err = ioutil.ReadDir(secondMountPoint) 168 if err != nil { 169 t.Fatal(err) 170 } 171 172 if len(filesInDir) == 0 { 173 t.Fatal("there should be something here") 174 } 175 176 log.Debug("swarmfs cli test: traversing file tree to see it matches previous mount") 177 178 for _, file := range filesToAssert { 179 file.filePath = strings.Replace(file.filePath, mountPoint, secondMountPoint, -1) 180 fileBytes, err := ioutil.ReadFile(file.filePath) 181 182 if err != nil { 183 t.Fatal(err) 184 } 185 if !bytes.Equal(fileBytes, bytes.NewBufferString(file.content).Bytes()) { 186 t.Fatal("this should be equal") 187 } 188 } 189 190 log.Debug("swarmfs cli test: unmounting second run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 191 192 unmountSec := runSwarm(t, []string{ 193 fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 194 "fs", 195 "unmount", 196 secondMountPoint, 197 }...) 198 199 _, matches = unmountSec.ExpectRegexp(hashRegexp) 200 unmountSec.ExpectExit() 201 202 if matches[0] != hash { 203 t.Fatal("these should be equal - no changes made") 204 } 205 } 206 207 func doUploadEmptyDir(t *testing.T, node *testNode) string { 208 //创建tmp dir 209 tmpDir, err := ioutil.TempDir("", "swarm-test") 210 if err != nil { 211 t.Fatal(err) 212 } 213 defer os.RemoveAll(tmpDir) 214 215 hashRegexp := `[a-f\d]{64}` 216 217 flags := []string{ 218 "--bzzapi", node.URL, 219 "--recursive", 220 "up", 221 tmpDir} 222 223 log.Info("swarmfs cli test: uploading dir with 'swarm up'") 224 up := runSwarm(t, flags...) 225 _, matches := up.ExpectRegexp(hashRegexp) 226 up.ExpectExit() 227 hash := matches[0] 228 log.Info("swarmfs cli test: dir uploaded", "hash", hash) 229 return hash 230 } 231 232 func createDirInDir(createInDir string, dirToCreate string) (string, error) { 233 fullpath := filepath.Join(createInDir, dirToCreate) 234 err := os.MkdirAll(fullpath, 0777) 235 if err != nil { 236 return "", err 237 } 238 return fullpath, nil 239 } 240 241 func createTestFileInPath(dir, filename, content string) (*testFile, error) { 242 tFile := &testFile{} 243 filePath := filepath.Join(dir, filename) 244 if file, err := os.Create(filePath); err == nil { 245 tFile.content = content 246 tFile.filePath = filePath 247 248 _, err = io.WriteString(file, content) 249 if err != nil { 250 return nil, err 251 } 252 file.Close() 253 } 254 255 return tFile, nil 256 } 257