From ae8be495c8c9bf729e59526e8fbd526a19f98910 Mon Sep 17 00:00:00 2001 From: Alejandro Lopez Date: Mon, 14 Aug 2023 14:01:39 +0300 Subject: [PATCH] [#xx] Avoid manual management of files in tests Signed-off-by: Alejandro Lopez --- .../blobstor/blobovniczatree/generic_test.go | 17 +++-------------- .../engine/control_test.go | 18 ++++-------------- pkg/local_object_storage/engine/delete_test.go | 3 --- pkg/local_object_storage/engine/engine_test.go | 8 +++----- pkg/local_object_storage/engine/head_test.go | 3 --- pkg/local_object_storage/engine/inhume_test.go | 3 --- pkg/local_object_storage/engine/list_test.go | 10 +++------- pkg/local_object_storage/engine/lock_test.go | 4 ---- pkg/local_object_storage/engine/shards_test.go | 2 -- .../metabase/counter_test.go | 4 ---- pkg/local_object_storage/metabase/db_test.go | 5 ++--- .../metabase/generic_test.go | 9 +-------- .../pilorama/generic_test.go | 10 +--------- 13 files changed, 17 insertions(+), 79 deletions(-) diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/generic_test.go b/pkg/local_object_storage/blobstor/blobovniczatree/generic_test.go index f1bc0b225..7f95e750b 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/generic_test.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/generic_test.go @@ -1,9 +1,6 @@ package blobovniczatree import ( - "os" - "path/filepath" - "strconv" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common" @@ -15,8 +12,6 @@ import ( func TestGeneric(t *testing.T) { const maxObjectSize = 1 << 16 - defer func() { _ = os.RemoveAll(t.Name()) }() - helper := func(t *testing.T, dir string) common.Storage { return NewBlobovniczaTree( WithLogger(&logger.Logger{Logger: zaptest.NewLogger(t)}), @@ -27,16 +22,14 @@ func TestGeneric(t *testing.T) { WithBlobovniczaSize(1<<20)) } - var n int newTree := func(t *testing.T) common.Storage { - dir := filepath.Join(t.Name(), strconv.Itoa(n)) - return helper(t, dir) + return helper(t, t.TempDir()) } blobstortest.TestAll(t, newTree, 1024, maxObjectSize) t.Run("info", func(t *testing.T) { - dir := filepath.Join(t.Name(), "info") + dir := t.TempDir() blobstortest.TestInfo(t, func(t *testing.T) common.Storage { return helper(t, dir) }, Type, dir) @@ -46,17 +39,13 @@ func TestGeneric(t *testing.T) { func TestControl(t *testing.T) { const maxObjectSize = 2048 - defer func() { _ = os.RemoveAll(t.Name()) }() - - var n int newTree := func(t *testing.T) common.Storage { - dir := filepath.Join(t.Name(), strconv.Itoa(n)) return NewBlobovniczaTree( WithLogger(&logger.Logger{Logger: zaptest.NewLogger(t)}), WithObjectSizeLimit(maxObjectSize), WithBlobovniczaShallowWidth(2), WithBlobovniczaShallowDepth(2), - WithRootPath(dir), + WithRootPath(t.TempDir()), WithBlobovniczaSize(1<<20)) } diff --git a/pkg/local_object_storage/engine/control_test.go b/pkg/local_object_storage/engine/control_test.go index 593eb9971..804bef6ec 100644 --- a/pkg/local_object_storage/engine/control_test.go +++ b/pkg/local_object_storage/engine/control_test.go @@ -44,18 +44,12 @@ func TestInitializationFailure(t *testing.T) { sid, err := generateShardID() require.NoError(t, err) - tempDir := t.TempDir() - blobstorPath := filepath.Join(tempDir, "bs") - metabasePath := filepath.Join(tempDir, "mb") - writecachePath := filepath.Join(tempDir, "wc") - piloramaPath := filepath.Join(tempDir, "pl") - - storages, smallFileStorage, largeFileStorage := newTestStorages(blobstorPath, 1<<20) + storages, smallFileStorage, largeFileStorage := newTestStorages(t.TempDir(), 1<<20) wcOpts := writecacheconfig.Options{ Type: writecacheconfig.TypeBBolt, BBoltOptions: []writecachebbolt.Option{ - writecachebbolt.WithPath(writecachePath), + writecachebbolt.WithPath(t.TempDir()), }, } @@ -69,13 +63,13 @@ func TestInitializationFailure(t *testing.T) { Timeout: 100 * time.Millisecond, OpenFile: opts.openFileMetabase, }), - meta.WithPath(metabasePath), + meta.WithPath(filepath.Join(t.TempDir(), "metabase")), meta.WithPermissions(0700), meta.WithEpochState(epochState{})), shard.WithWriteCache(true), shard.WithWriteCacheOptions(wcOpts), shard.WithPiloramaOptions( - pilorama.WithPath(piloramaPath), + pilorama.WithPath(filepath.Join(t.TempDir(), "pilorama")), pilorama.WithOpenFile(opts.openFilePilorama), ), }, smallFileStorage, largeFileStorage @@ -178,10 +172,6 @@ func testEngineFailInitAndReload(t *testing.T, errOnAdd bool, opts []shard.Optio func TestExecBlocks(t *testing.T) { e := testNewEngine(t).setShardsNum(t, 2).engine // number doesn't matter in this test, 2 is several but not many - t.Cleanup(func() { - os.RemoveAll(t.Name()) - }) - // put some object obj := testutil.GenerateObjectWithCID(cidtest.ID()) diff --git a/pkg/local_object_storage/engine/delete_test.go b/pkg/local_object_storage/engine/delete_test.go index 3ea0291da..e4a516512 100644 --- a/pkg/local_object_storage/engine/delete_test.go +++ b/pkg/local_object_storage/engine/delete_test.go @@ -2,7 +2,6 @@ package engine import ( "context" - "os" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" @@ -20,8 +19,6 @@ import ( func TestDeleteBigObject(t *testing.T) { t.Parallel() - defer os.RemoveAll(t.Name()) - cnr := cidtest.ID() parentID := oidtest.ID() splitID := objectSDK.NewSplitID() diff --git a/pkg/local_object_storage/engine/engine_test.go b/pkg/local_object_storage/engine/engine_test.go index 26de2e5eb..e3dd49728 100644 --- a/pkg/local_object_storage/engine/engine_test.go +++ b/pkg/local_object_storage/engine/engine_test.go @@ -2,7 +2,6 @@ package engine import ( "context" - "fmt" "os" "path/filepath" "sync/atomic" @@ -204,11 +203,10 @@ func testDefaultShardOptions(t testing.TB, id int) []shard.Option { shard.WithLogger(&logger.Logger{Logger: zap.L()}), shard.WithBlobStorOptions( blobstor.WithStorages( - newStorages(filepath.Join(t.Name(), fmt.Sprintf("%d.blobstor", id)), - 1<<20))), - shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(t.Name(), fmt.Sprintf("%d.pilorama", id)))), + newStorages(t.TempDir(), 1<<20))), + shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(t.TempDir(), "pilorama"))), shard.WithMetaBaseOptions( - meta.WithPath(filepath.Join(t.Name(), fmt.Sprintf("%d.metabase", id))), + meta.WithPath(filepath.Join(t.TempDir(), "metabase")), meta.WithPermissions(0700), meta.WithEpochState(epochState{}), )} diff --git a/pkg/local_object_storage/engine/head_test.go b/pkg/local_object_storage/engine/head_test.go index d5bf8429c..e212ffa32 100644 --- a/pkg/local_object_storage/engine/head_test.go +++ b/pkg/local_object_storage/engine/head_test.go @@ -2,7 +2,6 @@ package engine import ( "context" - "os" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil" @@ -14,8 +13,6 @@ import ( ) func TestHeadRaw(t *testing.T) { - defer os.RemoveAll(t.Name()) - cnr := cidtest.ID() splitID := objectSDK.NewSplitID() diff --git a/pkg/local_object_storage/engine/inhume_test.go b/pkg/local_object_storage/engine/inhume_test.go index 924cf518b..a5f242071 100644 --- a/pkg/local_object_storage/engine/inhume_test.go +++ b/pkg/local_object_storage/engine/inhume_test.go @@ -2,7 +2,6 @@ package engine import ( "context" - "os" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" @@ -14,8 +13,6 @@ import ( ) func TestStorageEngine_Inhume(t *testing.T) { - defer os.RemoveAll(t.Name()) - cnr := cidtest.ID() splitID := objectSDK.NewSplitID() diff --git a/pkg/local_object_storage/engine/list_test.go b/pkg/local_object_storage/engine/list_test.go index 6cea2d0f4..9d5d12683 100644 --- a/pkg/local_object_storage/engine/list_test.go +++ b/pkg/local_object_storage/engine/list_test.go @@ -2,8 +2,6 @@ package engine import ( "context" - "fmt" - "os" "path/filepath" "sort" "testing" @@ -71,11 +69,10 @@ func TestListWithCursor(t *testing.T) { shard.WithLogger(&logger.Logger{Logger: zap.L()}), shard.WithBlobStorOptions( blobstor.WithStorages( - newStorages(filepath.Join(t.Name(), tt.name, fmt.Sprintf("%d.blobstor", id)), - 1<<20))), - shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(t.Name(), tt.name, fmt.Sprintf("%d.pilorama", id)))), + newStorages(t.TempDir(), 1<<20))), + shard.WithPiloramaOptions(pilorama.WithPath(filepath.Join(t.TempDir(), "pilorama"))), shard.WithMetaBaseOptions( - meta.WithPath(filepath.Join(t.Name(), tt.name, fmt.Sprintf("%d.metabase", id))), + meta.WithPath(filepath.Join(t.TempDir(), "metabase")), meta.WithPermissions(0700), meta.WithEpochState(epochState{}), )} @@ -85,7 +82,6 @@ func TestListWithCursor(t *testing.T) { t.Cleanup(func() { e.Close() - os.RemoveAll(t.Name()) }) expected := make([]object.AddressWithType, 0, tt.objectNum) diff --git a/pkg/local_object_storage/engine/lock_test.go b/pkg/local_object_storage/engine/lock_test.go index 3e253bfc7..906a867e2 100644 --- a/pkg/local_object_storage/engine/lock_test.go +++ b/pkg/local_object_storage/engine/lock_test.go @@ -2,7 +2,6 @@ package engine import ( "context" - "os" "strconv" "testing" "time" @@ -65,7 +64,6 @@ func TestLockUserScenario(t *testing.T) { t.Cleanup(func() { _ = e.Close() - _ = os.RemoveAll(t.Name()) }) lockerID := oidtest.ID() @@ -174,7 +172,6 @@ func TestLockExpiration(t *testing.T) { t.Cleanup(func() { _ = e.Close() - _ = os.RemoveAll(t.Name()) }) const lockerExpiresAfter = 13 @@ -254,7 +251,6 @@ func TestLockForceRemoval(t *testing.T) { require.NoError(t, e.Init(context.Background())) t.Cleanup(func() { _ = e.Close() - _ = os.RemoveAll(t.Name()) }) cnr := cidtest.ID() diff --git a/pkg/local_object_storage/engine/shards_test.go b/pkg/local_object_storage/engine/shards_test.go index 1bc0b880c..3631e33ed 100644 --- a/pkg/local_object_storage/engine/shards_test.go +++ b/pkg/local_object_storage/engine/shards_test.go @@ -1,7 +1,6 @@ package engine import ( - "os" "testing" "github.com/stretchr/testify/require" @@ -14,7 +13,6 @@ func TestRemoveShard(t *testing.T) { e, ids := te.engine, te.shardIDs t.Cleanup(func() { e.Close() - os.RemoveAll(t.Name()) }) require.Equal(t, numOfShards, len(e.shardPools)) diff --git a/pkg/local_object_storage/metabase/counter_test.go b/pkg/local_object_storage/metabase/counter_test.go index 2c5fa0233..89b52c887 100644 --- a/pkg/local_object_storage/metabase/counter_test.go +++ b/pkg/local_object_storage/metabase/counter_test.go @@ -2,7 +2,6 @@ package meta_test import ( "context" - "os" "testing" objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" @@ -18,9 +17,6 @@ const objCount = 10 func TestCounters(t *testing.T) { t.Parallel() - t.Cleanup(func() { - require.NoError(t, os.RemoveAll(t.Name())) - }) t.Run("defaults", func(t *testing.T) { t.Parallel() diff --git a/pkg/local_object_storage/metabase/db_test.go b/pkg/local_object_storage/metabase/db_test.go index 9ef7bf8bc..6169dd81e 100644 --- a/pkg/local_object_storage/metabase/db_test.go +++ b/pkg/local_object_storage/metabase/db_test.go @@ -2,6 +2,7 @@ package meta_test import ( "os" + "path/filepath" "strconv" "testing" @@ -40,11 +41,9 @@ func testSelect(t *testing.T, db *meta.DB, cnr cid.ID, fs objectSDK.SearchFilter } func newDB(t testing.TB, opts ...meta.Option) *meta.DB { - path := t.Name() - bdb := meta.New( append([]meta.Option{ - meta.WithPath(path), + meta.WithPath(filepath.Join(t.TempDir(), "metabase")), meta.WithPermissions(0600), meta.WithEpochState(epochState{}), }, opts...)..., diff --git a/pkg/local_object_storage/metabase/generic_test.go b/pkg/local_object_storage/metabase/generic_test.go index 9d15b6f7a..52581b2a0 100644 --- a/pkg/local_object_storage/metabase/generic_test.go +++ b/pkg/local_object_storage/metabase/generic_test.go @@ -1,9 +1,7 @@ package meta import ( - "os" "path/filepath" - "strconv" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/storagetest" @@ -12,15 +10,10 @@ import ( func TestGeneric(t *testing.T) { t.Parallel() - defer func() { _ = os.RemoveAll(t.Name()) }() - - var n int newMetabase := func(t *testing.T) storagetest.Component { - n++ - dir := filepath.Join(t.Name(), strconv.Itoa(n)) return New( WithEpochState(epochStateImpl{}), - WithPath(dir)) + WithPath(filepath.Join(t.TempDir(), "metabase"))) } storagetest.TestAll(t, newMetabase) diff --git a/pkg/local_object_storage/pilorama/generic_test.go b/pkg/local_object_storage/pilorama/generic_test.go index cac03c4e1..d6a9f333b 100644 --- a/pkg/local_object_storage/pilorama/generic_test.go +++ b/pkg/local_object_storage/pilorama/generic_test.go @@ -1,23 +1,15 @@ package pilorama import ( - "os" "path/filepath" - "strconv" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/storagetest" ) func TestGeneric(t *testing.T) { - defer func() { _ = os.RemoveAll(t.Name()) }() - - var n int newPilorama := func(t *testing.T) storagetest.Component { - n++ - dir := filepath.Join(t.Name(), strconv.Itoa(n)) - return NewBoltForest( - WithPath(dir)) + return NewBoltForest(WithPath(filepath.Join(t.TempDir(), "pilorama"))) } storagetest.TestAll(t, newPilorama)