github.com/humaniq/go-ethereum@v1.6.8-0.20171225131628-061223a13848/cmd/swarm/upload_test.go (about) 1 // Copyright 2016 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 package main 18 19 import ( 20 "io" 21 "io/ioutil" 22 "net/http" 23 "os" 24 "testing" 25 ) 26 27 // TestCLISwarmUp tests that running 'swarm up' makes the resulting file 28 // available from all nodes via the HTTP API 29 func TestCLISwarmUp(t *testing.T) { 30 // start 3 node cluster 31 t.Log("starting 3 node cluster") 32 cluster := newTestCluster(t, 3) 33 defer cluster.Shutdown() 34 35 // create a tmp file 36 tmp, err := ioutil.TempFile("", "swarm-test") 37 assertNil(t, err) 38 defer tmp.Close() 39 defer os.Remove(tmp.Name()) 40 _, err = io.WriteString(tmp, "data") 41 assertNil(t, err) 42 43 // upload the file with 'swarm up' and expect a hash 44 t.Log("uploading file with 'swarm up'") 45 up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", tmp.Name()) 46 _, matches := up.ExpectRegexp(`[a-f\d]{64}`) 47 up.ExpectExit() 48 hash := matches[0] 49 t.Logf("file uploaded with hash %s", hash) 50 51 // get the file from the HTTP API of each node 52 for _, node := range cluster.Nodes { 53 t.Logf("getting file from %s", node.Name) 54 res, err := http.Get(node.URL + "/bzz:/" + hash) 55 assertNil(t, err) 56 assertHTTPResponse(t, res, http.StatusOK, "data") 57 } 58 } 59 60 func assertNil(t *testing.T, err error) { 61 if err != nil { 62 t.Fatal(err) 63 } 64 } 65 66 func assertHTTPResponse(t *testing.T, res *http.Response, expectedStatus int, expectedBody string) { 67 defer res.Body.Close() 68 if res.StatusCode != expectedStatus { 69 t.Fatalf("expected HTTP status %d, got %s", expectedStatus, res.Status) 70 } 71 data, err := ioutil.ReadAll(res.Body) 72 assertNil(t, err) 73 if string(data) != expectedBody { 74 t.Fatalf("expected HTTP body %q, got %q", expectedBody, data) 75 } 76 }