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