Skip to content

Commit

Permalink
client: Drop overriding of session and netmap calls via global vars
Browse files Browse the repository at this point in the history
There is already an interface for this. Also, changing global variables
is always weird.

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Nov 19, 2024
1 parent 843085c commit 924a998
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 67 deletions.
10 changes: 2 additions & 8 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/session"
)

var (
// special variables for test purposes only, to overwrite real RPC calls.
rpcAPINetMapSnapshot = rpcapi.NetMapSnapshot
rpcAPICreateSession = rpcapi.CreateSession
)

// interface of NeoFS API server. Exists for test purposes only.
type neoFSAPIServer interface {
createSession(context.Context, session.CreateRequest) (*session.CreateResponse, error)
Expand All @@ -35,7 +29,7 @@ func rpcErr(e error) error {
// executes NetmapService.NetmapSnapshot RPC declared in NeoFS API protocol
// using underlying client.Client.
func (x *coreServer) netMapSnapshot(ctx context.Context, req v2netmap.SnapshotRequest) (*v2netmap.SnapshotResponse, error) {
resp, err := rpcAPINetMapSnapshot((*client.Client)(x), &req, client.WithContext(ctx))
resp, err := rpcapi.NetMapSnapshot((*client.Client)(x), &req, client.WithContext(ctx))
if err != nil {
return nil, rpcErr(err)
}
Expand All @@ -46,7 +40,7 @@ func (x *coreServer) netMapSnapshot(ctx context.Context, req v2netmap.SnapshotRe
// executes SessionService.Create RPC declared in NeoFS API protocol
// using underlying client.Client.
func (x *coreServer) createSession(ctx context.Context, req session.CreateRequest) (*session.CreateResponse, error) {
resp, err := rpcAPICreateSession((*client.Client)(x), &req, client.WithContext(ctx))
resp, err := rpcapi.CreateSession((*client.Client)(x), &req, client.WithContext(ctx))
if err != nil {
return nil, rpcErr(err)
}
Expand Down
52 changes: 11 additions & 41 deletions client/container_statistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,29 +480,15 @@ func TestClientStatistic_ContainerEndpointInfo(t *testing.T) {
func TestClientStatistic_ContainerNetMapSnapshot(t *testing.T) {
usr := usertest.User()
ctx := context.Background()
c := newClient(t, nil)

rpcAPINetMapSnapshot = func(_ *client.Client, _ *netmapv2.SnapshotRequest, _ ...client.CallOption) (*netmapv2.SnapshotResponse, error) {
var resp netmapv2.SnapshotResponse
var meta session.ResponseMetaHeader
var netMap netmapv2.NetMap

body := netmapv2.SnapshotResponseBody{}
body.SetNetMap(&netMap)

resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(usr, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

return &resp, nil
srv := serverNetMap{
signResponse: true,
statusOK: true,
setNetMap: true,
signer: usr,
}

c := newClient(t, &srv)
collector := newCollector()
c.prm.statisticCallback = collector.Collect
c.setNeoFSAPIServer((*coreServer)(&c.c))

_, err := c.NetMapSnapshot(ctx, PrmNetMapSnapshot{})
require.NoError(t, err)
Expand All @@ -513,30 +499,14 @@ func TestClientStatistic_ContainerNetMapSnapshot(t *testing.T) {
func TestClientStatistic_CreateSession(t *testing.T) {
usr := usertest.User()
ctx := context.Background()
c := newClient(t, nil)

rpcAPICreateSession = func(_ *client.Client, _ *session.CreateRequest, _ ...client.CallOption) (*session.CreateResponse, error) {
var resp session.CreateResponse
var meta session.ResponseMetaHeader

body := session.CreateResponseBody{}
body.SetID(randBytes(10))

body.SetSessionKey(neofscrypto.PublicKeyBytes(usr.Public()))

resp.SetBody(&body)
resp.SetMetaHeader(&meta)

if err := signServiceMessage(usr, &resp, nil); err != nil {
panic(fmt.Sprintf("sign response: %v", err))
}

return &resp, nil
srv := sessionAPIServer{
signer: usr,
id: randBytes(10),
key: neofscrypto.PublicKeyBytes(usr.Public()),
}

c := newClient(t, &srv)
collector := newCollector()
c.prm.statisticCallback = collector.Collect
c.setNeoFSAPIServer((*coreServer)(&c.c))

var prm PrmSessionCreate

Expand Down
6 changes: 2 additions & 4 deletions client/netmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

type serverNetMap struct {
unimplementedNeoFSAPIServer

errTransport error

signResponse bool
Expand All @@ -28,10 +30,6 @@ type serverNetMap struct {
signer neofscrypto.Signer
}

func (x *serverNetMap) createSession(context.Context, session.CreateRequest) (*session.CreateResponse, error) {
return nil, errors.New("unimplemented")
}

func (x *serverNetMap) netMapSnapshot(_ context.Context, req v2netmap.SnapshotRequest) (*v2netmap.SnapshotResponse, error) {
err := verifyServiceMessage(&req)
if err != nil {
Expand Down
22 changes: 8 additions & 14 deletions client/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ package client

import (
"context"
"errors"
"testing"

v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/nspcc-dev/neofs-api-go/v2/session"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
usertest "github.com/nspcc-dev/neofs-sdk-go/user/test"
"github.com/stretchr/testify/require"
)

type sessionAPIServer struct {
signer neofscrypto.Signer
setBody func(body *session.CreateResponseBody)
}
unimplementedNeoFSAPIServer
signer neofscrypto.Signer

func (m sessionAPIServer) netMapSnapshot(context.Context, v2netmap.SnapshotRequest) (*v2netmap.SnapshotResponse, error) {
return nil, errors.New("unimplemented")
id []byte
key []byte
}

func (m sessionAPIServer) createSession(context.Context, session.CreateRequest) (*session.CreateResponse, error) {
var body session.CreateResponseBody
m.setBody(&body)
body.SetID(m.id)
body.SetSessionKey(m.key)

var resp session.CreateResponse
resp.SetBody(&body)
Expand All @@ -45,9 +43,7 @@ func TestClient_SessionCreate(t *testing.T) {
prmSessionCreate.SetExp(1)

t.Run("missing session id", func(t *testing.T) {
c.setNeoFSAPIServer(&sessionAPIServer{signer: usr, setBody: func(body *session.CreateResponseBody) {
body.SetSessionKey([]byte{1})
}})
c.setNeoFSAPIServer(&sessionAPIServer{signer: usr, key: []byte{1}})

result, err := c.SessionCreate(ctx, usr, prmSessionCreate)
require.Nil(t, result)
Expand All @@ -56,9 +52,7 @@ func TestClient_SessionCreate(t *testing.T) {
})

t.Run("missing session key", func(t *testing.T) {
c.setNeoFSAPIServer(&sessionAPIServer{signer: usr, setBody: func(body *session.CreateResponseBody) {
body.SetID([]byte{1})
}})
c.setNeoFSAPIServer(&sessionAPIServer{signer: usr, id: []byte{1}})

result, err := c.SessionCreate(ctx, usr, prmSessionCreate)
require.Nil(t, result)
Expand Down
18 changes: 18 additions & 0 deletions client/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package client

import (
"context"
"errors"

"github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/nspcc-dev/neofs-api-go/v2/session"
)

type unimplementedNeoFSAPIServer struct{}

func (unimplementedNeoFSAPIServer) createSession(context.Context, session.CreateRequest) (*session.CreateResponse, error) {
return nil, errors.New("unimplemented")
}
func (unimplementedNeoFSAPIServer) netMapSnapshot(context.Context, netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
return nil, errors.New("unimplemented")
}

0 comments on commit 924a998

Please sign in to comment.