Skip to content

Commit f5d5571

Browse files
committed
[e2e] Abstract usage of ginkgo with a new test context
1 parent 312808d commit f5d5571

20 files changed

+611
-472
lines changed

tests/colors.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/context_helpers.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package tests
5+
6+
import (
7+
"context"
8+
"time"
9+
10+
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
11+
)
12+
13+
// A long default timeout used to timeout failed operations but unlikely to induce
14+
// flaking due to unexpected resource contention.
15+
const DefaultTimeout = 2 * time.Minute
16+
17+
// Helper simplifying use of a timed context by canceling the context with the test context.
18+
func ContextWithTimeout(tc TestContext, duration time.Duration) context.Context {
19+
ctx, cancel := context.WithTimeout(context.Background(), duration)
20+
tc.DeferCleanup(cancel)
21+
return ctx
22+
}
23+
24+
// Helper simplifying use of a timed context configured with the default timeout.
25+
func DefaultContext(tc TestContext) context.Context {
26+
return ContextWithTimeout(tc, DefaultTimeout)
27+
}
28+
29+
// Helper simplifying use via an option of a timed context configured with the default timeout.
30+
func WithDefaultContext(tc TestContext) common.Option {
31+
return common.WithContext(DefaultContext(tc))
32+
}

tests/e2e/banff/suites.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/stretchr/testify/require"
99

1010
"github.com/ava-labs/avalanchego/ids"
11-
"github.com/ava-labs/avalanchego/tests"
1211
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
1312
"github.com/ava-labs/avalanchego/utils/constants"
1413
"github.com/ava-labs/avalanchego/utils/units"
@@ -20,12 +19,14 @@ import (
2019
)
2120

