run_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. "fmt"
  19. "io/ioutil"
  20. "net"
  21. "os"
  22. "path/filepath"
  23. "runtime"
  24. "testing"
  25. "time"
  26. "github.com/docker/docker/pkg/reexec"
  27. "github.com/ethereum/go-ethereum/accounts"
  28. "github.com/ethereum/go-ethereum/accounts/keystore"
  29. "github.com/ethereum/go-ethereum/internal/cmdtest"
  30. "github.com/ethereum/go-ethereum/node"
  31. "github.com/ethereum/go-ethereum/p2p"
  32. "github.com/ethereum/go-ethereum/rpc"
  33. "github.com/ethereum/go-ethereum/swarm"
  34. )
  35. func init() {
  36. // Run the app if we've been exec'd as "swarm-test" in runSwarm.
  37. reexec.Register("swarm-test", func() {
  38. if err := app.Run(os.Args); err != nil {
  39. fmt.Fprintln(os.Stderr, err)
  40. os.Exit(1)
  41. }
  42. os.Exit(0)
  43. })
  44. }
  45. func TestMain(m *testing.M) {
  46. // check if we have been reexec'd
  47. if reexec.Init() {
  48. return
  49. }
  50. os.Exit(m.Run())
  51. }
  52. func runSwarm(t *testing.T, args ...string) *cmdtest.TestCmd {
  53. tt := cmdtest.NewTestCmd(t, nil)
  54. // Boot "swarm". This actually runs the test binary but the TestMain
  55. // function will prevent any tests from running.
  56. tt.Run("swarm-test", args...)
  57. return tt
  58. }
  59. type testCluster struct {
  60. Nodes []*testNode
  61. TmpDir string
  62. }
  63. // newTestCluster starts a test swarm cluster of the given size.
  64. //
  65. // A temporary directory is created and each node gets a data directory inside
  66. // it.
  67. //
  68. // Each node listens on 127.0.0.1 with random ports for both the HTTP and p2p
  69. // ports (assigned by first listening on 127.0.0.1:0 and then passing the ports
  70. // as flags).
  71. //
  72. // When starting more than one node, they are connected together using the
  73. // admin SetPeer RPC method.
  74. func newTestCluster(t *testing.T, size int) *testCluster {
  75. cluster := &testCluster{}
  76. defer func() {
  77. if t.Failed() {
  78. cluster.Shutdown()
  79. }
  80. }()
  81. tmpdir, err := ioutil.TempDir("", "swarm-test")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. cluster.TmpDir = tmpdir
  86. // start the nodes
  87. cluster.Nodes = make([]*testNode, 0, size)
  88. for i := 0; i < size; i++ {
  89. dir := filepath.Join(cluster.TmpDir, fmt.Sprintf("swarm%02d", i))
  90. if err := os.Mkdir(dir, 0700); err != nil {
  91. t.Fatal(err)
  92. }
  93. node := newTestNode(t, dir)
  94. node.Name = fmt.Sprintf("swarm%02d", i)
  95. cluster.Nodes = append(cluster.Nodes, node)
  96. }
  97. if size == 1 {
  98. return cluster
  99. }
  100. // connect the nodes together
  101. for _, node := range cluster.Nodes {
  102. if err := node.Client.Call(nil, "admin_addPeer", cluster.Nodes[0].Enode); err != nil {
  103. t.Fatal(err)
  104. }
  105. }
  106. // wait until all nodes have the correct number of peers
  107. outer:
  108. for _, node := range cluster.Nodes {
  109. var peers []*p2p.PeerInfo
  110. for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(50 * time.Millisecond) {
  111. if err := node.Client.Call(&peers, "admin_peers"); err != nil {
  112. t.Fatal(err)
  113. }
  114. if len(peers) == len(cluster.Nodes)-1 {
  115. continue outer
  116. }
  117. }
  118. t.Fatalf("%s only has %d / %d peers", node.Name, len(peers), len(cluster.Nodes)-1)
  119. }
  120. return cluster
  121. }
  122. func (c *testCluster) Shutdown() {
  123. for _, node := range c.Nodes {
  124. node.Shutdown()
  125. }
  126. os.RemoveAll(c.TmpDir)
  127. }
  128. type testNode struct {
  129. Name string
  130. Addr string
  131. URL string
  132. Enode string
  133. Dir string
  134. Client *rpc.Client
  135. Cmd *cmdtest.TestCmd
  136. }
  137. const testPassphrase = "swarm-test-passphrase"
  138. func getTestAccount(t *testing.T, dir string) (conf *node.Config, account accounts.Account) {
  139. // create key
  140. conf = &node.Config{
  141. DataDir: dir,
  142. IPCPath: "bzzd.ipc",
  143. NoUSB: true,
  144. }
  145. n, err := node.New(conf)
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. account, err = n.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore).NewAccount(testPassphrase)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. // use a unique IPCPath when running tests on Windows
  154. if runtime.GOOS == "windows" {
  155. conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", account.Address.String())
  156. }
  157. return conf, account
  158. }
  159. func newTestNode(t *testing.T, dir string) *testNode {
  160. conf, account := getTestAccount(t, dir)
  161. node := &testNode{Dir: dir}
  162. // assign ports
  163. httpPort, err := assignTCPPort()
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. p2pPort, err := assignTCPPort()
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. // start the node
  172. node.Cmd = runSwarm(t,
  173. "--port", p2pPort,
  174. "--nodiscover",
  175. "--datadir", dir,
  176. "--ipcpath", conf.IPCPath,
  177. "--ens-api", "",
  178. "--bzzaccount", account.Address.String(),
  179. "--bzznetworkid", "321",
  180. "--bzzport", httpPort,
  181. "--verbosity", "6",
  182. )
  183. node.Cmd.InputLine(testPassphrase)
  184. defer func() {
  185. if t.Failed() {
  186. node.Shutdown()
  187. }
  188. }()
  189. // wait for the node to start
  190. for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
  191. node.Client, err = rpc.Dial(conf.IPCEndpoint())
  192. if err == nil {
  193. break
  194. }
  195. }
  196. if node.Client == nil {
  197. t.Fatal(err)
  198. }
  199. // load info
  200. var info swarm.Info
  201. if err := node.Client.Call(&info, "bzz_info"); err != nil {
  202. t.Fatal(err)
  203. }
  204. node.Addr = net.JoinHostPort("127.0.0.1", info.Port)
  205. node.URL = "http://" + node.Addr
  206. var nodeInfo p2p.NodeInfo
  207. if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
  208. t.Fatal(err)
  209. }
  210. node.Enode = fmt.Sprintf("enode://%s@127.0.0.1:%s", nodeInfo.ID, p2pPort)
  211. return node
  212. }
  213. func (n *testNode) Shutdown() {
  214. if n.Cmd != nil {
  215. n.Cmd.Kill()
  216. }
  217. }
  218. func assignTCPPort() (string, error) {
  219. l, err := net.Listen("tcp", "127.0.0.1:0")
  220. if err != nil {
  221. return "", err
  222. }
  223. l.Close()
  224. _, port, err := net.SplitHostPort(l.Addr().String())
  225. if err != nil {
  226. return "", err
  227. }
  228. return port, nil
  229. }