2021-07-23 09:07:30 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
2022-07-12 18:57:33 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
sessionv2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/accounting"
|
2023-04-14 05:56:58 +00:00
|
|
|
sdkClient "github.com/nspcc-dev/neofs-sdk-go/client"
|
2022-07-13 06:57:04 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-07-12 18:57:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2023-04-25 08:31:27 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
2022-07-12 18:57:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2021-07-23 09:07:30 +00:00
|
|
|
)
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
type mockClient struct {
|
2023-04-25 08:31:27 +00:00
|
|
|
signer neofscrypto.Signer
|
2022-07-18 08:42:29 +00:00
|
|
|
clientStatusMonitor
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
errorOnDial bool
|
2022-07-12 18:57:33 +00:00
|
|
|
errorOnCreateSession bool
|
|
|
|
errorOnEndpointInfo bool
|
|
|
|
errorOnNetworkInfo bool
|
2022-07-13 06:57:04 +00:00
|
|
|
stOnGetObject apistatus.Status
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
func newMockClient(addr string, signer neofscrypto.Signer) *mockClient {
|
2022-07-12 18:57:33 +00:00
|
|
|
return &mockClient{
|
2023-04-25 08:31:27 +00:00
|
|
|
signer: signer,
|
2022-07-22 08:06:16 +00:00
|
|
|
clientStatusMonitor: newClientStatusMonitor(addr, 10),
|
2022-07-12 18:57:33 +00:00
|
|
|
}
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
func (m *mockClient) setThreshold(threshold uint32) {
|
|
|
|
m.errorThreshold = threshold
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) errOnCreateSession() {
|
|
|
|
m.errorOnCreateSession = true
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) errOnEndpointInfo() {
|
|
|
|
m.errorOnEndpointInfo = true
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) errOnNetworkInfo() {
|
|
|
|
m.errorOnEndpointInfo = true
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
func (m *mockClient) errOnDial() {
|
|
|
|
m.errorOnDial = true
|
|
|
|
m.errOnCreateSession()
|
|
|
|
m.errOnEndpointInfo()
|
|
|
|
m.errOnNetworkInfo()
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
func (m *mockClient) statusOnGetObject(st apistatus.Status) {
|
|
|
|
m.stOnGetObject = st
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
func newToken(signer neofscrypto.Signer) *session.Object {
|
2022-07-12 18:57:33 +00:00
|
|
|
var tok session.Object
|
|
|
|
tok.SetID(uuid.New())
|
2023-04-25 08:31:27 +00:00
|
|
|
|
|
|
|
public := signer.Public()
|
|
|
|
|
|
|
|
tok.SetAuthKey(public)
|
2021-11-25 08:28:23 +00:00
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
return &tok
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) balanceGet(context.Context, PrmBalanceGet) (accounting.Decimal, error) {
|
|
|
|
return accounting.Decimal{}, nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) containerPut(context.Context, PrmContainerPut) (cid.ID, error) {
|
|
|
|
return cid.ID{}, nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) containerGet(context.Context, PrmContainerGet) (container.Container, error) {
|
|
|
|
return container.Container{}, nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) containerList(context.Context, PrmContainerList) ([]cid.ID, error) {
|
|
|
|
return nil, nil
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) containerDelete(context.Context, PrmContainerDelete) error {
|
|
|
|
return nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) containerEACL(context.Context, PrmContainerEACL) (eacl.Table, error) {
|
|
|
|
return eacl.Table{}, nil
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) containerSetEACL(context.Context, PrmContainerSetEACL) error {
|
|
|
|
return nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) endpointInfo(context.Context, prmEndpointInfo) (netmap.NodeInfo, error) {
|
|
|
|
var ni netmap.NodeInfo
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
if m.errorOnEndpointInfo {
|
2023-05-19 07:00:11 +00:00
|
|
|
err := errors.New("endpoint info")
|
|
|
|
m.updateErrorRate(err)
|
|
|
|
return ni, err
|
2022-07-12 18:57:33 +00:00
|
|
|
}
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
ni.SetNetworkEndpoints(m.addr)
|
2022-08-08 09:15:10 +00:00
|
|
|
return ni, nil
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) networkInfo(context.Context, prmNetworkInfo) (netmap.NetworkInfo, error) {
|
|
|
|
var ni netmap.NetworkInfo
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
if m.errorOnNetworkInfo {
|
2023-05-19 07:00:11 +00:00
|
|
|
err := errors.New("network info")
|
|
|
|
m.updateErrorRate(err)
|
|
|
|
return ni, err
|
2022-07-12 18:57:33 +00:00
|
|
|
}
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return ni, nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) objectPut(context.Context, PrmObjectPut) (oid.ID, error) {
|
|
|
|
return oid.ID{}, nil
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) objectDelete(context.Context, PrmObjectDelete) error {
|
|
|
|
return nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) objectGet(context.Context, PrmObjectGet) (ResGetObject, error) {
|
|
|
|
var res ResGetObject
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
if m.stOnGetObject == nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return res, nil
|
2022-07-13 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 12:32:42 +00:00
|
|
|
err := apistatus.ErrFromStatus(m.stOnGetObject)
|
2023-05-19 07:00:11 +00:00
|
|
|
m.updateErrorRate(err)
|
|
|
|
return res, err
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) objectHead(context.Context, PrmObjectHead) (object.Object, error) {
|
|
|
|
return object.Object{}, nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) objectRange(context.Context, PrmObjectRange) (ResObjectRange, error) {
|
|
|
|
return ResObjectRange{}, nil
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) objectSearch(context.Context, PrmObjectSearch) (ResObjectSearch, error) {
|
|
|
|
return ResObjectSearch{}, nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
func (m *mockClient) sessionCreate(context.Context, prmCreateSession) (resCreateSession, error) {
|
2022-07-12 18:57:33 +00:00
|
|
|
if m.errorOnCreateSession {
|
2023-05-19 07:00:11 +00:00
|
|
|
err := errors.New("create session")
|
|
|
|
m.updateErrorRate(err)
|
|
|
|
return resCreateSession{}, err
|
2022-07-12 18:57:33 +00:00
|
|
|
}
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
tok := newToken(m.signer)
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
var v2tok sessionv2.Token
|
|
|
|
tok.WriteToV2(&v2tok)
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return resCreateSession{
|
2022-07-12 18:57:33 +00:00
|
|
|
id: v2tok.GetBody().GetID(),
|
|
|
|
sessionKey: v2tok.GetBody().GetSessionKey(),
|
|
|
|
}, nil
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
|
|
|
|
func (m *mockClient) dial(context.Context) error {
|
|
|
|
if m.errorOnDial {
|
|
|
|
return errors.New("dial error")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockClient) restartIfUnhealthy(ctx context.Context) (healthy bool, changed bool) {
|
|
|
|
_, err := m.endpointInfo(ctx, prmEndpointInfo{})
|
|
|
|
healthy = err == nil
|
|
|
|
changed = healthy != m.isHealthy()
|
|
|
|
if healthy {
|
|
|
|
m.setHealthy()
|
|
|
|
} else {
|
|
|
|
m.setUnhealthy()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-04-14 05:56:58 +00:00
|
|
|
|
|
|
|
func (m *mockClient) getClient() (*sdkClient.Client, error) {
|
|
|
|
return nil, errors.New("now supported to return sdkClient from mockClient")
|
|
|
|
}
|