cmd/swarm: cleanup

This commit is contained in:
Anton Evangelatov 2018-03-29 21:05:30 +02:00
parent 765bc18cf8
commit 84759594dc
2 changed files with 49 additions and 49 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2017 The go-ethereum Authors // Copyright 2018 The go-ethereum Authors
// This file is part of go-ethereum. // This file is part of go-ethereum.
// //
// go-ethereum is free software: you can redistribute it and/or modify // go-ethereum is free software: you can redistribute it and/or modify
@ -18,75 +18,47 @@ package main
import ( import (
"crypto/rand" "crypto/rand"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"testing" "testing"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/swarm" "github.com/ethereum/go-ethereum/swarm"
) )
func TestCLISwarmExport(t *testing.T) { // TestCLISwarmExportImport perform the following test:
// 1. runs swarm node
// 2. uploads a random file
// 3. runs an export of the local datastore
// 4. runs a second swarm node
// 5. imports the exported datastore
// 6. fetches the uploaded random file from the second node
func TestCLISwarmExportImport(t *testing.T) {
cluster := newTestCluster(t, 1) cluster := newTestCluster(t, 1)
defer cluster.Cleanup()
// create a tmp file // generate random 10mb file
tmp, err := ioutil.TempFile("", "swarm-test") f, cleanup := generateRandomFile(t, 10000000)
if err != nil { defer cleanup()
t.Fatal(err)
}
defer tmp.Close()
defer os.Remove(tmp.Name())
// write 10mb random data to file
buf := make([]byte, 10000000)
_, err = rand.Read(buf)
if err != nil {
panic(err)
}
ioutil.WriteFile(tmp.Name(), buf, 0755)
// upload the file with 'swarm up' and expect a hash // upload the file with 'swarm up' and expect a hash
log.Info("uploading file with 'swarm up'") up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", f.Name())
up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", tmp.Name())
_, matches := up.ExpectRegexp(`[a-f\d]{64}`) _, matches := up.ExpectRegexp(`[a-f\d]{64}`)
up.ExpectExit() up.ExpectExit()
hash := matches[0] hash := matches[0]
log.Info("file uploaded", "hash", hash)
var info swarm.Info var info swarm.Info
if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil { if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil {
t.Fatal(err) t.Fatal(err)
} }
spew.Dump(info)
spew.Dump(cluster.TmpDir)
spew.Dump(cluster.Nodes[0].Name)
spew.Dump(cluster.Nodes[0].Addr)
spew.Dump(cluster.Nodes[0].URL)
spew.Dump(cluster.Nodes[0].Enode)
spew.Dump(cluster.Nodes[0].Dir)
cluster.Stop() cluster.Stop()
defer cluster.Cleanup() defer cluster.Cleanup()
file, err := ioutil.TempFile("", "swarm-test") // generate an export.tar
if err != nil { exportCmd := runSwarm(t, "db", "export", info.Path+"/chunks", info.Path+"/export.tar", info.BzzKey[2:])
t.Fatal(err) exportCmd.ExpectExit()
}
defer file.Close()
defer os.Remove(file.Name())
fmt.Println("==================================")
spew.Dump(info.BzzKey[2:])
up2 := runSwarm(t, "db", "export", info.Path+"/chunks", file.Name(), info.BzzKey[2:])
up2.ExpectExit()
// start second cluster
cluster2 := newTestCluster(t, 1) cluster2 := newTestCluster(t, 1)
var info2 swarm.Info var info2 swarm.Info
@ -94,15 +66,18 @@ func TestCLISwarmExport(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// stop second cluster, so that we close LevelDB
cluster2.Stop() cluster2.Stop()
defer cluster2.Cleanup() defer cluster2.Cleanup()
spew.Dump(info2.BzzKey[2:]) // import the export.tar
up3 := runSwarm(t, "db", "import", info2.Path+"/chunks", file.Name(), info2.BzzKey[2:]) importCmd := runSwarm(t, "db", "import", info2.Path+"/chunks", info.Path+"/export.tar", info2.BzzKey[2:])
up3.ExpectExit() importCmd.ExpectExit()
// spin second cluster back up
cluster2.StartExistingNodes(t, 1, info2.BzzAccount[2:]) cluster2.StartExistingNodes(t, 1, info2.BzzAccount[2:])
// try to fetch imported file
res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash) res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -111,4 +86,30 @@ func TestCLISwarmExport(t *testing.T) {
if res.StatusCode != 200 { if res.StatusCode != 200 {
t.Fatalf("expected HTTP status %d, got %s", 200, res.Status) t.Fatalf("expected HTTP status %d, got %s", 200, res.Status)
} }
//TODO: compare res with generated random file
}
func generateRandomFile(t *testing.T, size int) (f *os.File, teardown func()) {
// create a tmp file
tmp, err := ioutil.TempFile("", "swarm-test")
if err != nil {
t.Fatal(err)
}
// callback for tmp file cleanup
teardown = func() {
tmp.Close()
os.Remove(tmp.Name())
}
// write 10mb random data to file
buf := make([]byte, 10000000)
_, err = rand.Read(buf)
if err != nil {
t.Fatal(err)
}
ioutil.WriteFile(tmp.Name(), buf, 0755)
return tmp, teardown
} }

View file

@ -208,7 +208,6 @@ func getTestAccount(t *testing.T, dir string) (conf *node.Config, account accoun
} }
func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode { func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {
conf, _ := getTestAccount(t, dir) conf, _ := getTestAccount(t, dir)
node := &testNode{Dir: dir} node := &testNode{Dir: dir}