fs_test.go 6.3 KB

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