diff --git a/cmd/swarm/export_test.go b/cmd/swarm/export_test.go index b60dbb575f..7a0ab4858d 100644 --- a/cmd/swarm/export_test.go +++ b/cmd/swarm/export_test.go @@ -1,4 +1,4 @@ -// Copyright 2017 The go-ethereum Authors +// 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 @@ -18,75 +18,47 @@ package main import ( "crypto/rand" - "fmt" "io/ioutil" "net/http" "os" "testing" - "github.com/davecgh/go-spew/spew" - "github.com/ethereum/go-ethereum/log" "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) - defer cluster.Cleanup() - // create a tmp file - tmp, err := ioutil.TempFile("", "swarm-test") - if err != nil { - 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) + // generate random 10mb file + f, cleanup := generateRandomFile(t, 10000000) + defer cleanup() // 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", tmp.Name()) + up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", f.Name()) _, matches := up.ExpectRegexp(`[a-f\d]{64}`) up.ExpectExit() hash := matches[0] - log.Info("file uploaded", "hash", hash) var info swarm.Info if err := cluster.Nodes[0].Client.Call(&info, "bzz_info"); err != nil { 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() defer cluster.Cleanup() - file, err := ioutil.TempFile("", "swarm-test") - if err != nil { - t.Fatal(err) - } - 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() + // generate an export.tar + exportCmd := runSwarm(t, "db", "export", info.Path+"/chunks", info.Path+"/export.tar", info.BzzKey[2:]) + exportCmd.ExpectExit() + // start second cluster cluster2 := newTestCluster(t, 1) var info2 swarm.Info @@ -94,15 +66,18 @@ func TestCLISwarmExport(t *testing.T) { t.Fatal(err) } + // stop second cluster, so that we close LevelDB cluster2.Stop() defer cluster2.Cleanup() - spew.Dump(info2.BzzKey[2:]) - up3 := runSwarm(t, "db", "import", info2.Path+"/chunks", file.Name(), info2.BzzKey[2:]) - up3.ExpectExit() + // import the export.tar + importCmd := runSwarm(t, "db", "import", info2.Path+"/chunks", info.Path+"/export.tar", info2.BzzKey[2:]) + importCmd.ExpectExit() + // spin second cluster back up cluster2.StartExistingNodes(t, 1, info2.BzzAccount[2:]) + // try to fetch imported file res, err := http.Get(cluster2.Nodes[0].URL + "/bzz:/" + hash) if err != nil { t.Fatal(err) @@ -111,4 +86,30 @@ func TestCLISwarmExport(t *testing.T) { if res.StatusCode != 200 { 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 } diff --git a/cmd/swarm/run_test.go b/cmd/swarm/run_test.go index 26f1260b30..b54be26052 100644 --- a/cmd/swarm/run_test.go +++ b/cmd/swarm/run_test.go @@ -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 { - conf, _ := getTestAccount(t, dir) node := &testNode{Dir: dir}