fs_test.go 6.2 KB

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