github.com/klaytn/klaytn@v1.10.2/cmd/utils/nodecmd/run_test.go (about) 1 // Modifications Copyright 2019 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of go-ethereum. 4 // 5 // go-ethereum is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // go-ethereum is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from cmd/geth/run_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package nodecmd 22 23 import ( 24 "fmt" 25 "io/ioutil" 26 "os" 27 "runtime" 28 "sort" 29 "testing" 30 31 "github.com/docker/docker/pkg/reexec" 32 "github.com/klaytn/klaytn/api/debug" 33 "github.com/klaytn/klaytn/cmd/utils" 34 "github.com/klaytn/klaytn/console" 35 metricutils "github.com/klaytn/klaytn/metrics/utils" 36 "github.com/klaytn/klaytn/node" 37 "gopkg.in/urfave/cli.v1" 38 ) 39 40 func tmpdir(t *testing.T) string { 41 dir, err := ioutil.TempDir("", "klay-test") 42 if err != nil { 43 t.Fatal(err) 44 } 45 return dir 46 } 47 48 type testklay struct { 49 *utils.TestCmd 50 51 // template variables for expect 52 Datadir string 53 Rewardbase string 54 } 55 56 var ( 57 // The app that holds all commands and flags. 58 app = utils.NewApp(GetGitCommit(), "the Klaytn command line interface") 59 60 // flags that configure the node 61 nodeFlags = CommonNodeFlags 62 63 rpcFlags = CommonRPCFlags 64 ) 65 66 func init() { 67 // Initialize the CLI app and start Klay 68 app.Action = RunKlaytnNode 69 app.HideVersion = true // we have a command to print the version 70 app.Copyright = "Copyright 2018-2019 The klaytn Authors" 71 app.Commands = []cli.Command{ 72 // See chaincmd.go: 73 InitCommand, 74 75 // See accountcmd.go 76 AccountCommand, 77 78 // See consolecmd.go: 79 GetConsoleCommand(nodeFlags, rpcFlags), 80 AttachCommand, 81 82 // See versioncmd.go: 83 VersionCommand, 84 85 // See dumpconfigcmd.go: 86 GetDumpConfigCommand(nodeFlags, rpcFlags), 87 } 88 sort.Sort(cli.CommandsByName(app.Commands)) 89 90 app.Flags = allNodeFlags() 91 92 app.Before = func(ctx *cli.Context) error { 93 runtime.GOMAXPROCS(runtime.NumCPU()) 94 logDir := (&node.Config{DataDir: utils.MakeDataDir(ctx)}).ResolvePath("logs") 95 debug.CreateLogDir(logDir) 96 if err := debug.Setup(ctx); err != nil { 97 return err 98 } 99 metricutils.StartMetricCollectionAndExport(ctx) 100 setupNetwork(ctx) 101 return nil 102 } 103 104 app.After = func(ctx *cli.Context) error { 105 debug.Exit() 106 console.Stdin.Close() // Resets terminal mode. 107 return nil 108 } 109 110 // Run the app if we've been exec'd as "klay-test" in runKlay. 111 reexec.Register("klay-test", func() { 112 if err := app.Run(os.Args); err != nil { 113 fmt.Fprintln(os.Stderr, err) 114 os.Exit(1) 115 } 116 os.Exit(0) 117 }) 118 reexec.Register("klay-test-flag", func() { 119 app.Action = RunTestKlaytnNode 120 if err := app.Run(os.Args); err != nil { 121 fmt.Fprintln(os.Stderr, err) 122 os.Exit(1) 123 } 124 os.Exit(0) 125 }) 126 } 127 128 func TestMain(m *testing.M) { 129 // check if we have been reexec'd 130 if reexec.Init() { 131 return 132 } 133 os.Exit(m.Run()) 134 } 135 136 // spawns klay with the given command line args. If the args don't set --datadir, the 137 // child g gets a temporary data directory. 138 func runKlay(t *testing.T, name string, args ...string) *testklay { 139 tt := &testklay{} 140 tt.TestCmd = utils.NewTestCmd(t, tt) 141 for i, arg := range args { 142 switch { 143 case arg == "-datadir" || arg == "--datadir": 144 if i < len(args)-1 { 145 tt.Datadir = args[i+1] 146 } 147 case arg == "-rewardbase" || arg == "--rewardbase": 148 if i < len(args)-1 { 149 tt.Rewardbase = args[i+1] 150 } 151 } 152 } 153 if tt.Datadir == "" { 154 tt.Datadir = tmpdir(t) 155 tt.Cleanup = func() { os.RemoveAll(tt.Datadir) } 156 args = append([]string{"-datadir", tt.Datadir}, args...) 157 // Remove the temporary datadir if something fails below. 158 defer func() { 159 if t.Failed() { 160 tt.Cleanup() 161 } 162 }() 163 } 164 165 // Boot "klay". This actually runs the test binary but the TestMain 166 // function will prevent any tests from running. 167 tt.Run(name, args...) 168 169 return tt 170 } 171 172 func RunTestKlaytnNode(ctx *cli.Context) error { 173 fullNode := MakeFullNode(ctx) 174 fullNode.Wait() 175 return nil 176 }