Skip to content

Commit

Permalink
[testing] Support direnv to simplify usage of test tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
maru-ava committed Jan 16, 2025
1 parent e973af8 commit 0946fdc
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 31 deletions.
11 changes: 11 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Repo-local commands like ginkgo and tmpnetctl
PATH_add bin

# Built binaries like avalanchego and xsvm
PATH_add build

# Configure the explicit built path of avalanchego for tmpnet usage
export AVALANCHEGO_PATH=$PWD/build/avalanchego

# Configure the local plugin directory for both avalanchego and tmpnet usage
export AVAGO_PLUGIN_DIR=$PWD/build/plugins
10 changes: 10 additions & 0 deletions bin/ginkgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -euo pipefail

# Ensure the go command is run from the root of the repository so that its go.mod file is used
AVALANCHE_PATH=$(cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd )
cd "${AVALANCHE_PATH}"

# If an explicit version is not specified, go run uses the ginkgo version from go.mod
go run github.com/onsi/ginkgo/v2/ginkgo "${@}"
16 changes: 11 additions & 5 deletions scripts/build_xsvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ source ./scripts/constants.sh
echo "Building xsvm plugin..."
go build -o ./build/xsvm ./vms/example/xsvm/cmd/xsvm/

PLUGIN_DIR="$HOME/.avalanchego/plugins"
PLUGIN_PATH="${PLUGIN_DIR}/v3m4wPxaHpvGr8qfMeyK6PRW3idZrPHmYcMTt7oXdK47yurVH"
echo "Symlinking ./build/xsvm to ${PLUGIN_PATH}"
mkdir -p "${PLUGIN_DIR}"
ln -sf "${PWD}/build/xsvm" "${PLUGIN_PATH}"
# Symlink to both global and local plugin directories to simplify
# usage for testing. The local directory should be preferred but the
# global directory remains supported for backwards compatibility.
LOCAL_PLUGIN_PATH="${PWD}/build/plugins"
GLOBAL_PLUGIN_PATH="${HOME}/.avalanchego/plugins"
for plugin_dir in "${GLOBAL_PLUGIN_PATH}" "${LOCAL_PLUGIN_PATH}"; do
PLUGIN_PATH="${plugin_dir}/v3m4wPxaHpvGr8qfMeyK6PRW3idZrPHmYcMTt7oXdK47yurVH"
echo "Symlinking ./build/xsvm to ${PLUGIN_PATH}"
mkdir -p "${plugin_dir}"
ln -sf "${PWD}/build/xsvm" "${PLUGIN_PATH}"
done
6 changes: 0 additions & 6 deletions scripts/ginkgo.sh

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/tests.e2e.bootstrap_monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ ensure_command "kind-with-registry.sh" "https://raw.githubusercontent.com/kubern
# call them without a qualifying path.
PATH="${PWD}/bin:$PATH" bash -x "${PWD}/bin/kind-with-registry.sh"

