Skip to content

Commit 0cef8d8

Browse files
committed
feat: refactor path manager and add cobra to commands
1 parent 2bc944c commit 0cef8d8

16 files changed

+965
-203
lines changed

cmd/prepare.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/vegaprotocol/snapshot-testing/config"
10+
"github.com/vegaprotocol/snapshot-testing/logging"
11+
"github.com/vegaprotocol/snapshot-testing/networkutils"
12+
"go.uber.org/zap"
13+
)
14+
15+
var prepareCmd = &cobra.Command{
16+
Use: "prepare",
17+
Short: "Prepare local node only and print command to start it.",
18+
Run: func(cmd *cobra.Command, args []string) {
19+
if err := ensureWorkingDirectory(workDir); err != nil {
20+
panic(err)
21+
}
22+
23+
// We do not want to log this to file
24+
stdoutOnlyLogger := logging.CreateLogger(zap.InfoLevel, logging.DoNotLogToFile, true)
25+
networkConfig, err := config.NetworkConfigForEnvironmentName(environment)
26+
if err != nil {
27+
stdoutOnlyLogger.Fatal("failed to get network config", zap.Error(err))
28+
}
29+
30+
pathManager := networkutils.NewPathManager(workDir)
31+
32+
if err := prepareNetwork(stdoutOnlyLogger, pathManager, *networkConfig, config.DefaultCredentials); err != nil {
33+
stdoutOnlyLogger.Fatal("failed to setup local network", zap.Error(err))
34+
}
35+
36+
stdoutOnlyLogger.Info("")
37+
stdoutOnlyLogger.Info("")
38+
stdoutOnlyLogger.Info("To run your local node start: ")
39+
stdoutOnlyLogger.Sugar().Infof("%s run --home %s", pathManager.VisorBin(), pathManager.VisorHome())
40+
},
41+
}
42+
43+
func prepareNetwork(
44+
logger *zap.Logger,
45+
pathManager networkutils.PathManager,
46+
networkConfig config.Network,
47+
postgreSQLCredentials config.PostgreSQLCreds,
48+
) error {
49+
network, err := networkutils.NewNetwork(logger, networkConfig, pathManager)
50+
if err != nil {
51+
return fmt.Errorf("failed to create network utils: %w", err)
52+
}
53+
54+
if err := network.SetupLocalNode(postgreSQLCredentials); err != nil {
55+
return fmt.Errorf("failed to setup local node: %w", err)
56+
}
57+
58+
return nil
59+
}
60+
61+
func ensureWorkingDirectory(path string) error {
62+
// logger.Sugar().Infof("Ensure the working directory(%s) exists", path) // TODO: We have no loger at this point
63+
if err := os.MkdirAll(workDir, os.ModePerm); err != nil {
64+
return fmt.Errorf("failed to create working directory")
65+
}
66+
67+
logsDir := filepath.Join(path, "logs")
68+
// logger.Sugar().Infof("Ensure the directory for logs (%s) exists", logsDir)
69+
if err := os.MkdirAll(logsDir, os.ModePerm); err != nil {
70+
return fmt.Errorf("failed to create logs directory")
71+
}
72+
73+
return nil
74+
}

cmd/root.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
var (
6+
workDir string
7+
environment string
8+
9+
rootCmd = &cobra.Command{
10+
Use: "snapshot-testing",
11+
Short: "Command that runs the snapshot-testing",
12+
Long: `The command setup local node to start it from the remote snapshot, then
13+
starts it and runs it for given time.`,
14+
}
15+
)
16+
17+
// Execute executes the root command.
18+
func Execute() error {
19+
return rootCmd.Execute()
20+
}
21+
22+
func init() {
23+
rootCmd.PersistentFlags().StringVar(
24+
&workDir,
25+
"work-dir",
26+
"/tmp/snapshot-testing",
27+
"the working directory, where binaries are downloaded and local node home directories are generated",
28+
)
29+
30+
rootCmd.PersistentFlags().StringVar(
31+
&environment,
32+
"environment",
33+
"mainnet",
34+
"the environment you want to run testing on, available values are: mainnet, fairground, stagnet1, devnet1",
35+
)
36+
rootCmd.AddCommand(prepareCmd)
37+
}

