mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
cmd/swarm: get rid of assertNil and only once used method
This commit is contained in:
parent
34daf0559d
commit
c59b61824e
1 changed files with 31 additions and 26 deletions
|
|
@ -22,55 +22,60 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestCLISwarmUp tests that running 'swarm up' makes the resulting file
|
// TestCLISwarmUp tests that running 'swarm up' makes the resulting file
|
||||||
// available from all nodes via the HTTP API
|
// available from all nodes via the HTTP API
|
||||||
func TestCLISwarmUp(t *testing.T) {
|
func TestCLISwarmUp(t *testing.T) {
|
||||||
// start 3 node cluster
|
log.Info("starting 3 node cluster")
|
||||||
t.Log("starting 3 node cluster")
|
|
||||||
cluster := newTestCluster(t, 3)
|
cluster := newTestCluster(t, 3)
|
||||||
defer cluster.Shutdown()
|
defer cluster.Shutdown()
|
||||||
|
|
||||||
// create a tmp file
|
// create a tmp file
|
||||||
tmp, err := ioutil.TempFile("", "swarm-test")
|
tmp, err := ioutil.TempFile("", "swarm-test")
|
||||||
assertNil(t, err)
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
defer tmp.Close()
|
defer tmp.Close()
|
||||||
defer os.Remove(tmp.Name())
|
defer os.Remove(tmp.Name())
|
||||||
_, err = io.WriteString(tmp, "data")
|
|
||||||
assertNil(t, err)
|
// write data to file
|
||||||
|
data := "randomdata"
|
||||||
|
_, err = io.WriteString(tmp, data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// upload the file with 'swarm up' and expect a hash
|
// upload the file with 'swarm up' and expect a hash
|
||||||
t.Log("uploading file with 'swarm up'")
|
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", 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]
|
||||||
t.Logf("file uploaded with hash %s", hash)
|
log.Info("file uploaded", "hash", hash)
|
||||||
|
|
||||||
// get the file from the HTTP API of each node
|
// get the file from the HTTP API of each node
|
||||||
for _, node := range cluster.Nodes {
|
for _, node := range cluster.Nodes {
|
||||||
t.Logf("getting file from %s", node.Name)
|
log.Info("getting file from node", "node", node.Name)
|
||||||
res, err := http.Get(node.URL + "/bzz:/" + hash)
|
|
||||||
assertNil(t, err)
|
|
||||||
assertHTTPResponse(t, res, http.StatusOK, "data")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertNil(t *testing.T, err error) {
|
res, err := http.Get(node.URL + "/bzz:/" + hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
t.Fatalf("expected HTTP status %d, got %s", 200, res.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertHTTPResponse(t *testing.T, res *http.Response, expectedStatus int, expectedBody string) {
|
reply, err := ioutil.ReadAll(res.Body)
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
if res.StatusCode != expectedStatus {
|
if err != nil {
|
||||||
t.Fatalf("expected HTTP status %d, got %s", expectedStatus, res.Status)
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(reply) != data {
|
||||||
|
t.Fatalf("expected HTTP body %q, got %q", data, reply)
|
||||||
}
|
}
|
||||||
data, err := ioutil.ReadAll(res.Body)
|
|
||||||
assertNil(t, err)
|
|
||||||
if string(data) != expectedBody {
|
|
||||||
t.Fatalf("expected HTTP body %q, got %q", expectedBody, data)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue