github.com/MetalBlockchain/metalgo@v1.11.9/tests/fixture/e2e/flags.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package e2e
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"os"
    10  	"time"
    11  
    12  	"github.com/MetalBlockchain/metalgo/tests/fixture/tmpnet"
    13  )
    14  
    15  type FlagVars struct {
    16  	avalancheGoExecPath  string
    17  	pluginDir            string
    18  	networkDir           string
    19  	reuseNetwork         bool
    20  	delayNetworkShutdown bool
    21  	stopNetwork          bool
    22  	nodeCount            int
    23  }
    24  
    25  func (v *FlagVars) AvalancheGoExecPath() string {
    26  	return v.avalancheGoExecPath
    27  }
    28  
    29  func (v *FlagVars) PluginDir() string {
    30  	return v.pluginDir
    31  }
    32  
    33  func (v *FlagVars) NetworkDir() string {
    34  	if !v.reuseNetwork {
    35  		return ""
    36  	}
    37  	if len(v.networkDir) > 0 {
    38  		return v.networkDir
    39  	}
    40  	return os.Getenv(tmpnet.NetworkDirEnvName)
    41  }
    42  
    43  func (v *FlagVars) ReuseNetwork() bool {
    44  	return v.reuseNetwork
    45  }
    46  
    47  func (v *FlagVars) NetworkShutdownDelay() time.Duration {
    48  	if v.delayNetworkShutdown {
    49  		// Only return a non-zero value if the delay is enabled.  Make sure this value takes
    50  		// into account the scrape_interval defined in scripts/run_prometheus.sh.
    51  		return 12 * time.Second
    52  	}
    53  	return 0
    54  }
    55  
    56  func (v *FlagVars) StopNetwork() bool {
    57  	return v.stopNetwork
    58  }
    59  
    60  func (v *FlagVars) NodeCount() int {
    61  	return v.nodeCount
    62  }
    63  
    64  func RegisterFlags() *FlagVars {
    65  	vars := FlagVars{}
    66  	flag.StringVar(
    67  		&vars.avalancheGoExecPath,
    68  		"metalgo-path",
    69  		os.Getenv(tmpnet.AvalancheGoPathEnvName),
    70  		fmt.Sprintf("metalgo executable path (required if not using an existing network). Also possible to configure via the %s env variable.", tmpnet.AvalancheGoPathEnvName),
    71  	)
    72  	flag.StringVar(
    73  		&vars.pluginDir,
    74  		"plugin-dir",
    75  		os.ExpandEnv("$HOME/.metalgo/plugins"),
    76  		"[optional] the dir containing VM plugins.",
    77  	)
    78  	flag.StringVar(
    79  		&vars.networkDir,
    80  		"network-dir",
    81  		"",
    82  		fmt.Sprintf("[optional] the dir containing the configuration of an existing network to target for testing. Will only be used if --reuse-network is specified. Also possible to configure via the %s env variable.", tmpnet.NetworkDirEnvName),
    83  	)
    84  	flag.BoolVar(
    85  		&vars.reuseNetwork,
    86  		"reuse-network",
    87  		false,
    88  		"[optional] reuse an existing network. If an existing network is not already running, create a new one and leave it running for subsequent usage.",
    89  	)
    90  	flag.BoolVar(
    91  		&vars.delayNetworkShutdown,
    92  		"delay-network-shutdown",
    93  		false,
    94  		"[optional] whether to delay network shutdown to allow a final metrics scrape.",
    95  	)
    96  	flag.BoolVar(
    97  		&vars.stopNetwork,
    98  		"stop-network",
    99  		false,
   100  		"[optional] stop an existing network and exit without executing any tests.",
   101  	)
   102  	flag.IntVar(
   103  		&vars.nodeCount,
   104  		"node-count",
   105  		tmpnet.DefaultNodeCount,
   106  		"number of nodes the network should initially consist of",
   107  	)
   108  
   109  	return &vars
   110  }