forked from TrueCloudLab/frostfs-sdk-go
106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"fmt"
|
|
netmap "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap/grpc"
|
|
"testing"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
/*
|
|
File contains common functionality used for client package testing.
|
|
*/
|
|
|
|
var key, _ = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
var statusErr apistatus.ServerInternal
|
|
|
|
func init() {
|
|
statusErr.SetMessage("test status error")
|
|
}
|
|
|
|
func assertStatusErr(tb testing.TB, res interface{ Status() apistatus.Status }) {
|
|
require.IsType(tb, &statusErr, res.Status())
|
|
require.Equal(tb, statusErr.Message(), res.Status().(*apistatus.ServerInternal).Message())
|
|
}
|
|
|
|
func newClient(server frostFSAPIServer) *Client {
|
|
prm := PrmInit{
|
|
Key: *key,
|
|
}
|
|
|
|
var c Client
|
|
c.Init(prm)
|
|
c.setFrostFSAPIServer(server)
|
|
|
|
return &c
|
|
}
|
|
|
|
func TestClient_DialContext(t *testing.T) {
|
|
var c Client
|
|
|
|
// try to connect to any host
|
|
prm := PrmDial{
|
|
Endpoint: "localhost:8080",
|
|
}
|
|
|
|
assert := func(ctx context.Context, errExpected error) {
|
|
// expect particular context error according to Dial docs
|
|
require.ErrorIs(t, c.Dial(ctx, prm), errExpected)
|
|
}
|
|
|
|
// create pre-abandoned context
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
assert(ctx, context.Canceled)
|
|
|
|
// create "pre-deadlined" context
|
|
ctx, cancel = context.WithTimeout(context.Background(), 0)
|
|
defer cancel()
|
|
|
|
assert(ctx, context.DeadlineExceeded)
|
|
}
|
|
|
|
func TestClient_NetMapDialContext(t *testing.T) {
|
|
var prmInit PrmInit
|
|
var c Client
|
|
publicKey := "foo"
|
|
endpoint := "localhost:8080"
|
|
|
|
prmInit.NetMap = &netmap.Netmap{
|
|
Epoch: 0,
|
|
Nodes: []*netmap.NodeInfo{
|
|
{
|
|
PublicKey: []byte(publicKey),
|
|
Addresses: []string{endpoint},
|
|
},
|
|
},
|
|
}
|
|
|
|
c.Init(prmInit)
|
|
|
|
assert := func(ctx context.Context, errExpected error) {
|
|
// expect particular context error according to Dial docs
|
|
//require.ErrorIs(t, c.Dial(ctx, prm), errExpected)
|
|
require.ErrorIs(t, c.NetMapDial(ctx, fmt.Sprintf("frostfs://%s", publicKey)), errExpected)
|
|
}
|
|
|
|
// create pre-abandoned context
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
assert(ctx, context.Canceled)
|
|
|
|
// create "pre-deadlined" context
|
|
ctx, cancel = context.WithTimeout(context.Background(), 0)
|
|
defer cancel()
|
|
|
|
assert(ctx, context.DeadlineExceeded)
|
|
}
|