Skip to content

Commit ca512a2

Browse files
committed
[tmpnet] Refactor runtime configuration in preparation for kube
1 parent b58e9b8 commit ca512a2

File tree

16 files changed

+309
-188
lines changed

16 files changed

+309
-188
lines changed

tests/antithesis/compose.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ func GenerateComposeConfig(network *tmpnet.Network, baseImageName string) error
6161
return errPluginDirEnvVarNotSet
6262
}
6363

64+
network.DefaultRuntimeConfig = tmpnet.NodeRuntimeConfig{
65+
Process: &tmpnet.ProcessRuntimeConfig{
66+
AvalancheGoPath: avalancheGoPath,
67+
PluginDir: pluginDir,
68+
},
69+
}
70+
6471
bootstrapVolumePath, err := getBootstrapVolumePath(targetPath)
6572
if err != nil {
6673
return fmt.Errorf("failed to get bootstrap volume path: %w", err)
6774
}
6875

69-
if err := initBootstrapDB(network, avalancheGoPath, pluginDir, bootstrapVolumePath); err != nil {
76+
if err := initBootstrapDB(network, bootstrapVolumePath); err != nil {
7077
return fmt.Errorf("failed to initialize db volumes: %w", err)
7178
}
7279
}

tests/antithesis/init_db.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ func getBootstrapVolumePath(targetPath string) (string, error) {
2828
// Bootstraps a local process-based network, creates its subnets and chains, and copies
2929
// the resulting db state from one of the nodes to the provided path. The path will be
3030
// created if it does not already exist.
31-
func initBootstrapDB(network *tmpnet.Network, avalancheGoPath string, pluginDir string, destPath string) error {
31+
func initBootstrapDB(network *tmpnet.Network, destPath string) error {
3232
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
3333
defer cancel()
3434
if err := tmpnet.BootstrapNewNetwork(
3535
ctx,
3636
tests.NewDefaultLogger(""),
3737
network,
3838
"",
39-
avalancheGoPath,
40-
pluginDir,
4139
); err != nil {
4240
return fmt.Errorf("failed to bootstrap network: %w", err)
4341
}

tests/fixture/e2e/env.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/stretchr/testify/require"
1616
"go.uber.org/zap"
1717

18-
"github.com/ava-labs/avalanchego/config"
1918
"github.com/ava-labs/avalanchego/tests"
2019
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
2120
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
@@ -154,14 +153,13 @@ func NewTestEnvironment(tc tests.TestContext, flagVars *FlagVars, desiredNetwork
154153
// Start a new network
155154
if network == nil {
156155
network = desiredNetwork
157-
avalancheBinaryPath, err := flagVars.AvalancheGoExecPath()
156+
runtimeConfig, err := flagVars.NodeRuntimeConfig()
158157
require.NoError(err)
158+
network.DefaultRuntimeConfig = *runtimeConfig
159159

160160
StartNetwork(
161161
tc,
162162
network,
163-
avalancheBinaryPath,
164-
flagVars.PluginDir(),
165163
flagVars.NetworkShutdownDelay(),
166164
flagVars.StartNetwork(),
167165
flagVars.ReuseNetwork(),
@@ -228,15 +226,11 @@ func (te *TestEnvironment) StartPrivateNetwork(network *tmpnet.Network) {
228226
// Use the same configuration as the shared network
229227
sharedNetwork, err := tmpnet.ReadNetwork(te.NetworkDir)
230228
require.NoError(err)
231-
232-
pluginDir, err := sharedNetwork.DefaultFlags.GetStringVal(config.PluginDirKey)
233-
require.NoError(err)
229+
network.DefaultRuntimeConfig = sharedNetwork.DefaultRuntimeConfig
234230

235231
StartNetwork(
236232
te.testContext,
237233
network,
238-
sharedNetwork.DefaultRuntimeConfig.AvalancheGoPath,
239-
pluginDir,
240234
te.PrivateNetworkShutdownDelay,
241235
false, /* skipShutdown */
242236
false, /* reuseNetwork */

tests/fixture/e2e/flags.go

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,29 @@ import (
77
"flag"
88
"fmt"
99
"os"
10-
"path/filepath"
1110
"time"
1211

1312
"github.com/spf13/cast"
1413

1514
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
15+
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet/flags"
1616
)
1717

1818
type FlagVars struct {
19-
avalancheGoExecPath string
20-
pluginDir string
21-
networkDir string
22-
reuseNetwork bool
23-
startCollectors bool
24-
checkMonitoring bool
25-
startNetwork bool
26-
stopNetwork bool
27-
restartNetwork bool
28-
nodeCount int
29-
activateFortuna bool
19+
runtimeConfigVars *flags.RuntimeConfigVars
20+
networkDir string
21+
reuseNetwork bool
22+
startCollectors bool
23+
checkMonitoring bool
24+
startNetwork bool
25+
stopNetwork bool
26+
restartNetwork bool
27+
nodeCount int
28+
activateFortuna bool
3029
}
3130

32-
func (v *FlagVars) AvalancheGoExecPath() (string, error) {
33-
if err := v.validateAvalancheGoExecPath(); err != nil {
34-
return "", err
35-
}
36-
return v.avalancheGoExecPath, nil
37-
}
38-
39-
func (v *FlagVars) validateAvalancheGoExecPath() error {
40-
if !filepath.IsAbs(v.avalancheGoExecPath) {
41-
absPath, err := filepath.Abs(v.avalancheGoExecPath)
42-
if err != nil {
43-
return fmt.Errorf("avalanchego-path (%s) is a relative path but its absolute path cannot be determined: %w",
44-
v.avalancheGoExecPath, err)
45-
}
46-
47-
// If the absolute path file doesn't exist, it means it won't work out of the box.
48-
if _, err := os.Stat(absPath); err != nil {
49-
return fmt.Errorf("avalanchego-path (%s) is a relative path but must be an absolute path", v.avalancheGoExecPath)
50-
}
51-
}
52-
return nil
53-
}
54-
55-
func (v *FlagVars) PluginDir() string {
56-
return v.pluginDir
31+
func (v *FlagVars) NodeRuntimeConfig() (*tmpnet.NodeRuntimeConfig, error) {
32+
return v.runtimeConfigVars.GetNodeRuntimeConfig()
5733
}
5834

5935
func (v *FlagVars) NetworkDir() string {
@@ -109,24 +85,7 @@ func (v *FlagVars) ActivateFortuna() bool {
10985

11086
func RegisterFlags() *FlagVars {
11187
vars := FlagVars{}
112-
flag.StringVar(
113-
&vars.avalancheGoExecPath,
114-
"avalanchego-path",
115-
os.Getenv(tmpnet.AvalancheGoPathEnvName),
116-
fmt.Sprintf(
117-
"[optional] avalanchego executable path if creating a new network. Also possible to configure via the %s env variable.",
118-
tmpnet.AvalancheGoPathEnvName,
119-
),
120-
)
121-
flag.StringVar(
122-
&vars.pluginDir,
123-
"plugin-dir",
124-
tmpnet.GetEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
125-
fmt.Sprintf(
126-
"[optional] the dir containing VM plugins. Also possible to configure via the %s env variable.",
127-
tmpnet.AvalancheGoPluginDirEnvName,
128-
),
129-
)
88+
vars.runtimeConfigVars = flags.NewRuntimeConfigFlagVars()
13089
flag.StringVar(
13190
&vars.networkDir,
13291
"network-dir",

tests/fixture/e2e/helpers.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ func CheckBootstrapIsPossible(tc tests.TestContext, network *tmpnet.Network) *tm
271271
func StartNetwork(
272272
tc tests.TestContext,
273273
network *tmpnet.Network,
274-
avalancheGoExecPath string,
275-
pluginDir string,
276274
shutdownDelay time.Duration,
277275
skipShutdown bool,
278276
reuseNetwork bool,
@@ -284,8 +282,6 @@ func StartNetwork(
284282
tc.Log(),
285283
network,
286284
DefaultNetworkDir,
287-
avalancheGoExecPath,
288-
pluginDir,
289285
)
290286
if err != nil {
291287
// Ensure nodes are stopped if bootstrap fails. The network configuration

tests/fixture/tmpnet/README.md

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,31 @@ repositories.
5757
The functionality in this package is grouped by logical purpose into
5858
the following non-test files:
5959

60-
| Filename | Types | Purpose |
61-
|:----------------------------|:------------|:------------------------------------------------------------|
62-
| check_monitoring.go | | Enables checking if logs and metrics were collected |
63-
| defaults.go | | Defines common default configuration |
64-
| detached_process_default.go | | Configures detached processes for darwin and linux |
65-
| detached_process_windows.go | | No-op detached process configuration for windows |
66-
| flags.go | FlagsMap | Simplifies configuration of avalanchego flags |
67-
| genesis.go | | Creates test genesis |
68-
| kube.go | | Library for Kubernetes interaction |
69-
| local_network.go | | Defines configuration for the default local network |
70-
| monitor_processes.go | | Enables collection of logs and metrics from local processes |
71-
| network.go | Network | Orchestrates and configures temporary networks |
72-
| network_config.go | Network | Reads and writes network configuration |
73-
| network_test.go | | Simple test round-tripping Network serialization |
74-
| node.go | Node | Orchestrates and configures nodes |
75-
| node_config.go | Node | Reads and writes node configuration |
76-
| node_process.go | NodeProcess | Orchestrates node processes |
77-
| start_kind_cluster.go | | Starts a local kind cluster |
78-
| subnet.go | Subnet | Orchestrates subnets |
79-
| utils.go | | Defines shared utility functions |
60+
| Filename | Types | Purpose |
61+
|:----------------------------|:------------|:-----------------------------------------------------------------------|
62+
| flags/ | | Directory defining flags usable with both stdlib flags and spf13/pflag |
63+
| flags/common.go | | Defines type definitions common across other files |
64+
| flags/process_runtime.go | | Defines flags configuring the process node runtime |
65+
| flags/runtime.go | | Defines flags configuring node runtime |
66+
| tmpnetctl/ | | Directory containing main entrypoint for tmpnetctl command |
67+
| check_monitoring.go | | Enables checking if logs and metrics were collected |
68+
| defaults.go | | Defines common default configuration |
69+
| detached_process_default.go | | Configures detached processes for darwin and linux |
70+
| detached_process_windows.go | | No-op detached process configuration for windows |
71+
| flags.go | FlagsMap | Simplifies configuration of avalanchego flags |
72+
| genesis.go | | Creates test genesis |
73+
| kube.go | | Library for Kubernetes interaction |
74+
| local_network.go | | Defines configuration for the default local network |
75+
| monitor_processes.go | | Enables collection of logs and metrics from local processes |
76+
| network.go | Network | Orchestrates and configures temporary networks |
77+
| network_config.go | Network | Reads and writes network configuration |
78+
| network_test.go | | Simple test round-tripping Network serialization |
79+
| node.go | Node | Orchestrates and configures nodes |
80+
| node_config.go | Node | Reads and writes node configuration |
81+
| node_process.go | NodeProcess | Orchestrates node processes |
82+
| start_kind_cluster.go | | Starts a local kind cluster |
83+
| subnet.go | Subnet | Orchestrates subnets |
84+
| utils.go | | Defines shared utility functions |
8085

8186
## Usage
8287

@@ -133,17 +138,19 @@ flags.
133138
A temporary network can be managed in code:
134139

135140
```golang
136-
network := &tmpnet.Network{ // Configure non-default values for the new network
141+
network := &tmpnet.Network{ // Configure non-default values for the new network
137142
DefaultRuntimeConfig{
138-
ReuseDynamicPorts: true, // Configure process-based nodes to reuse a dynamically allocated API port when restarting
139-
},
143+
Process: &tmpnet.ProcessRuntimeConfig{
144+
ReuseDynamicPorts: true, // Configure process-based nodes to reuse a dynamically allocated API port when restarting
145+
},
146+
}
140147
DefaultFlags: tmpnet.FlagsMap{
141-
config.LogLevelKey: "INFO", // Change one of the network's defaults
148+
config.LogLevelKey: "INFO", // Change one of the network's defaults
142149
},
143-
Nodes: tmpnet.NewNodesOrPanic(5), // Number of initial validating nodes
144-
Subnets: []*tmpnet.Subnet{ // Subnets to create on the new network once it is running
150+
Nodes: tmpnet.NewNodesOrPanic(5), // Number of initial validating nodes
151+
Subnets: []*tmpnet.Subnet{ // Subnets to create on the new network once it is running
145152
{
146-
Name: "xsvm-a", // User-defined name used to reference subnet in code and on disk
153+
Name: "xsvm-a", // User-defined name used to reference subnet in code and on disk
147154
Chains: []*tmpnet.Chain{
148155
{
149156
VMName: "xsvm", // Name of the VM the chain will run, will be used to derive the name of the VM binary

tests/fixture/tmpnet/flags/common.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package flags
5+
6+
// The following function signatures are common across flag and
7+
// spf13/pflag. They can be used to define a single registration method that
8+
// supports both flag libraries.
9+
10+
type stringVarFunc func(p *string, name string, value string, usage string)
11+
12+
type boolVarFunc func(p *bool, name string, value bool, usage string)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package flags
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"os"
10+
"path/filepath"
11+
12+
"github.com/spf13/pflag"
13+
14+
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
15+
)
16+
17+
const (
18+
processRuntime = "process"
19+
processDocPrefix = "[process runtime] "
20+
21+
avalanchegoPathFlag = "avalanchego-path"
22+
)
23+
24+
var errAvalancheGoRequired = fmt.Errorf("--%s or %s are required", avalanchegoPathFlag, tmpnet.AvalancheGoPathEnvName)
25+
26+
type processRuntimeVars struct {
27+
config tmpnet.ProcessRuntimeConfig
28+
}
29+
30+
func (v *processRuntimeVars) registerWithFlag() {
31+
v.register(flag.StringVar, flag.BoolVar)
32+
}
33+
34+
func (v *processRuntimeVars) registerWithFlagSet(flagSet *pflag.FlagSet) {
35+
v.register(flagSet.StringVar, flagSet.BoolVar)
36+
}
37+
38+
func (v *processRuntimeVars) register(stringVar stringVarFunc, boolVar boolVarFunc) {
39+
stringVar(
40+
&v.config.AvalancheGoPath,
41+
avalanchegoPathFlag,
42+
os.Getenv(tmpnet.AvalancheGoPathEnvName),
43+
processDocPrefix+fmt.Sprintf(
44+
"The avalanchego executable path. Also possible to configure via the %s env variable.",
45+
tmpnet.AvalancheGoPathEnvName,
46+
),
47+
)
48+
stringVar(
49+
&v.config.PluginDir,
50+
"plugin-dir",
51+
tmpnet.GetEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
52+
processDocPrefix+fmt.Sprintf(
53+
"The dir containing VM plugins. Also possible to configure via the %s env variable.",
54+
tmpnet.AvalancheGoPluginDirEnvName,
55+
),
56+
)
57+
boolVar(
58+
&v.config.ReuseDynamicPorts,
59+
"reuse-dynamic-ports",
60+
false,
61+
processDocPrefix+"Whether to attempt to reuse dynamically allocated ports across node restarts.",
62+
)
63+
}
64+
65+
func (v *processRuntimeVars) validate() error {
66+
path := v.config.AvalancheGoPath
67+
68+
if len(path) == 0 {
69+
return errAvalancheGoRequired
70+
}
71+
72+
if filepath.IsAbs(path) {
73+
if _, err := os.Stat(path); err != nil {
74+
return fmt.Errorf("--%s (%s) not found: %w", avalanchegoPathFlag, path, err)
75+
}
76+
return nil
77+
}
78+
79+
// A relative path must be resolvable to an absolute path
80+
absPath, err := filepath.Abs(path)
81+
if err != nil {
82+
return fmt.Errorf(
83+
"--%s (%s) is a relative path but its absolute path cannot be determined: %w",
84+
avalanchegoPathFlag,
85+
path,
86+
err,
87+
)
88+
}
89+
90+
// The absolute path must exist
91+
if _, err := os.Stat(absPath); err != nil {
92+
return fmt.Errorf("--%s (%s) is a relative path but must be an absolute path", avalanchegoPathFlag, path)
93+
}
94+
return nil
95+
}

0 commit comments

Comments
 (0)