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