-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
e2e: Refactor suite setup and helpers to tests/fixture/e2e for reuse …
…by coreth (#2265)
- Loading branch information
Showing
17 changed files
with
212 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package e2e | ||
|
||
import ( | ||
"encoding/json" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
ginkgo "github.com/onsi/ginkgo/v2" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/tests" | ||
"github.com/ava-labs/avalanchego/tests/fixture" | ||
"github.com/ava-labs/avalanchego/tests/fixture/testnet" | ||
"github.com/ava-labs/avalanchego/tests/fixture/testnet/local" | ||
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1" | ||
"github.com/ava-labs/avalanchego/utils/perms" | ||
"github.com/ava-labs/avalanchego/vms/secp256k1fx" | ||
) | ||
|
||
// Env is used to access shared test fixture. Intended to be | ||
// initialized from SynchronizedBeforeSuite. | ||
var Env *TestEnvironment | ||
|
||
func InitSharedTestEnvironment(envBytes []byte) { | ||
require := require.New(ginkgo.GinkgoT()) | ||
require.Nil(Env, "env already initialized") | ||
Env = &TestEnvironment{ | ||
require: require, | ||
} | ||
require.NoError(json.Unmarshal(envBytes, Env)) | ||
} | ||
|
||
type TestEnvironment struct { | ||
// The directory where the test network configuration is stored | ||
NetworkDir string | ||
// URIs used to access the API endpoints of nodes of the network | ||
URIs []testnet.NodeURI | ||
// The URI used to access the http server that allocates test data | ||
TestDataServerURI string | ||
|
||
require *require.Assertions | ||
} | ||
|
||
func (te *TestEnvironment) Marshal() []byte { | ||
bytes, err := json.Marshal(te) | ||
require.NoError(ginkgo.GinkgoT(), err) | ||
return bytes | ||
} | ||
|
||
// Initialize a new test environment with a shared network (either pre-existing or newly created). | ||
func NewTestEnvironment(flagVars *FlagVars) *TestEnvironment { | ||
require := require.New(ginkgo.GinkgoT()) | ||
|
||
persistentNetworkDir := flagVars.PersistentNetworkDir() | ||
|
||
// Load or create a test network | ||
var network *local.LocalNetwork | ||
if len(persistentNetworkDir) > 0 { | ||
tests.Outf("{{yellow}}Using a persistent network configured at %s{{/}}\n", persistentNetworkDir) | ||
|
||
var err error | ||
network, err = local.ReadNetwork(persistentNetworkDir) | ||
require.NoError(err) | ||
} else { | ||
network = StartLocalNetwork(flagVars.AvalancheGoExecPath(), DefaultNetworkDir) | ||
} | ||
|
||
uris := network.GetURIs() | ||
require.NotEmpty(uris, "network contains no nodes") | ||
tests.Outf("{{green}}network URIs: {{/}} %+v\n", uris) | ||
|
||
testDataServerURI, err := fixture.ServeTestData(fixture.TestData{ | ||
FundedKeys: network.FundedKeys, | ||
}) | ||
tests.Outf("{{green}}test data server URI: {{/}} %+v\n", testDataServerURI) | ||
require.NoError(err) | ||
|
||
return &TestEnvironment{ | ||
NetworkDir: network.Dir, | ||
URIs: uris, | ||
TestDataServerURI: testDataServerURI, | ||
} | ||
} | ||
|
||
// Retrieve a random URI to naively attempt to spread API load across | ||
// nodes. | ||
func (te *TestEnvironment) GetRandomNodeURI() testnet.NodeURI { | ||
r := rand.New(rand.NewSource(time.Now().Unix())) //#nosec G404 | ||
nodeURI := te.URIs[r.Intn(len(te.URIs))] | ||
tests.Outf("{{blue}} targeting node %s with URI: %s{{/}}\n", nodeURI.NodeID, nodeURI.URI) | ||
return nodeURI | ||
} | ||
|
||
// Retrieve the network to target for testing. | ||
func (te *TestEnvironment) GetNetwork() testnet.Network { | ||
network, err := local.ReadNetwork(te.NetworkDir) | ||
te.require.NoError(err) | ||
return network | ||
} | ||
|
||
// Retrieve the specified number of funded keys allocated for the caller's exclusive use. | ||
func (te *TestEnvironment) AllocateFundedKeys(count int) []*secp256k1.PrivateKey { | ||
keys, err := fixture.AllocateFundedKeys(te.TestDataServerURI, count) | ||
te.require.NoError(err) | ||
tests.Outf("{{blue}} allocated funded key(s): %+v{{/}}\n", keys) | ||
return keys | ||
} | ||
|
||
// Retrieve a funded key allocated for the caller's exclusive use. | ||
func (te *TestEnvironment) AllocateFundedKey() *secp256k1.PrivateKey { | ||
return te.AllocateFundedKeys(1)[0] | ||
} | ||
|
||
// Create a new keychain with the specified number of test keys. | ||
func (te *TestEnvironment) NewKeychain(count int) *secp256k1fx.Keychain { | ||
keys := te.AllocateFundedKeys(count) | ||
return secp256k1fx.NewKeychain(keys...) | ||
} | ||
|
||
// Create a new private network that is not shared with other tests. | ||
func (te *TestEnvironment) NewPrivateNetwork() testnet.Network { | ||
// Load the shared network to retrieve its path and exec path | ||
sharedNetwork, err := local.ReadNetwork(te.NetworkDir) | ||
te.require.NoError(err) | ||
|
||
// The private networks dir is under the shared network dir to ensure it | ||
// will be included in the artifact uploaded in CI. | ||
privateNetworksDir := filepath.Join(sharedNetwork.Dir, PrivateNetworksDirName) | ||
te.require.NoError(os.MkdirAll(privateNetworksDir, perms.ReadWriteExecute)) | ||
|
||
return StartLocalNetwork(sharedNetwork.ExecPath, privateNetworksDir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package e2e | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ava-labs/avalanchego/tests/fixture/testnet/local" | ||
) | ||
|
||
type FlagVars struct { | ||
avalancheGoExecPath string | ||
persistentNetworkDir string | ||
usePersistentNetwork bool | ||
} | ||
|
||
func (v *FlagVars) PersistentNetworkDir() string { | ||
if v.usePersistentNetwork && len(v.persistentNetworkDir) == 0 { | ||
return os.Getenv(local.NetworkDirEnvName) | ||
} | ||
return v.persistentNetworkDir | ||
} | ||
|
||
func (v *FlagVars) AvalancheGoExecPath() string { | ||
return v.avalancheGoExecPath | ||
} | ||
|
||
func (v *FlagVars) UsePersistentNetwork() bool { | ||
return v.usePersistentNetwork | ||
} | ||
|
||
func RegisterFlags() *FlagVars { | ||
vars := FlagVars{} | ||
flag.StringVar( | ||
&vars.avalancheGoExecPath, | ||
"avalanchego-path", | ||
os.Getenv(local.AvalancheGoPathEnvName), | ||
fmt.Sprintf("avalanchego executable path (required if not using a persistent network). Also possible to configure via the %s env variable.", local.AvalancheGoPathEnvName), | ||
) | ||
flag.StringVar( | ||
&vars.persistentNetworkDir, | ||
"network-dir", | ||
"", | ||
fmt.Sprintf("[optional] the dir containing the configuration of a persistent network to target for testing. Useful for speeding up test development. Also possible to configure via the %s env variable.", local.NetworkDirEnvName), | ||
) | ||
flag.BoolVar( | ||
&vars.usePersistentNetwork, | ||
"use-persistent-network", | ||
false, | ||
"[optional] whether to target the persistent network identified by --network-dir.", | ||
) | ||
|
||
return &vars | ||
} |
Oops, something went wrong.