| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- // Copyright 2018 The go-ethereum Authors
- // This file is part of go-ethereum.
- //
- // go-ethereum is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // go-ethereum is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
- // +build linux freebsd
- package main
- import (
- "bytes"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "time"
- "github.com/ethereum/go-ethereum/cmd/utils"
- "github.com/ethereum/go-ethereum/log"
- )
- type testFile struct {
- filePath string
- content string
- }
- // TestCLISwarmFsDefaultIPCPath tests if the most basic fs command, i.e., list
- // can find and correctly connect to a running Swarm node on the default
- // IPCPath.
- func TestCLISwarmFsDefaultIPCPath(t *testing.T) {
- cluster := newTestCluster(t, 1)
- defer cluster.Shutdown()
- handlingNode := cluster.Nodes[0]
- list := runSwarm(t, []string{
- "--datadir", handlingNode.Dir,
- "fs",
- "list",
- }...)
- list.WaitExit()
- if list.Err != nil {
- t.Fatal(list.Err)
- }
- }
- // TestCLISwarmFs is a high-level test of swarmfs
- //
- // This test fails on travis for macOS as this executable exits with code 1
- // and without any log messages in the log:
- // /Library/Filesystems/osxfuse.fs/Contents/Resources/load_osxfuse.
- // This is the reason for this file not being built on darwin architecture.
- func TestCLISwarmFs(t *testing.T) {
- cluster := newTestCluster(t, 3)
- defer cluster.Shutdown()
- // create a tmp dir
- mountPoint, err := ioutil.TempDir("", "swarm-test")
- log.Debug("swarmfs cli test", "1st mount", mountPoint)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(mountPoint)
- handlingNode := cluster.Nodes[0]
- mhash := doUploadEmptyDir(t, handlingNode)
- log.Debug("swarmfs cli test: mounting first run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
- mount := runSwarm(t, []string{
- fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
- "fs",
- "mount",
- mhash,
- mountPoint,
- }...)
- mount.ExpectExit()
- filesToAssert := []*testFile{}
- dirPath, err := createDirInDir(mountPoint, "testSubDir")
- if err != nil {
- t.Fatal(err)
- }
- dirPath2, err := createDirInDir(dirPath, "AnotherTestSubDir")
- if err != nil {
- t.Fatal(err)
- }
- dummyContent := "somerandomtestcontentthatshouldbeasserted"
- dirs := []string{
- mountPoint,
- dirPath,
- dirPath2,
- }
- files := []string{"f1.tmp", "f2.tmp"}
- for _, d := range dirs {
- for _, entry := range files {
- tFile, err := createTestFileInPath(d, entry, dummyContent)
- if err != nil {
- t.Fatal(err)
- }
- filesToAssert = append(filesToAssert, tFile)
- }
- }
- if len(filesToAssert) != len(dirs)*len(files) {
- t.Fatalf("should have %d files to assert now, got %d", len(dirs)*len(files), len(filesToAssert))
- }
- hashRegexp := `[a-f\d]{64}`
- log.Debug("swarmfs cli test: unmounting first run...", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
- unmount := runSwarm(t, []string{
- fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
- "fs",
- "unmount",
- mountPoint,
- }...)
- _, matches := unmount.ExpectRegexp(hashRegexp)
- unmount.ExpectExit()
- hash := matches[0]
- if hash == mhash {
- t.Fatal("this should not be equal")
- }
- log.Debug("swarmfs cli test: asserting no files in mount point")
- //check that there's nothing in the mount folder
- filesInDir, err := ioutil.ReadDir(mountPoint)
- if err != nil {
- t.Fatalf("had an error reading the directory: %v", err)
- }
- if len(filesInDir) != 0 {
- t.Fatal("there shouldn't be anything here")
- }
- secondMountPoint, err := ioutil.TempDir("", "swarm-test")
- log.Debug("swarmfs cli test", "2nd mount point at", secondMountPoint)
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(secondMountPoint)
- log.Debug("swarmfs cli test: remounting at second mount point", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
- //remount, check files
- newMount := runSwarm(t, []string{
- fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
- "fs",
- "mount",
- hash, // the latest hash
- secondMountPoint,
- }...)
- newMount.ExpectExit()
- time.Sleep(1 * time.Second)
- filesInDir, err = ioutil.ReadDir(secondMountPoint)
- if err != nil {
- t.Fatal(err)
- }
- if len(filesInDir) == 0 {
- t.Fatal("there should be something here")
- }
- log.Debug("swarmfs cli test: traversing file tree to see it matches previous mount")
- for _, file := range filesToAssert {
- file.filePath = strings.Replace(file.filePath, mountPoint, secondMountPoint, -1)
- fileBytes, err := ioutil.ReadFile(file.filePath)
- if err != nil {
- t.Fatal(err)
- }
- if !bytes.Equal(fileBytes, bytes.NewBufferString(file.content).Bytes()) {
- t.Fatal("this should be equal")
- }
- }
- log.Debug("swarmfs cli test: unmounting second run", "ipc path", filepath.Join(handlingNode.Dir, handlingNode.IpcPath))
- unmountSec := runSwarm(t, []string{
- fmt.Sprintf("--%s", utils.IPCPathFlag.Name), filepath.Join(handlingNode.Dir, handlingNode.IpcPath),
- "fs",
- "unmount",
- secondMountPoint,
- }...)
- _, matches = unmountSec.ExpectRegexp(hashRegexp)
- unmountSec.ExpectExit()
- if matches[0] != hash {
- t.Fatal("these should be equal - no changes made")
- }
- }
- func doUploadEmptyDir(t *testing.T, node *testNode) string {
- // create a tmp dir
- tmpDir, err := ioutil.TempDir("", "swarm-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
- hashRegexp := `[a-f\d]{64}`
- flags := []string{
- "--bzzapi", node.URL,
- "--recursive",
- "up",
- tmpDir}
- log.Info("swarmfs cli test: uploading dir with 'swarm up'")
- up := runSwarm(t, flags...)
- _, matches := up.ExpectRegexp(hashRegexp)
- up.ExpectExit()
- hash := matches[0]
- log.Info("swarmfs cli test: dir uploaded", "hash", hash)
- return hash
- }
- func createDirInDir(createInDir string, dirToCreate string) (string, error) {
- fullpath := filepath.Join(createInDir, dirToCreate)
- err := os.MkdirAll(fullpath, 0777)
- if err != nil {
- return "", err
- }
- return fullpath, nil
- }
- func createTestFileInPath(dir, filename, content string) (*testFile, error) {
- tFile := &testFile{}
- filePath := filepath.Join(dir, filename)
- if file, err := os.Create(filePath); err == nil {
- tFile.content = content
- tFile.filePath = filePath
- _, err = io.WriteString(file, content)
- if err != nil {
- return nil, err
- }
- file.Close()
- }
- return tFile, nil
- }
|