cmd/run.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cmd
2+
3+
// import (
4+
// "fmt"
5+
// "path/filepath"
6+
7+
// "github.com/spf13/cobra"
8+
// "github.com/vegaprotocol/snapshot-testing/clients/docker"
9+
// "github.com/vegaprotocol/snapshot-testing/components"
10+
// "github.com/vegaprotocol/snapshot-testing/config"
11+
// "github.com/vegaprotocol/snapshot-testing/logging"
12+
// "go.uber.org/zap"
13+
// )
14+
15+
// var runCmd = &cobra.Command{
16+
// Use: "run",
17+
// Short: "Prepare local node and run it for given time.",
18+
// Run: func(cmd *cobra.Command, args []string) {
19+
// if err := ensureWorkingDirectory(workDir); err != nil {
20+
// panic(err)
21+
// }
22+
23+
// // We do not want to log this to file
24+
// stdoutOnlyLogger := logging.CreateLogger(zap.InfoLevel, filepath.Join(workDir, "logs", "main.log"), true)
25+
// networkConfig, err := config.NetworkConfigForEnvironmentName(environment)
26+
// if err != nil {
27+
// stdoutOnlyLogger.Fatal("failed to get network config", zap.Error(err))
28+
// }
29+
30+
// localNodeDetails, err := prepareNetwork(stdoutOnlyLogger, workDir, *networkConfig, config.DefaultCredentials)
31+
// if err != nil {
32+
// stdoutOnlyLogger.Fatal("failed to setup local network", zap.Error(err))
33+
// }
34+
35+
// dockerClient, err := docker.NewClient()
36+
// if err != nil {
37+
// return fmt.Errorf("failed to create docker client: %w", err)
38+
// }
39+
40+
// postgresql, err := components.NewPostgresql(dockerClient)
41+
42+
// },
43+
// }

components/component.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package components
22

33
import (
44
"context"
5-
"io"
65
)
76

87
type Component interface {
98
Start(ctx context.Context) error
109
Stop(ctx context.Context) error
11-
Stdout(ctx context.Context) (io.ReadCloser, error)
12-
Stderr(ctx context.Context) (io.ReadCloser, error)
1310
Healthy() (bool, error)
1411
Cleanup(ctx context.Context) error
1512
}

components/controller.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package components
2+
3+
import "go.uber.org/zap"
4+
5+
func Run(mainLogger *zap.Logger, components []Component) error {
6+
7+
return nil
8+
}

