github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/swarm/fs_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 // 26 27 package main 28 29 import ( 30 "bytes" 31 "io" 32 "io/ioutil" 33 "os" 34 "path/filepath" 35 "strings" 36 "testing" 37 "time" 38 39 "github.com/ethereum/go-ethereum/log" 40 colorable "github.com/mattn/go-colorable" 41 ) 42 43 func init() { 44 log.PrintOrigins(true) 45 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) 46 } 47 48 type testFile struct { 49 filePath string 50 content string 51 } 52 53 // 54 // 55 // 56 // 57 // 58 // 59 func TestCLISwarmFs(t *testing.T) { 60 cluster := newTestCluster(t, 3) 61 defer cluster.Shutdown() 62 63 // 64 mountPoint, err := ioutil.TempDir("", "swarm-test") 65 log.Debug("swarmfs cli test", "1st mount", mountPoint) 66 if err != nil { 67 t.Fatal(err) 68 } 69 defer os.RemoveAll(mountPoint) 70 71 handlingNode := cluster.Nodes[0] 72 mhash := doUploadEmptyDir(t, handlingNode) 73 log.Debug("swarmfs cli test: mounting first run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 74 75 mount := runSwarm(t, []string{ 76 "fs", 77 "mount", 78 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 79 mhash, 80 mountPoint, 81 }...) 82 mount.ExpectExit() 83 84 filesToAssert := []*testFile{} 85 86 dirPath, err := createDirInDir(mountPoint, "testSubDir") 87 if err != nil { 88 t.Fatal(err) 89 } 90 dirPath2, err := createDirInDir(dirPath, "AnotherTestSubDir") 91 92 dummyContent := "somerandomtestcontentthatshouldbeasserted" 93 dirs := []string{ 94 mountPoint, 95 dirPath, 96 dirPath2, 97 } 98 files := []string{"f1.tmp", "f2.tmp"} 99 for _, d := range dirs { 100 for _, entry := range files { 101 tFile, err := createTestFileInPath(d, entry, dummyContent) 102 if err != nil { 103 t.Fatal(err) 104 } 105 filesToAssert = append(filesToAssert, tFile) 106 } 107 } 108 if len(filesToAssert) != len(dirs)*len(files) { 109 t.Fatalf("should have %d files to assert now, got %d", len(dirs)*len(files), len(filesToAssert)) 110 } 111 hashRegexp := `[a-f\d]{64}` 112 log.Debug("swarmfs cli test: unmounting first run...", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 113 114 unmount := runSwarm(t, []string{ 115 "fs", 116 "unmount", 117 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 118 mountPoint, 119 }...) 120 _, matches := unmount.ExpectRegexp(hashRegexp) 121 unmount.ExpectExit() 122 123 hash := matches[0] 124 if hash == mhash { 125 t.Fatal("this should not be equal") 126 } 127 log.Debug("swarmfs cli test: asserting no files in mount point") 128 129 // 130 filesInDir, err := ioutil.ReadDir(mountPoint) 131 if err != nil { 132 t.Fatalf("had an error reading the directory: %v", err) 133 } 134 135 if len(filesInDir) != 0 { 136 t.Fatal("there shouldn't be anything here") 137 } 138 139 secondMountPoint, err := ioutil.TempDir("", "swarm-test") 140 log.Debug("swarmfs cli test", "2nd mount point at", secondMountPoint) 141 if err != nil { 142 t.Fatal(err) 143 } 144 defer os.RemoveAll(secondMountPoint) 145 146 log.Debug("swarmfs cli test: remounting at second mount point", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 147 148 // 149 newMount := runSwarm(t, []string{ 150 "fs", 151 "mount", 152 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 153 hash, //最新散列 154 secondMountPoint, 155 }...) 156 157 newMount.ExpectExit() 158 time.Sleep(1 * time.Second) 159 160 filesInDir, err = ioutil.ReadDir(secondMountPoint) 161 if err != nil { 162 t.Fatal(err) 163 } 164 165 if len(filesInDir) == 0 { 166 t.Fatal("there should be something here") 167 } 168 169 log.Debug("swarmfs cli test: traversing file tree to see it matches previous mount") 170 171 for _, file := range filesToAssert { 172 file.filePath = strings.Replace(file.filePath, mountPoint, secondMountPoint, -1) 173 fileBytes, err := ioutil.ReadFile(file.filePath) 174 175 if err != nil { 176 t.Fatal(err) 177 } 178 if !bytes.Equal(fileBytes, bytes.NewBufferString(file.content).Bytes()) { 179 t.Fatal("this should be equal") 180 } 181 } 182 183 log.Debug("swarmfs cli test: unmounting second run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath)) 184 185 unmountSec := runSwarm(t, []string{ 186 "fs", 187 "unmount", 188 "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath), 189 secondMountPoint, 190 }...) 191 192 _, matches = unmountSec.ExpectRegexp(hashRegexp) 193 unmountSec.ExpectExit() 194 195 if matches[0] != hash { 196 t.Fatal("these should be equal - no changes made") 197 } 198 } 199 200 func doUploadEmptyDir(t *testing.T, node *testNode) string { 201 //创建tmp dir 202 tmpDir, err := ioutil.TempDir("", "swarm-test") 203 if err != nil { 204 t.Fatal(err) 205 } 206 defer os.RemoveAll(tmpDir) 207 208 hashRegexp := `[a-f\d]{64}` 209 210 flags := []string{ 211 "--bzzapi", node.URL, 212 "--recursive", 213 "up", 214 tmpDir} 215 216 log.Info("swarmfs cli test: uploading dir with 'swarm up'") 217 up := runSwarm(t, flags...) 218 _, matches := up.ExpectRegexp(hashRegexp) 219 up.ExpectExit() 220 hash := matches[0] 221 log.Info("swarmfs cli test: dir uploaded", "hash", hash) 222 return hash 223 } 224 225 func createDirInDir(createInDir string, dirToCreate string) (string, error) { 226 fullpath := filepath.Join(createInDir, dirToCreate) 227 err := os.MkdirAll(fullpath, 0777) 228 if err != nil { 229 return "", err 230 } 231 return fullpath, nil 232 } 233 234 func createTestFileInPath(dir, filename, content string) (*testFile, error) { 235 tFile := &testFile{} 236 filePath := filepath.Join(dir, filename) 237 if file, err := os.Create(filePath); err == nil { 238 tFile.content = content 239 tFile.filePath = filePath 240 241 _, err = io.WriteString(file, content) 242 if err != nil { 243 return nil, err 244 } 245 file.Close() 246 } 247 248 return tFile, nil 249 }