Skip to content

Commit

Permalink
fixup: Use time-based network directory name to ensure consistent sor…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
marun committed Mar 7, 2024
1 parent 0e8d6b5 commit 5309c5e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 33 deletions.
6 changes: 3 additions & 3 deletions tests/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ $ ./scripts/build_tmpnetctl.sh
# Start a new network
$ ./build/tmpnetctl start-network --avalanchego-path=/path/to/avalanchego
...
Started network /home/me/.tmpnet/networks/c975a433-503d-4d66-957d-643b79f0a7dd
Started network /home/me/.tmpnet/networks/20240306-152305.924531 (UUID: abaab590-b375-44f6-9ca5-f8a6dc061725)

Configure tmpnetctl and the test suite to target this network by default
with one of the following statements:
- source /home/me/.tmpnet/networks/c975a433-503d-4d66-957d-643b79f0a7dd/network.env
- export TMPNET_NETWORK_DIR=/home/me/.tmpnet/networks/c975a433-503d-4d66-957d-643b79f0a7dd
- source /home/me/.tmpnet/networks/20240306-152305.924531/network.env
- export TMPNET_NETWORK_DIR=/home/me/.tmpnet/networks/20240306-152305.924531
- export TMPNET_NETWORK_DIR=/home/me/.tmpnet/networks/latest

