From 973af12854e876c48861bc82e832408b7f2d89cb Mon Sep 17 00:00:00 2001 From: Alejandro Lopez Date: Fri, 5 May 2023 10:38:59 +0300 Subject: [PATCH] [#327] tests: replace os.MkdirTemp with t.TempDir Signed-off-by: Alejandro Lopez --- pkg/local_object_storage/blobstor/exists_test.go | 7 +------ pkg/local_object_storage/blobstor/perf_test.go | 8 +------- pkg/local_object_storage/engine/control_test.go | 3 +-- pkg/local_object_storage/engine/error_test.go | 14 ++++---------- pkg/local_object_storage/engine/evacuate_test.go | 5 +---- pkg/local_object_storage/pilorama/forest_test.go | 8 +------- 6 files changed, 9 insertions(+), 36 deletions(-) diff --git a/pkg/local_object_storage/blobstor/exists_test.go b/pkg/local_object_storage/blobstor/exists_test.go index f5c5fbbef..7a3dcd617 100644 --- a/pkg/local_object_storage/blobstor/exists_test.go +++ b/pkg/local_object_storage/blobstor/exists_test.go @@ -2,7 +2,6 @@ package blobstor import ( "context" - "os" "testing" objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" @@ -15,13 +14,9 @@ import ( ) func TestExists(t *testing.T) { - dir, err := os.MkdirTemp("", "frostfs*") - require.NoError(t, err) - t.Cleanup(func() { _ = os.RemoveAll(dir) }) - const smallSizeLimit = 512 - storages, _, largeFileStorage := defaultTestStorages(dir, smallSizeLimit) + storages, _, largeFileStorage := defaultTestStorages(t.TempDir(), smallSizeLimit) b := New(WithStorages(storages)) diff --git a/pkg/local_object_storage/blobstor/perf_test.go b/pkg/local_object_storage/blobstor/perf_test.go index c88dc85ed..f21982530 100644 --- a/pkg/local_object_storage/blobstor/perf_test.go +++ b/pkg/local_object_storage/blobstor/perf_test.go @@ -3,7 +3,6 @@ package blobstor import ( "context" "fmt" - "os" "testing" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/blobovniczatree" @@ -21,18 +20,13 @@ type storage struct { } func (s storage) open(b *testing.B) common.Storage { - dir, err := os.MkdirTemp(os.TempDir(), s.desc) - if err != nil { - b.Fatalf("creating %s root path: %v", s.desc, err) - } - st := s.create(dir) + st := s.create(b.TempDir()) require.NoError(b, st.Open(false)) require.NoError(b, st.Init()) b.Cleanup(func() { require.NoError(b, st.Close()) - require.NoError(b, os.RemoveAll(dir)) }) return st diff --git a/pkg/local_object_storage/engine/control_test.go b/pkg/local_object_storage/engine/control_test.go index 046968dbe..d7eaae1d8 100644 --- a/pkg/local_object_storage/engine/control_test.go +++ b/pkg/local_object_storage/engine/control_test.go @@ -233,8 +233,7 @@ func TestExecBlocks(t *testing.T) { } func TestPersistentShardID(t *testing.T) { - dir, err := os.MkdirTemp("", "*") - require.NoError(t, err) + dir := t.TempDir() te := newEngineWithErrorThreshold(t, dir, 1) diff --git a/pkg/local_object_storage/engine/error_test.go b/pkg/local_object_storage/engine/error_test.go index 017b635d4..dc28d35fa 100644 --- a/pkg/local_object_storage/engine/error_test.go +++ b/pkg/local_object_storage/engine/error_test.go @@ -41,11 +41,7 @@ type testShard struct { func newEngineWithErrorThreshold(t testing.TB, dir string, errThreshold uint32) *testEngine { if dir == "" { - var err error - - dir, err = os.MkdirTemp("", "*") - require.NoError(t, err) - t.Cleanup(func() { _ = os.RemoveAll(dir) }) + dir = t.TempDir() } var testShards [2]*testShard @@ -171,9 +167,7 @@ func TestErrorReporting(t *testing.T) { } func TestBlobstorFailback(t *testing.T) { - dir, err := os.MkdirTemp("", "*") - require.NoError(t, err) - t.Cleanup(func() { require.NoError(t, os.RemoveAll(dir)) }) + dir := t.TempDir() te := newEngineWithErrorThreshold(t, dir, 1) @@ -185,7 +179,7 @@ func TestBlobstorFailback(t *testing.T) { var prm shard.PutPrm prm.SetObject(obj) te.ng.mtx.RLock() - _, err = te.ng.shards[te.shards[0].id.String()].Shard.Put(context.Background(), prm) + _, err := te.ng.shards[te.shards[0].id.String()].Shard.Put(context.Background(), prm) te.ng.mtx.RUnlock() require.NoError(t, err) objs = append(objs, obj) @@ -193,7 +187,7 @@ func TestBlobstorFailback(t *testing.T) { for i := range objs { addr := object.AddressOf(objs[i]) - _, err = te.ng.Get(context.Background(), GetPrm{addr: addr}) + _, err := te.ng.Get(context.Background(), GetPrm{addr: addr}) require.NoError(t, err) _, err = te.ng.GetRange(context.Background(), RngPrm{addr: addr}) require.NoError(t, err) diff --git a/pkg/local_object_storage/engine/evacuate_test.go b/pkg/local_object_storage/engine/evacuate_test.go index 11cb362e0..bea6d4ff5 100644 --- a/pkg/local_object_storage/engine/evacuate_test.go +++ b/pkg/local_object_storage/engine/evacuate_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "os" "path/filepath" "strconv" "testing" @@ -25,9 +24,7 @@ import ( ) func newEngineEvacuate(t *testing.T, shardNum int, objPerShard int) (*StorageEngine, []*shard.ID, []*objectSDK.Object) { - dir, err := os.MkdirTemp("", "*") - require.NoError(t, err) - t.Cleanup(func() { _ = os.RemoveAll(dir) }) + dir := t.TempDir() te := testNewEngine(t, WithShardPoolSize(1)). setShardsNumOpts(t, shardNum, func(id int) []shard.Option { diff --git a/pkg/local_object_storage/pilorama/forest_test.go b/pkg/local_object_storage/pilorama/forest_test.go index caa999f94..0cff28c3f 100644 --- a/pkg/local_object_storage/pilorama/forest_test.go +++ b/pkg/local_object_storage/pilorama/forest_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "math/rand" - "os" "path/filepath" "strconv" "sync" @@ -31,19 +30,14 @@ var providers = []struct { return f }}, {"bbolt", func(t testing.TB, opts ...Option) Forest { - // Use `os.TempDir` because we construct multiple times in the same test. - tmpDir, err := os.MkdirTemp(os.TempDir(), "*") - require.NoError(t, err) - f := NewBoltForest( append([]Option{ - WithPath(filepath.Join(tmpDir, "test.db")), + WithPath(filepath.Join(t.TempDir(), "test.db")), WithMaxBatchSize(1)}, opts...)...) require.NoError(t, f.Open(false)) require.NoError(t, f.Init()) t.Cleanup(func() { require.NoError(t, f.Close()) - require.NoError(t, os.RemoveAll(tmpDir)) }) return f }},