components/postgresql.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ import (
1212
type postgresql struct {
1313
containerName string
1414

15-
envName string
1615
dockerClient *docker.Client
1716
}
1817

19-
func NewPostgresql(envName string, dockerClient *docker.Client) (Component, error) {
18+
func NewPostgresql(dockerClient *docker.Client) (Component, error) {
2019
return &postgresql{
21-
envName: envName,
2220
dockerClient: dockerClient,
2321
}, nil
2422
}

config/networks.go

+17-23
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@ package config
22

33
import "fmt"
44

5-
func (n Network) Validate() error {
6-
if len(n.DataNodesREST) == 0 {
7-
return fmt.Errorf("no data nodes rest endpoints")
8-
}
9-
10-
if len(n.BootstrapPeers) == 0 {
11-
return fmt.Errorf("no bootstrap peers")
12-
}
13-
14-
if len(n.RPCPeers) == 0 {
15-
return fmt.Errorf("no rpc peers")
16-
}
17-
18-
if len(n.Seeds) == 0 {
19-
return fmt.Errorf("no seeds")
20-
}
21-
22-
if len(n.GenesisURL) == 0 {
23-
return fmt.Errorf("no genesis url")
24-
}
5+
const (
6+
NetworkNameMainnet string = "mainnet"
7+
NetworkNameFairground string = "fairground"
8+
NetworkNameStagnet1 string = "stagnet1"
9+
NetworkNameDevnet1 string = "devnet1"
10+
)
2511

26-
if len(n.ArtifactsRepository) == 0 {
27-
return fmt.Errorf("empty artifacts repository")
12+
func NetworkConfigForEnvironmentName(envName string) (*Network, error) {
13+
switch envName {
14+
case NetworkNameMainnet:
15+
return &Mainnet, nil
16+
case NetworkNameFairground:
17+
return &Fairground, nil
18+
case NetworkNameStagnet1:
19+
return &Stagnet1, nil
20+
case NetworkNameDevnet1:
21+
return &Devnet1, nil
2822
}
2923

30-
return nil
24+
return nil, fmt.Errorf("unknown network name: expected one of [mainnet, fairground, stagnet1, devnet1], %s got", envName)
3125
}
3226

3327
var (

config/postgresql.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package config
33
var PostgresqlConfig = ContainerConfig{
44
Name: "snapshot-testing-postgresql",
55
Image: "timescale/timescaledb:2.8.0-pg14",
6-
Environment: map[string]string{
7-
"POSTGRES_USER": "vega",
8-
"POSTGRES_DB": "vega",
9-
"POSTGRES_PASSWORD": "vega",
10-
},
6+
// Environment: map[string]string{
7+
// "POSTGRES_USER": "vega",
8+
// "POSTGRES_DB": "vega",
9+
// "POSTGRES_PASSWORD": "vega",
10+
// },
1111
Command: []string{
1212
"postgres",
1313
"-c", "max_connections=50",
@@ -19,7 +19,16 @@ var PostgresqlConfig = ContainerConfig{
1919
"-c", "shared_buffers=2GB",
2020
"-c", "temp_buffers=5MB",
2121
},
22-
Ports: map[uint16]uint16{
23-
5432: 5432,
24-
},
22+
// Ports: map[uint16]uint16{
23+
// 5432: 5432,
24+
// },
25+
}
26+
27+
// TODO: For now we are hardcoding it
28+
var DefaultCredentials = PostgreSQLCreds{
29+
Host: "localhost",
30+
Port: 5432,
31+
User: "vega",
32+
Pass: "vega",
33+
DbName: "vega",
2534
}

config/types.go

+38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package config
22

3+
import "fmt"
4+
5+
type PostgreSQLCreds struct {
6+
Host string
7+
Port uint16
8+
User string
9+
Pass string
10+
DbName string
11+
}
12+
313
type ContainerConfig struct {
414
Name string
515
Image string
@@ -23,3 +33,31 @@ type Network struct {
2333
Seeds []string
2434
BootstrapPeers []string
2535
}
36+
37+
func (n Network) Validate() error {
38+
if len(n.DataNodesREST) == 0 {
39+
return fmt.Errorf("no data nodes rest endpoints")
40+
}
41+
42+
if len(n.BootstrapPeers) == 0 {
43+
return fmt.Errorf("no bootstrap peers")
44+
}
45+
46+
if len(n.RPCPeers) == 0 {
47+
return fmt.Errorf("no rpc peers")
48+
}
49+
50+
if len(n.Seeds) == 0 {
51+
return fmt.Errorf("no seeds")
52+
}
53+
54+
if len(n.GenesisURL) == 0 {
55+
return fmt.Errorf("no genesis url")
56+
}
57+
58+
if len(n.ArtifactsRepository) == 0 {
59+
return fmt.Errorf("empty artifacts repository")
60+
}
61+
62+
return nil
63+
}

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/docker/docker v26.1.1+incompatible
77
github.com/docker/go-connections v0.5.0
88
github.com/opencontainers/image-spec v1.1.0
9+
github.com/spf13/cobra v1.2.1
910
github.com/tomwright/dasel v1.27.3
1011
go.uber.org/zap v1.27.0
1112
gopkg.in/natefinch/lumberjack.v2 v2.2.1
@@ -30,6 +31,7 @@ require (
3031
github.com/google/uuid v1.1.2 // indirect
3132
github.com/huandu/xstrings v1.3.1 // indirect
3233
github.com/imdario/mergo v0.3.11 // indirect
34+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
3335
github.com/mitchellh/copystructure v1.0.0 // indirect
3436
github.com/mitchellh/reflectwalk v1.0.0 // indirect
3537
github.com/moby/docker-image-spec v1.3.1 // indirect
@@ -40,6 +42,7 @@ require (
4042
github.com/pkg/errors v0.9.1 // indirect
4143
github.com/shopspring/decimal v1.2.0 // indirect
4244
github.com/spf13/cast v1.3.1 // indirect
45+
github.com/spf13/pflag v1.0.5 // indirect
4346
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
4447
go.opentelemetry.io/otel v1.26.0 // indirect
4548
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 // indirect
@@ -52,6 +55,8 @@ require (
5255
golang.org/x/sys v0.19.0 // indirect
5356
golang.org/x/text v0.14.0 // indirect
5457
golang.org/x/time v0.5.0 // indirect
58+
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
59+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
5560
gopkg.in/yaml.v2 v2.4.0 // indirect
5661
gotest.tools/v3 v3.5.1 // indirect
5762
)

0 commit comments

Comments
 (0)