github.com/klaytn/klaytn@v1.12.1/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  	"os"
    26  	"runtime"
    27  	"sort"
    28  	"testing"
    29  
    30  	"github.com/docker/docker/pkg/reexec"
    31  	"github.com/klaytn/klaytn/api/debug"
    32  	"github.com/klaytn/klaytn/cmd/utils"
    33  	"github.com/klaytn/klaytn/console"
    34  	metricutils "github.com/klaytn/klaytn/metrics/utils"
    35  	"github.com/klaytn/klaytn/node"
    36  	"github.com/urfave/cli/v2"
    37  )
    38  
    39  func tmpdir(t *testing.T) string {
    40  	dir, err := os.MkdirTemp("", "klay-test")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	return dir
    45  }
    46  
    47  type testklay struct {
    48  	*utils.TestCmd
    49  
    50  	// template variables for expect
    51  	Datadir    string
    52  	Rewardbase string
    53  }
    54  
    55  var (
    56  	// The app that holds all commands and flags.
    57  	app = utils.NewApp(GetGitCommit(), "the Klaytn command line interface")
    58  
    59  	// flags that configure the node
    60  	nodeFlags = utils.CommonNodeFlags
    61  
    62  	rpcFlags = utils.CommonRPCFlags
    63  )
    64  
    65  func init() {
    66  	// Initialize the CLI app and start Klay
    67  	app.Action = RunKlaytnNode
    68  	app.HideVersion = true // we have a command to print the version
    69  	app.Copyright = "Copyright 2018-2023 The klaytn Authors"
    70  	app.Commands = []*cli.Command{
    71  		// See chaincmd.go:
    72  		InitCommand,
    73  
    74  		// See accountcmd.go
    75  		AccountCommand,
    76  
    77  		// See consolecmd.go:
    78  		GetConsoleCommand(nodeFlags, rpcFlags),
    79  		AttachCommand,
    80  
    81  		// See versioncmd.go:
    82  		VersionCommand,
    83  
    84  		// See dumpconfigcmd.go:
    85  		GetDumpConfigCommand(nodeFlags, rpcFlags),
    86  	}
    87  	sort.Sort(cli.CommandsByName(app.Commands))
    88  
    89  	app.Flags = utils.AllNodeFlags()
    90  
    91  	app.Before = func(ctx *cli.Context) error {
    92  		MigrateGlobalFlags(ctx)
    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  }