@@ -11,6 +11,7 @@ import (
1111 "fmt"
1212 "math/rand"
1313 "os"
14+ "path/filepath"
1415 "strings"
1516 "time"
1617
@@ -30,6 +31,7 @@ import (
3031 "github.com/ava-labs/avalanchego/tests/fixture/testnet"
3132 "github.com/ava-labs/avalanchego/tests/fixture/testnet/local"
3233 "github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
34+ "github.com/ava-labs/avalanchego/utils/perms"
3335 "github.com/ava-labs/avalanchego/vms/secp256k1fx"
3436 "github.com/ava-labs/avalanchego/wallet/subnet/primary"
3537 "github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
@@ -62,6 +64,14 @@ const (
6264 // checks. Useful for speeding up iteration during test
6365 // development.
6466 SkipBootstrapChecksEnvName = "E2E_SKIP_BOOTSTRAP_CHECKS"
67+
68+ // An empty string prompts the use of the default path which ensures a
69+ // predictable target for github's upload-artifact action.
70+ DefaultNetworkDir = ""
71+
72+ // Directory used to store private networks (specific to a single test)
73+ // under the shared network dir.
74+ PrivateNetworksDirName = "private_networks"
6575)
6676
6777// Env is used to access shared test fixture. Intended to be
@@ -127,6 +137,7 @@ func (te *TestEnvironment) NewKeychain(count int) *secp256k1fx.Keychain {
127137}
128138
129139// Create a new wallet for the provided keychain against the specified node URI.
140+ // TODO(marun) Make this a regular function.
130141func (te * TestEnvironment ) NewWallet (keychain * secp256k1fx.Keychain , uri string ) primary.Wallet {
131142 tests .Outf ("{{blue}} initializing a new wallet for URI: %s {{/}}\n " , uri )
132143 wallet , err := primary .MakeWallet (DefaultContext (), & primary.WalletConfig {
@@ -139,6 +150,7 @@ func (te *TestEnvironment) NewWallet(keychain *secp256k1fx.Keychain, uri string)
139150}
140151
141152// Create a new eth client targeting the specified node URI.
153+ // TODO(marun) Make this a regular function.
142154func (te * TestEnvironment ) NewEthClient (nodeURI string ) ethclient.Client {
143155 tests .Outf ("{{blue}} initializing a new eth client for URI: %s {{/}}\n " , nodeURI )
144156 nodeAddress := strings .Split (nodeURI , "//" )[1 ]
@@ -148,6 +160,20 @@ func (te *TestEnvironment) NewEthClient(nodeURI string) ethclient.Client {
148160 return client
149161}
150162
163+ // Create a new private network that is not shared with other tests.
164+ func (te * TestEnvironment ) NewPrivateNetwork () testnet.Network {
165+ // Load the shared network to retrieve its path and exec path
166+ sharedNetwork , err := local .ReadNetwork (te .NetworkDir )
167+ te .require .NoError (err )
168+
169+ // The private networks dir is under the shared network dir to ensure it
170+ // will be included in the artifact uploaded in CI.
171+ privateNetworksDir := filepath .Join (sharedNetwork .Dir , PrivateNetworksDirName )
172+ te .require .NoError (os .MkdirAll (privateNetworksDir , perms .ReadWriteExecute ))
173+
174+ return StartLocalNetwork (sharedNetwork .ExecPath , privateNetworksDir )
175+ }
176+
151177// Helper simplifying use of a timed context by canceling the context on ginkgo teardown.
152178func ContextWithTimeout (duration time.Duration ) context.Context {
153179 ctx , cancel := context .WithTimeout (context .Background (), duration )
@@ -267,3 +293,32 @@ func CheckBootstrapIsPossible(network testnet.Network) {
267293 node := AddEphemeralNode (network , testnet.FlagsMap {})
268294 WaitForHealthy (node )
269295}
296+
297+ // Start a local test-managed network with the provided avalanchego binary.
298+ func StartLocalNetwork (avalancheGoExecPath string , networkDir string ) * local.LocalNetwork {
299+ require := require .New (ginkgo .GinkgoT ())
300+
301+ tests .Outf ("{{magenta}}Starting network with %q{{/}}\n " , avalancheGoExecPath )
302+
303+ network , err := local .StartNetwork (
304+ DefaultContext (),
305+ ginkgo .GinkgoWriter ,
306+ networkDir ,
307+ & local.LocalNetwork {
308+ LocalConfig : local.LocalConfig {
309+ ExecPath : avalancheGoExecPath ,
310+ },
311+ },
312+ testnet .DefaultNodeCount ,
313+ testnet .DefaultFundedKeyCount ,
314+ )
315+ require .NoError (err )
316+ ginkgo .DeferCleanup (func () {
317+ tests .Outf ("Shutting down network\n " )
318+ require .NoError (network .Stop ())
319+ })
320+
321+ tests .Outf ("{{green}}Successfully started network{{/}}\n " )
322+
323+ return network
324+ }
0 commit comments