git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/pool/mock_test.go (about)

     1  package pool
     2  
     3  import (
     4  	"context"
     5  	"crypto/ecdsa"
     6  	"errors"
     7  
     8  	"go.uber.org/zap"
     9  
    10  	sessionv2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
    11  	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/accounting"
    12  	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ape"
    13  	apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
    14  	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
    15  	cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
    16  	frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
    17  	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
    18  	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
    19  	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
    20  	"github.com/google/uuid"
    21  )
    22  
    23  type mockClient struct {
    24  	key ecdsa.PrivateKey
    25  	clientStatusMonitor
    26  
    27  	errorOnDial          bool
    28  	errorOnCreateSession bool
    29  	errorOnEndpointInfo  error
    30  	resOnEndpointInfo    netmap.NodeInfo
    31  	healthcheckFn        func()
    32  	errorOnNetworkInfo   bool
    33  	stOnGetObject        apistatus.Status
    34  }
    35  
    36  var _ client = (*mockClient)(nil)
    37  
    38  func newMockClient(addr string, key ecdsa.PrivateKey) *mockClient {
    39  	return &mockClient{
    40  		key:                 key,
    41  		clientStatusMonitor: newClientStatusMonitor(zap.NewExample(), addr, 10),
    42  	}
    43  }
    44  
    45  func newMockClientHealthy(addr string, key ecdsa.PrivateKey, healthy bool) *mockClient {
    46  	m := newMockClient(addr, key)
    47  	if healthy {
    48  		m.setHealthy()
    49  	} else {
    50  		m.setUnhealthy()
    51  	}
    52  	return m
    53  }
    54  
    55  func (m *mockClient) setThreshold(threshold uint32) {
    56  	m.errorThreshold = threshold
    57  }
    58  
    59  func (m *mockClient) errOnCreateSession() {
    60  	m.errorOnCreateSession = true
    61  }
    62  
    63  func (m *mockClient) errOnEndpointInfo() {
    64  	m.errorOnEndpointInfo = errors.New("error")
    65  }
    66  
    67  func (m *mockClient) errOnNetworkInfo() {
    68  	m.errorOnEndpointInfo = errors.New("error")
    69  }
    70  
    71  func (m *mockClient) errOnDial() {
    72  	m.errorOnDial = true
    73  	m.errOnCreateSession()
    74  	m.errOnEndpointInfo()
    75  	m.errOnNetworkInfo()
    76  }
    77  
    78  func (m *mockClient) statusOnGetObject(st apistatus.Status) {
    79  	m.stOnGetObject = st
    80  }
    81  
    82  func newToken(key ecdsa.PrivateKey) *session.Object {
    83  	var tok session.Object
    84  	tok.SetID(uuid.New())
    85  	pk := frostfsecdsa.PublicKey(key.PublicKey)
    86  	tok.SetAuthKey(&pk)
    87  
    88  	return &tok
    89  }
    90  
    91  func (m *mockClient) balanceGet(context.Context, PrmBalanceGet) (accounting.Decimal, error) {
    92  	return accounting.Decimal{}, nil
    93  }
    94  
    95  func (m *mockClient) containerPut(context.Context, PrmContainerPut) (cid.ID, error) {
    96  	return cid.ID{}, nil
    97  }
    98  
    99  func (m *mockClient) containerGet(context.Context, PrmContainerGet) (container.Container, error) {
   100  	return container.Container{}, nil
   101  }
   102  
   103  func (m *mockClient) containerList(context.Context, PrmContainerList) ([]cid.ID, error) {
   104  	return nil, nil
   105  }
   106  
   107  func (m *mockClient) containerDelete(context.Context, PrmContainerDelete) error {
   108  	return nil
   109  }
   110  
   111  func (m *mockClient) apeManagerAddChain(context.Context, PrmAddAPEChain) error {
   112  	return nil
   113  }
   114  
   115  func (m *mockClient) apeManagerRemoveChain(context.Context, PrmRemoveAPEChain) error {
   116  	return nil
   117  }
   118  
   119  func (m *mockClient) apeManagerListChains(context.Context, PrmListAPEChains) ([]ape.Chain, error) {
   120  	return []ape.Chain{}, nil
   121  }
   122  
   123  func (m *mockClient) endpointInfo(ctx context.Context, _ prmEndpointInfo) (netmap.NodeInfo, error) {
   124  	if m.errorOnEndpointInfo != nil {
   125  		return netmap.NodeInfo{}, m.handleError(ctx, nil, m.errorOnEndpointInfo)
   126  	}
   127  
   128  	m.resOnEndpointInfo.SetNetworkEndpoints(m.addr)
   129  	return m.resOnEndpointInfo, nil
   130  }
   131  
   132  func (m *mockClient) healthcheck(ctx context.Context) (netmap.NodeInfo, error) {
   133  	if m.healthcheckFn != nil {
   134  		m.healthcheckFn()
   135  	}
   136  	return m.endpointInfo(ctx, prmEndpointInfo{})
   137  }
   138  
   139  func (m *mockClient) networkInfo(ctx context.Context, _ prmNetworkInfo) (netmap.NetworkInfo, error) {
   140  	var ni netmap.NetworkInfo
   141  
   142  	if m.errorOnNetworkInfo {
   143  		return ni, m.handleError(ctx, nil, errors.New("error"))
   144  	}
   145  
   146  	return ni, nil
   147  }
   148  
   149  func (m *mockClient) netMapSnapshot(context.Context, prmNetMapSnapshot) (netmap.NetMap, error) {
   150  	var nm netmap.NetMap
   151  	return nm, nil
   152  }
   153  
   154  func (m *mockClient) objectPut(context.Context, PrmObjectPut) (ResPutObject, error) {
   155  	return ResPutObject{}, nil
   156  }
   157  
   158  func (m *mockClient) objectPatch(context.Context, PrmObjectPatch) (ResPatchObject, error) {
   159  	return ResPatchObject{}, nil
   160  }
   161  
   162  func (m *mockClient) objectDelete(context.Context, PrmObjectDelete) error {
   163  	return nil
   164  }
   165  
   166  func (m *mockClient) objectGet(ctx context.Context, _ PrmObjectGet) (ResGetObject, error) {
   167  	var res ResGetObject
   168  
   169  	if m.stOnGetObject == nil {
   170  		return res, nil
   171  	}
   172  
   173  	status := apistatus.ErrFromStatus(m.stOnGetObject)
   174  	return res, m.handleError(ctx, status, nil)
   175  }
   176  
   177  func (m *mockClient) objectHead(context.Context, PrmObjectHead) (object.Object, error) {
   178  	return object.Object{}, nil
   179  }
   180  
   181  func (m *mockClient) objectRange(context.Context, PrmObjectRange) (ResObjectRange, error) {
   182  	return ResObjectRange{}, nil
   183  }
   184  
   185  func (m *mockClient) objectSearch(context.Context, PrmObjectSearch) (ResObjectSearch, error) {
   186  	return ResObjectSearch{}, nil
   187  }
   188  
   189  func (m *mockClient) sessionCreate(ctx context.Context, _ prmCreateSession) (resCreateSession, error) {
   190  	if m.errorOnCreateSession {
   191  		return resCreateSession{}, m.handleError(ctx, nil, errors.New("error"))
   192  	}
   193  
   194  	tok := newToken(m.key)
   195  
   196  	var v2tok sessionv2.Token
   197  	tok.WriteToV2(&v2tok)
   198  
   199  	return resCreateSession{
   200  		id:         v2tok.GetBody().GetID(),
   201  		sessionKey: v2tok.GetBody().GetSessionKey(),
   202  	}, nil
   203  }
   204  
   205  func (m *mockClient) dial(context.Context) error {
   206  	if m.errorOnDial {
   207  		return errors.New("dial error")
   208  	}
   209  	return nil
   210  }
   211  
   212  func (m *mockClient) restart(context.Context) error {
   213  	if m.errorOnDial {
   214  		return errors.New("restart dial error")
   215  	}
   216  
   217  	return nil
   218  }
   219  
   220  func (m *mockClient) close() error {
   221  	return nil
   222  }