upload_test.go 2.3 KB

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