Skip to content

Commit bf50ecf

Browse files
authored
[tmpnet] Ensure tmpnet methods always have a logger (#3893)
1 parent 0554b91 commit bf50ecf

File tree

9 files changed

+55
-43
lines changed

9 files changed

+55
-43
lines changed

tests/e2e/e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,5 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
7979
// Run in every ginkgo process
8080

8181
// Initialize the local test environment from the global state
82-
e2e.InitSharedTestEnvironment(ginkgo.GinkgoT(), envBytes)
82+
e2e.InitSharedTestEnvironment(e2e.NewTestContext(), envBytes)
8383
})

tests/fixture/e2e/env.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import (
2626
// access to the shared env to GetEnv which adds a test context.
2727
var env *TestEnvironment
2828

29-
func InitSharedTestEnvironment(t require.TestingT, envBytes []byte) {
30-
require := require.New(t)
29+
func InitSharedTestEnvironment(tc tests.TestContext, envBytes []byte) {
30+
require := require.New(tc)
3131
require.Nil(env, "env already initialized")
3232
env = &TestEnvironment{}
3333
require.NoError(json.Unmarshal(envBytes, env))
34+
env.testContext = tc
3435

3536
// Ginkgo parallelization is at the process level, so a given key
3637
// can safely be used by all tests in a given process without fear
@@ -126,7 +127,7 @@ func NewTestEnvironment(tc tests.TestContext, flagVars *FlagVars, desiredNetwork
126127

127128
if len(networkDir) > 0 {
128129
var err error
129-
network, err = tmpnet.ReadNetwork(networkDir)
130+
network, err = tmpnet.ReadNetwork(tc.Log(), networkDir)
130131
require.NoError(err)
131132
tc.Log().Info("loaded a network",
132133
zap.String("networkDir", networkDir),
@@ -153,7 +154,7 @@ func NewTestEnvironment(tc tests.TestContext, flagVars *FlagVars, desiredNetwork
153154
}
154155

155156
if network != nil && networkCmd == RestartNetworkCmd {
156-
require.NoError(network.Restart(tc.DefaultContext(), tc.Log()))
157+
require.NoError(network.Restart(tc.DefaultContext()))
157158
}
158159
}
159160

@@ -221,8 +222,9 @@ func (te *TestEnvironment) GetRandomNodeURI() tmpnet.NodeURI {
221222

222223
// Retrieve the network to target for testing.
223224
func (te *TestEnvironment) GetNetwork() *tmpnet.Network {
224-
network, err := tmpnet.ReadNetwork(te.NetworkDir)
225-
require.NoError(te.testContext, err)
225+
tc := te.testContext
226+
network, err := tmpnet.ReadNetwork(tc.Log(), te.NetworkDir)
227+
require.NoError(tc, err)
226228
return network
227229
}
228230

@@ -233,14 +235,15 @@ func (te *TestEnvironment) NewKeychain() *secp256k1fx.Keychain {
233235

234236
// Create a new private network that is not shared with other tests.
235237
func (te *TestEnvironment) StartPrivateNetwork(network *tmpnet.Network) {
236-
require := require.New(te.testContext)
238+
tc := te.testContext
239+
require := require.New(tc)
237240
// Use the same configuration as the shared network
238-
sharedNetwork, err := tmpnet.ReadNetwork(te.NetworkDir)
241+
sharedNetwork, err := tmpnet.ReadNetwork(tc.Log(), te.NetworkDir)
239242
require.NoError(err)
240243
network.DefaultRuntimeConfig = sharedNetwork.DefaultRuntimeConfig
241244

242245
StartNetwork(
243-
te.testContext,
246+
tc,
244247
network,
245248
te.RootNetworkDir,
246249
te.PrivateNetworkShutdownDelay,

tests/fixture/e2e/helpers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func NewEthClient(tc tests.TestContext, nodeURI tmpnet.NodeURI) ethclient.Client
136136
func AddEphemeralNode(tc tests.TestContext, network *tmpnet.Network, node *tmpnet.Node) *tmpnet.Node {
137137
require := require.New(tc)
138138

139-
require.NoError(network.StartNode(tc.DefaultContext(), tc.Log(), node))
139+
require.NoError(network.StartNode(tc.DefaultContext(), node))
140140

141141
tc.DeferCleanup(func() {
142142
tc.Log().Info("shutting down ephemeral node",
@@ -236,7 +236,7 @@ func CheckBootstrapIsPossible(tc tests.TestContext, network *tmpnet.Network) *tm
236236
}
237237

238238
node := tmpnet.NewEphemeralNode(flags)
239-
require.NoError(network.StartNode(tc.DefaultContext(), tc.Log(), node))
239+
require.NoError(network.StartNode(tc.DefaultContext(), node))
240240
// StartNode will initiate node stop if an error is encountered during start,
241241
// so no further cleanup effort is required if an error is seen here.
242242

tests/fixture/tmpnet/network.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ type Network struct {
128128

129129
// Subnets that have been enabled on the network
130130
Subnets []*Subnet
131+
132+
log logging.Logger
131133
}
132134

133135
func NewDefaultNetwork(owner string) *Network {
@@ -172,8 +174,8 @@ func BootstrapNewNetwork(
172174
}
173175

174176
// Stops the nodes of the network configured in the provided directory.
175-
func StopNetwork(ctx context.Context, dir string) error {
176-
network, err := ReadNetwork(dir)
177+
func StopNetwork(ctx context.Context, log logging.Logger, dir string) error {
178+
network, err := ReadNetwork(log, dir)
177179
if err != nil {
178180
return err
179181
}
@@ -182,21 +184,22 @@ func StopNetwork(ctx context.Context, dir string) error {
182184

183185
// Restarts the nodes of the network configured in the provided directory.
184186
func RestartNetwork(ctx context.Context, log logging.Logger, dir string) error {
185-
network, err := ReadNetwork(dir)
187+
network, err := ReadNetwork(log, dir)
186188
if err != nil {
187189
return err
188190
}
189-
return network.Restart(ctx, log)
191+
return network.Restart(ctx)
190192
}
191193

192194
// Reads a network from the provided directory.
193-
func ReadNetwork(dir string) (*Network, error) {
195+
func ReadNetwork(log logging.Logger, dir string) (*Network, error) {
194196
canonicalDir, err := toCanonicalDir(dir)
195197
if err != nil {
196198
return nil, err
197199
}
198200
network := &Network{
199201
Dir: canonicalDir,
202+
log: log,
200203
}
201204
if err := network.Read(); err != nil {
202205
return nil, fmt.Errorf("failed to read network: %w", err)
@@ -213,6 +216,8 @@ func (n *Network) EnsureDefaultConfig(log logging.Logger) error {
213216
zap.Any("runtimeConfig", n.DefaultRuntimeConfig),
214217
)
215218

219+
n.log = log
220+
216221
// A UUID supports centralized metrics collection
217222
if len(n.UUID) == 0 {
218223
n.UUID = uuid.NewString()
@@ -342,9 +347,8 @@ func (n *Network) StartNodes(ctx context.Context, log logging.Logger, nodesToSta
342347
// Record the time before nodes are started to ensure visibility of subsequently collected metrics via the emitted link
343348
startTime := time.Now()
344349

345-
// Configure the networking for each node and start
346350
for _, node := range nodesToStart {
347-
if err := n.StartNode(ctx, log, node); err != nil {
351+
if err := n.StartNode(ctx, node); err != nil {
348352
return err
349353
}
350354
}
@@ -438,7 +442,7 @@ func (n *Network) Bootstrap(ctx context.Context, log logging.Logger) error {
438442

439443
if len(n.Nodes) == 1 {
440444
// Ensure the node is restarted to pick up subnet and chain configuration
441-
return n.RestartNode(ctx, log, bootstrapNode)
445+
return n.RestartNode(ctx, bootstrapNode)
442446
}
443447

444448
// TODO(marun) This last restart of the bootstrap node might be unnecessary if:
@@ -450,7 +454,7 @@ func (n *Network) Bootstrap(ctx context.Context, log logging.Logger) error {
450454
if err := bootstrapNode.Stop(ctx); err != nil {
451455
return fmt.Errorf("failed to stop node %s: %w", bootstrapNode.NodeID, err)
452456
}
453-
if err := n.StartNode(ctx, log, bootstrapNode); err != nil {
457+
if err := n.StartNode(ctx, bootstrapNode); err != nil {
454458
return fmt.Errorf("failed to start node %s: %w", bootstrapNode.NodeID, err)
455459
}
456460

@@ -459,19 +463,19 @@ func (n *Network) Bootstrap(ctx context.Context, log logging.Logger) error {
459463
}
460464

461465
// Starts the provided node after configuring it for the network.
462-
func (n *Network) StartNode(ctx context.Context, log logging.Logger, node *Node) error {
466+
func (n *Network) StartNode(ctx context.Context, node *Node) error {
463467
if err := n.EnsureNodeConfig(node); err != nil {
464468
return err
465469
}
466470
if err := node.Write(); err != nil {
467471
return err
468472
}
469473

470-
if err := n.writeNodeFlags(log, node); err != nil {
474+
if err := n.writeNodeFlags(node); err != nil {
471475
return fmt.Errorf("writing node flags: %w", err)
472476
}
473477

474-
if err := node.Start(log); err != nil {
478+
if err := node.Start(); err != nil {
475479
// Attempt to stop an unhealthy node to provide some assurance to the caller
476480
// that an error condition will not result in a lingering process.
477481
err = errors.Join(err, node.Stop(ctx))
@@ -482,7 +486,7 @@ func (n *Network) StartNode(ctx context.Context, log logging.Logger, node *Node)
482486
}
483487

484488
// Restart a single node.
485-
func (n *Network) RestartNode(ctx context.Context, log logging.Logger, node *Node) error {
489+
func (n *Network) RestartNode(ctx context.Context, node *Node) error {
486490
runtimeConfig := node.getRuntimeConfig()
487491
if runtimeConfig.Process != nil && runtimeConfig.Process.ReuseDynamicPorts {
488492
// Attempt to save the API port currently being used so the
@@ -497,10 +501,10 @@ func (n *Network) RestartNode(ctx context.Context, log logging.Logger, node *Nod
497501
if err := node.Stop(ctx); err != nil {
498502
return fmt.Errorf("failed to stop node %s: %w", node.NodeID, err)
499503
}
500-
if err := n.StartNode(ctx, log, node); err != nil {
504+
if err := n.StartNode(ctx, node); err != nil {
501505
return fmt.Errorf("failed to start node %s: %w", node.NodeID, err)
502506
}
503-
log.Info("waiting for node to report healthy",
507+
n.log.Info("waiting for node to report healthy",
504508
zap.Stringer("nodeID", node.NodeID),
505509
)
506510
return WaitForHealthy(ctx, node)
@@ -535,11 +539,11 @@ func (n *Network) Stop(ctx context.Context) error {
535539
return nil
536540
}
537541

538-
// Restarts all non-ephemeral nodes in the network.
539-
func (n *Network) Restart(ctx context.Context, log logging.Logger) error {
540-
log.Info("restarting network")
542+
// Restarts all nodes in the network.
543+
func (n *Network) Restart(ctx context.Context) error {
544+
n.log.Info("restarting network")
541545
for _, node := range n.Nodes {
542-
if err := n.RestartNode(ctx, log, node); err != nil {
546+
if err := n.RestartNode(ctx, node); err != nil {
543547
return err
544548
}
545549
}
@@ -669,7 +673,7 @@ func (n *Network) CreateSubnets(ctx context.Context, log logging.Logger, apiURI
669673
// Only running nodes should be restarted
670674
continue
671675
}
672-
if err := n.RestartNode(ctx, log, node); err != nil {
676+
if err := n.RestartNode(ctx, node); err != nil {
673677
return err
674678
}
675679
}
@@ -737,7 +741,7 @@ func (n *Network) CreateSubnets(ctx context.Context, log logging.Logger, apiURI
737741
if !validatorsToRestart.Contains(node.NodeID) {
738742
continue
739743
}
740-
if err := n.RestartNode(ctx, log, node); err != nil {
744+
if err := n.RestartNode(ctx, node); err != nil {
741745
return err
742746
}
743747
}
@@ -877,7 +881,7 @@ func (n *Network) GetChainConfigContent() (string, error) {
877881

878882
// writeNodeFlags determines the set of flags that should be used to
879883
// start the given node and writes them to a file in the node path.
880-
func (n *Network) writeNodeFlags(log logging.Logger, node *Node) error {
884+
func (n *Network) writeNodeFlags(node *Node) error {
881885
flags := maps.Clone(node.Flags)
882886

883887
// Convert the network id to a string to ensure consistency in JSON round-tripping.
@@ -902,7 +906,7 @@ func (n *Network) writeNodeFlags(log logging.Logger, node *Node) error {
902906

903907
isSingleNodeNetwork := (len(n.Nodes) == 1 && len(n.Genesis.InitialStakers) == 1)
904908
if isSingleNodeNetwork {
905-
log.Info("defaulting to sybil protection disabled to enable a single-node network to start")
909+
n.log.Info("defaulting to sybil protection disabled to enable a single-node network to start")
906910
flags.SetDefault(config.SybilProtectionEnabledKey, "false")
907911
}
908912
}

tests/fixture/tmpnet/network_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestNetworkSerialization(t *testing.T) {
2626
// Ensure node runtime is initialized
2727
require.NoError(network.readNodes())
2828

29-
loadedNetwork, err := ReadNetwork(network.Dir)
29+
loadedNetwork, err := ReadNetwork(logging.NoLog{}, network.Dir)
3030
require.NoError(err)
3131
for _, key := range loadedNetwork.PreFundedKeys {
3232
// Address() enables comparison with the original network by

tests/fixture/tmpnet/node.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/ava-labs/avalanchego/ids"
1919
"github.com/ava-labs/avalanchego/staking"
2020
"github.com/ava-labs/avalanchego/utils/crypto/bls/signer/localsigner"
21-
"github.com/ava-labs/avalanchego/utils/logging"
2221
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
2322
)
2423

@@ -40,8 +39,8 @@ type NodeRuntime interface {
4039
readState() error
4140
GetLocalURI(ctx context.Context) (string, func(), error)
4241
GetLocalStakingAddress(ctx context.Context) (netip.AddrPort, func(), error)
43-
Start(log logging.Logger) error
4442
InitiateStop() error
43+
Start() error
4544
WaitForStopped(ctx context.Context) error
4645
IsHealthy(ctx context.Context) (bool, error)
4746
}
@@ -141,8 +140,8 @@ func (n *Node) IsHealthy(ctx context.Context) (bool, error) {
141140
return n.getRuntime().IsHealthy(ctx)
142141
}
143142

144-
func (n *Node) Start(log logging.Logger) error {
145-
return n.getRuntime().Start(log)
143+
func (n *Node) Start() error {
144+
return n.getRuntime().Start()
146145
}
147146

148147
func (n *Node) InitiateStop(ctx context.Context) error {

tests/fixture/tmpnet/process_runtime.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ func (p *ProcessRuntime) readState() error {
8181
// its staking port. The network will start faster with this
8282
// synchronization due to the avoidance of exponential backoff
8383
// if a node tries to connect to a beacon that is not ready.
84-
func (p *ProcessRuntime) Start(log logging.Logger) error {
84+
func (p *ProcessRuntime) Start() error {
85+
log := p.node.network.log
86+
8587
// Avoid attempting to start an already running node.
8688
proc, err := p.getProcess()
8789
if err != nil {

tests/fixture/tmpnet/tmpnetctl/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ func main() {
121121
}
122122
ctx, cancel := context.WithTimeout(context.Background(), tmpnet.DefaultNetworkTimeout)
123123
defer cancel()
124-
if err := tmpnet.StopNetwork(ctx, networkDir); err != nil {
124+
log, err := tests.LoggerForFormat("", rawLogFormat)
125+
if err != nil {
126+
return err
127+
}
128+
if err := tmpnet.StopNetwork(ctx, log, networkDir); err != nil {
125129
return err
126130
}
127131
fmt.Fprintf(os.Stdout, "Stopped network configured at: %s\n", networkDir)

tests/upgrade/upgrade_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ var _ = ginkgo.Describe("[Upgrade]", func() {
9999
},
100100
}
101101

102-
require.NoError(network.StartNode(tc.DefaultContext(), tc.Log(), node))
102+
require.NoError(network.StartNode(tc.DefaultContext(), node))
103103

104104
tc.By(fmt.Sprintf("waiting for node %q to report healthy after restart", node.NodeID))
105105
e2e.WaitForHealthy(tc, node)

0 commit comments

Comments
 (0)