fs_test.go 6.7 KB

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