Skip to content

Commit

Permalink
Better error handling and other requested changes implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykiselev committed Sep 13, 2024
1 parent 8235936 commit 548e3cb
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 55 deletions.
76 changes: 37 additions & 39 deletions itests/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package config

import (
"encoding/json"
stderrs "errors"
"fmt"
"html/template"
"log"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/ory/dockertest/v3"
"github.com/pkg/errors"
Expand All @@ -32,7 +31,7 @@ const (
RESTAPIPort = "6869"
GRPCAPIPort = "6870"
BindPort = "6868"
Localhost = "0.0.0.0"
DefaultIP = "0.0.0.0"
NetTCP = "/tcp"
)

Expand Down Expand Up @@ -66,15 +65,9 @@ func (c *ScalaConfigurator) WithGoNode(goNodeIP string) *ScalaConfigurator {
}

func (c *ScalaConfigurator) DockerRunOptions() *dockertest.RunOptions {
kpb := new(strings.Builder)
kps := ""
for i, kp := range c.knownPeers {
kpb.WriteString("-Dwaves.network.known-peers.")
kpb.WriteString(strconv.Itoa(i))
kpb.WriteString("=")
kpb.WriteString(kp)
kpb.WriteString(":")
kpb.WriteString(BindPort)
kpb.WriteString(" ")
kps += fmt.Sprintf("-Dwaves.network.known-peers.%d=%s:%s ", i, kp, BindPort)
}
opt := &dockertest.RunOptions{
Repository: "wavesplatform/wavesnode",
Expand All @@ -88,7 +81,7 @@ func (c *ScalaConfigurator) DockerRunOptions() *dockertest.RunOptions {
"WAVES_LOG_LEVEL=TRACE",
"WAVES_NETWORK=custom",
"JAVA_OPTS=" +
kpb.String() +
kps +
"-Dwaves.network.declared-address=scala-node:" + BindPort + " " +
"-Dwaves.network.port=" + BindPort + " " +
"-Dwaves.rest-api.port=" + RESTAPIPort + " " +
Expand All @@ -104,39 +97,40 @@ func (c *ScalaConfigurator) DockerRunOptions() *dockertest.RunOptions {
return opt
}

func (c *ScalaConfigurator) createNodeConfig() error {
configDir, err := createConfigDir(c.suite)
func (c *ScalaConfigurator) createNodeConfig() (err error) {
var configDir string
configDir, err = createConfigDir(c.suite)
if err != nil {
return errors.Wrap(err, "failed to create scala node configuration")
}
configPath := filepath.Join(configDir, scalaConfigFilename)
f, err := os.Create(configPath)
var f *os.File
f, err = os.Create(configPath)
if err != nil {
return err
return errors.Wrap(err, "failed to create scala node configuration")
}
defer func() {
if synErr := f.Sync(); synErr != nil {
log.Printf("Failed to sync file %q to disk: %v", f.Name(), err)
return
err = stderrs.Join(err, errors.Wrapf(synErr, "failed to sync file %q to disk", f.Name()))
}
if clErr := f.Close(); clErr != nil {
log.Printf("Failed to close file %q: %v", f.Name(), clErr)
err = stderrs.Join(err, errors.Wrapf(clErr, "failed to close file %q", f.Name()))
}
}()
pwd, err := os.Getwd()
if err != nil {
return err
return errors.Wrap(err, "failed to create scala node configuration")
}
templatePath := filepath.Join(pwd, configFolder, templateScalaCfgFilename)
t, err := template.ParseFiles(templatePath)
if err != nil {
return err
return errors.Wrap(err, "failed to create scala node configuration")
}
if exErr := t.Execute(f, c.cfg); exErr != nil {
return errors.Wrap(exErr, "failed to create scala node configuration")
}
c.configFolder = configDir
return nil
return err
}

type GoConfigurator struct {
Expand Down Expand Up @@ -164,9 +158,9 @@ func (c *GoConfigurator) DockerRunOptions() *dockertest.RunOptions {
User: "gowaves",
Hostname: "go-node",
Env: []string{
"GRPC_ADDR=" + Localhost + ":" + GRPCAPIPort,
"API_ADDR=" + Localhost + ":" + RESTAPIPort,
"BIND_ADDR=" + Localhost + ":" + BindPort,
"GRPC_ADDR=" + DefaultIP + ":" + GRPCAPIPort,
"API_ADDR=" + DefaultIP + ":" + RESTAPIPort,
"BIND_ADDR=" + DefaultIP + ":" + BindPort,
"DECLARED_ADDR=" + "go-node:" + BindPort,
"PEERS=",
"WALLET_PASSWORD=itest",
Expand All @@ -192,38 +186,42 @@ func (c *GoConfigurator) setAndVerifyWalletFolder() error {
return err
}
c.walletFolder = filepath.Clean(filepath.Join(pwd, walletPath))
if _, flErr := os.Stat(c.walletFolder); os.IsNotExist(flErr) {
return errors.New("wallet folder does not exist")
if _, flErr := os.Stat(c.walletFolder); flErr != nil {
if os.IsNotExist(flErr) {
return errors.New("wallet folder does not exist")
}
return errors.Wrap(err, "unexpected error while verifying wallet folder")
}
return nil
}

func (c *GoConfigurator) createNodeConfig() error {
configDir, err := createConfigDir(c.suite)
func (c *GoConfigurator) createNodeConfig() (err error) {
var configDir string
configDir, err = createConfigDir(c.suite)
if err != nil {
return errors.Wrap(err, "failed to create go node configuration")
}
configPath := filepath.Join(configDir, goConfigFilename)
f, err := os.Create(configPath)
var f *os.File
f, err = os.Create(configPath)
if err != nil {
return err
return errors.Wrap(err, "failed to create go node configuration")
}
defer func() {
if err := f.Sync(); err != nil {
log.Printf("Failed to sync file '%s' to disk: %v", f.Name(), err)
return
if synErr := f.Sync(); synErr != nil {
err = stderrs.Join(err, errors.Wrapf(synErr, "failed to sync file %q to disk", f.Name()))
}
if err := f.Close(); err != nil {
log.Printf("Failed to close file '%s': %v", f.Name(), err)
if clErr := f.Close(); clErr != nil {
err = stderrs.Join(err, errors.Wrapf(clErr, "failed to close file %q", f.Name()))
}
}()
jsonWriter := json.NewEncoder(f)
jsonWriter.SetIndent("", "\t")
if jsErr := jsonWriter.Encode(c.cfg.Settings); jsErr != nil {
return errors.Wrap(jsErr, "failed to encode genesis settings")
return errors.Wrap(jsErr, "failed to create go node configuration")
}
c.configFolder = configDir
return nil
return err
}

func createConfigDir(suiteName string) (string, error) {
Expand Down
8 changes: 4 additions & 4 deletions itests/config/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"encoding/json"
"log"
stderrs "errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -38,21 +38,21 @@ func NewRewardSettingsFromFile(dir, file string) (*RewardSettings, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to read reward settings")
}
rewardSettingsPath := filepath.Clean(filepath.Join(pwd, testdataFolder, rewardSettingsFolder, dir, file))
rewardSettingsPath := filepath.Join(pwd, testdataFolder, rewardSettingsFolder, dir, file)
f, err := os.Open(rewardSettingsPath)
if err != nil {
return nil, errors.Wrap(err, "failed to read reward settings")
}
defer func() {
if clErr := f.Close(); clErr != nil {
log.Printf("Failed to close file %q: %v", f.Name(), clErr)
err = stderrs.Join(err, errors.Wrapf(clErr, "failed to close reward settings file %q", f.Name()))
}
}()

js := json.NewDecoder(f)
s := &RewardSettings{}
if jsErr := js.Decode(s); jsErr != nil {
return nil, errors.Wrap(err, "failed to read reward settings")
return nil, errors.Wrap(jsErr, "failed to read reward settings")
}
return s, nil
}
14 changes: 8 additions & 6 deletions itests/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"context"
stderrs "errors"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -43,7 +44,7 @@ type NodeContainer struct {
}

func (c *NodeContainer) RestAPIURL() string {
return fmt.Sprintf("http://%s", net.JoinHostPort(config.Localhost, c.ports.RESTAPIPort))
return fmt.Sprintf("http://%s", net.JoinHostPort(config.DefaultIP, c.ports.RESTAPIPort))
}

func (c *NodeContainer) Ports() *PortConfig {
Expand All @@ -55,17 +56,18 @@ func (c *NodeContainer) ContainerNetworkIP() string {
}

func (c *NodeContainer) closeFiles() error {
var err error
if c.logs != nil {
if err := c.logs.Close(); err != nil {
return errors.Wrap(err, "failed to close logs file")
if clErr := c.logs.Close(); clErr != nil {
err = stderrs.Join(err, errors.Wrapf(clErr, "failed to close logs file %q", c.logs.Name()))
}
}
if c.errors != nil {
if err := c.errors.Close(); err != nil {
return errors.Wrap(err, "failed to close errors file")
if clErr := c.errors.Close(); clErr != nil {
err = stderrs.Join(err, errors.Wrapf(clErr, "failed to close errors file %q", c.errors.Name()))
}
}
return nil
return err
}

type Docker struct {
Expand Down
7 changes: 4 additions & 3 deletions itests/net/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,21 @@ func NewNodeConnections(goPorts, scalaPorts *d.PortConfig) (NodeConnections, err
func establishConnections(goPorts, scalaPorts *d.PortConfig) (NodeConnections, error) {
goCon, err := NewConnection(
proto.TCPAddr{},
config.Localhost+":"+goPorts.BindPort,
config.DefaultIP+":"+goPorts.BindPort,
proto.ProtocolVersion(), "wavesL",
)
if err != nil {
return NodeConnections{}, errors.Wrap(err, "failed to create connection to go node")
}
scalaCon, err := NewConnection(
proto.TCPAddr{},
config.Localhost+":"+scalaPorts.BindPort,
config.DefaultIP+":"+scalaPorts.BindPort,
proto.ProtocolVersion(), "wavesL",
)
if err != nil {
if closeErr := goCon.Close(); closeErr != nil {
return NodeConnections{}, errors.Wrap(stderrs.Join(closeErr, err), "failed to create connection to scala node")
return NodeConnections{}, errors.Wrap(stderrs.Join(closeErr, err),
"failed to create connection to scala node and close go node connection")
}
return NodeConnections{}, errors.Wrap(err, "failed to create connection to scala node")
}
Expand Down
2 changes: 1 addition & 1 deletion itests/node_client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type GrpcClient struct {
}

func NewGrpcClient(t *testing.T, port string) *GrpcClient {
conn, err := grpc.NewClient(config.Localhost+":"+port, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(config.DefaultIP+":"+port, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(t, err, "failed to dial grpc")
return &GrpcClient{conn: conn, timeout: 30 * time.Second}
}
Expand Down
2 changes: 1 addition & 1 deletion itests/node_client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type HttpClient struct {

func NewHttpClient(t *testing.T, port string) *HttpClient {
c, err := client.NewClient(client.Options{
BaseUrl: "http://" + config.Localhost + ":" + port + "/",
BaseUrl: "http://" + config.DefaultIP + ":" + port + "/",
Client: &http.Client{Timeout: d.DefaultTimeout},
ApiKey: "itest-api-key",
ChainID: 'L', // I tried to use constant `utilities.TestChainID`, but after all decided that a little duplication is better in this case.
Expand Down
2 changes: 1 addition & 1 deletion itests/node_client/node_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *NodesClients) reportFirstDivergedHeight(t *testing.T, height uint64) {
}
}
if first == 0 {
t.Errorf("couldn't find the height when state hashes diverged. should not happen")
t.Error("couldn't find the height when state hashes diverged. should not happen")
return
}

Expand Down

0 comments on commit 548e3cb

Please sign in to comment.