run_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "context"
  19. "fmt"
  20. "os"
  21. "testing"
  22. "time"
  23. "github.com/docker/docker/pkg/reexec"
  24. "github.com/ethereum/go-ethereum/internal/cmdtest"
  25. "github.com/ethereum/go-ethereum/rpc"
  26. )
  27. type testgeth struct {
  28. *cmdtest.TestCmd
  29. // template variables for expect
  30. Datadir string
  31. Etherbase string
  32. }
  33. func init() {
  34. // Run the app if we've been exec'd as "geth-test" in runGeth.
  35. reexec.Register("geth-test", func() {
  36. if err := app.Run(os.Args); err != nil {
  37. fmt.Fprintln(os.Stderr, err)
  38. os.Exit(1)
  39. }
  40. os.Exit(0)
  41. })
  42. }
  43. func TestMain(m *testing.M) {
  44. // check if we have been reexec'd
  45. if reexec.Init() {
  46. return
  47. }
  48. os.Exit(m.Run())
  49. }
  50. // spawns geth with the given command line args. If the args don't set --datadir, the
  51. // child g gets a temporary data directory.
  52. func runGeth(t *testing.T, args ...string) *testgeth {
  53. tt := &testgeth{}
  54. tt.TestCmd = cmdtest.NewTestCmd(t, tt)
  55. for i, arg := range args {
  56. switch arg {
  57. case "--datadir":
  58. if i < len(args)-1 {
  59. tt.Datadir = args[i+1]
  60. }
  61. case "--miner.etherbase":
  62. if i < len(args)-1 {
  63. tt.Etherbase = args[i+1]
  64. }
  65. }
  66. }
  67. if tt.Datadir == "" {
  68. // The temporary datadir will be removed automatically if something fails below.
  69. tt.Datadir = t.TempDir()
  70. args = append([]string{"--datadir", tt.Datadir}, args...)
  71. }
  72. // Boot "geth". This actually runs the test binary but the TestMain
  73. // function will prevent any tests from running.
  74. tt.Run("geth-test", args...)
  75. return tt
  76. }
  77. // waitForEndpoint attempts to connect to an RPC endpoint until it succeeds.
  78. func waitForEndpoint(t *testing.T, endpoint string, timeout time.Duration) {
  79. probe := func() bool {
  80. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  81. defer cancel()
  82. c, err := rpc.DialContext(ctx, endpoint)
  83. if c != nil {
  84. _, err = c.SupportedModules()
  85. c.Close()
  86. }
  87. return err == nil
  88. }
  89. start := time.Now()
  90. for {
  91. if probe() {
  92. return
  93. }
  94. if time.Since(start) > timeout {
  95. t.Fatal("endpoint", endpoint, "did not open within", timeout)
  96. }
  97. time.Sleep(200 * time.Millisecond)
  98. }
  99. }