github.com/codingfuture/orig-energi3@v0.8.4/cmd/swarm/fs_test.go (about)

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