github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/cmd/geth/run_test.go (about) 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 17 package main 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 "testing" 24 "time" 25 26 "github.com/docker/docker/pkg/reexec" 27 "github.com/ethereum/go-ethereum/internal/cmdtest" 28 "github.com/ethereum/go-ethereum/rpc" 29 ) 30 31 type testgeth struct { 32 *cmdtest.TestCmd 33 34 // template variables for expect 35 Datadir string 36 Etherbase string 37 } 38 39 func init() { 40 // Run the app if we've been exec'd as "geth-test" in runGeth. 41 reexec.Register("geth-test", func() { 42 if err := app.Run(os.Args); err != nil { 43 fmt.Fprintln(os.Stderr, err) 44 os.Exit(1) 45 } 46 os.Exit(0) 47 }) 48 } 49 50 func TestMain(m *testing.M) { 51 // check if we have been reexec'd 52 if reexec.Init() { 53 return 54 } 55 os.Exit(m.Run()) 56 } 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 // The temporary datadir will be removed automatically if something fails below. 77 tt.Datadir = t.TempDir() 78 args = append([]string{"--datadir", tt.Datadir}, args...) 79 } 80 81 // Boot "geth". This actually runs the test binary but the TestMain 82 // function will prevent any tests from running. 83 tt.Run("geth-test", args...) 84 85 return tt 86 } 87 88 // waitForEndpoint attempts to connect to an RPC endpoint until it succeeds. 89 func waitForEndpoint(t *testing.T, endpoint string, timeout time.Duration) { 90 probe := func() bool { 91 ctx, cancel := context.WithTimeout(context.Background(), timeout) 92 defer cancel() 93 c, err := rpc.DialContext(ctx, endpoint) 94 if c != nil { 95 _, err = c.SupportedModules() 96 c.Close() 97 } 98 return err == nil 99 } 100 101 start := time.Now() 102 for { 103 if probe() { 104 return 105 } 106 if time.Since(start) > timeout { 107 t.Fatal("endpoint", endpoint, "did not open within", timeout) 108 } 109 time.Sleep(200 * time.Millisecond) 110 } 111 }