run_test.go 3.0 KB

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