From 221203beeb0c7e69688d0476726ea7df3dd7e24b Mon Sep 17 00:00:00 2001 From: Airat Arifullin Date: Tue, 28 Mar 2023 17:16:03 +0300 Subject: [PATCH] [#180] node: Refactor panics in unit test * Replace panics in unit tests by require.NoError and t.Fatalf Signed-off-by: Airat Arifullin --- .../config/apiclient/config_test.go | 2 +- .../config/contracts/config_test.go | 2 +- .../config/control/config_test.go | 2 +- cmd/frostfs-node/config/engine/config_test.go | 2 +- cmd/frostfs-node/config/grpc/config_test.go | 2 +- cmd/frostfs-node/config/logger/config_test.go | 2 +- .../config/metrics/config_test.go | 2 +- cmd/frostfs-node/config/morph/config_test.go | 2 +- cmd/frostfs-node/config/node/config_test.go | 2 +- cmd/frostfs-node/config/object/config_test.go | 2 +- .../config/policer/config_test.go | 2 +- .../config/profiler/config_test.go | 2 +- .../config/replicator/config_test.go | 2 +- cmd/frostfs-node/config/test/config.go | 20 ++++++++-------- cmd/frostfs-node/config/tree/config_test.go | 2 +- .../blobovnicza/blobovnicza_test.go | 2 +- .../internal/blobstortest/get_range.go | 2 +- .../blobstor/perf_test.go | 6 ++--- .../engine/delete_test.go | 2 +- .../engine/engine_test.go | 10 ++++---- pkg/local_object_storage/engine/head_test.go | 2 +- .../engine/inhume_test.go | 2 +- pkg/local_object_storage/engine/list_test.go | 2 +- .../internal/testutil/generators.go | 23 ++++++++++--------- .../internal/testutil/generators_test.go | 2 +- pkg/services/object/get/get_test.go | 6 ++--- pkg/services/object/search/search_test.go | 8 +++---- pkg/services/tree/getsubtree_test.go | 9 ++++++-- pkg/util/logger/test/logger.go | 10 ++++---- pkg/util/test/keys.go | 21 +++++++---------- 30 files changed, 76 insertions(+), 79 deletions(-) diff --git a/cmd/frostfs-node/config/apiclient/config_test.go b/cmd/frostfs-node/config/apiclient/config_test.go index 4d67753bd..f4f09d4ff 100644 --- a/cmd/frostfs-node/config/apiclient/config_test.go +++ b/cmd/frostfs-node/config/apiclient/config_test.go @@ -32,6 +32,6 @@ func TestApiclientSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/contracts/config_test.go b/cmd/frostfs-node/config/contracts/config_test.go index 8cfafb5c9..93956d6bb 100644 --- a/cmd/frostfs-node/config/contracts/config_test.go +++ b/cmd/frostfs-node/config/contracts/config_test.go @@ -56,6 +56,6 @@ func TestContractsSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/control/config_test.go b/cmd/frostfs-node/config/control/config_test.go index 0c04cda6f..1aed229e2 100644 --- a/cmd/frostfs-node/config/control/config_test.go +++ b/cmd/frostfs-node/config/control/config_test.go @@ -32,6 +32,6 @@ func TestControlSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/engine/config_test.go b/cmd/frostfs-node/config/engine/config_test.go index 83f5a65ed..acf8c2a60 100644 --- a/cmd/frostfs-node/config/engine/config_test.go +++ b/cmd/frostfs-node/config/engine/config_test.go @@ -167,6 +167,6 @@ func TestEngineSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/grpc/config_test.go b/cmd/frostfs-node/config/grpc/config_test.go index 25677ed63..1ada792ec 100644 --- a/cmd/frostfs-node/config/grpc/config_test.go +++ b/cmd/frostfs-node/config/grpc/config_test.go @@ -49,6 +49,6 @@ func TestGRPCSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/logger/config_test.go b/cmd/frostfs-node/config/logger/config_test.go index 850f71d74..8d37bdb1b 100644 --- a/cmd/frostfs-node/config/logger/config_test.go +++ b/cmd/frostfs-node/config/logger/config_test.go @@ -25,6 +25,6 @@ func TestLoggerSection_Level(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/metrics/config_test.go b/cmd/frostfs-node/config/metrics/config_test.go index ed8509165..4c03729ee 100644 --- a/cmd/frostfs-node/config/metrics/config_test.go +++ b/cmd/frostfs-node/config/metrics/config_test.go @@ -34,6 +34,6 @@ func TestMetricsSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/morph/config_test.go b/cmd/frostfs-node/config/morph/config_test.go index e62351017..be61d8608 100644 --- a/cmd/frostfs-node/config/morph/config_test.go +++ b/cmd/frostfs-node/config/morph/config_test.go @@ -40,6 +40,6 @@ func TestMorphSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/node/config_test.go b/cmd/frostfs-node/config/node/config_test.go index 303292652..ff085c506 100644 --- a/cmd/frostfs-node/config/node/config_test.go +++ b/cmd/frostfs-node/config/node/config_test.go @@ -162,6 +162,6 @@ func TestNodeSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/object/config_test.go b/cmd/frostfs-node/config/object/config_test.go index e29c86dc9..81c1ccd57 100644 --- a/cmd/frostfs-node/config/object/config_test.go +++ b/cmd/frostfs-node/config/object/config_test.go @@ -29,6 +29,6 @@ func TestObjectSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/policer/config_test.go b/cmd/frostfs-node/config/policer/config_test.go index 97b7bcefd..a4c23eae7 100644 --- a/cmd/frostfs-node/config/policer/config_test.go +++ b/cmd/frostfs-node/config/policer/config_test.go @@ -26,6 +26,6 @@ func TestPolicerSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/profiler/config_test.go b/cmd/frostfs-node/config/profiler/config_test.go index 1b132a886..bb1b20eb8 100644 --- a/cmd/frostfs-node/config/profiler/config_test.go +++ b/cmd/frostfs-node/config/profiler/config_test.go @@ -34,6 +34,6 @@ func TestProfilerSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/replicator/config_test.go b/cmd/frostfs-node/config/replicator/config_test.go index 1c366df80..0118aa0b2 100644 --- a/cmd/frostfs-node/config/replicator/config_test.go +++ b/cmd/frostfs-node/config/replicator/config_test.go @@ -28,6 +28,6 @@ func TestReplicatorSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/cmd/frostfs-node/config/test/config.go b/cmd/frostfs-node/config/test/config.go index ac6d92c4e..cd10edaec 100644 --- a/cmd/frostfs-node/config/test/config.go +++ b/cmd/frostfs-node/config/test/config.go @@ -4,8 +4,10 @@ import ( "bufio" "os" "strings" + "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config" + "github.com/stretchr/testify/require" ) func fromFile(path string) *config.Config { @@ -18,10 +20,10 @@ func fromFile(path string) *config.Config { ) } -func fromEnvFile(path string) *config.Config { +func fromEnvFile(t testing.TB, path string) *config.Config { var p config.Prm - loadEnv(path) // github.com/joho/godotenv can do that as well + loadEnv(t, path) // github.com/joho/godotenv can do that as well return config.New(p) } @@ -43,8 +45,8 @@ func ForEachFileType(pref string, f func(*config.Config)) { } // ForEnvFileType creates config from `.env` file. -func ForEnvFileType(pref string, f func(*config.Config)) { - f(fromEnvFile(pref + ".env")) +func ForEnvFileType(t testing.TB, pref string, f func(*config.Config)) { + f(fromEnvFile(t, pref+".env")) } // EmptyConfig returns config without any values and sections. @@ -55,11 +57,9 @@ func EmptyConfig() *config.Config { } // loadEnv reads .env file, parses `X=Y` records and sets OS ENVs. -func loadEnv(path string) { +func loadEnv(t testing.TB, path string) { f, err := os.Open(path) - if err != nil { - panic("can't open .env file") - } + require.NoError(t, err, "can't open .env file") defer f.Close() @@ -73,8 +73,6 @@ func loadEnv(path string) { v = strings.Trim(v, `"`) err = os.Setenv(k, v) - if err != nil { - panic("can't set environment variable") - } + require.NoError(t, err, "can't set environment variable") } } diff --git a/cmd/frostfs-node/config/tree/config_test.go b/cmd/frostfs-node/config/tree/config_test.go index 90837d43d..a39aa4553 100644 --- a/cmd/frostfs-node/config/tree/config_test.go +++ b/cmd/frostfs-node/config/tree/config_test.go @@ -39,6 +39,6 @@ func TestTreeSection(t *testing.T) { configtest.ForEachFileType(path, fileConfigTest) t.Run("ENV", func(t *testing.T) { - configtest.ForEnvFileType(path, fileConfigTest) + configtest.ForEnvFileType(t, path, fileConfigTest) }) } diff --git a/pkg/local_object_storage/blobovnicza/blobovnicza_test.go b/pkg/local_object_storage/blobovnicza/blobovnicza_test.go index 43dbe1b98..4499c5d14 100644 --- a/pkg/local_object_storage/blobovnicza/blobovnicza_test.go +++ b/pkg/local_object_storage/blobovnicza/blobovnicza_test.go @@ -64,7 +64,7 @@ func TestBlobovnicza(t *testing.T) { WithPath(p), WithObjectSizeLimit(objSizeLim), WithFullSizeLimit(sizeLim), - WithLogger(test.NewLogger(false)), + WithLogger(test.NewLogger(t, false)), ) defer os.Remove(p) diff --git a/pkg/local_object_storage/blobstor/internal/blobstortest/get_range.go b/pkg/local_object_storage/blobstor/internal/blobstortest/get_range.go index 9e4b1a449..e105fe6e8 100644 --- a/pkg/local_object_storage/blobstor/internal/blobstortest/get_range.go +++ b/pkg/local_object_storage/blobstor/internal/blobstortest/get_range.go @@ -28,7 +28,7 @@ func TestGetRange(t *testing.T, cons Constructor, min, max uint64) { var start, stop uint64 = 11, 100 if uint64(len(payload)) < stop { - panic("unexpected: invalid test object generated") + t.Fatalf("unexpected: invalid test object generated") } var gPrm common.GetRangePrm diff --git a/pkg/local_object_storage/blobstor/perf_test.go b/pkg/local_object_storage/blobstor/perf_test.go index 7da4619d5..0fd3048d3 100644 --- a/pkg/local_object_storage/blobstor/perf_test.go +++ b/pkg/local_object_storage/blobstor/perf_test.go @@ -103,7 +103,7 @@ func BenchmarkSubstorageReadPerf(b *testing.B) { // Fill database for i := 0; i < tt.size; i++ { obj := objGen.Next() - addr := testutil.AddressFromObject(obj) + addr := testutil.AddressFromObject(b, obj) raw, err := obj.Marshal() require.NoError(b, err) if _, err := st.Put(common.PutPrm{ @@ -159,7 +159,7 @@ func BenchmarkSubstorageWritePerf(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { obj := gen.Next() - addr := testutil.AddressFromObject(obj) + addr := testutil.AddressFromObject(b, obj) raw, err := obj.Marshal() require.NoError(b, err) if _, err := st.Put(common.PutPrm{ @@ -202,7 +202,7 @@ func BenchmarkSubstorageIteratePerf(b *testing.B) { // Fill database for i := 0; i < tt.size; i++ { obj := objGen.Next() - addr := testutil.AddressFromObject(obj) + addr := testutil.AddressFromObject(b, obj) raw, err := obj.Marshal() require.NoError(b, err) if _, err := st.Put(common.PutPrm{ diff --git a/pkg/local_object_storage/engine/delete_test.go b/pkg/local_object_storage/engine/delete_test.go index abe3ea930..ccba3d903 100644 --- a/pkg/local_object_storage/engine/delete_test.go +++ b/pkg/local_object_storage/engine/delete_test.go @@ -53,7 +53,7 @@ func TestDeleteBigObject(t *testing.T) { s2 := testNewShard(t, 2) s3 := testNewShard(t, 3) - e := testNewEngineWithShards(s1, s2, s3) + e := testNewEngineWithShards(t, s1, s2, s3) e.log = &logger.Logger{Logger: zaptest.NewLogger(t)} defer e.Close() diff --git a/pkg/local_object_storage/engine/engine_test.go b/pkg/local_object_storage/engine/engine_test.go index 19e98ade4..2eb026427 100644 --- a/pkg/local_object_storage/engine/engine_test.go +++ b/pkg/local_object_storage/engine/engine_test.go @@ -48,7 +48,7 @@ func benchmarkExists(b *testing.B, shardNum int) { shards[i] = testNewShard(b, i) } - e := testNewEngineWithShards(shards...) + e := testNewEngineWithShards(b, shards...) b.Cleanup(func() { _ = e.Close() _ = os.RemoveAll(b.Name()) @@ -73,14 +73,12 @@ func benchmarkExists(b *testing.B, shardNum int) { } } -func testNewEngineWithShards(shards ...*shard.Shard) *StorageEngine { +func testNewEngineWithShards(t testing.TB, shards ...*shard.Shard) *StorageEngine { engine := New() for _, s := range shards { pool, err := ants.NewPool(10, ants.WithNonblocking(true)) - if err != nil { - panic(err) - } + require.NoError(t, err) engine.shards[s.ID().String()] = hashedShard{ shardWrapper: shardWrapper{ @@ -172,5 +170,5 @@ func testNewEngineWithShardNum(t *testing.T, num int) *StorageEngine { shards = append(shards, testNewShard(t, i)) } - return testNewEngineWithShards(shards...) + return testNewEngineWithShards(t, shards...) } diff --git a/pkg/local_object_storage/engine/head_test.go b/pkg/local_object_storage/engine/head_test.go index 82feaf6f9..1ddfedc5c 100644 --- a/pkg/local_object_storage/engine/head_test.go +++ b/pkg/local_object_storage/engine/head_test.go @@ -44,7 +44,7 @@ func TestHeadRaw(t *testing.T) { s1 := testNewShard(t, 1) s2 := testNewShard(t, 2) - e := testNewEngineWithShards(s1, s2) + e := testNewEngineWithShards(t, s1, s2) defer e.Close() var putPrmLeft shard.PutPrm diff --git a/pkg/local_object_storage/engine/inhume_test.go b/pkg/local_object_storage/engine/inhume_test.go index 8f7f6b07b..b001c6df9 100644 --- a/pkg/local_object_storage/engine/inhume_test.go +++ b/pkg/local_object_storage/engine/inhume_test.go @@ -59,7 +59,7 @@ func TestStorageEngine_Inhume(t *testing.T) { s1 := testNewShard(t, 1) s2 := testNewShard(t, 2) - e := testNewEngineWithShards(s1, s2) + e := testNewEngineWithShards(t, s1, s2) defer e.Close() var putChild shard.PutPrm diff --git a/pkg/local_object_storage/engine/list_test.go b/pkg/local_object_storage/engine/list_test.go index 39ae1fba1..ad0eb1911 100644 --- a/pkg/local_object_storage/engine/list_test.go +++ b/pkg/local_object_storage/engine/list_test.go @@ -16,7 +16,7 @@ import ( func TestListWithCursor(t *testing.T) { s1 := testNewShard(t, 1) s2 := testNewShard(t, 2) - e := testNewEngineWithShards(s1, s2) + e := testNewEngineWithShards(t, s1, s2) t.Cleanup(func() { e.Close() diff --git a/pkg/local_object_storage/internal/testutil/generators.go b/pkg/local_object_storage/internal/testutil/generators.go index 9b4937534..a45ea72aa 100644 --- a/pkg/local_object_storage/internal/testutil/generators.go +++ b/pkg/local_object_storage/internal/testutil/generators.go @@ -2,10 +2,12 @@ package testutil import ( "encoding/binary" + "testing" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" + "github.com/stretchr/testify/require" "go.uber.org/atomic" "golang.org/x/exp/rand" ) @@ -94,17 +96,16 @@ func (g *OverwriteObjGenerator) Next() *object.Object { return generateObjectWithOIDWithCIDWithSize(id, cid.ID{}, g.ObjSize) } -func AddressFromObject(obj *object.Object) oid.Address { +func AddressFromObject(t testing.TB, obj *object.Object) oid.Address { var addr oid.Address - if id, isSet := obj.ID(); isSet { - addr.SetObject(id) - } else { - panic("object ID is not set") - } - if cid, isSet := obj.ContainerID(); isSet { - addr.SetContainer(cid) - } else { - panic("container ID is not set") - } + + id, isSet := obj.ID() + require.True(t, isSet, "object ID is not set") + addr.SetObject(id) + + cid, isSet := obj.ContainerID() + require.True(t, isSet, "container ID is not set") + addr.SetContainer(cid) + return addr } diff --git a/pkg/local_object_storage/internal/testutil/generators_test.go b/pkg/local_object_storage/internal/testutil/generators_test.go index 996848d32..f2c60f9b4 100644 --- a/pkg/local_object_storage/internal/testutil/generators_test.go +++ b/pkg/local_object_storage/internal/testutil/generators_test.go @@ -39,7 +39,7 @@ func TestSeqObjGenerator(t *testing.T) { for i := 1; i <= 10; i++ { obj := gen.Next() id, isSet := obj.ID() - addrs = append(addrs, AddressFromObject(obj).EncodeToString()) + addrs = append(addrs, AddressFromObject(t, obj).EncodeToString()) require.True(t, isSet) require.Equal(t, gen.ObjSize, uint64(len(obj.Payload()))) diff --git a/pkg/services/object/get/get_test.go b/pkg/services/object/get/get_test.go index 09d0c38fa..36a0e4976 100644 --- a/pkg/services/object/get/get_test.go +++ b/pkg/services/object/get/get_test.go @@ -246,7 +246,7 @@ func TestGetLocalOnly(t *testing.T) { newSvc := func(storage *testStorage) *Service { svc := &Service{cfg: new(cfg)} - svc.log = test.NewLogger(false) + svc.log = test.NewLogger(t, false) svc.localStorage = storage return svc @@ -507,7 +507,7 @@ func TestGetRemoteSmall(t *testing.T) { newSvc := func(b *testPlacementBuilder, c *testClientCache) *Service { svc := &Service{cfg: new(cfg)} - svc.log = test.NewLogger(false) + svc.log = test.NewLogger(t, false) svc.localStorage = newTestStorage() const curEpoch = 13 @@ -1640,7 +1640,7 @@ func TestGetFromPastEpoch(t *testing.T) { c22.addResult(addr, obj, nil) svc := &Service{cfg: new(cfg)} - svc.log = test.NewLogger(false) + svc.log = test.NewLogger(t, false) svc.localStorage = newTestStorage() const curEpoch = 13 diff --git a/pkg/services/object/search/search_test.go b/pkg/services/object/search/search_test.go index aacfb9509..1d902e7aa 100644 --- a/pkg/services/object/search/search_test.go +++ b/pkg/services/object/search/search_test.go @@ -152,7 +152,7 @@ func TestGetLocalOnly(t *testing.T) { newSvc := func(storage *testStorage) *Service { svc := &Service{cfg: new(cfg)} - svc.log = test.NewLogger(false) + svc.log = test.NewLogger(t, false) svc.localStorage = storage return svc @@ -254,7 +254,7 @@ func TestGetRemoteSmall(t *testing.T) { newSvc := func(b *testPlacementBuilder, c *testClientCache) *Service { svc := &Service{cfg: new(cfg)} - svc.log = test.NewLogger(false) + svc.log = test.NewLogger(t, false) svc.localStorage = newTestStorage() const curEpoch = 13 @@ -363,7 +363,7 @@ func TestGetFromPastEpoch(t *testing.T) { c22.addResult(idCnr, ids22, nil) svc := &Service{cfg: new(cfg)} - svc.log = test.NewLogger(false) + svc.log = test.NewLogger(t, false) svc.localStorage = newTestStorage() const curEpoch = 13 @@ -476,7 +476,7 @@ func TestGetWithSessionToken(t *testing.T) { w := new(simpleIDWriter) svc := &Service{cfg: new(cfg)} - svc.log = test.NewLogger(false) + svc.log = test.NewLogger(t, false) svc.localStorage = localStorage const curEpoch = 13 diff --git a/pkg/services/tree/getsubtree_test.go b/pkg/services/tree/getsubtree_test.go index 5feeace91..fd65ac3f0 100644 --- a/pkg/services/tree/getsubtree_test.go +++ b/pkg/services/tree/getsubtree_test.go @@ -118,7 +118,10 @@ func TestGetSubTree(t *testing.T) { }) } -var errSubTreeSend = errors.New("test error") +var ( + errSubTreeSend = errors.New("send finished with error") + errSubTreeSendAfterError = errors.New("send was invoked after an error occurred") +) type subTreeAcc struct { grpc.ServerStream // to satisfy the interface @@ -127,6 +130,8 @@ type subTreeAcc struct { errIndex int } +var _ TreeService_GetSubTreeServer = &subTreeAcc{} + func (s *subTreeAcc) Send(r *GetSubTreeResponse) error { s.seen = append(s.seen, r) if s.errIndex >= 0 { @@ -134,7 +139,7 @@ func (s *subTreeAcc) Send(r *GetSubTreeResponse) error { return errSubTreeSend } if s.errIndex >= 0 && len(s.seen) > s.errIndex { - panic("called Send after an error was returned") + return errSubTreeSendAfterError } } return nil diff --git a/pkg/util/logger/test/logger.go b/pkg/util/logger/test/logger.go index b2f0f3eb7..e78a117fa 100644 --- a/pkg/util/logger/test/logger.go +++ b/pkg/util/logger/test/logger.go @@ -1,7 +1,10 @@ package test import ( + "testing" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger" + "github.com/stretchr/testify/require" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -11,7 +14,7 @@ const sampling = 1000 // NewLogger creates a new logger. // // If debug, development logger is created. -func NewLogger(debug bool) *logger.Logger { +func NewLogger(t testing.TB, debug bool) *logger.Logger { var l logger.Logger l.Logger = zap.L() @@ -25,10 +28,7 @@ func NewLogger(debug bool) *logger.Logger { cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder log, err := cfg.Build() - if err != nil { - panic("could not prepare logger: " + err.Error()) - } - + require.NoError(t, err, "could not prepare logger") l.Logger = log } diff --git a/pkg/util/test/keys.go b/pkg/util/test/keys.go index 3b87bfb3f..d233a633a 100644 --- a/pkg/util/test/keys.go +++ b/pkg/util/test/keys.go @@ -6,7 +6,9 @@ import ( "crypto/rand" "crypto/x509" "encoding/hex" - "strconv" + "testing" + + "github.com/stretchr/testify/require" ) // Keys is a list of test private keys in hex format. @@ -114,29 +116,22 @@ var Keys = []string{ } // DecodeKey creates a test private key. -func DecodeKey(i int) *ecdsa.PrivateKey { +func DecodeKey(t testing.TB, i int) *ecdsa.PrivateKey { if i < 0 { key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - panic("could not generate uniq key") - } + require.NoError(t, err, "could not generate uniq key") return key } if current, size := i, len(Keys); current >= size { - panic("add more test keys, used " + strconv.Itoa(current) + " from " + strconv.Itoa(size)) + t.Fatalf("add more test keys, used %d from %d", current, size) } buf, err := hex.DecodeString(Keys[i]) - if err != nil { - panic("could not hex.Decode: " + err.Error()) - } + require.NoError(t, err, "could not to decode hex string") key, err := x509.ParseECPrivateKey(buf) - if err != nil { - panic("could x509.ParseECPrivateKey: " + err.Error()) - } - + require.NoError(t, err, "could not to parse ec private key") return key } -- 2.45.2