2021-07-23 09:07:30 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
2022-07-12 18:57:33 +00:00
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2023-02-17 14:13:05 +00:00
|
|
|
|
2023-01-11 12:48:42 +00:00
|
|
|
"go.uber.org/zap"
|
2022-07-12 18:57:33 +00:00
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/accounting"
|
2024-07-03 14:20:58 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
|
2024-10-07 14:20:25 +00:00
|
|
|
sessionv2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
2023-03-07 11:20:03 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2022-07-12 18:57:33 +00:00
|
|
|
"github.com/google/uuid"
|
2021-07-23 09:07:30 +00:00
|
|
|
)
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
type mockClient struct {
|
2022-07-13 06:57:04 +00:00
|
|
|
key ecdsa.PrivateKey
|
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
|
2024-10-17 13:26:13 +00:00
|
|
|
errorOnEndpointInfo error
|
|
|
|
resOnEndpointInfo netmap.NodeInfo
|
|
|
|
healthcheckFn func()
|
2022-07-12 18:57:33 +00:00
|
|
|
errorOnNetworkInfo bool
|
2022-07-13 06:57:04 +00:00
|
|
|
stOnGetObject apistatus.Status
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
var _ client = (*mockClient)(nil)
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func newMockClient(addr string, key ecdsa.PrivateKey) *mockClient {
|
|
|
|
return &mockClient{
|
2022-07-13 06:57:04 +00:00
|
|
|
key: key,
|
2023-01-11 12:48:42 +00:00
|
|
|
clientStatusMonitor: newClientStatusMonitor(zap.NewExample(), addr, 10),
|
2022-07-12 18:57:33 +00:00
|
|
|
}
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
func newMockClientHealthy(addr string, key ecdsa.PrivateKey, healthy bool) *mockClient {
|
|
|
|
m := newMockClient(addr, key)
|
|
|
|
if healthy {
|
|
|
|
m.setHealthy()
|
|
|
|
} else {
|
|
|
|
m.setUnhealthy()
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
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() {
|
2024-10-17 13:26:13 +00:00
|
|
|
m.errorOnEndpointInfo = errors.New("error")
|
2021-11-25 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func (m *mockClient) errOnNetworkInfo() {
|
2024-10-17 13:26:13 +00:00
|
|
|
m.errorOnEndpointInfo = errors.New("error")
|
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
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func newToken(key ecdsa.PrivateKey) *session.Object {
|
|
|
|
var tok session.Object
|
|
|
|
tok.SetID(uuid.New())
|
2022-12-13 14:36:35 +00:00
|
|
|
pk := frostfsecdsa.PublicKey(key.PublicKey)
|
2022-07-12 18:57:33 +00:00
|
|
|
tok.SetAuthKey(&pk)
|
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
|
|
|
}
|
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
func (m *mockClient) apeManagerAddChain(context.Context, PrmAddAPEChain) error {
|
2024-07-03 14:20:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
func (m *mockClient) apeManagerRemoveChain(context.Context, PrmRemoveAPEChain) error {
|
2024-07-03 14:20:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
func (m *mockClient) apeManagerListChains(context.Context, PrmListAPEChains) ([]ape.Chain, error) {
|
2024-07-03 14:20:58 +00:00
|
|
|
return []ape.Chain{}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
func (m *mockClient) endpointInfo(ctx context.Context, _ prmEndpointInfo) (netmap.NodeInfo, error) {
|
2024-10-17 13:26:13 +00:00
|
|
|
if m.errorOnEndpointInfo != nil {
|
|
|
|
return netmap.NodeInfo{}, m.handleError(ctx, nil, m.errorOnEndpointInfo)
|
2022-07-12 18:57:33 +00:00
|
|
|
}
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
m.resOnEndpointInfo.SetNetworkEndpoints(m.addr)
|
|
|
|
return m.resOnEndpointInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockClient) healthcheck(ctx context.Context) (netmap.NodeInfo, error) {
|
|
|
|
if m.healthcheckFn != nil {
|
|
|
|
m.healthcheckFn()
|
|
|
|
}
|
|
|
|
return m.endpointInfo(ctx, prmEndpointInfo{})
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
func (m *mockClient) networkInfo(ctx context.Context, _ prmNetworkInfo) (netmap.NetworkInfo, error) {
|
2022-08-08 09:15:10 +00:00
|
|
|
var ni netmap.NetworkInfo
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
if m.errorOnNetworkInfo {
|
2023-03-29 10:20:37 +00:00
|
|
|
return ni, m.handleError(ctx, nil, errors.New("error"))
|
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
|
|
|
}
|
|
|
|
|
2023-10-18 08:30:25 +00:00
|
|
|
func (m *mockClient) netMapSnapshot(context.Context, prmNetMapSnapshot) (netmap.NetMap, error) {
|
|
|
|
var nm netmap.NetMap
|
|
|
|
return nm, nil
|
|
|
|
}
|
|
|
|
|
2024-07-12 11:35:01 +00:00
|
|
|
func (m *mockClient) objectPut(context.Context, PrmObjectPut) (ResPutObject, error) {
|
|
|
|
return ResPutObject{}, nil
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 20:18:00 +00:00
|
|
|
func (m *mockClient) objectPatch(context.Context, PrmObjectPatch) (ResPatchObject, error) {
|
|
|
|
return ResPatchObject{}, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
func (m *mockClient) objectGet(ctx context.Context, _ PrmObjectGet) (ResGetObject, error) {
|
2022-08-08 09:15:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
status := apistatus.ErrFromStatus(m.stOnGetObject)
|
2023-03-29 10:20:37 +00:00
|
|
|
return res, m.handleError(ctx, status, nil)
|
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
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
func (m *mockClient) sessionCreate(ctx context.Context, _ prmCreateSession) (resCreateSession, error) {
|
2022-07-12 18:57:33 +00:00
|
|
|
if m.errorOnCreateSession {
|
2023-03-29 10:20:37 +00:00
|
|
|
return resCreateSession{}, m.handleError(ctx, nil, errors.New("error"))
|
2022-07-12 18:57:33 +00:00
|
|
|
}
|
2021-07-23 09:07:30 +00:00
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
tok := newToken(m.key)
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
func (m *mockClient) restart(context.Context) error {
|
|
|
|
if m.errorOnDial {
|
|
|
|
return errors.New("restart dial error")
|
2022-11-03 14:58:38 +00:00
|
|
|
}
|
2024-10-17 13:26:13 +00:00
|
|
|
|
|
|
|
return nil
|
2022-11-03 14:58:38 +00:00
|
|
|
}
|
2023-09-20 09:14:37 +00:00
|
|
|
|
|
|
|
func (m *mockClient) close() error {
|
|
|
|
return nil
|
|
|
|
}
|