From 548e3cb1d23f512e274430c1182c0c5d1b595f37 Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Fri, 13 Sep 2024 12:47:26 +0400 Subject: [PATCH] Better error handling and other requested changes implemented. --- itests/config/config.go | 76 +++++++++++++++---------------- itests/config/reward.go | 8 ++-- itests/docker/docker.go | 14 +++--- itests/net/connection.go | 7 +-- itests/node_client/grpc_client.go | 2 +- itests/node_client/http_client.go | 2 +- itests/node_client/node_client.go | 2 +- 7 files changed, 56 insertions(+), 55 deletions(-) diff --git a/itests/config/config.go b/itests/config/config.go index 9126010e1..8d885d927 100644 --- a/itests/config/config.go +++ b/itests/config/config.go @@ -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" @@ -32,7 +31,7 @@ const ( RESTAPIPort = "6869" GRPCAPIPort = "6870" BindPort = "6868" - Localhost = "0.0.0.0" + DefaultIP = "0.0.0.0" NetTCP = "/tcp" ) @@ -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", @@ -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 + " " + @@ -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 { @@ -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", @@ -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) { diff --git a/itests/config/reward.go b/itests/config/reward.go index 3119101ac..2bc20263b 100644 --- a/itests/config/reward.go +++ b/itests/config/reward.go @@ -2,7 +2,7 @@ package config import ( "encoding/json" - "log" + stderrs "errors" "os" "path/filepath" @@ -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 } diff --git a/itests/docker/docker.go b/itests/docker/docker.go index a326521f7..51a06501e 100644 --- a/itests/docker/docker.go +++ b/itests/docker/docker.go @@ -2,6 +2,7 @@ package docker import ( "context" + stderrs "errors" "fmt" "log" "net" @@ -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 { @@ -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 { diff --git a/itests/net/connection.go b/itests/net/connection.go index b1be27fb2..7fafe65f1 100644 --- a/itests/net/connection.go +++ b/itests/net/connection.go @@ -89,7 +89,7 @@ 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 { @@ -97,12 +97,13 @@ func establishConnections(goPorts, scalaPorts *d.PortConfig) (NodeConnections, e } 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") } diff --git a/itests/node_client/grpc_client.go b/itests/node_client/grpc_client.go index 68502e954..1df9d4761 100644 --- a/itests/node_client/grpc_client.go +++ b/itests/node_client/grpc_client.go @@ -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} } diff --git a/itests/node_client/http_client.go b/itests/node_client/http_client.go index 72a405119..10693379c 100644 --- a/itests/node_client/http_client.go +++ b/itests/node_client/http_client.go @@ -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. diff --git a/itests/node_client/node_client.go b/itests/node_client/node_client.go index 35df3e1d4..3c1320d86 100644 --- a/itests/node_client/node_client.go +++ b/itests/node_client/node_client.go @@ -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 }