KUBECONFIG="$HOME/.kube/config" PATH="${PWD}/bin:$PATH" ./scripts/ginkgo.sh -v ./tests/fixture/bootstrapmonitor/e2e
KUBECONFIG="$HOME/.kube/config" PATH="${PWD}/bin:$PATH" ./bin/ginkgo -v ./tests/fixture/bootstrapmonitor/e2e
2 changes: 1 addition & 1 deletion scripts/tests.e2e.existing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function print_separator {
function cleanup {
print_separator
echo "cleaning up reusable network"
./scripts/ginkgo.sh -v ./tests/e2e -- --stop-network
./bin/ginkgo -v ./tests/e2e -- --stop-network
}
trap cleanup EXIT

Expand Down
2 changes: 1 addition & 1 deletion scripts/tests.e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ fi

#################################
# shellcheck disable=SC2086
./scripts/ginkgo.sh ${GINKGO_ARGS} -v ./tests/e2e -- "${E2E_ARGS[@]}" "${@}"
./bin/ginkgo ${GINKGO_ARGS} -v ./tests/e2e -- "${E2E_ARGS[@]}" "${@}"
2 changes: 1 addition & 1 deletion scripts/tests.upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ source ./scripts/constants.sh
#################################
# By default, it runs all upgrade test cases!
echo "running upgrade tests against the local cluster with ${AVALANCHEGO_PATH}"
./scripts/ginkgo.sh -v ./tests/upgrade -- \
./bin/ginkgo -v ./tests/upgrade -- \
--avalanchego-path="/tmp/avalanchego-v${VERSION}/avalanchego" \
--avalanchego-path-to-upgrade-to="${AVALANCHEGO_PATH}"
18 changes: 13 additions & 5 deletions tests/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
```bash
./scripts/build.sh # Builds avalanchego for use in deploying a test network
./scripts/build_xsvm.sh # Builds xsvm for use in deploying a test network with a subnet
./scripts/ginkgo.sh -v ./tests/e2e -- --avalanchego-path=./build/avalanchego
./bin/ginkgo -v ./tests/e2e -- --avalanchego-path=./build/avalanchego
```

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

### Simplifying usage with direnv

The repo includes an [.envrc](../../.envrc) that can be applied by
[direnv](https://direnv.net/) when in a shell. This will enable
`ginkgo` to be invoked directly (without a `./bin/` prefix ) and
without having to specify the `--avalanchego-path` or `--plugin-dir`
flags.

### Filtering test execution with labels

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


```bash
./scripts/ginkgo.sh -v --label-filter=x ./tests/e2e -- --avalanchego-path=./build/avalanchego
./bin/ginkgo -v --label-filter=x ./tests/e2e -- --avalanchego-path=./build/avalanchego
```

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

```bash
./scripts/gingko.sh -v ./tests/e2e -- --avalanchego-path=/path/to/avalanchego --reuse-network
./bin/gingko -v ./tests/e2e -- --avalanchego-path=/path/to/avalanchego --reuse-network
```

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

```bash
./scripts/gingko.sh -v ./tests/e2e -- --stop-network
./bin/gingko -v ./tests/e2e -- --stop-network
```

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

```bash
E2E_SKIP_BOOTSTRAP_CHECKS=1 ./scripts/ginkgo.sh -v ./tests/e2e ...
E2E_SKIP_BOOTSTRAP_CHECKS=1 ./bin/ginkgo -v ./tests/e2e ...
```
4 changes: 2 additions & 2 deletions tests/fixture/e2e/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (v *FlagVars) NodeCount() int {
return v.nodeCount
}

func getEnvWithDefault(envVar, defaultVal string) string {
func GetEnvWithDefault(envVar, defaultVal string) string {
val := os.Getenv(envVar)
if len(val) == 0 {
return defaultVal
Expand All @@ -96,7 +96,7 @@ func RegisterFlags() *FlagVars {
flag.StringVar(
&vars.pluginDir,
"plugin-dir",
getEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
GetEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
fmt.Sprintf(
"[optional] the dir containing VM plugins. Also possible to configure via the %s env variable.",
tmpnet.AvalancheGoPluginDirEnvName,
Expand Down
17 changes: 11 additions & 6 deletions tests/fixture/tmpnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ A temporary network can be managed by the `tmpnetctl` cli tool:
```bash
# From the root of the avalanchego repo

# Build the tmpnetctl binary
$ ./scripts/build_tmpnetctl.sh

# Start a new network. Possible to specify the number of nodes (> 1) with --node-count.
$ ./build/tmpnetctl start-network --avalanchego-path=/path/to/avalanchego
$ ./bin/tmpnetctl start-network --avalanchego-path=/path/to/avalanchego
...
Started network /home/me/.tmpnet/networks/20240306-152305.924531 (UUID: abaab590-b375-44f6-9ca5-f8a6dc061725)

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

# Stop the network
$ ./build/tmpnetctl stop-network --network-dir=/path/to/network
$ ./bin/tmpnetctl stop-network --network-dir=/path/to/network
```

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

### Simplifying usage with direnv

The repo includes an [.envrc](../../.envrc) that can be applied by
[direnv](https://direnv.net/) when in a shell. This will enable
`tmpnetctl` to be invoked directly (without a `./bin/` prefix ) and
without having to specify the `--avalanchego-path` or `--plugin-dir`
flags.

#### Deprecated usage with e2e suite

`tmpnetctl` was previously used to create temporary networks for use
Expand Down Expand Up @@ -269,7 +274,7 @@ PROMETHEUS_ID=<id> PROMETHEUS_PASSWORD=<password> ./scripts/run_prometheus.sh
LOKI_ID=<id> LOKI_PASSWORD=<password> ./scripts/run_promtail.sh

# Network start emits link to grafana displaying collected logs and metrics
./build/tmpnetctl start-network
./bin/tmpnetctl start-network

# Configure metrics collection from a local node binding to the default API
# port of 9650 and storing its logs in ~/.avalanchego/logs. The script will
Expand Down
9 changes: 8 additions & 1 deletion tests/fixture/tmpnet/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/tests"
"github.com/ava-labs/avalanchego/tests/fixture/e2e"
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/version"
Expand Down Expand Up @@ -117,9 +118,15 @@ func main() {
return nil
},
}
// TODO(marun) Enable reuse of flags across tmpnetctl and e2e
startNetworkCmd.PersistentFlags().StringVar(&rootDir, "root-dir", os.Getenv(tmpnet.RootDirEnvName), "The path to the root directory for temporary networks")
startNetworkCmd.PersistentFlags().StringVar(&avalancheGoPath, "avalanchego-path", os.Getenv(tmpnet.AvalancheGoPathEnvName), "The path to an avalanchego binary")
startNetworkCmd.PersistentFlags().StringVar(&pluginDir, "plugin-dir", os.ExpandEnv("$HOME/.avalanchego/plugins"), "[optional] the dir containing VM plugins")
startNetworkCmd.PersistentFlags().StringVar(
&pluginDir,
"plugin-dir",
e2e.GetEnvWithDefault(tmpnet.AvalancheGoPluginDirEnvName, os.ExpandEnv("$HOME/.avalanchego/plugins")),
"[optional] the dir containing VM plugins",
)
startNetworkCmd.PersistentFlags().Uint8Var(&nodeCount, "node-count", tmpnet.DefaultNodeCount, "Number of nodes the network should initially consist of")
startNetworkCmd.PersistentFlags().StringVar(&networkOwner, "network-owner", "", "The string identifying the intended owner of the network")
rootCmd.AddCommand(startNetworkCmd)
Expand Down
1 change: 1 addition & 0 deletions tests/fixture/tmpnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func ReadNetwork(dir string) (*Network, error) {
func (n *Network) EnsureDefaultConfig(log logging.Logger, avalancheGoPath string, pluginDir string) error {
log.Info("preparing configuration for new network",
zap.String("avalanchegoPath", avalancheGoPath),
zap.String("pluginDir", pluginDir),
)

// A UUID supports centralized metrics collection
Expand Down
5 changes: 3 additions & 2 deletions tests/fixture/tmpnet/node_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
)

const (
AvalancheGoPathEnvName = "AVALANCHEGO_PATH"
AvalancheGoPluginDirEnvName = "AVALANCHEGO_PLUGIN_DIR"
AvalancheGoPathEnvName = "AVALANCHEGO_PATH"

defaultNodeInitTimeout = 10 * time.Second
)

var (
AvalancheGoPluginDirEnvName = config.EnvVarName(config.EnvPrefix, config.PluginDirKey)

errNodeAlreadyRunning = errors.New("failed to start node: node is already running")
errNotRunning = errors.New("node is not running")
)
Expand Down

0 comments on commit 0946fdc

Please sign in to comment.