Skip to content

Commit ab46198

Browse files
marunmaru-ava
andauthored
[testing] Support direnv to simplify usage of test tooling (#3651)
Co-authored-by: Maru Newby <[email protected]>
1 parent 6077958 commit ab46198

16 files changed

+87
-38
lines changed

.envrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Repo-local commands like ginkgo and tmpnetctl
2+
PATH_add bin
3+
4+
# Built binaries like avalanchego and xsvm
5+
PATH_add build
6+
7+
# Configure the explicit built path of avalanchego for tmpnet usage
8+
export AVALANCHEGO_PATH=$PWD/build/avalanchego
9+
10+
# Configure the local plugin directory for both avalanchego and tmpnet usage
11+
mkdir -p $PWD/build/plugins # avalanchego will FATAL if the directory does not exist
12+
export AVAGO_PLUGIN_DIR="${AVAGO_PLUGIN_DIR:-$PWD/build/plugins}" # Use an existing value if set

bin/ginkgo

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Ensure the go command is run from the root of the repository so that its go.mod file is used
6+
AVALANCHE_PATH=$(cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )
7+
cd "${AVALANCHE_PATH}"
8+
9+
# If an explicit version is not specified, go run uses the ginkgo version from go.mod
10+
go run github.com/onsi/ginkgo/v2/ginkgo "${@}"

scripts/build_antithesis_images.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ else
8585
echo "Generating compose configuration for ${TEST_SETUP}"
8686
gen_antithesis_compose_config "${IMAGE_TAG}" "${AVALANCHE_PATH}/tests/antithesis/xsvm/gencomposeconfig" \
8787
"${AVALANCHE_PATH}/build/antithesis/xsvm" \
88-
"AVALANCHEGO_PATH=${AVALANCHE_PATH}/build/avalanchego AVALANCHEGO_PLUGIN_DIR=${HOME}/.avalanchego/plugins"
88+
"AVALANCHEGO_PATH=${AVALANCHE_PATH}/build/avalanchego AVAGO_PLUGIN_DIR=${AVALANCHE_PATH}/build/plugins"
8989

9090
build_antithesis_images_for_avalanchego "${TEST_SETUP}" "${IMAGE_PREFIX}" "${AVALANCHE_PATH}/vms/example/xsvm/Dockerfile"
9191
fi

scripts/build_xsvm.sh

+11-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ source ./scripts/constants.sh
1212
echo "Building xsvm plugin..."
1313
go build -o ./build/xsvm ./vms/example/xsvm/cmd/xsvm/
1414

15-
PLUGIN_DIR="$HOME/.avalanchego/plugins"
16-
PLUGIN_PATH="${PLUGIN_DIR}/v3m4wPxaHpvGr8qfMeyK6PRW3idZrPHmYcMTt7oXdK47yurVH"
17-
echo "Symlinking ./build/xsvm to ${PLUGIN_PATH}"
18-
mkdir -p "${PLUGIN_DIR}"
19-
ln -sf "${PWD}/build/xsvm" "${PLUGIN_PATH}"
15+
# Symlink to both global and local plugin directories to simplify
16+
# usage for testing. The local directory should be preferred but the
17+
# global directory remains supported for backwards compatibility.
18+
LOCAL_PLUGIN_PATH="${PWD}/build/plugins"
19+
GLOBAL_PLUGIN_PATH="${HOME}/.avalanchego/plugins"
20+
for plugin_dir in "${GLOBAL_PLUGIN_PATH}" "${LOCAL_PLUGIN_PATH}"; do
21+
PLUGIN_PATH="${plugin_dir}/v3m4wPxaHpvGr8qfMeyK6PRW3idZrPHmYcMTt7oXdK47yurVH"
22+
echo "Symlinking ./build/xsvm to ${PLUGIN_PATH}"
23+
mkdir -p "${plugin_dir}"
24+
ln -sf "${PWD}/build/xsvm" "${PLUGIN_PATH}"
25+
done

scripts/ginkgo.sh

-6
This file was deleted.

scripts/tests.e2e.bootstrap_monitor.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ ensure_command "kind-with-registry.sh" "https://raw.githubusercontent.com/kubern
4545
# call them without a qualifying path.
4646
PATH="${PWD}/bin:$PATH" bash -x "${PWD}/bin/kind-with-registry.sh"
4747

48-
KUBECONFIG="$HOME/.kube/config" PATH="${PWD}/bin:$PATH" ./scripts/ginkgo.sh -v ./tests/fixture/bootstrapmonitor/e2e
48+
KUBECONFIG="$HOME/.kube/config" PATH="${PWD}/bin:$PATH" ./bin/ginkgo -v ./tests/fixture/bootstrapmonitor/e2e

scripts/tests.e2e.existing.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function print_separator {
2222
function cleanup {
2323
print_separator
2424
echo "cleaning up reusable network"
25-
./scripts/ginkgo.sh -v ./tests/e2e -- --stop-network
25+
./bin/ginkgo -v ./tests/e2e -- --stop-network
2626
}
2727
trap cleanup EXIT
2828

scripts/tests.e2e.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ fi
5959

6060
#################################
6161
# shellcheck disable=SC2086
62-
./scripts/ginkgo.sh ${GINKGO_ARGS} -v ./tests/e2e -- "${E2E_ARGS[@]}" "${@}"
62+
./bin/ginkgo ${GINKGO_ARGS} -v ./tests/e2e -- "${E2E_ARGS[@]}" "${@}"

scripts/tests.upgrade.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ source ./scripts/constants.sh
6565
#################################
6666
# By default, it runs all upgrade test cases!
6767
echo "running upgrade tests against the local cluster with ${AVALANCHEGO_PATH}"
68-
./scripts/ginkgo.sh -v ./tests/upgrade -- \
68+
./bin/ginkgo -v ./tests/upgrade -- \
6969
--avalanchego-path="/tmp/avalanchego-v${VERSION}/avalanchego" \
7070
--avalanchego-path-to-upgrade-to="${AVALANCHEGO_PATH}"

tests/antithesis/compose.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,28 @@ import (
2222

2323
const bootstrapIndex = 0
2424

25+
const (
26+
targetPathEnvName = "TARGET_PATH"
27+
imageTagEnvName = "IMAGE_TAG"
28+
)
29+
2530
var (
26-
errTargetPathEnvVarNotSet = errors.New("TARGET_PATH environment variable not set")
27-
errImageTagEnvVarNotSet = errors.New("IMAGE_TAG environment variable not set")
28-
errAvalancheGoEvVarNotSet = errors.New("AVALANCHEGO_PATH environment variable not set")
29-
errPluginDirEnvVarNotSet = errors.New("AVALANCHEGO_PLUGIN_DIR environment variable not set")
31+
errTargetPathEnvVarNotSet = errors.New(targetPathEnvName + " environment variable not set")
32+
errImageTagEnvVarNotSet = errors.New(imageTagEnvName + " environment variable not set")
33+
errAvalancheGoEvVarNotSet = errors.New(tmpnet.AvalancheGoPathEnvName + " environment variable not set")
34+
errPluginDirEnvVarNotSet = errors.New(tmpnet.AvalancheGoPluginDirEnvName + " environment variable not set")
3035
)
3136

3237
// Creates docker compose configuration for an antithesis test setup. Configuration is via env vars to
3338
// simplify usage by main entrypoints. If the provided network includes a subnet, the initial DB state for
3439
// the subnet will be created and written to the target path.
3540
func GenerateComposeConfig(network *tmpnet.Network, baseImageName string) error {
36-
targetPath := os.Getenv("TARGET_PATH")
41+
targetPath := os.Getenv(targetPathEnvName)
3742
if len(targetPath) == 0 {
3843
return errTargetPathEnvVarNotSet
3944
}
4045

41-
imageTag := os.Getenv("IMAGE_TAG")
46+
imageTag := os.Getenv(imageTagEnvName)
4247
if len(imageTag) == 0 {
4348
return errImageTagEnvVarNotSet
4449
}

tests/e2e/README.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@
88
```bash
99
./scripts/build.sh # Builds avalanchego for use in deploying a test network
1010
./scripts/build_xsvm.sh # Builds xsvm for use in deploying a test network with a subnet
11-
./scripts/ginkgo.sh -v ./tests/e2e -- --avalanchego-path=./build/avalanchego
11+
./bin/ginkgo -v ./tests/e2e -- --avalanchego-path=./build/avalanchego
1212
```
1313

1414
See [`tests.e2e.sh`](../../scripts/tests.e2e.sh) for an example.
1515

16+
### Simplifying usage with direnv
17+
18+
The repo includes a [.envrc](../../.envrc) that can be applied by
19+
[direnv](https://direnv.net/) when in a shell. This will enable
20+
`ginkgo` to be invoked directly (without a `./bin/` prefix ) and
21+
without having to specify the `--avalanchego-path` or `--plugin-dir`
22+
flags.
23+
1624
### Filtering test execution with labels
1725

1826
In cases where a change can be verified against only a subset of
@@ -24,7 +32,7 @@ primarily target the X-Chain:
2432

2533

2634
```bash
27-
./scripts/ginkgo.sh -v --label-filter=x ./tests/e2e -- --avalanchego-path=./build/avalanchego
35+
./bin/ginkgo -v --label-filter=x ./tests/e2e -- --avalanchego-path=./build/avalanchego
2836
```
2937

3038
The ginkgo docs provide further detail on [how to compose label
@@ -62,7 +70,7 @@ To enable network reuse across test runs, pass `--reuse-network` as an
6270
argument to the test suite:
6371

6472
```bash
65-
./scripts/gingko.sh -v ./tests/e2e -- --avalanchego-path=/path/to/avalanchego --reuse-network
73+
./bin/gingko -v ./tests/e2e -- --avalanchego-path=/path/to/avalanchego --reuse-network
6674
```
6775

6876
If a network is not already running the first time the suite runs with
@@ -85,7 +93,7 @@ To stop a network configured for reuse, invoke the test suite with the
8593
immediately without executing any tests:
8694

8795
```bash
88-
./scripts/gingko.sh -v ./tests/e2e -- --stop-network
96+
./bin/gingko -v ./tests/e2e -- --stop-network
8997
```
9098

9199
## Skipping bootstrap checks
@@ -97,5 +105,5 @@ these bootstrap checks during development, set the
97105
`E2E_SKIP_BOOTSTRAP_CHECKS` env var to a non-empty value:
98106

99107
```bash
100-
E2E_SKIP_BOOTSTRAP_CHECKS=1 ./scripts/ginkgo.sh -v ./tests/e2e ...
108+
E2E_SKIP_BOOTSTRAP_CHECKS=1 ./bin/ginkgo -v ./tests/e2e ...
101109
```

tests/fixture/e2e/flags.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (v *FlagVars) NodeCount() int {
7474
return v.nodeCount
7575
}
7676

77-
func getEnvWithDefault(envVar, defaultVal string) string {
77+
func GetEnvWithDefault(envVar, defaultVal string) string {
7878
val := os.Getenv(envVar)
7979
if len(val) == 0 {
8080
return defaultVal
@@ -96,7 +96,7 @@ func RegisterFlags() *FlagVars {
9696
flag.StringVar(
9797
&vars.pluginDir,
9898
"plugin-dir",
99-
getEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
99+
GetEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
100100
fmt.Sprintf(
101101
"[optional] the dir containing VM plugins. Also possible to configure via the %s env variable.",
102102
tmpnet.AvalancheGoPluginDirEnvName,

tests/fixture/tmpnet/README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,8 @@ A temporary network can be managed by the `tmpnetctl` cli tool:
4646
```bash
4747
# From the root of the avalanchego repo
4848

49-
# Build the tmpnetctl binary
50-
$ ./scripts/build_tmpnetctl.sh
51-
5249
# Start a new network. Possible to specify the number of nodes (> 1) with --node-count.
53-
$ ./build/tmpnetctl start-network --avalanchego-path=/path/to/avalanchego
50+
$ ./bin/tmpnetctl start-network --avalanchego-path=/path/to/avalanchego
5451
...
5552
Started network /home/me/.tmpnet/networks/20240306-152305.924531 (UUID: abaab590-b375-44f6-9ca5-f8a6dc061725)
5653

@@ -60,7 +57,7 @@ Configure tmpnetctl to target this network by default with one of the following
6057
- export TMPNET_NETWORK_DIR=/home/me/.tmpnet/networks/latest
6158

6259
# Stop the network
63-
$ ./build/tmpnetctl stop-network --network-dir=/path/to/network
60+
$ ./bin/tmpnetctl stop-network --network-dir=/path/to/network
6461
```
6562

6663
Note the export of the path ending in `latest`. This is a symlink that
@@ -69,6 +66,14 @@ the `TMPNET_NETWORK_DIR` env var to this symlink ensures that
6966
`tmpnetctl` commands target the most recently deployed temporary
7067
network.
7168

69+
### Simplifying usage with direnv
70+
71+
The repo includes a [.envrc](../../../.envrc) that can be applied by
72+
[direnv](https://direnv.net/) when in a shell. This will enable
73+
`tmpnetctl` to be invoked directly (without a `./bin/` prefix ) and
74+
without having to specify the `--avalanchego-path` or `--plugin-dir`
75+
flags.
76+
7277
#### Deprecated usage with e2e suite
7378

7479
`tmpnetctl` was previously used to create temporary networks for use
@@ -269,7 +274,7 @@ PROMETHEUS_USERNAME=<username> PROMETHEUS_PASSWORD=<password> ./scripts/run_prom
269274
LOKI_USERNAME=<username> LOKI_PASSWORD=<password> ./scripts/run_promtail.sh
270275

271276
# Network start emits link to grafana displaying collected logs and metrics
272-
./build/tmpnetctl start-network
277+
./bin/tmpnetctl start-network
273278

274279
# Configure metrics collection from a local node binding to the default API
275280
# port of 9650 and storing its logs in ~/.avalanchego/logs. The script will

tests/fixture/tmpnet/cmd/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"go.uber.org/zap"
1717

1818
"github.com/ava-labs/avalanchego/tests"
19+
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
1920
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
2021
"github.com/ava-labs/avalanchego/utils/logging"
2122
"github.com/ava-labs/avalanchego/version"
@@ -117,9 +118,15 @@ func main() {
117118
return nil
118119
},
119120
}
121+
// TODO(marun) Enable reuse of flags across tmpnetctl and e2e
120122
startNetworkCmd.PersistentFlags().StringVar(&rootDir, "root-dir", os.Getenv(tmpnet.RootDirEnvName), "The path to the root directory for temporary networks")
121123
startNetworkCmd.PersistentFlags().StringVar(&avalancheGoPath, "avalanchego-path", os.Getenv(tmpnet.AvalancheGoPathEnvName), "The path to an avalanchego binary")
122-
startNetworkCmd.PersistentFlags().StringVar(&pluginDir, "plugin-dir", os.ExpandEnv("$HOME/.avalanchego/plugins"), "[optional] the dir containing VM plugins")
124+
startNetworkCmd.PersistentFlags().StringVar(
125+
&pluginDir,
126+
"plugin-dir",
127+
e2e.GetEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
128+
"[optional] the dir containing VM plugins",
129+
)
123130
startNetworkCmd.PersistentFlags().Uint8Var(&nodeCount, "node-count", tmpnet.DefaultNodeCount, "Number of nodes the network should initially consist of")
124131
startNetworkCmd.PersistentFlags().StringVar(&networkOwner, "network-owner", "", "The string identifying the intended owner of the network")
125132
rootCmd.AddCommand(startNetworkCmd)

tests/fixture/tmpnet/network.go

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func ReadNetwork(dir string) (*Network, error) {
197197
func (n *Network) EnsureDefaultConfig(log logging.Logger, avalancheGoPath string, pluginDir string) error {
198198
log.Info("preparing configuration for new network",
199199
zap.String("avalanchegoPath", avalancheGoPath),
200+
zap.String("pluginDir", pluginDir),
200201
)
201202

202203
// A UUID supports centralized metrics collection

tests/fixture/tmpnet/node_process.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ import (
2828
)
2929

3030
const (
31-
AvalancheGoPathEnvName = "AVALANCHEGO_PATH"
32-
AvalancheGoPluginDirEnvName = "AVALANCHEGO_PLUGIN_DIR"
31+
AvalancheGoPathEnvName = "AVALANCHEGO_PATH"
3332

3433
defaultNodeInitTimeout = 10 * time.Second
3534
)
3635

3736
var (
37+
AvalancheGoPluginDirEnvName = config.EnvVarName(config.EnvPrefix, config.PluginDirKey)
38+
3839
errNodeAlreadyRunning = errors.New("failed to start node: node is already running")
3940
errNotRunning = errors.New("node is not running")
4041
)

0 commit comments

Comments
 (0)