upload_test.go 2.2 KB

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