Skip to content

Commit bf44343

Browse files
committed
[antithesis] Refactor compose config generation to simplify reuse
1 parent 2af3890 commit bf44343

File tree

4 files changed

+55
-59
lines changed

4 files changed

+55
-59
lines changed

tests/antithesis/avalanchego/gencomposeconfig/main.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
package main
55

66
import (
7-
"fmt"
87
"log"
9-
"os"
108

119
"github.com/ava-labs/avalanchego/tests/antithesis"
1210
"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
@@ -16,22 +14,8 @@ const baseImageName = "antithesis-avalanchego"
1614

1715
// Creates docker-compose.yml and its associated volumes in the target path.
1816
func main() {
19-
targetPath := os.Getenv("TARGET_PATH")
20-
if len(targetPath) == 0 {
21-
log.Fatal("TARGET_PATH environment variable not set")
22-
}
23-
24-
imageTag := os.Getenv("IMAGE_TAG")
25-
if len(imageTag) == 0 {
26-
log.Fatal("IMAGE_TAG environment variable not set")
27-
}
28-
29-
nodeImageName := fmt.Sprintf("%s-node:%s", baseImageName, imageTag)
30-
workloadImageName := fmt.Sprintf("%s-workload:%s", baseImageName, imageTag)
31-
3217
network := tmpnet.LocalNetworkOrPanic()
33-
err := antithesis.GenerateComposeConfig(network, nodeImageName, workloadImageName, targetPath)
34-
if err != nil {
35-
log.Fatalf("failed to generate config for docker-compose: %s", err)
18+
if err := antithesis.GenerateComposeConfig(network, baseImageName); err != nil {
19+
log.Fatalf("failed to generate compose config: %v", err)
3620
}
3721
}

tests/antithesis/compose.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package antithesis
55

66
import (
7+
"errors"
78
"fmt"
89
"os"
910
"path/filepath"
@@ -22,9 +23,56 @@ import (
2223

2324
const bootstrapIndex = 0
2425

26+
// Creates docker-compose configuration for an antithesis test
27+
// setup. Configuration is via env vars to simplify usage by main entrypoints. If
28+
// the provided network includes a subnet, the initial DB state for the subnet
29+
// will be created and written to the target path.
30+
func GenerateComposeConfig(network *tmpnet.Network, baseImageName string) error {
31+
targetPath := os.Getenv("TARGET_PATH")
32+
if len(targetPath) == 0 {
33+
return errors.New("TARGET_PATH environment variable not set")
34+
}
35+
36+
imageTag := os.Getenv("IMAGE_TAG")
37+
if len(imageTag) == 0 {
38+
return errors.New("IMAGE_TAG environment variable not set")
39+
}
40+
41+
// Subnet testing requires creating an initial db state for the bootstrap node
42+
if len(network.Subnets) > 0 {
43+
avalancheGoPath := os.Getenv("AVALANCHEGO_PATH")
44+
if len(avalancheGoPath) == 0 {
45+
return errors.New("AVALANCHEGO_PATH environment variable not set")
46+
}
47+
48+
pluginDir := os.Getenv("AVALANCHEGO_PLUGIN_DIR")
49+
if len(pluginDir) == 0 {
50+
return errors.New("AVALANCHEGO_PLUGIN_DIR environment variable not set")
51+
}
52+
53+
bootstrapVolumePath, err := getBootstrapVolumePath(targetPath)
54+
if err != nil {
55+
return fmt.Errorf("failed to get bootstrap volume path: %w", err)
56+
}
57+
58+
if err := initBootstrapDB(network, avalancheGoPath, pluginDir, bootstrapVolumePath); err != nil {
59+
return fmt.Errorf("failed to initialize db volumes: %w", err)
60+
}
61+
}
62+
63+
nodeImageName := fmt.Sprintf("%s-node:%s", baseImageName, imageTag)
64+
workloadImageName := fmt.Sprintf("%s-workload:%s", baseImageName, imageTag)
65+
66+
if err := initComposeConfig(network, nodeImageName, workloadImageName, targetPath); err != nil {
67+
return fmt.Errorf("failed to generate compose config: %w", err)
68+
}
69+
70+
return nil
71+
}
72+
2573
// Initialize the given path with the docker-compose configuration (compose file and
2674
// volumes) needed for an Antithesis test setup.
27-
func GenerateComposeConfig(
75+
func initComposeConfig(
2876
network *tmpnet.Network,
2977
nodeImageName string,
3078
workloadImageName string,

tests/antithesis/init_db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
// Given a path, compose the expected path of the bootstrap node's docker compose db volume.
19-
func GetBootstrapVolumePath(targetPath string) (string, error) {
19+
func getBootstrapVolumePath(targetPath string) (string, error) {
2020
absPath, err := filepath.Abs(targetPath)
2121
if err != nil {
2222
return "", fmt.Errorf("failed to convert target path to absolute path: %w", err)
@@ -27,7 +27,7 @@ func GetBootstrapVolumePath(targetPath string) (string, error) {
2727
// Bootstraps a local process-based network, creates its subnets and chains, and copies
2828
// the resulting db state from one of the nodes to the provided path. The path will be
2929
// created if it does not already exist.
30-
func InitBootstrapDB(network *tmpnet.Network, avalancheGoPath string, pluginDir string, destPath string) error {
30+
func initBootstrapDB(network *tmpnet.Network, avalancheGoPath string, pluginDir string, destPath string) error {
3131
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*2)
3232
defer cancel()
3333
if err := tmpnet.BootstrapNewNetwork(

tests/antithesis/xsvm/gencomposeconfig/main.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
package main
55

66
import (
7-
"fmt"
87
"log"
9-
"os"
108

119
"github.com/ava-labs/avalanchego/genesis"
1210
"github.com/ava-labs/avalanchego/tests/antithesis"
@@ -18,45 +16,11 @@ const baseImageName = "antithesis-xsvm"
1816

1917
// Creates docker-compose.yml and its associated volumes in the target path.
2018
func main() {
21-
avalancheGoPath := os.Getenv("AVALANCHEGO_PATH")
22-
if len(avalancheGoPath) == 0 {
23-
log.Fatal("AVALANCHEGO_PATH environment variable not set")
24-
}
25-
26-
pluginDir := os.Getenv("AVALANCHEGO_PLUGIN_DIR")
27-
if len(pluginDir) == 0 {
28-
log.Fatal("AVALANCHEGO_PLUGIN_DIR environment variable not set")
29-
}
30-
31-
targetPath := os.Getenv("TARGET_PATH")
32-
if len(targetPath) == 0 {
33-
log.Fatal("TARGET_PATH environment variable not set")
34-
}
35-
36-
imageTag := os.Getenv("IMAGE_TAG")
37-
if len(imageTag) == 0 {
38-
log.Fatal("IMAGE_TAG environment variable not set")
39-
}
40-
41-
nodeImageName := fmt.Sprintf("%s-node:%s", baseImageName, imageTag)
42-
workloadImageName := fmt.Sprintf("%s-workload:%s", baseImageName, imageTag)
43-
44-
// Create a network with an xsvm subnet
4519
network := tmpnet.LocalNetworkOrPanic()
4620
network.Subnets = []*tmpnet.Subnet{
4721
subnet.NewXSVMOrPanic("xsvm", genesis.VMRQKey, network.Nodes...),
4822
}
49-
50-
bootstrapVolumePath, err := antithesis.GetBootstrapVolumePath(targetPath)
51-
if err != nil {
52-
log.Fatalf("failed to get bootstrap volume path: %v", err)
53-
}
54-
55-
if err := antithesis.InitBootstrapDB(network, avalancheGoPath, pluginDir, bootstrapVolumePath); err != nil {
56-
log.Fatalf("failed to initialize db volumes: %v", err)
57-
}
58-
59-
if err := antithesis.GenerateComposeConfig(network, nodeImageName, workloadImageName, targetPath); err != nil {
60-
log.Fatalf("failed to generate config for docker-compose: %v", err)
23+
if err := antithesis.GenerateComposeConfig(network, baseImageName); err != nil {
24+
log.Fatalf("failed to generate compose config: %v", err)
6125
}
6226
}

0 commit comments

Comments
 (0)