2221
var _ = ginkgo.Describe("[Banff]", func() {
23-
require := require.New(ginkgo.GinkgoT())
22+
tc := e2e.NewTestContext()
23+
require := require.New(tc)
2424

2525
ginkgo.It("can send custom assets X->P and P->X",
2626
func() {
27-
keychain := e2e.Env.NewKeychain(1)
28-
wallet := e2e.NewWallet(keychain, e2e.Env.GetRandomNodeURI())
27+
env := e2e.GetEnv(tc)
28+
keychain := env.NewKeychain(1)
29+
wallet := e2e.NewWallet(tc, keychain, env.GetRandomNodeURI())
2930

3031
// Get the P-chain and the X-chain wallets
3132
pWallet := wallet.P()
@@ -43,7 +44,7 @@ var _ = ginkgo.Describe("[Banff]", func() {
4344
}
4445

4546
var assetID ids.ID
46-
ginkgo.By("create new X-chain asset", func() {
47+
tc.By("create new X-chain asset", func() {
4748
assetTx, err := xWallet.IssueCreateAssetTx(
4849
"RnM",
4950
"RNM",
@@ -56,15 +57,15 @@ var _ = ginkgo.Describe("[Banff]", func() {
5657
},
5758
},
5859
},
59-
e2e.WithDefaultContext(),
60+
tc.WithDefaultContext(),
6061
)
6162
require.NoError(err)
6263
assetID = assetTx.ID()
6364

64-
tests.Outf("{{green}}created new X-chain asset{{/}}: %s\n", assetID)
65+
tc.Outf("{{green}}created new X-chain asset{{/}}: %s\n", assetID)
6566
})
6667

67-
ginkgo.By("export new X-chain asset to P-chain", func() {
68+
tc.By("export new X-chain asset to P-chain", func() {
6869
tx, err := xWallet.IssueExportTx(
6970
constants.PlatformChainID,
7071
[]*avax.TransferableOutput{
@@ -78,25 +79,25 @@ var _ = ginkgo.Describe("[Banff]", func() {
7879
},
7980
},
8081
},
81-
e2e.WithDefaultContext(),
82+
tc.WithDefaultContext(),
8283
)
8384
require.NoError(err)
8485

85-
tests.Outf("{{green}}issued X-chain export{{/}}: %s\n", tx.ID())
86+
tc.Outf("{{green}}issued X-chain export{{/}}: %s\n", tx.ID())
8687
})
8788

88-
ginkgo.By("import new asset from X-chain on the P-chain", func() {
89+
tc.By("import new asset from X-chain on the P-chain", func() {
8990
tx, err := pWallet.IssueImportTx(
9091
xChainID,
9192
owner,
92-
e2e.WithDefaultContext(),
93+
tc.WithDefaultContext(),
9394
)
9495
require.NoError(err)
9596

96-
tests.Outf("{{green}}issued P-chain import{{/}}: %s\n", tx.ID())
97+
tc.Outf("{{green}}issued P-chain import{{/}}: %s\n", tx.ID())
9798
})
9899

99-
ginkgo.By("export asset from P-chain to the X-chain", func() {
100+
tc.By("export asset from P-chain to the X-chain", func() {
100101
tx, err := pWallet.IssueExportTx(
101102
xChainID,
102103
[]*avax.TransferableOutput{
@@ -110,22 +111,22 @@ var _ = ginkgo.Describe("[Banff]", func() {
110111
},
111112
},
112113
},
113-
e2e.WithDefaultContext(),
114+
tc.WithDefaultContext(),
114115
)
115116
require.NoError(err)
116117

117-
tests.Outf("{{green}}issued P-chain export{{/}}: %s\n", tx.ID())
118+
tc.Outf("{{green}}issued P-chain export{{/}}: %s\n", tx.ID())
118119
})
119120

120-
ginkgo.By("import asset from P-chain on the X-chain", func() {
121+
tc.By("import asset from P-chain on the X-chain", func() {
121122
tx, err := xWallet.IssueImportTx(
122123
constants.PlatformChainID,
123124
owner,
124-
e2e.WithDefaultContext(),
125+
tc.WithDefaultContext(),
125126
)
126127
require.NoError(err)
127128

128-
tests.Outf("{{green}}issued X-chain import{{/}}: %s\n", tx.ID())
129+
tc.Outf("{{green}}issued X-chain import{{/}}: %s\n", tx.ID())
129130
})
130131
})
131132
})

tests/e2e/c/dynamic_fees.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/ethereum/go-ethereum/common"
1515
"github.com/stretchr/testify/require"
1616

17-
"github.com/ava-labs/avalanchego/tests"
1817
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
1918
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
2019
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
@@ -26,7 +25,8 @@ import (
2625
// well as its ABI contained in `hashing_contract.go`.
2726

2827
var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
29-
require := require.New(ginkgo.GinkgoT())
28+
tc := e2e.NewTestContext()
29+
require := require.New(tc)
3030

3131
// Need a gas limit much larger than the standard 21_000 to enable
3232
// the contract to induce a gas price increase
@@ -36,24 +36,24 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
3636
gasTip := big.NewInt(1000 * params.GWei)
3737

3838
ginkgo.It("should ensure that the gas price is affected by load", func() {
39-
ginkgo.By("creating a new private network to ensure isolation from other tests")
39+
tc.By("creating a new private network to ensure isolation from other tests")
4040
privateNetwork := tmpnet.NewDefaultNetwork("avalanchego-e2e-dynamic-fees")
41-
e2e.Env.StartPrivateNetwork(privateNetwork)
41+
e2e.GetEnv(tc).StartPrivateNetwork(privateNetwork)
4242

43-
ginkgo.By("allocating a pre-funded key")
43+
tc.By("allocating a pre-funded key")
4444
key := privateNetwork.PreFundedKeys[0]
4545
ethAddress := evm.GetEthAddress(key)
4646

47-
ginkgo.By("initializing a coreth client")
47+
tc.By("initializing a coreth client")
4848
node := privateNetwork.Nodes[0]
4949
nodeURI := tmpnet.NodeURI{
5050
NodeID: node.NodeID,
5151
URI: node.URI,
5252
}
53-
ethClient := e2e.NewEthClient(nodeURI)
53+
ethClient := e2e.NewEthClient(tc, nodeURI)
5454

55-
ginkgo.By("initializing a transaction signer")
56-
cChainID, err := ethClient.ChainID(e2e.DefaultContext())
55+
tc.By("initializing a transaction signer")
56+
cChainID, err := ethClient.ChainID(tc.DefaultContext())
5757
require.NoError(err)
5858
signer := types.NewEIP155Signer(cChainID)
5959
ecdsaKey := key.ToECDSA()
@@ -64,9 +64,9 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
6464
}
6565

6666
var contractAddress common.Address
67-
ginkgo.By("deploying an expensive contract", func() {
67+
tc.By("deploying an expensive contract", func() {
6868
// Create transaction
69-
nonce, err := ethClient.AcceptedNonceAt(e2e.DefaultContext(), ethAddress)
69+
nonce, err := ethClient.AcceptedNonceAt(tc.DefaultContext(), ethAddress)
7070
require.NoError(err)
7171
compiledContract := common.Hex2Bytes(hashingCompiledContract)
7272
tx := types.NewTx(&types.LegacyTx{
@@ -79,36 +79,36 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
7979

8080
// Send the transaction and wait for acceptance
8181
signedTx := sign(tx)
82-
receipt := e2e.SendEthTransaction(ethClient, signedTx)
82+
receipt := e2e.SendEthTransaction(tc, ethClient, signedTx)
8383

8484
contractAddress = receipt.ContractAddress
8585
})
8686

8787
var gasPrice *big.Int
88-
ginkgo.By("calling the expensive contract repeatedly until a gas price increase is detected", func() {
88+
tc.By("calling the expensive contract repeatedly until a gas price increase is detected", func() {
8989
// Evaluate the bytes representation of the contract
9090
hashingABI, err := abi.JSON(strings.NewReader(hashingABIJson))
9191
require.NoError(err)
9292
contractData, err := hashingABI.Pack("hashIt")
9393
require.NoError(err)
9494

9595
var initialGasPrice *big.Int
96-
e2e.Eventually(func() bool {
96+
tc.Eventually(func() bool {
9797
// Check the gas price
9898
var err error
99-
gasPrice, err = ethClient.SuggestGasPrice(e2e.DefaultContext())
99+
gasPrice, err = ethClient.SuggestGasPrice(tc.DefaultContext())
100100
require.NoError(err)
101101
if initialGasPrice == nil {
102102
initialGasPrice = gasPrice
103-
tests.Outf("{{blue}}initial gas price is %v{{/}}\n", initialGasPrice)
103+
tc.Outf("{{blue}}initial gas price is %v{{/}}\n", initialGasPrice)
104104
} else if gasPrice.Cmp(initialGasPrice) > 0 {
105105
// Gas price has increased
106-
tests.Outf("{{blue}}gas price has increased to %v{{/}}\n", gasPrice)
106+
tc.Outf("{{blue}}gas price has increased to %v{{/}}\n", gasPrice)
107107
return true
108108
}
109109

110110
// Create the transaction
111-
nonce, err := ethClient.AcceptedNonceAt(e2e.DefaultContext(), ethAddress)
111+
nonce, err := ethClient.AcceptedNonceAt(tc.DefaultContext(), ethAddress)
112112
require.NoError(err)
113113
tx := types.NewTx(&types.LegacyTx{
114114
Nonce: nonce,
@@ -121,33 +121,33 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
121121

122122
// Send the transaction and wait for acceptance
123123
signedTx := sign(tx)
124-
_ = e2e.SendEthTransaction(ethClient, signedTx)
124+
_ = e2e.SendEthTransaction(tc, ethClient, signedTx)
125125

126126
// The gas price will be checked at the start of the next iteration
127127
return false
128128
}, e2e.DefaultTimeout, e2e.DefaultPollingInterval, "failed to see gas price increase before timeout")
129129
})
130130

131-
ginkgo.By("waiting for the gas price to decrease...", func() {
131+
tc.By("waiting for the gas price to decrease...", func() {
132132
initialGasPrice := gasPrice
133-
e2e.Eventually(func() bool {
133+
tc.Eventually(func() bool {
134134
var err error
135-
gasPrice, err = ethClient.SuggestGasPrice(e2e.DefaultContext())
135+
gasPrice, err = ethClient.SuggestGasPrice(tc.DefaultContext())
136136
require.NoError(err)
137-
tests.Outf("{{blue}}.{{/}}")
137+
tc.Outf("{{blue}}.{{/}}")
138138
return initialGasPrice.Cmp(gasPrice) > 0
139139
}, e2e.DefaultTimeout, e2e.DefaultPollingInterval, "failed to see gas price decrease before timeout")
140-
tests.Outf("\n{{blue}}gas price has decreased to %v{{/}}\n", gasPrice)
140+
tc.Outf("\n{{blue}}gas price has decreased to %v{{/}}\n", gasPrice)
141141
})
142142

143-
ginkgo.By("sending funds at the current gas price", func() {
143+
tc.By("sending funds at the current gas price", func() {
144144
// Create a recipient address
145145
recipientKey, err := secp256k1.NewPrivateKey()
146146
require.NoError(err)
147147
recipientEthAddress := evm.GetEthAddress(recipientKey)
148148

149149
// Create transaction
150-
nonce, err := ethClient.AcceptedNonceAt(e2e.DefaultContext(), ethAddress)
150+
nonce, err := ethClient.AcceptedNonceAt(tc.DefaultContext(), ethAddress)
151151
require.NoError(err)
152152
tx := types.NewTx(&types.LegacyTx{
153153
Nonce: nonce,
@@ -159,9 +159,9 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
159159

160160
// Send the transaction and wait for acceptance
161161
signedTx := sign(tx)
162-
_ = e2e.SendEthTransaction(ethClient, signedTx)
162+
_ = e2e.SendEthTransaction(tc, ethClient, signedTx)
163163
})
164164

165-
_ = e2e.CheckBootstrapIsPossible(privateNetwork)
165+
_ = e2e.CheckBootstrapIsPossible(tc, privateNetwork)
166166
})
167167
})

0 commit comments

Comments
 (0)