Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory (#5574)
Browse files Browse the repository at this point in the history
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee authored Aug 6, 2022
1 parent 9812cea commit 9f21c1d
Show file tree
Hide file tree
Showing 29 changed files with 91 additions and 286 deletions.
10 changes: 4 additions & 6 deletions cmd/thanos/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,15 @@ func (b *erroringBucket) Name() string {
// Testing for https://github.com/thanos-io/thanos/issues/4960.
func TestRegression4960_Deadlock(t *testing.T) {
logger := log.NewLogfmtLogger(os.Stderr)
dir, err := os.MkdirTemp("", "test-compact-cleanup")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
dir := t.TempDir()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

bkt := objstore.WithNoopInstr(objstore.NewInMemBucket())
bkt = &erroringBucket{bkt: bkt}
var id, id2, id3 ulid.ULID
var err error
{
id, err = e2eutil.CreateBlock(
ctx,
Expand Down Expand Up @@ -167,15 +166,14 @@ func TestRegression4960_Deadlock(t *testing.T) {

func TestCleanupDownsampleCacheFolder(t *testing.T) {
logger := log.NewLogfmtLogger(os.Stderr)
dir, err := os.MkdirTemp("", "test-compact-cleanup")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
dir := t.TempDir()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

bkt := objstore.WithNoopInstr(objstore.NewInMemBucket())
var id ulid.ULID
var err error
{
id, err = e2eutil.CreateBlock(
ctx,
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/blocks/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func testEndpoint(t *testing.T, test endpointTestCase, name string, responseComp

func TestMarkBlockEndpoint(t *testing.T) {
ctx := context.Background()
tmpDir, err := os.MkdirTemp("", "test-read-mark")
testutil.Ok(t, err)
tmpDir := t.TempDir()

// create block
b1, err := e2eutil.CreateBlock(ctx, tmpDir, []labels.Labels{
Expand Down
6 changes: 2 additions & 4 deletions pkg/api/query/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"math/rand"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -680,8 +679,7 @@ func TestMetadataEndpoints(t *testing.T) {
},
}

dir, err := os.MkdirTemp("", "prometheus-test")
testutil.Ok(t, err)
dir := t.TempDir()

const chunkRange int64 = 600_000
var series []storage.Series
Expand All @@ -699,7 +697,7 @@ func TestMetadataEndpoints(t *testing.T) {
series = append(series, storage.NewListSeries(lbl, samples))
}

_, err = tsdb.CreateBlock(series, dir, chunkRange, log.NewNopLogger())
_, err := tsdb.CreateBlock(series, dir, chunkRange, log.NewNopLogger())
testutil.Ok(t, err)

opts := tsdb.DefaultOptions()
Expand Down
26 changes: 6 additions & 20 deletions pkg/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func TestUpload(t *testing.T) {

ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-block-upload")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt := objstore.NewInMemBucket()
b1, err := e2eutil.CreateBlock(ctx, tmpDir, []labels.Labels{
Expand Down Expand Up @@ -233,9 +231,7 @@ func TestDelete(t *testing.T) {
defer testutil.TolerantVerifyLeak(t)
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-block-delete")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt := objstore.NewInMemBucket()
{
Expand Down Expand Up @@ -280,9 +276,7 @@ func TestMarkForDeletion(t *testing.T) {
defer testutil.TolerantVerifyLeak(t)
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-block-mark-for-delete")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

for _, tcase := range []struct {
name string
Expand Down Expand Up @@ -336,9 +330,7 @@ func TestMarkForNoCompact(t *testing.T) {
defer testutil.TolerantVerifyLeak(t)
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-block-mark-for-no-compact")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

for _, tcase := range []struct {
name string
Expand Down Expand Up @@ -396,11 +388,7 @@ func TestHashDownload(t *testing.T) {

ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-block-download")
testutil.Ok(t, err)
t.Cleanup(func() {
testutil.Ok(t, os.RemoveAll(tmpDir))
})
tmpDir := t.TempDir()

bkt := objstore.NewInMemBucket()
r := prometheus.NewRegistry()
Expand Down Expand Up @@ -492,9 +480,7 @@ func TestUploadCleanup(t *testing.T) {

ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-block-upload")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt := objstore.NewInMemBucket()
b1, err := e2eutil.CreateBlock(ctx, tmpDir, []labels.Labels{
Expand Down
4 changes: 1 addition & 3 deletions pkg/block/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ func TestMetaFetcher_Fetch(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

dir, err := os.MkdirTemp("", "test-meta-fetcher")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
dir := t.TempDir()

var ulidToDelete ulid.ULID
r := prometheus.NewRegistry()
Expand Down
9 changes: 3 additions & 6 deletions pkg/block/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
func TestRewrite(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

b, err := e2eutil.CreateBlock(ctx, tmpDir, []labels.Labels{
{{Name: "a", Value: "1"}},
Expand Down Expand Up @@ -87,10 +85,9 @@ func TestRewrite(t *testing.T) {
}

func TestGatherIndexHealthStatsReturnsOutOfOrderChunksErr(t *testing.T) {
blockDir, err := os.MkdirTemp("", "test-ooo-index")
testutil.Ok(t, err)
blockDir := t.TempDir()

err = testutil.PutOutOfOrderIndex(blockDir, 0, math.MaxInt64)
err := testutil.PutOutOfOrderIndex(blockDir, 0, math.MaxInt64)
testutil.Ok(t, err)

stats, err := GatherIndexHealthStats(log.NewLogfmtLogger(os.Stderr), blockDir+"/"+IndexFilename, 0, math.MaxInt64)
Expand Down
17 changes: 4 additions & 13 deletions pkg/block/indexheader/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"math"
"os"
"path/filepath"
"strconv"
"testing"
Expand All @@ -31,9 +30,7 @@ import (
func TestReaders(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down Expand Up @@ -332,9 +329,7 @@ func prepareIndexV2Block(t testing.TB, tmpDir string, bkt objstore.Bucket) *meta
func BenchmarkBinaryWrite(t *testing.B) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "bench-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand All @@ -351,9 +346,7 @@ func BenchmarkBinaryWrite(t *testing.B) {

func BenchmarkBinaryReader(t *testing.B) {
ctx := context.Background()
tmpDir, err := os.MkdirTemp("", "bench-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down Expand Up @@ -384,9 +377,7 @@ func benchmarkBinaryReaderLookupSymbol(b *testing.B, numSeries int) {
ctx := context.Background()
logger := log.NewNopLogger()

tmpDir, err := os.MkdirTemp("", "benchmark-lookupsymbol")
testutil.Ok(b, err)
defer func() { testutil.Ok(b, os.RemoveAll(tmpDir)) }()
tmpDir := b.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(b, err)
Expand Down
24 changes: 6 additions & 18 deletions pkg/block/indexheader/lazy_binary_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (
func TestNewLazyBinaryReader_ShouldFailIfUnableToBuildIndexHeader(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand All @@ -41,9 +39,7 @@ func TestNewLazyBinaryReader_ShouldFailIfUnableToBuildIndexHeader(t *testing.T)
func TestNewLazyBinaryReader_ShouldBuildIndexHeaderFromBucket(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down Expand Up @@ -82,9 +78,7 @@ func TestNewLazyBinaryReader_ShouldBuildIndexHeaderFromBucket(t *testing.T) {
func TestNewLazyBinaryReader_ShouldRebuildCorruptedIndexHeader(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down Expand Up @@ -122,9 +116,7 @@ func TestNewLazyBinaryReader_ShouldRebuildCorruptedIndexHeader(t *testing.T) {
func TestLazyBinaryReader_ShouldReopenOnUsageAfterClose(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down Expand Up @@ -174,9 +166,7 @@ func TestLazyBinaryReader_ShouldReopenOnUsageAfterClose(t *testing.T) {
func TestLazyBinaryReader_unload_ShouldReturnErrorIfNotIdle(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down Expand Up @@ -225,9 +215,7 @@ func TestLazyBinaryReader_LoadUnloadRaceCondition(t *testing.T) {

ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down
9 changes: 2 additions & 7 deletions pkg/block/indexheader/reader_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package indexheader

import (
"context"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -41,9 +40,7 @@ func TestReaderPool_NewBinaryReader(t *testing.T) {

ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down Expand Up @@ -79,9 +76,7 @@ func TestReaderPool_ShouldCloseIdleLazyReaders(t *testing.T) {

ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
Expand Down
4 changes: 1 addition & 3 deletions pkg/block/metadata/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
)

func TestHashSmoke(t *testing.T) {
dir, err := os.MkdirTemp("", "testhash")
testutil.Ok(t, err)
t.Cleanup(func() { os.RemoveAll(dir) })
dir := t.TempDir()
f, err := os.CreateTemp(dir, "hash")
testutil.Ok(t, err)

Expand Down
5 changes: 1 addition & 4 deletions pkg/block/metadata/markers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"context"
"encoding/json"
"os"
"path"
"testing"
"time"
Expand All @@ -28,9 +27,7 @@ func TestMain(m *testing.M) {
func TestReadMarker(t *testing.T) {
ctx := context.Background()

tmpDir, err := os.MkdirTemp("", "test-read-mark")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
tmpDir := t.TempDir()

bkt := objstore.WithNoopInstr(objstore.NewInMemBucket())
t.Run(DeletionMarkFilename, func(t *testing.T) {
Expand Down
8 changes: 2 additions & 6 deletions pkg/compact/compact_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ func testGroupCompactE2e(t *testing.T, mergeFunc storage.VerticalChunkSeriesMerg
defer cancel()

// Create fresh, empty directory for actual test.
dir, err := os.MkdirTemp("", "test-compact")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
dir := t.TempDir()

logger := log.NewLogfmtLogger(os.Stderr)

Expand Down Expand Up @@ -430,9 +428,7 @@ type blockgenSpec struct {
}

func createAndUpload(t testing.TB, bkt objstore.Bucket, blocks []blockgenSpec, blocksWithOutOfOrderChunks []blockgenSpec) (metas []*metadata.Meta) {
prepareDir, err := os.MkdirTemp("", "test-compact-prepare")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(prepareDir)) }()
prepareDir := t.TempDir()

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
Expand Down
Loading

0 comments on commit 9f21c1d

Please sign in to comment.