github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/cmd/swarm/export_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:33</date>
    10  //</624450070720286720>
    11  
    12  
    13  package main
    14  
    15  import (
    16  	"bytes"
    17  	"crypto/md5"
    18  	"io"
    19  	"net/http"
    20  	"os"
    21  	"runtime"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/swarm"
    26  	"github.com/ethereum/go-ethereum/swarm/testutil"
    27  )
    28  
    29  //testcliswarmexportimport执行以下测试:
    30  //1。运行群节点
    31  //
    32  //三。运行本地数据存储的导出
    33  //
    34  //5。导入导出的数据存储
    35  //6。从第二个节点获取上载的随机文件
    36  func TestCLISwarmExportImport(t *testing.T) {
    37  	if runtime.GOOS == "windows" {
    38  		t.Skip()
    39  	}
    40  	cluster := newTestCluster(t, 1)
    41  
    42  //
    43  	content := testutil.RandomBytes(1, 1000000)
    44  	fileName := testutil.TempFileWithContent(t, string(content))
    45  	defer os.Remove(fileName)
    46  
    47  //用“swarm up”上传文件,并期望得到一个哈希值
    48  	up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", fileName)
    49  	_, matches := up.ExpectRegexp(`[a-f\d]{64}`)
    50  	up.ExpectExit()
    51  	hash := matches[0]
    52  
    53  	var info swarm.Info
    54  	if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	cluster.Stop()
    59  	defer cluster.Cleanup()
    60  
    61  //生成export.tar
    62  	exportCmd := runSwarm(t, "db", "export", info.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info.BzzKey, "0x"))
    63  	exportCmd.ExpectExit()
    64  
    65  //
    66  	cluster2 := newTestCluster(t, 1)
    67  
    68  	var info2 swarm.Info
    69  	if err := cluster2.Nodes[0].Client.Call(&info2, "bzz_info"); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  //
    74  	cluster2.Stop()
    75  	defer cluster2.Cleanup()
    76  
    77  //导入export.tar
    78  	importCmd := runSwarm(t, "db", "import", info2.Path+"/chunks", info.Path+"/export.tar", strings.TrimPrefix(info2.BzzKey, "0x"))
    79  	importCmd.ExpectExit()
    80  
    81  //旋转第二个群集备份
    82  	cluster2.StartExistingNodes(t, 1, strings.TrimPrefix(info2.BzzAccount, "0x"))
    83  
    84  //尝试获取导入的文件
    85  	res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	if res.StatusCode != 200 {
    91  		t.Fatalf("expected HTTP status %d, got %s", 200, res.Status)
    92  	}
    93  
    94  //
    95  	mustEqualFiles(t, bytes.NewReader(content), res.Body)
    96  }
    97  
    98  func mustEqualFiles(t *testing.T, up io.Reader, down io.Reader) {
    99  	h := md5.New()
   100  	upLen, err := io.Copy(h, up)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	upHash := h.Sum(nil)
   105  	h.Reset()
   106  	downLen, err := io.Copy(h, down)
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	downHash := h.Sum(nil)
   111  
   112  	if !bytes.Equal(upHash, downHash) || upLen != downLen {
   113  		t.Fatalf("downloaded imported file md5=%x (length %v) is not the same as the generated one mp5=%x (length %v)", downHash, downLen, upHash, upLen)
   114  	}
   115  }
   116