From 35f81729e4749a2de2beaefc676315b563f507f4 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 16 Jun 2021 14:08:07 +0300 Subject: [PATCH] [#607] network: Do not return error from `Address.HostAddrString` method Panic if internal `manet.DialArgs` call returns error since this is unexpected according to `AddressFromString` implementation. Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/root.go | 7 +------ pkg/network/address.go | 10 +++++++--- pkg/network/address_test.go | 6 ++---- pkg/network/cache/client.go | 8 +------- pkg/services/object/get/get_test.go | 10 ++-------- pkg/services/object/search/search_test.go | 10 ++-------- 6 files changed, 15 insertions(+), 36 deletions(-) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 19db6432f..dca718641 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -257,13 +257,8 @@ func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) { return nil, err } - hostAddr, err := netAddr.HostAddrString() - if err != nil { - return nil, errInvalidEndpoint - } - options := []client.Option{ - client.WithAddress(hostAddr), + client.WithAddress(netAddr.HostAddrString()), client.WithDefaultPrivateKey(key), } diff --git a/pkg/network/address.go b/pkg/network/address.go index b39ed02a6..1bba70f69 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -64,13 +64,17 @@ func (a Address) IPAddrString() (string, error) { } // HostAddrString returns host address in string format. -func (a Address) HostAddrString() (string, error) { +// +// Panics if host address cannot be fetched from Address. +func (a Address) HostAddrString() string { _, host, err := manet.DialArgs(a.ma) if err != nil { - return "", fmt.Errorf("could not get host addr: %w", err) + // the only correct way to construct Address is AddressFromString + // which makes this error appear unexpected + panic(fmt.Errorf("could not get host addr: %w", err)) } - return host, nil + return host } // AddressFromString restores address from a string representation. diff --git a/pkg/network/address_test.go b/pkg/network/address_test.go index aa6f155ec..6ffeaacb9 100644 --- a/pkg/network/address_test.go +++ b/pkg/network/address_test.go @@ -62,8 +62,7 @@ func TestAddress_HostAddrString(t *testing.T) { for _, testcase := range testcases { addr := Address{testcase.ma} - got, err := addr.HostAddrString() - require.NoError(t, err) + got := addr.HostAddrString() require.Equal(t, testcase.exp, got) } @@ -76,8 +75,7 @@ func TestAddress_HostAddrString(t *testing.T) { for _, testcase := range testcases { addr := Address{testcase} - _, err := addr.HostAddrString() - require.Error(t, err) + require.Panics(t, func() { addr.HostAddrString() }) } }) } diff --git a/pkg/network/cache/client.go b/pkg/network/cache/client.go index 2e77ada0b..e62f37334 100644 --- a/pkg/network/cache/client.go +++ b/pkg/network/cache/client.go @@ -2,7 +2,6 @@ package cache import ( "crypto/tls" - "fmt" "sync" "github.com/nspcc-dev/neofs-api-go/pkg/client" @@ -55,12 +54,7 @@ func (c *ClientCache) Get(netAddr *network.Address) (client.Client, error) { return cli, nil } - hostAddr, err := netAddr.HostAddrString() - if err != nil { - return nil, fmt.Errorf("could not parse address as a string: %w", err) - } - - opts := append(c.opts, client.WithAddress(hostAddr)) + opts := append(c.opts, client.WithAddress(netAddr.HostAddrString())) if netAddr.TLSEnabled() { opts = append(opts, client.WithTLSConfig(&tls.Config{})) diff --git a/pkg/services/object/get/get_test.go b/pkg/services/object/get/get_test.go index b0232d686..70841a05a 100644 --- a/pkg/services/object/get/get_test.go +++ b/pkg/services/object/get/get_test.go @@ -83,12 +83,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap } func (c *testClientCache) get(mAddr *network.Address) (getClient, error) { - hostAddr, err := mAddr.HostAddrString() - if err != nil { - return nil, err - } - - v, ok := c.clients[hostAddr] + v, ok := c.clients[mAddr.HostAddrString()] if !ok { return nil, errors.New("could not construct client") } @@ -415,8 +410,7 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) { na, err := network.AddressFromString(a) require.NoError(t, err) - as[j], err = na.HostAddrString() - require.NoError(t, err) + as[j] = na.HostAddrString() ni := netmap.NewNodeInfo() ni.SetAddress(a) diff --git a/pkg/services/object/search/search_test.go b/pkg/services/object/search/search_test.go index 394b6ef8f..ab7c8250a 100644 --- a/pkg/services/object/search/search_test.go +++ b/pkg/services/object/search/search_test.go @@ -85,12 +85,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap } func (c *testClientCache) get(mAddr *network.Address) (searchClient, error) { - hostAddr, err := mAddr.HostAddrString() - if err != nil { - return nil, err - } - - v, ok := c.clients[hostAddr] + v, ok := c.clients[mAddr.HostAddrString()] if !ok { return nil, errors.New("could not construct client") } @@ -209,8 +204,7 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) { na, err := network.AddressFromString(a) require.NoError(t, err) - as[j], err = na.HostAddrString() - require.NoError(t, err) + as[j] = na.HostAddrString() ni := netmap.NewNodeInfo() ni.SetAddress(a)