github.com/myafeier/go-ethereum@v1.6.8-0.20170719123245-3e0dbe0eaa72/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 t.Skip("flaky test") 31 32 // start 3 node cluster 33 t.Log("starting 3 node cluster") 34 cluster := newTestCluster(t, 3) 35 defer cluster.Shutdown() 36 37 // create a tmp file 38 tmp, err := ioutil.TempFile("", "swarm-test") 39 assertNil(t, err) 40 defer tmp.Close() 41 defer os.Remove(tmp.Name()) 42 _, err = io.WriteString(tmp, "data") 43 assertNil(t, err) 44 45 // upload the file with 'swarm up' and expect a hash 46 t.Log("uploading file with 'swarm up'") 47 up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", tmp.Name()) 48 _, matches := up.ExpectRegexp(`[a-f\d]{64}`) 49 up.ExpectExit() 50 hash := matches[0] 51 t.Logf("file uploaded with hash %s", hash) 52 53 // get the file from the HTTP API of each node 54 for _, node := range cluster.Nodes { 55 t.Logf("getting file from %s", node.Name) 56 res, err := http.Get(node.URL + "/bzz:/" + hash) 57 assertNil(t, err) 58 assertHTTPResponse(t, res, http.StatusOK, "data") 59 } 60 } 61 62 func assertNil(t *testing.T, err error) { 63 if err != nil { 64 t.Fatal(err) 65 } 66 } 67 68 func assertHTTPResponse(t *testing.T, res *http.Response, expectedStatus int, expectedBody string) { 69 defer res.Body.Close() 70 if res.StatusCode != expectedStatus { 71 t.Fatalf("expected HTTP status %d, got %s", expectedStatus, res.Status) 72 } 73 data, err := ioutil.ReadAll(res.Body) 74 assertNil(t, err) 75 if string(data) != expectedBody { 76 t.Fatalf("expected HTTP body %q, got %q", expectedBody, data) 77 } 78 }