fs_test.go 6.0 KB

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