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
|
|
|
sessionv2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/accounting"
|
|
|
|
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/eacl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"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
|
|
|
|
errorOnEndpointInfo bool
|
|
|
|
errorOnNetworkInfo bool
|
2022-07-13 06:57:04 +00:00
|
|
|
stOnGetObject apistatus.Status
|
2021-07-23 09:07:30 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
func (m *mockClient) endpointInfo(ctx context.Context, _ prmEndpointInfo) (netmap.NodeInfo, error) {
|
2022-08-08 09:15:10 +00:00
|
|
|
var ni netmap.NodeInfo
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
if m.errorOnEndpointInfo {
|
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-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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|