# Start a new test run using the existing network
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/c/dynamic_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {

ginkgo.It("should ensure that the gas price is affected by load", func() {
ginkgo.By("creating a new private network to ensure isolation from other tests")
privateNetwork := e2e.Env.NewPrivateNetwork()
privateNetwork := e2e.Env.NewPrivateNetwork("avalanchego-e2e-dynamic-fees")

ginkgo.By("allocating a pre-funded key")
key := privateNetwork.PreFundedKeys[0]
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {

var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
// Run only once in the first ginkgo process
return e2e.NewTestEnvironment(flagVars, &tmpnet.Network{}).Marshal()
return e2e.NewTestEnvironment(flagVars, "avalanchego-e2e", &tmpnet.Network{}).Marshal()
}, func(envBytes []byte) {
// Run in every ginkgo process

Expand Down
7 changes: 4 additions & 3 deletions tests/fixture/e2e/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (te *TestEnvironment) Marshal() []byte {
}

// Initialize a new test environment with a shared network (either pre-existing or newly created).
func NewTestEnvironment(flagVars *FlagVars, desiredNetwork *tmpnet.Network) *TestEnvironment {
func NewTestEnvironment(flagVars *FlagVars, networkDirSuffix string, desiredNetwork *tmpnet.Network) *TestEnvironment {
require := require.New(ginkgo.GinkgoT())

networkDir := flagVars.NetworkDir()
Expand All @@ -74,7 +74,7 @@ func NewTestEnvironment(flagVars *FlagVars, desiredNetwork *tmpnet.Network) *Tes
}
} else {
network = desiredNetwork
StartNetwork(network, flagVars.AvalancheGoExecPath(), flagVars.PluginDir())
StartNetwork(network, networkDirSuffix, flagVars.AvalancheGoExecPath(), flagVars.PluginDir())
}

// A new network will always need subnet creation and an existing
Expand Down Expand Up @@ -155,7 +155,7 @@ func (te *TestEnvironment) NewKeychain(count int) *secp256k1fx.Keychain {
}

// Create a new private network that is not shared with other tests.
func (te *TestEnvironment) NewPrivateNetwork() *tmpnet.Network {
func (te *TestEnvironment) NewPrivateNetwork(networkDirSuffix string) *tmpnet.Network {
// Use the same configuration as the shared network
sharedNetwork, err := tmpnet.ReadNetwork(te.NetworkDir)
te.require.NoError(err)
Expand All @@ -166,6 +166,7 @@ func (te *TestEnvironment) NewPrivateNetwork() *tmpnet.Network {
network := &tmpnet.Network{}
StartNetwork(
network,
networkDirSuffix,
sharedNetwork.DefaultRuntimeConfig.AvalancheGoPath,
pluginDir,
)
Expand Down
3 changes: 2 additions & 1 deletion tests/fixture/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func CheckBootstrapIsPossible(network *tmpnet.Network) {
}

// Start a temporary network with the provided avalanchego binary.
func StartNetwork(network *tmpnet.Network, avalancheGoExecPath string, pluginDir string) {
func StartNetwork(network *tmpnet.Network, networkDirSuffix string, avalancheGoExecPath string, pluginDir string) {
require := require.New(ginkgo.GinkgoT())

require.NoError(
Expand All @@ -225,6 +225,7 @@ func StartNetwork(network *tmpnet.Network, avalancheGoExecPath string, pluginDir
ginkgo.GinkgoWriter,
network,
DefaultNetworkDir,
networkDirSuffix,
avalancheGoExecPath,
pluginDir,
tmpnet.DefaultNodeCount,
Expand Down
8 changes: 4 additions & 4 deletions tests/fixture/tmpnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ $ ./scripts/build_tmpnetctl.sh
# Start a new network
$ ./build/tmpnetctl start-network --avalanchego-path=/path/to/avalanchego
...
Started network /home/me/.tmpnet/networks/c975a433-503d-4d66-957d-643b79f0a7dd
Started network /home/me/.tmpnet/networks/20240306-152305.924531 (UUID: abaab590-b375-44f6-9ca5-f8a6dc061725)

Configure tmpnetctl to target this network by default with one of the following statements:
- source /home/me/.tmpnet/networks/c975a433-503d-4d66-957d-643b79f0a7dd/network.env
- export TMPNET_NETWORK_DIR=/home/me/.tmpnet/networks/c975a433-503d-4d66-957d-643b79f0a7dd
- source /home/me/.tmpnet/networks/20240306-152305.924531/network.env
- export TMPNET_NETWORK_DIR=/home/me/.tmpnet/networks/20240306-152305.924531
- export TMPNET_NETWORK_DIR=/home/me/.tmpnet/networks/latest

# Stop the network
Expand Down Expand Up @@ -129,7 +129,7 @@ A temporary network relies on configuration written to disk in the following str
HOME
└── .tmpnet // Root path for the temporary network fixture
└── networks // Default parent directory for temporary networks
└── c975a433-503d-4d66-957d-643b79f0a7dd // The UUID of the temporary network is the name of its directory
└── 20240306-152305.924531 // The timestamp of creation is the name of a network's directory
├── NodeID-37E8UK3x2YFsHE3RdALmfWcppcZ1eTuj9 // The ID of a node is the name of its data dir
│ ├── chainData
│ │ └── ...
Expand Down
11 changes: 7 additions & 4 deletions tests/fixture/tmpnet/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func main() {
rootCmd.AddCommand(versionCmd)

var (
rootDir string
avalancheGoPath string
pluginDir string
nodeCount uint8
rootDir string
networkDirSuffix string
avalancheGoPath string
pluginDir string
nodeCount uint8
)
startNetworkCmd := &cobra.Command{
Use: "start-network",
Expand All @@ -75,6 +76,7 @@ func main() {
os.Stdout,
network,
rootDir,
networkDirSuffix,
avalancheGoPath,
pluginDir,
int(nodeCount),
Expand Down Expand Up @@ -103,6 +105,7 @@ func main() {
},
}
startNetworkCmd.PersistentFlags().StringVar(&rootDir, "root-dir", os.Getenv(tmpnet.RootDirEnvName), "The path to the root directory for temporary networks")
startNetworkCmd.PersistentFlags().StringVar(&networkDirSuffix, "network-dir-suffix", "", "The suffix to append to the name of the network's directory")
startNetworkCmd.PersistentFlags().StringVar(&avalancheGoPath, "avalanchego-path", os.Getenv(tmpnet.AvalancheGoPathEnvName), "The path to an avalanchego binary")
startNetworkCmd.PersistentFlags().StringVar(&pluginDir, "plugin-dir", os.ExpandEnv("$HOME/.avalanchego/plugins"), "[optional] the dir containing VM plugins")
startNetworkCmd.PersistentFlags().Uint8Var(&nodeCount, "node-count", tmpnet.DefaultNodeCount, "Number of nodes the network should initially consist of")
Expand Down
39 changes: 24 additions & 15 deletions tests/fixture/tmpnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
// increase the time for a network's nodes to be seen as healthy.
networkHealthCheckInterval = 200 * time.Millisecond

// All temporary network will use this arbitrary default network ID by default.
// All temporary networks will use this arbitrary default network ID by default.
defaultNetworkID = 88888

// eth address: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Expand All @@ -64,11 +64,11 @@ func init() {

// Collects the configuration for running a temporary avalanchego network
type Network struct {
// Uniquely identifies the temporary network for storage on disk and
// metrics collection. Distinct from avalanchego's concept of network ID
// since the utility of special network ID values (e.g. to trigger specific
// fork behavior in a given network) precludes requiring unique network ID
// values across all temporary networks.
// Uniquely identifies the temporary network for metrics
// collection. Distinct from avalanchego's concept of network ID
// since the utility of special network ID values (e.g. to trigger
// specific fork behavior in a given network) precludes requiring
// unique network ID values across all temporary networks.
UUID string

// Path where network configuration and data is stored
Expand Down Expand Up @@ -108,14 +108,15 @@ func StartNewNetwork(
w io.Writer,
network *Network,
rootNetworkDir string,
networkDirSuffix string,
avalancheGoExecPath string,
pluginDir string,
nodeCount int,
) error {
if err := network.EnsureDefaultConfig(w, avalancheGoExecPath, pluginDir, nodeCount); err != nil {
return err
}
if err := network.Create(rootNetworkDir); err != nil {
if err := network.Create(rootNetworkDir, networkDirSuffix); err != nil {
return err
}
return network.Start(ctx, w)
Expand Down Expand Up @@ -160,6 +161,11 @@ func (n *Network) EnsureDefaultConfig(w io.Writer, avalancheGoPath string, plugi
return err
}

// A UUID supports centralized metrics collection
if len(n.UUID) == 0 {
n.UUID = uuid.NewString()
}

// Ensure default flags
if n.DefaultFlags == nil {
n.DefaultFlags = FlagsMap{}
Expand Down Expand Up @@ -216,8 +222,8 @@ func (n *Network) EnsureDefaultConfig(w io.Writer, avalancheGoPath string, plugi
return nil
}

// Creates the network on disk, choosing its network id and generating its genesis in the process.
func (n *Network) Create(rootDir string) error {
// Creates the network on disk, generating its genesis and configuring its nodes in the process.
func (n *Network) Create(rootDir string, networkDirSuffix string) error {
// Ensure creation of the root dir
if len(rootDir) == 0 {
// Use the default root dir
Expand All @@ -231,11 +237,14 @@ func (n *Network) Create(rootDir string) error {
return fmt.Errorf("failed to create root network dir: %w", err)
}

// Ensure creation of the network dir
if len(n.UUID) == 0 {
n.UUID = uuid.NewString()
// A time-based name ensures consistent directory ordering
dirName := time.Now().Format("20060102-150405.999999")
if len(networkDirSuffix) > 0 {
dirName = fmt.Sprintf("%s-%s", dirName, networkDirSuffix)
}
networkDir := filepath.Join(rootDir, n.UUID)

// Ensure creation of the network dir
networkDir := filepath.Join(rootDir, dirName)
if err := os.MkdirAll(networkDir, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to create network dir: %w", err)
}
Expand Down Expand Up @@ -289,7 +298,7 @@ func (n *Network) Create(rootDir string) error {

// Starts all nodes in the network
func (n *Network) Start(ctx context.Context, w io.Writer) error {
if _, err := fmt.Fprintf(w, "Starting network %s\n", n.Dir); err != nil {
if _, err := fmt.Fprintf(w, "Starting network %s (UUID: %s)\n", n.Dir, n.UUID); err != nil {
return err
}

Expand All @@ -306,7 +315,7 @@ func (n *Network) Start(ctx context.Context, w io.Writer) error {
if err := n.WaitForHealthy(ctx, w); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "\nStarted network %s\n", n.Dir); err != nil {
if _, err := fmt.Fprintf(w, "\nStarted network %s (UUID: %s)\n", n.Dir, n.UUID); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion tests/upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = ginkgo.Describe("[Upgrade]", func() {

ginkgo.It("can upgrade versions", func() {
network := &tmpnet.Network{}
e2e.StartNetwork(network, avalancheGoExecPath, "" /* pluginDir */)
e2e.StartNetwork(network, "avalanchego-upgrade", avalancheGoExecPath, "" /* pluginDir */)

ginkgo.By(fmt.Sprintf("restarting all nodes with %q binary", avalancheGoExecPathToUpgradeTo))
for _, node := range network.Nodes {
Expand Down

0 comments on commit 5309c5e

Please sign in to comment.