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