fs_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. package main
  17. import (
  18. "bytes"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "path/filepath"
  23. "strings"
  24. "testing"
  25. "time"
  26. "github.com/ethereum/go-ethereum/log"
  27. colorable "github.com/mattn/go-colorable"
  28. )
  29. func init() {
  30. log.PrintOrigins(true)
  31. log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
  32. }
  33. type testFile struct {
  34. filePath string
  35. content string
  36. }
  37. // TestCLISwarmFs is a high-level test of swarmfs
  38. func TestCLISwarmFs(t *testing.T) {
  39. cluster := newTestCluster(t, 3)
  40. defer cluster.Shutdown()
  41. // create a tmp dir
  42. mountPoint, err := ioutil.TempDir("", "swarm-test")
  43. log.Debug("swarmfs cli test", "1st mount", mountPoint)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. defer os.RemoveAll(mountPoint)
  48. handlingNode := cluster.Nodes[0]
  49. mhash := doUploadEmptyDir(t, handlingNode)
  50. log.Debug("swarmfs cli test: mounting first run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
  51. mount := runSwarm(t, []string{
  52. "fs",
  53. "mount",
  54. "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
  55. mhash,
  56. mountPoint,
  57. }...)
  58. mount.ExpectExit()
  59. filesToAssert := []*testFile{}
  60. dirPath, err := createDirInDir(mountPoint, "testSubDir")
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. dirPath2, err := createDirInDir(dirPath, "AnotherTestSubDir")
  65. dummyContent := "somerandomtestcontentthatshouldbeasserted"
  66. dirs := []string{
  67. mountPoint,
  68. dirPath,
  69. dirPath2,
  70. }
  71. files := []string{"f1.tmp", "f2.tmp"}
  72. for _, d := range dirs {
  73. for _, entry := range files {
  74. tFile, err := createTestFileInPath(d, entry, dummyContent)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. filesToAssert = append(filesToAssert, tFile)
  79. }
  80. }
  81. if len(filesToAssert) != len(dirs)*len(files) {
  82. t.Fatalf("should have %d files to assert now, got %d", len(dirs)*len(files), len(filesToAssert))
  83. }
  84. hashRegexp := `[a-f\d]{64}`
  85. log.Debug("swarmfs cli test: unmounting first run...", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
  86. unmount := runSwarm(t, []string{
  87. "fs",
  88. "unmount",
  89. "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
  90. mountPoint,
  91. }...)
  92. _, matches := unmount.ExpectRegexp(hashRegexp)
  93. unmount.ExpectExit()
  94. hash := matches[0]
  95. if hash == mhash {
  96. t.Fatal("this should not be equal")
  97. }
  98. log.Debug("swarmfs cli test: asserting no files in mount point")
  99. //check that there's nothing in the mount folder
  100. filesInDir, err := ioutil.ReadDir(mountPoint)
  101. if err != nil {
  102. t.Fatalf("had an error reading the directory: %v", err)
  103. }
  104. if len(filesInDir) != 0 {
  105. t.Fatal("there shouldn't be anything here")
  106. }
  107. secondMountPoint, err := ioutil.TempDir("", "swarm-test")
  108. log.Debug("swarmfs cli test", "2nd mount point at", secondMountPoint)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. defer os.RemoveAll(secondMountPoint)
  113. log.Debug("swarmfs cli test: remounting at second mount point", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
  114. //remount, check files
  115. newMount := runSwarm(t, []string{
  116. "fs",
  117. "mount",
  118. "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
  119. hash, // the latest hash
  120. secondMountPoint,
  121. }...)
  122. newMount.ExpectExit()
  123. time.Sleep(1 * time.Second)
  124. filesInDir, err = ioutil.ReadDir(secondMountPoint)
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. if len(filesInDir) == 0 {
  129. t.Fatal("there should be something here")
  130. }
  131. log.Debug("swarmfs cli test: traversing file tree to see it matches previous mount")
  132. for _, file := range filesToAssert {
  133. file.filePath = strings.Replace(file.filePath, mountPoint, secondMountPoint, -1)
  134. fileBytes, err := ioutil.ReadFile(file.filePath)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. if !bytes.Equal(fileBytes, bytes.NewBufferString(file.content).Bytes()) {
  139. t.Fatal("this should be equal")
  140. }
  141. }
  142. log.Debug("swarmfs cli test: unmounting second run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
  143. unmountSec := runSwarm(t, []string{
  144. "fs",
  145. "unmount",
  146. "--ipcpath", filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
  147. secondMountPoint,
  148. }...)
  149. _, matches = unmountSec.ExpectRegexp(hashRegexp)
  150. unmountSec.ExpectExit()
  151. if matches[0] != hash {
  152. t.Fatal("these should be equal - no changes made")
  153. }
  154. }
  155. func doUploadEmptyDir(t *testing.T, node *testNode) string {
  156. // create a tmp dir
  157. tmpDir, err := ioutil.TempDir("", "swarm-test")
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. defer os.RemoveAll(tmpDir)
  162. hashRegexp := `[a-f\d]{64}`
  163. flags := []string{
  164. "--bzzapi", node.URL,
  165. "--recursive",
  166. "up",
  167. tmpDir}
  168. log.Info("swarmfs cli test: uploading dir with 'swarm up'")
  169. up := runSwarm(t, flags...)
  170. _, matches := up.ExpectRegexp(hashRegexp)
  171. up.ExpectExit()
  172. hash := matches[0]
  173. log.Info("swarmfs cli test: dir uploaded", "hash", hash)
  174. return hash
  175. }
  176. func createDirInDir(createInDir string, dirToCreate string) (string, error) {
  177. fullpath := filepath.Join(createInDir, dirToCreate)
  178. err := os.MkdirAll(fullpath, 0777)
  179. if err != nil {
  180. return "", err
  181. }
  182. return fullpath, nil
  183. }
  184. func createTestFileInPath(dir, filename, content string) (*testFile, error) {
  185. tFile := &testFile{}
  186. filePath := filepath.Join(dir, filename)
  187. if file, err := os.Create(filePath); err == nil {
  188. tFile.content = content
  189. tFile.filePath = filePath
  190. _, err = io.WriteString(file, content)
  191. if err != nil {
  192. return nil, err
  193. }
  194. file.Close()
  195. }
  196. return tFile, nil
  197. }