2021-07-20 08:02:14 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-22 12:38:15 +00:00
|
|
|
"crypto/ecdsa"
|
2022-07-12 18:57:33 +00:00
|
|
|
"errors"
|
2024-10-17 13:26:13 +00:00
|
|
|
"math/rand"
|
2021-07-20 08:02:14 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
2024-10-17 13:26:13 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2023-03-07 11:20:03 +00:00
|
|
|
"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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-12-10 13:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-07-20 08:02:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-07 14:34:20 +00:00
|
|
|
"go.uber.org/zap"
|
2024-10-16 12:22:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2024-10-17 13:26:13 +00:00
|
|
|
"go.uber.org/zap/zaptest/observer"
|
2021-07-20 08:02:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildPoolClientFailed(t *testing.T) {
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
|
|
|
mockCli := newMockClient(addr, *newPrivateKey(t))
|
|
|
|
mockCli.errOnDial()
|
|
|
|
return mockCli
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-10-03 11:04:30 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{{1, "peer0", 1}},
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildPoolCreateSessionFailed(t *testing.T) {
|
2022-11-03 14:58:38 +00:00
|
|
|
clientMockBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
mockCli := newMockClient(addr, *newPrivateKey(t))
|
|
|
|
mockCli.errOnCreateSession()
|
2022-11-03 14:58:38 +00:00
|
|
|
return mockCli
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-10-03 11:04:30 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{{1, "peer0", 1}},
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(clientMockBuilder)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
func newPrivateKey(t *testing.T) *ecdsa.PrivateKey {
|
2021-12-10 13:56:04 +00:00
|
|
|
p, err := keys.NewPrivateKey()
|
2021-11-22 12:38:15 +00:00
|
|
|
require.NoError(t, err)
|
2021-12-10 13:56:04 +00:00
|
|
|
return &p.PrivateKey
|
2021-11-22 12:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func TestBuildPoolOneNodeFailed(t *testing.T) {
|
|
|
|
nodes := []NodeParam{
|
|
|
|
{1, "peer0", 1},
|
|
|
|
{2, "peer1", 1},
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
var clientKeys []*ecdsa.PrivateKey
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
|
|
|
clientKeys = append(clientKeys, key)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
if addr == nodes[0].address {
|
|
|
|
mockCli := newMockClient(addr, *key)
|
|
|
|
mockCli.errOnEndpointInfo()
|
2022-11-03 14:58:38 +00:00
|
|
|
return mockCli
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
2022-07-12 18:57:33 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
return newMockClient(addr, *key)
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log, err := zap.NewProduction()
|
|
|
|
require.NoError(t, err)
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
clientRebalanceInterval: 1000 * time.Millisecond,
|
|
|
|
logger: log,
|
2022-07-12 18:57:33 +00:00
|
|
|
nodeParams: nodes,
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-09-07 14:34:20 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
clientPool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = clientPool.Dial(context.Background())
|
2021-09-07 14:34:20 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(clientPool.Close)
|
2021-09-07 14:34:20 +00:00
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
expectedAuthKey := frostfsecdsa.PublicKey(clientKeys[1].PublicKey)
|
2021-09-07 14:34:20 +00:00
|
|
|
condition := func() bool {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := clientPool.connection()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := clientPool.cache.Get(formCacheKey(cp.address(), clientPool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
return st.AssertAuthKey(&expectedAuthKey)
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
require.Never(t, condition, 900*time.Millisecond, 100*time.Millisecond)
|
|
|
|
require.Eventually(t, condition, 3*time.Second, 300*time.Millisecond)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildPoolZeroNodes(t *testing.T) {
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-03-11 14:55:02 +00:00
|
|
|
_, err := NewPool(opts)
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOneNode(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
key1 := newPrivateKey(t)
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
|
|
|
return newMockClient(addr, *key1)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-10-03 11:04:30 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{{1, "peer0", 1}},
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-12-13 14:36:35 +00:00
|
|
|
expectedAuthKey := frostfsecdsa.PublicKey(key1.PublicKey)
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, st.AssertAuthKey(&expectedAuthKey))
|
2022-03-25 08:34:09 +00:00
|
|
|
}
|
2021-11-17 15:52:43 +00:00
|
|
|
|
2022-03-25 08:34:09 +00:00
|
|
|
func TestTwoNodes(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
var clientKeys []*ecdsa.PrivateKey
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
|
|
|
clientKeys = append(clientKeys, key)
|
2022-11-03 14:58:38 +00:00
|
|
|
return newMockClient(addr, *key)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
{1, "peer1", 1},
|
|
|
|
},
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, assertAuthKeyForAny(st, clientKeys))
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func assertAuthKeyForAny(st session.Object, clientKeys []*ecdsa.PrivateKey) bool {
|
|
|
|
for _, key := range clientKeys {
|
2022-12-13 14:36:35 +00:00
|
|
|
expectedAuthKey := frostfsecdsa.PublicKey(key.PublicKey)
|
2022-07-12 18:57:33 +00:00
|
|
|
if st.AssertAuthKey(&expectedAuthKey) {
|
2022-03-25 08:34:09 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-11-17 15:52:43 +00:00
|
|
|
|
2022-03-25 08:34:09 +00:00
|
|
|
func TestOneOfTwoFailed(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
nodes := []NodeParam{
|
|
|
|
{1, "peer0", 1},
|
|
|
|
{9, "peer1", 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientKeys []*ecdsa.PrivateKey
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
|
|
|
clientKeys = append(clientKeys, key)
|
|
|
|
|
|
|
|
if addr == nodes[0].address {
|
2022-11-03 14:58:38 +00:00
|
|
|
return newMockClient(addr, *key)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-07-12 18:57:33 +00:00
|
|
|
|
|
|
|
mockCli := newMockClient(addr, *key)
|
|
|
|
mockCli.errOnEndpointInfo()
|
|
|
|
mockCli.errOnNetworkInfo()
|
2022-11-03 14:58:38 +00:00
|
|
|
return mockCli
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-07-12 18:57:33 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: nodes,
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 200 * time.Millisecond,
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
|
|
|
require.NoError(t, err)
|
2022-03-11 11:45:36 +00:00
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2024-09-03 10:20:30 +00:00
|
|
|
for range 5 {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, assertAuthKeyForAny(st, clientKeys))
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:26:13 +00:00
|
|
|
func TestUpdateNodesHealth(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
key := newPrivateKey(t)
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
wasHealthy bool
|
|
|
|
willHealthy bool
|
|
|
|
prepareCli func(*mockClient)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "healthy, maintenance, unhealthy",
|
|
|
|
wasHealthy: true,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) { c.resOnEndpointInfo.SetStatus(netmap.Maintenance) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unhealthy, maintenance, unhealthy",
|
|
|
|
wasHealthy: false,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) { c.resOnEndpointInfo.SetStatus(netmap.Maintenance) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "healthy, no error, healthy",
|
|
|
|
wasHealthy: true,
|
|
|
|
willHealthy: true,
|
|
|
|
prepareCli: func(c *mockClient) { c.resOnEndpointInfo.SetStatus(netmap.Online) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unhealthy, no error, healthy",
|
|
|
|
wasHealthy: false,
|
|
|
|
willHealthy: true,
|
|
|
|
prepareCli: func(c *mockClient) { c.resOnEndpointInfo.SetStatus(netmap.Online) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "healthy, error, failed restart, unhealthy",
|
|
|
|
wasHealthy: true,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) {
|
|
|
|
c.errOnEndpointInfo()
|
|
|
|
c.errorOnDial = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unhealthy, error, failed restart, unhealthy",
|
|
|
|
wasHealthy: false,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) {
|
|
|
|
c.errOnEndpointInfo()
|
|
|
|
c.errorOnDial = true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "healthy, error, restart, error, unhealthy",
|
|
|
|
wasHealthy: true,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) { c.errOnEndpointInfo() },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unhealthy, error, restart, error, unhealthy",
|
|
|
|
wasHealthy: false,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) { c.errOnEndpointInfo() },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "healthy, error, restart, maintenance, unhealthy",
|
|
|
|
wasHealthy: true,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) {
|
|
|
|
healthError := true
|
|
|
|
c.healthcheckFn = func() {
|
|
|
|
if healthError {
|
|
|
|
c.errorOnEndpointInfo = errors.New("error")
|
|
|
|
} else {
|
|
|
|
c.errorOnEndpointInfo = nil
|
|
|
|
c.resOnEndpointInfo.SetStatus(netmap.Maintenance)
|
|
|
|
}
|
|
|
|
healthError = !healthError
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unhealthy, error, restart, maintenance, unhealthy",
|
|
|
|
wasHealthy: false,
|
|
|
|
willHealthy: false,
|
|
|
|
prepareCli: func(c *mockClient) {
|
|
|
|
healthError := true
|
|
|
|
c.healthcheckFn = func() {
|
|
|
|
if healthError {
|
|
|
|
c.errorOnEndpointInfo = errors.New("error")
|
|
|
|
} else {
|
|
|
|
c.errorOnEndpointInfo = nil
|
|
|
|
c.resOnEndpointInfo.SetStatus(netmap.Maintenance)
|
|
|
|
}
|
|
|
|
healthError = !healthError
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "healthy, error, restart, healthy",
|
|
|
|
wasHealthy: true,
|
|
|
|
willHealthy: true,
|
|
|
|
prepareCli: func(c *mockClient) {
|
|
|
|
healthError := true
|
|
|
|
c.healthcheckFn = func() {
|
|
|
|
if healthError {
|
|
|
|
c.errorOnEndpointInfo = errors.New("error")
|
|
|
|
} else {
|
|
|
|
c.errorOnEndpointInfo = nil
|
|
|
|
}
|
|
|
|
healthError = !healthError
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unhealthy, error, restart, healthy",
|
|
|
|
wasHealthy: false,
|
|
|
|
willHealthy: true,
|
|
|
|
prepareCli: func(c *mockClient) {
|
|
|
|
healthError := true
|
|
|
|
c.healthcheckFn = func() {
|
|
|
|
if healthError {
|
|
|
|
c.errorOnEndpointInfo = errors.New("error")
|
|
|
|
} else {
|
|
|
|
c.errorOnEndpointInfo = nil
|
|
|
|
}
|
|
|
|
healthError = !healthError
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cli := newMockClientHealthy("peer0", *key, tc.wasHealthy)
|
|
|
|
tc.prepareCli(cli)
|
|
|
|
p, log := newPool(t, cli)
|
|
|
|
|
|
|
|
p.updateNodesHealth(ctx, [][]float64{{1}})
|
|
|
|
|
|
|
|
changed := tc.wasHealthy != tc.willHealthy
|
|
|
|
require.Equalf(t, tc.willHealthy, cli.isHealthy(), "healthy status should be: %v", tc.willHealthy)
|
|
|
|
require.Equalf(t, changed, 1 == log.Len(), "healthy status should be changed: %v", changed)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPool(t *testing.T, cli *mockClient) (*Pool, *observer.ObservedLogs) {
|
|
|
|
log, observedLog := getObservedLogger()
|
|
|
|
|
|
|
|
cache, err := newCache(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return &Pool{
|
|
|
|
innerPools: []*innerPool{{
|
|
|
|
sampler: newSampler([]float64{1}, rand.NewSource(0)),
|
|
|
|
clients: []client{cli},
|
|
|
|
}},
|
|
|
|
cache: cache,
|
|
|
|
key: newPrivateKey(t),
|
|
|
|
closedCh: make(chan struct{}),
|
|
|
|
rebalanceParams: rebalanceParameters{
|
|
|
|
nodesParams: []*nodesParam{{1, []string{"peer0"}, []float64{1}}},
|
|
|
|
nodeRequestTimeout: time.Second,
|
|
|
|
clientRebalanceInterval: 200 * time.Millisecond,
|
|
|
|
},
|
|
|
|
logger: log,
|
|
|
|
}, observedLog
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObservedLogger() (*zap.Logger, *observer.ObservedLogs) {
|
|
|
|
loggerCore, observedLog := observer.New(zap.DebugLevel)
|
|
|
|
return zap.New(loggerCore), observedLog
|
|
|
|
}
|
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
func TestTwoFailed(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
var clientKeys []*ecdsa.PrivateKey
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
|
|
|
clientKeys = append(clientKeys, key)
|
|
|
|
mockCli := newMockClient(addr, *key)
|
|
|
|
mockCli.errOnEndpointInfo()
|
2022-11-03 14:58:38 +00:00
|
|
|
return mockCli
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
{1, "peer1", 1},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 200 * time.Millisecond,
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-11 14:55:02 +00:00
|
|
|
err = pool.Dial(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
_, err = pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "no healthy")
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
func TestSessionCache(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
2022-12-13 14:36:35 +00:00
|
|
|
expectedAuthKey := frostfsecdsa.PublicKey(key.PublicKey)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
mockCli := newMockClient(addr, *key)
|
2023-08-02 07:01:03 +00:00
|
|
|
mockCli.statusOnGetObject(new(apistatus.SessionTokenNotFound))
|
2022-11-03 14:58:38 +00:00
|
|
|
return mockCli
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 30 * time.Second,
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(ctx)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-16 13:50:33 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
// cache must contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, st.AssertAuthKey(&expectedAuthKey))
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var prm PrmObjectGet
|
2022-05-25 09:03:22 +00:00
|
|
|
prm.SetAddress(oid.Address{})
|
2022-04-07 16:09:15 +00:00
|
|
|
prm.UseSession(session.Object{})
|
2022-03-11 13:06:08 +00:00
|
|
|
|
|
|
|
_, err = pool.GetObject(ctx, prm)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// cache must not contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err = pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
_, ok := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-04-07 16:09:15 +00:00
|
|
|
require.False(t, ok)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var prm2 PrmObjectPut
|
|
|
|
prm2.SetHeader(object.Object{})
|
|
|
|
|
|
|
|
_, err = pool.PutObject(ctx, prm2)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// cache must contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err = pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ = pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, st.AssertAuthKey(&expectedAuthKey))
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
func TestPriority(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
nodes := []NodeParam{
|
|
|
|
{1, "peer0", 1},
|
|
|
|
{2, "peer1", 100},
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientKeys []*ecdsa.PrivateKey
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
|
|
|
clientKeys = append(clientKeys, key)
|
|
|
|
|
|
|
|
if addr == nodes[0].address {
|
|
|
|
mockCli := newMockClient(addr, *key)
|
|
|
|
mockCli.errOnEndpointInfo()
|
2022-11-03 14:58:38 +00:00
|
|
|
return mockCli
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
2022-07-12 18:57:33 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
return newMockClient(addr, *key)
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-07-12 18:57:33 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: nodes,
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 1500 * time.Millisecond,
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-11-16 13:50:33 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(ctx)
|
2021-11-16 13:50:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(pool.Close)
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
expectedAuthKey1 := frostfsecdsa.PublicKey(clientKeys[0].PublicKey)
|
2021-11-16 13:50:33 +00:00
|
|
|
firstNode := func() bool {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-11-16 13:50:33 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
return st.AssertAuthKey(&expectedAuthKey1)
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
2022-07-12 18:57:33 +00:00
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
expectedAuthKey2 := frostfsecdsa.PublicKey(clientKeys[1].PublicKey)
|
2021-11-16 13:50:33 +00:00
|
|
|
secondNode := func() bool {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-11-16 13:50:33 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
return st.AssertAuthKey(&expectedAuthKey2)
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
require.Never(t, secondNode, time.Second, 200*time.Millisecond)
|
|
|
|
|
|
|
|
require.Eventually(t, secondNode, time.Second, 200*time.Millisecond)
|
|
|
|
require.Never(t, firstNode, time.Second, 200*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func TestSessionCacheWithKey(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
2022-12-13 14:36:35 +00:00
|
|
|
expectedAuthKey := frostfsecdsa.PublicKey(key.PublicKey)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
|
|
|
return newMockClient(addr, *key)
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 30 * time.Second,
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(ctx)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// cache must contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address(), pool.key, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, st.AssertAuthKey(&expectedAuthKey))
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-11-15 08:41:28 +00:00
|
|
|
var prm PrmObjectDelete
|
2022-05-25 09:03:22 +00:00
|
|
|
prm.SetAddress(oid.Address{})
|
2022-07-12 18:57:33 +00:00
|
|
|
anonKey := newPrivateKey(t)
|
|
|
|
prm.UseKey(anonKey)
|
2022-03-11 13:06:08 +00:00
|
|
|
|
2022-11-15 08:41:28 +00:00
|
|
|
err = pool.DeleteObject(ctx, prm)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-11 09:02:23 +00:00
|
|
|
st, _ = pool.cache.Get(formCacheKey(cp.address(), anonKey, false))
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, st.AssertAuthKey(&expectedAuthKey))
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 10:35:39 +00:00
|
|
|
func TestSessionTokenOwner(t *testing.T) {
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-12 18:57:33 +00:00
|
|
|
key := newPrivateKey(t)
|
2022-11-03 14:58:38 +00:00
|
|
|
return newMockClient(addr, *key)
|
2022-01-17 10:35:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
},
|
2022-01-17 10:35:39 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2022-01-17 10:35:39 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
p, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = p.Dial(ctx)
|
2022-01-17 10:35:39 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-09 09:40:23 +00:00
|
|
|
t.Cleanup(p.Close)
|
2022-01-17 10:35:39 +00:00
|
|
|
|
|
|
|
anonKey := newPrivateKey(t)
|
2022-04-11 06:30:22 +00:00
|
|
|
var anonOwner user.ID
|
|
|
|
user.IDFromKey(&anonOwner, anonKey.PublicKey)
|
2022-01-17 10:35:39 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var prm prmCommon
|
|
|
|
prm.UseKey(anonKey)
|
2022-03-25 08:34:09 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
var tkn session.Object
|
2022-03-17 09:53:04 +00:00
|
|
|
var cc callContext
|
2022-07-12 18:57:33 +00:00
|
|
|
cc.sessionTarget = func(tok session.Object) {
|
|
|
|
tkn = tok
|
|
|
|
}
|
2022-03-25 08:34:09 +00:00
|
|
|
err = p.initCallContext(&cc, prm, prmCtx)
|
2022-01-17 10:35:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
err = p.openDefaultSession(ctx, &cc)
|
2022-03-17 09:53:04 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
require.True(t, tkn.VerifySignature())
|
2022-07-12 18:57:33 +00:00
|
|
|
require.True(t, tkn.Issuer().Equals(anonOwner))
|
2022-01-17 10:35:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
func TestWaitPresence(t *testing.T) {
|
2022-07-12 18:57:33 +00:00
|
|
|
mockCli := newMockClient("", *newPrivateKey(t))
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
t.Run("context canceled", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
2024-03-01 12:16:16 +00:00
|
|
|
err := waitForContainerPresence(ctx, mockCli, PrmContainerGet{}, &WaitParams{
|
2023-08-03 09:14:00 +00:00
|
|
|
Timeout: 120 * time.Second,
|
|
|
|
PollInterval: 5 * time.Second,
|
2022-03-28 08:06:23 +00:00
|
|
|
})
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "context canceled")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("context deadline exceeded", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2024-03-01 12:16:16 +00:00
|
|
|
|
|
|
|
err := waitForContainerPresence(ctx, mockCli, PrmContainerGet{}, &WaitParams{
|
2023-08-03 09:14:00 +00:00
|
|
|
Timeout: 500 * time.Millisecond,
|
|
|
|
PollInterval: 5 * time.Second,
|
2021-07-20 08:02:14 +00:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "context deadline exceeded")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2024-03-01 12:16:16 +00:00
|
|
|
|
|
|
|
err := waitForContainerPresence(ctx, mockCli, PrmContainerGet{}, &WaitParams{
|
2023-08-03 09:14:00 +00:00
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
PollInterval: 500 * time.Millisecond,
|
2021-07-20 08:02:14 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
2022-07-12 18:57:33 +00:00
|
|
|
|
2022-07-13 09:55:38 +00:00
|
|
|
func TestStatusMonitor(t *testing.T) {
|
2023-01-11 12:48:42 +00:00
|
|
|
monitor := newClientStatusMonitor(zap.NewExample(), "", 10)
|
2022-07-18 08:42:29 +00:00
|
|
|
monitor.errorThreshold = 3
|
2022-07-13 09:55:38 +00:00
|
|
|
|
|
|
|
count := 10
|
2024-09-03 10:20:30 +00:00
|
|
|
for range count {
|
2022-07-13 09:55:38 +00:00
|
|
|
monitor.incErrorRate()
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, uint64(count), monitor.overallErrorRate())
|
2022-07-18 08:42:29 +00:00
|
|
|
require.Equal(t, uint32(1), monitor.currentErrorRate())
|
2023-10-03 13:49:57 +00:00
|
|
|
|
|
|
|
t.Run("healthy status", func(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
action func(*clientStatusMonitor)
|
|
|
|
status uint32
|
|
|
|
isDialed bool
|
|
|
|
isHealthy bool
|
|
|
|
description string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
action: func(m *clientStatusMonitor) { m.setUnhealthy() },
|
|
|
|
status: statusUnhealthyOnRequest,
|
|
|
|
isDialed: true,
|
|
|
|
isHealthy: false,
|
|
|
|
description: "set unhealthy on request",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: func(m *clientStatusMonitor) { m.setHealthy() },
|
|
|
|
status: statusHealthy,
|
|
|
|
isDialed: true,
|
|
|
|
isHealthy: true,
|
|
|
|
description: "set healthy",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc.action(&monitor)
|
|
|
|
require.Equal(t, tc.status, monitor.healthy.Load())
|
|
|
|
require.Equal(t, tc.isHealthy, monitor.isHealthy())
|
|
|
|
}
|
|
|
|
})
|
2022-07-13 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:57:33 +00:00
|
|
|
func TestHandleError(t *testing.T) {
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx := context.Background()
|
2024-10-16 12:22:05 +00:00
|
|
|
log := zaptest.NewLogger(t)
|
2022-07-12 18:57:33 +00:00
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
canceledCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
2024-10-16 12:22:05 +00:00
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
ctx context.Context
|
|
|
|
status apistatus.Status
|
|
|
|
err error
|
|
|
|
expectedError bool
|
|
|
|
countError bool
|
|
|
|
markedUnhealthy bool
|
2022-07-12 18:57:33 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, no status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2022-07-12 18:57:33 +00:00
|
|
|
status: nil,
|
|
|
|
err: nil,
|
|
|
|
expectedError: false,
|
|
|
|
countError: false,
|
|
|
|
},
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, success status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2023-08-02 07:01:03 +00:00
|
|
|
status: new(apistatus.SuccessDefaultV2),
|
2022-07-12 18:57:33 +00:00
|
|
|
err: nil,
|
|
|
|
expectedError: false,
|
|
|
|
countError: false,
|
|
|
|
},
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "error, success status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2023-08-02 07:01:03 +00:00
|
|
|
status: new(apistatus.SuccessDefaultV2),
|
2022-07-12 18:57:33 +00:00
|
|
|
err: errors.New("error"),
|
|
|
|
expectedError: true,
|
|
|
|
countError: true,
|
|
|
|
},
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "error, no status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2022-07-12 18:57:33 +00:00
|
|
|
status: nil,
|
|
|
|
err: errors.New("error"),
|
|
|
|
expectedError: true,
|
|
|
|
countError: true,
|
|
|
|
},
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, object not found status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2023-08-02 07:01:03 +00:00
|
|
|
status: new(apistatus.ObjectNotFound),
|
2022-07-12 18:57:33 +00:00
|
|
|
err: nil,
|
|
|
|
expectedError: true,
|
|
|
|
countError: false,
|
|
|
|
},
|
2024-08-14 13:48:06 +00:00
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "object not found error, object not found status",
|
2024-08-14 13:48:06 +00:00
|
|
|
ctx: ctx,
|
|
|
|
status: new(apistatus.ObjectNotFound),
|
|
|
|
err: &apistatus.ObjectNotFound{},
|
|
|
|
expectedError: true,
|
|
|
|
countError: false,
|
|
|
|
},
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "eacl not found error, no status",
|
2024-08-14 13:48:06 +00:00
|
|
|
ctx: ctx,
|
|
|
|
status: nil,
|
|
|
|
err: &apistatus.EACLNotFound{},
|
|
|
|
expectedError: true,
|
|
|
|
// we expect error be counted because status is nil
|
|
|
|
// currently we assume that DisableFrostFSErrorResolution be always false for pool
|
|
|
|
// and status be checked first in handleError
|
|
|
|
countError: true,
|
|
|
|
},
|
2022-07-12 18:57:33 +00:00
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, internal status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2023-08-02 07:01:03 +00:00
|
|
|
status: new(apistatus.ServerInternal),
|
2022-07-12 18:57:33 +00:00
|
|
|
err: nil,
|
|
|
|
expectedError: true,
|
|
|
|
countError: true,
|
|
|
|
},
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, wrong magic status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2023-08-02 07:01:03 +00:00
|
|
|
status: new(apistatus.WrongMagicNumber),
|
2022-07-12 18:57:33 +00:00
|
|
|
err: nil,
|
|
|
|
expectedError: true,
|
|
|
|
countError: true,
|
|
|
|
},
|
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, signature verification status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2023-08-02 07:01:03 +00:00
|
|
|
status: new(apistatus.SignatureVerification),
|
2022-07-12 18:57:33 +00:00
|
|
|
err: nil,
|
|
|
|
expectedError: true,
|
|
|
|
countError: true,
|
|
|
|
},
|
2022-07-13 06:57:04 +00:00
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, maintenance status",
|
|
|
|
ctx: ctx,
|
|
|
|
status: new(apistatus.NodeUnderMaintenance),
|
|
|
|
err: nil,
|
|
|
|
expectedError: true,
|
|
|
|
countError: true,
|
|
|
|
markedUnhealthy: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "maintenance error, no status",
|
|
|
|
ctx: ctx,
|
|
|
|
status: nil,
|
|
|
|
err: &apistatus.NodeUnderMaintenance{},
|
|
|
|
expectedError: true,
|
|
|
|
countError: true,
|
|
|
|
markedUnhealthy: true,
|
2022-07-13 06:57:04 +00:00
|
|
|
},
|
2022-09-15 05:35:27 +00:00
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "no error, invalid argument status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: ctx,
|
2024-10-02 08:12:49 +00:00
|
|
|
status: new(apistatus.InvalidArgument),
|
2022-09-15 05:35:27 +00:00
|
|
|
err: nil,
|
|
|
|
expectedError: true,
|
2024-10-02 08:12:49 +00:00
|
|
|
countError: false,
|
2022-09-15 05:35:27 +00:00
|
|
|
},
|
2023-03-29 10:20:37 +00:00
|
|
|
{
|
2024-10-16 12:22:05 +00:00
|
|
|
name: "context canceled error, no status",
|
2023-03-29 10:20:37 +00:00
|
|
|
ctx: canceledCtx,
|
|
|
|
status: nil,
|
|
|
|
err: errors.New("error"),
|
|
|
|
expectedError: true,
|
|
|
|
countError: false,
|
|
|
|
},
|
2022-07-12 18:57:33 +00:00
|
|
|
} {
|
2024-10-16 12:22:05 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
monitor := newClientStatusMonitor(log, "", 10)
|
|
|
|
errCount := monitor.overallErrorRate()
|
2023-03-29 10:20:37 +00:00
|
|
|
err := monitor.handleError(tc.ctx, tc.status, tc.err)
|
2022-07-12 18:57:33 +00:00
|
|
|
if tc.expectedError {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tc.countError {
|
|
|
|
errCount++
|
|
|
|
}
|
2024-10-16 12:22:05 +00:00
|
|
|
require.Equal(t, errCount, monitor.overallErrorRate())
|
|
|
|
if tc.markedUnhealthy {
|
|
|
|
require.False(t, monitor.isHealthy())
|
|
|
|
}
|
2022-07-12 18:57:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 06:57:04 +00:00
|
|
|
|
|
|
|
func TestSwitchAfterErrorThreshold(t *testing.T) {
|
|
|
|
nodes := []NodeParam{
|
|
|
|
{1, "peer0", 1},
|
|
|
|
{2, "peer1", 100},
|
|
|
|
}
|
|
|
|
|
|
|
|
errorThreshold := 5
|
|
|
|
|
|
|
|
var clientKeys []*ecdsa.PrivateKey
|
2022-11-03 14:58:38 +00:00
|
|
|
mockClientBuilder := func(addr string) client {
|
2022-07-13 06:57:04 +00:00
|
|
|
key := newPrivateKey(t)
|
|
|
|
clientKeys = append(clientKeys, key)
|
|
|
|
|
|
|
|
if addr == nodes[0].address {
|
|
|
|
mockCli := newMockClient(addr, *key)
|
|
|
|
mockCli.setThreshold(uint32(errorThreshold))
|
2023-08-02 07:01:03 +00:00
|
|
|
mockCli.statusOnGetObject(new(apistatus.ServerInternal))
|
2022-11-03 14:58:38 +00:00
|
|
|
return mockCli
|
2022-07-13 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
return newMockClient(addr, *key)
|
2022-07-13 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts := InitParameters{
|
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: nodes,
|
|
|
|
clientRebalanceInterval: 30 * time.Second,
|
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
opts.setClientBuilder(mockClientBuilder)
|
2022-07-13 06:57:04 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(pool.Close)
|
|
|
|
|
2024-09-03 10:20:30 +00:00
|
|
|
for range errorThreshold {
|
2022-07-13 06:57:04 +00:00
|
|
|
conn, err := pool.connection()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, nodes[0].address, conn.address())
|
|
|
|
_, err = conn.objectGet(ctx, PrmObjectGet{})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := pool.connection()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, nodes[1].address, conn.address())
|
|
|
|
_, err = conn.objectGet(ctx, PrmObjectGet{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|