From 2ddb3596ef4d2b1de482337ce1943476699b82fb Mon Sep 17 00:00:00 2001 From: Mile Druzijanic <153705375+zedGGs@users.noreply.github.com> Date: Thu, 28 Dec 2023 21:49:57 +0100 Subject: [PATCH] Adding small test update for temp dir using t.TempDir (#13293) * Adding small test update for temp dir using t.TempDir Signed-off-by: Mile Druzijanic Signed-off-by: Mile Druzijanic * removing not required cleanup Signed-off-by: Mile Druzijanic --------- Signed-off-by: Mile Druzijanic Signed-off-by: Mile Druzijanic --- promql/engine_test.go | 5 +---- tsdb/tsdbutil/dir_locker_testutil.go | 10 +++------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/promql/engine_test.go b/promql/engine_test.go index 105cdc10d..b3e7d4094 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -18,7 +18,6 @@ import ( "errors" "fmt" "math" - "os" "sort" "testing" "time" @@ -47,9 +46,7 @@ func TestMain(m *testing.M) { func TestQueryConcurrency(t *testing.T) { maxConcurrency := 10 - dir, err := os.MkdirTemp("", "test_concurrency") - require.NoError(t, err) - defer os.RemoveAll(dir) + dir := t.TempDir() queryTracker := NewActiveQueryTracker(dir, maxConcurrency, nil) t.Cleanup(queryTracker.Close) diff --git a/tsdb/tsdbutil/dir_locker_testutil.go b/tsdb/tsdbutil/dir_locker_testutil.go index a4cf5abd6..a7f43d8ee 100644 --- a/tsdb/tsdbutil/dir_locker_testutil.go +++ b/tsdb/tsdbutil/dir_locker_testutil.go @@ -60,12 +60,8 @@ func TestDirLockerUsage(t *testing.T, open func(t *testing.T, data string, creat for _, c := range cases { t.Run(fmt.Sprintf("%+v", c), func(t *testing.T) { - tmpdir, err := os.MkdirTemp("", "test") - require.NoError(t, err) - t.Cleanup(func() { - require.NoError(t, os.RemoveAll(tmpdir)) - }) - + tmpdir := t.TempDir() + // Test preconditions (file already exists + lockfile option) if c.fileAlreadyExists { tmpLocker, err := NewDirLocker(tmpdir, "tsdb", log.NewNopLogger(), nil) @@ -82,7 +78,7 @@ func TestDirLockerUsage(t *testing.T, open func(t *testing.T, data string, creat // Check that the lockfile is always deleted if !c.lockFileDisabled { - _, err = os.Stat(locker.path) + _, err := os.Stat(locker.path) require.True(t, os.IsNotExist(err), "lockfile was not deleted") } })