run_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.StartNewNodes(t, size)
  88. if size == 1 {
  89. return cluster
  90. }
  91. // connect the nodes together
  92. for _, node := range cluster.Nodes {
  93. if err := node.Client.Call(nil, "admin_addPeer", cluster.Nodes[0].Enode); err != nil {
  94. t.Fatal(err)
  95. }
  96. }
  97. // wait until all nodes have the correct number of peers
  98. outer:
  99. for _, node := range cluster.Nodes {
  100. var peers []*p2p.PeerInfo
  101. for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(50 * time.Millisecond) {
  102. if err := node.Client.Call(&peers, "admin_peers"); err != nil {
  103. t.Fatal(err)
  104. }
  105. if len(peers) == len(cluster.Nodes)-1 {
  106. continue outer
  107. }
  108. }
  109. t.Fatalf("%s only has %d / %d peers", node.Name, len(peers), len(cluster.Nodes)-1)
  110. }
  111. return cluster
  112. }
  113. func (c *testCluster) Shutdown() {
  114. for _, node := range c.Nodes {
  115. node.Shutdown()
  116. }
  117. os.RemoveAll(c.TmpDir)
  118. }
  119. func (c *testCluster) Stop() {
  120. for _, node := range c.Nodes {
  121. node.Shutdown()
  122. }
  123. }
  124. func (c *testCluster) StartNewNodes(t *testing.T, size int) {
  125. c.Nodes = make([]*testNode, 0, size)
  126. for i := 0; i < size; i++ {
  127. dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", i))
  128. if err := os.Mkdir(dir, 0700); err != nil {
  129. t.Fatal(err)
  130. }
  131. node := newTestNode(t, dir)
  132. node.Name = fmt.Sprintf("swarm%02d", i)
  133. c.Nodes = append(c.Nodes, node)
  134. }
  135. }
  136. func (c *testCluster) StartExistingNodes(t *testing.T, size int, bzzaccount string) {
  137. c.Nodes = make([]*testNode, 0, size)
  138. for i := 0; i < size; i++ {
  139. dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", i))
  140. node := existingTestNode(t, dir, bzzaccount)
  141. node.Name = fmt.Sprintf("swarm%02d", i)
  142. c.Nodes = append(c.Nodes, node)
  143. }
  144. }
  145. func (c *testCluster) Cleanup() {
  146. os.RemoveAll(c.TmpDir)
  147. }
  148. type testNode struct {
  149. Name string
  150. Addr string
  151. URL string
  152. Enode string
  153. Dir string
  154. IpcPath string
  155. Client *rpc.Client
  156. Cmd *cmdtest.TestCmd
  157. }
  158. const testPassphrase = "swarm-test-passphrase"
  159. func getTestAccount(t *testing.T, dir string) (conf *node.Config, account accounts.Account) {
  160. // create key
  161. conf = &node.Config{
  162. DataDir: dir,
  163. IPCPath: "bzzd.ipc",
  164. NoUSB: true,
  165. }
  166. n, err := node.New(conf)
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. account, err = n.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore).NewAccount(testPassphrase)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. // use a unique IPCPath when running tests on Windows
  175. if runtime.GOOS == "windows" {
  176. conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", account.Address.String())
  177. }
  178. return conf, account
  179. }
  180. func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {
  181. conf, _ := getTestAccount(t, dir)
  182. node := &testNode{Dir: dir}
  183. // use a unique IPCPath when running tests on Windows
  184. if runtime.GOOS == "windows" {
  185. conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", bzzaccount)
  186. }
  187. // assign ports
  188. httpPort, err := assignTCPPort()
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. p2pPort, err := assignTCPPort()
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. // start the node
  197. node.Cmd = runSwarm(t,
  198. "--port", p2pPort,
  199. "--nodiscover",
  200. "--datadir", dir,
  201. "--ipcpath", conf.IPCPath,
  202. "--ens-api", "",
  203. "--bzzaccount", bzzaccount,
  204. "--bzznetworkid", "321",
  205. "--bzzport", httpPort,
  206. "--verbosity", "6",
  207. )
  208. node.Cmd.InputLine(testPassphrase)
  209. defer func() {
  210. if t.Failed() {
  211. node.Shutdown()
  212. }
  213. }()
  214. // wait for the node to start
  215. for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
  216. node.Client, err = rpc.Dial(conf.IPCEndpoint())
  217. if err == nil {
  218. break
  219. }
  220. }
  221. if node.Client == nil {
  222. t.Fatal(err)
  223. }
  224. // load info
  225. var info swarm.Info
  226. if err := node.Client.Call(&info, "bzz_info"); err != nil {
  227. t.Fatal(err)
  228. }
  229. node.Addr = net.JoinHostPort("127.0.0.1", info.Port)
  230. node.URL = "http://" + node.Addr
  231. var nodeInfo p2p.NodeInfo
  232. if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
  233. t.Fatal(err)
  234. }
  235. node.Enode = fmt.Sprintf("enode://%s@127.0.0.1:%s", nodeInfo.ID, p2pPort)
  236. return node
  237. }
  238. func newTestNode(t *testing.T, dir string) *testNode {
  239. conf, account := getTestAccount(t, dir)
  240. node := &testNode{Dir: dir}
  241. // assign ports
  242. httpPort, err := assignTCPPort()
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. p2pPort, err := assignTCPPort()
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. // start the node
  251. node.Cmd = runSwarm(t,
  252. "--port", p2pPort,
  253. "--nodiscover",
  254. "--datadir", dir,
  255. "--ipcpath", conf.IPCPath,
  256. "--ens-api", "",
  257. "--bzzaccount", account.Address.String(),
  258. "--bzznetworkid", "321",
  259. "--bzzport", httpPort,
  260. "--verbosity", "6",
  261. )
  262. node.Cmd.InputLine(testPassphrase)
  263. defer func() {
  264. if t.Failed() {
  265. node.Shutdown()
  266. }
  267. }()
  268. // wait for the node to start
  269. for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
  270. node.Client, err = rpc.Dial(conf.IPCEndpoint())
  271. if err == nil {
  272. break
  273. }
  274. }
  275. if node.Client == nil {
  276. t.Fatal(err)
  277. }
  278. // load info
  279. var info swarm.Info
  280. if err := node.Client.Call(&info, "bzz_info"); err != nil {
  281. t.Fatal(err)
  282. }
  283. node.Addr = net.JoinHostPort("127.0.0.1", info.Port)
  284. node.URL = "http://" + node.Addr
  285. var nodeInfo p2p.NodeInfo
  286. if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
  287. t.Fatal(err)
  288. }
  289. node.Enode = fmt.Sprintf("enode://%s@127.0.0.1:%s", nodeInfo.ID, p2pPort)
  290. node.IpcPath = conf.IPCPath
  291. return node
  292. }
  293. func (n *testNode) Shutdown() {
  294. if n.Cmd != nil {
  295. n.Cmd.Kill()
  296. }
  297. }
  298. func assignTCPPort() (string, error) {
  299. l, err := net.Listen("tcp", "127.0.0.1:0")
  300. if err != nil {
  301. return "", err
  302. }
  303. l.Close()
  304. _, port, err := net.SplitHostPort(l.Addr().String())
  305. if err != nil {
  306. return "", err
  307. }
  308. return port, nil
  309. }