Merge pull request #10193 from Juneezee/refactor/testing.TempDir

refactor: use `T.TempDir()` and `B.TempDir` to create temporary directory
pull/10209/head
Julien Pivotto 2022-01-25 00:08:10 +01:00 committed by GitHub
commit 7c6595ed14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 86 additions and 397 deletions

View File

@ -53,12 +53,9 @@ type testRunner struct {
func newTestRunner(t *testing.T) *testRunner {
t.Helper()
tmpDir, err := ioutil.TempDir("", "prometheus-file-sd")
require.NoError(t, err)
return &testRunner{
T: t,
dir: tmpDir,
dir: t.TempDir(),
ch: make(chan []*targetgroup.Group),
done: make(chan struct{}),
stopped: make(chan struct{}),

View File

@ -913,11 +913,7 @@ func TestCalculateDesiredShardsDetail(t *testing.T) {
cfg := config.DefaultQueueConfig
mcfg := config.DefaultMetadataConfig
dir, err := ioutil.TempDir("", "TestCalculateDesiredShards")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
metrics := newQueueManagerMetrics(nil, "", "")
samplesIn := newEWMARate(ewmaWeight, shardUpdateDuration)

View File

@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"hash/crc32"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
@ -42,13 +41,9 @@ import (
// to 2. We had a migration in place resetting it to 1 but we should move immediately to
// version 3 next time to avoid confusion and issues.
func TestBlockMetaMustNeverBeVersion2(t *testing.T) {
dir, err := ioutil.TempDir("", "metaversion")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
_, err = writeMetaFile(log.NewNopLogger(), dir, &BlockMeta{})
_, err := writeMetaFile(log.NewNopLogger(), dir, &BlockMeta{})
require.NoError(t, err)
meta, _, err := readMetaFile(dir)
@ -57,11 +52,7 @@ func TestBlockMetaMustNeverBeVersion2(t *testing.T) {
}
func TestSetCompactionFailed(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
blockDir := createBlock(t, tmpdir, genSeries(1, 1, 0, 1))
b, err := OpenBlock(nil, blockDir, nil)
@ -78,11 +69,7 @@ func TestSetCompactionFailed(t *testing.T) {
}
func TestCreateBlock(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
b, err := OpenBlock(nil, createBlock(t, tmpdir, genSeries(1, 1, 0, 10)), nil)
if err == nil {
require.NoError(t, b.Close())
@ -173,11 +160,7 @@ func TestCorruptedChunk(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test_open_block_chunk_corrupted")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
series := storage.NewListSeries(labels.FromStrings("a", "b"), []tsdbutil.Sample{sample{1, 1}})
blockDir := createBlock(t, tmpdir, []storage.Series{series})
@ -215,11 +198,7 @@ func TestCorruptedChunk(t *testing.T) {
}
func TestLabelValuesWithMatchers(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test_block_label_values_with_matchers")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
var seriesEntries []storage.Series
for i := 0; i < 100; i++ {
@ -288,16 +267,13 @@ func TestLabelValuesWithMatchers(t *testing.T) {
// TestBlockSize ensures that the block size is calculated correctly.
func TestBlockSize(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test_blockSize")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
var (
blockInit *Block
expSizeInit int64
blockDirInit string
err error
)
// Create a block and compare the reported size vs actual disk size.
@ -376,11 +352,7 @@ func TestReadIndexFormatV1(t *testing.T) {
}
func BenchmarkLabelValuesWithMatchers(b *testing.B) {
tmpdir, err := ioutil.TempDir("", "bench_block_label_values_with_matchers")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(tmpdir))
}()
tmpdir := b.TempDir()
var seriesEntries []storage.Series
metricCount := 1000000
@ -419,9 +391,7 @@ func BenchmarkLabelValuesWithMatchers(b *testing.B) {
}
func TestLabelNamesWithMatchers(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test_block_label_names_with_matchers")
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, os.RemoveAll(tmpdir)) })
tmpdir := t.TempDir()
var seriesEntries []storage.Series
for i := 0; i < 100; i++ {

View File

@ -15,9 +15,7 @@ package tsdb
import (
"context"
"io/ioutil"
"math"
"os"
"path/filepath"
"testing"
@ -30,9 +28,7 @@ import (
func TestBlockWriter(t *testing.T) {
ctx := context.Background()
outputDir, err := ioutil.TempDir(os.TempDir(), "output")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(outputDir)) }()
outputDir := t.TempDir()
w, err := NewBlockWriter(log.NewNopLogger(), outputDir, DefaultBlockDuration)
require.NoError(t, err)

View File

@ -450,12 +450,7 @@ func TestHeadReadWriter_ReadRepairOnEmptyLastFile(t *testing.T) {
func createChunkDiskMapper(t *testing.T, dir string) *ChunkDiskMapper {
if dir == "" {
var err error
dir, err = ioutil.TempDir("", "data")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(dir))
})
dir = t.TempDir()
}
hrw, err := NewChunkDiskMapper(nil, dir, chunkenc.NewPool(), DefaultWriteBufferSize, DefaultWriteQueueSize)

View File

@ -16,7 +16,6 @@ package tsdb
import (
"context"
"fmt"
"io/ioutil"
"math"
"os"
"path"
@ -435,11 +434,7 @@ func TestCompactionFailWillCleanUpTempDir(t *testing.T) {
}, nil, nil)
require.NoError(t, err)
tmpdir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
require.Error(t, compactor.write(tmpdir, &BlockMeta{}, erringBReader{}))
_, err = os.Stat(filepath.Join(tmpdir, BlockMeta{}.ULID.String()) + tmpForCreationBlockDirSuffix)
@ -1049,11 +1044,7 @@ func BenchmarkCompaction(b *testing.B) {
for _, c := range cases {
nBlocks := len(c.ranges)
b.Run(fmt.Sprintf("type=%s,blocks=%d,series=%d,samplesPerSeriesPerBlock=%d", c.compactionType, nBlocks, nSeries, c.ranges[0][1]-c.ranges[0][0]+1), func(b *testing.B) {
dir, err := ioutil.TempDir("", "bench_compaction")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(dir))
}()
dir := b.TempDir()
blockDirs := make([]string, 0, len(c.ranges))
var blocks []*Block
for _, r := range c.ranges {
@ -1080,20 +1071,12 @@ func BenchmarkCompaction(b *testing.B) {
}
func BenchmarkCompactionFromHead(b *testing.B) {
dir, err := ioutil.TempDir("", "bench_compaction_from_head")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(dir))
}()
dir := b.TempDir()
totalSeries := 100000
for labelNames := 1; labelNames < totalSeries; labelNames *= 10 {
labelValues := totalSeries / labelNames
b.Run(fmt.Sprintf("labelnames=%d,labelvalues=%d", labelNames, labelValues), func(b *testing.B) {
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(chunkDir))
}()
chunkDir := b.TempDir()
opts := DefaultHeadOptions()
opts.ChunkRange = 1000
opts.ChunkDirRoot = chunkDir
@ -1175,11 +1158,7 @@ func TestDisableAutoCompactions(t *testing.T) {
// TestCancelCompactions ensures that when the db is closed
// any running compaction is cancelled to unblock closing the db.
func TestCancelCompactions(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "testCancelCompaction")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
// Create some blocks to fall within the compaction range.
createBlock(t, tmpdir, genSeries(1, 10000, 0, 1000))
@ -1188,7 +1167,7 @@ func TestCancelCompactions(t *testing.T) {
// Copy the db so we have an exact copy to compare compaction times.
tmpdirCopy := tmpdir + "Copy"
err = fileutil.CopyDirs(tmpdir, tmpdirCopy)
err := fileutil.CopyDirs(tmpdir, tmpdirCopy)
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdirCopy))

View File

@ -64,8 +64,8 @@ func TestMain(m *testing.M) {
}
func openTestDB(t testing.TB, opts *Options, rngs []int64) (db *DB) {
tmpdir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
tmpdir := t.TempDir()
var err error
if len(rngs) == 0 {
db, err = Open(tmpdir, nil, nil, opts, nil)
@ -76,9 +76,6 @@ func openTestDB(t testing.TB, opts *Options, rngs []int64) (db *DB) {
require.NoError(t, err)
// Do not Close() the test database by default as it will deadlock on test failures.
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(tmpdir))
})
return db
}
@ -573,17 +570,12 @@ func TestDB_Snapshot(t *testing.T) {
require.NoError(t, app.Commit())
// create snapshot
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(snap))
}()
snap := t.TempDir()
require.NoError(t, db.Snapshot(snap, true))
require.NoError(t, db.Close())
// reopen DB from snapshot
db, err = Open(snap, nil, nil, nil, nil)
db, err := Open(snap, nil, nil, nil, nil)
require.NoError(t, err)
defer func() { require.NoError(t, db.Close()) }()
@ -622,20 +614,16 @@ func TestDB_Snapshot_ChunksOutsideOfCompactedRange(t *testing.T) {
}
require.NoError(t, app.Commit())
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
snap := t.TempDir()
// Hackingly introduce "race", by having lower max time then maxTime in last chunk.
db.head.maxTime.Sub(10)
defer func() {
require.NoError(t, os.RemoveAll(snap))
}()
require.NoError(t, db.Snapshot(snap, true))
require.NoError(t, db.Close())
// Reopen DB from snapshot.
db, err = Open(snap, nil, nil, nil, nil)
db, err := Open(snap, nil, nil, nil, nil)
require.NoError(t, err)
defer func() { require.NoError(t, db.Close()) }()
@ -696,12 +684,8 @@ Outer:
}
// create snapshot
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
snap := t.TempDir()
defer func() {
require.NoError(t, os.RemoveAll(snap))
}()
require.NoError(t, db.Snapshot(snap, true))
// reopen DB from snapshot
@ -1124,17 +1108,12 @@ func TestTombstoneClean(t *testing.T) {
// Delete the ranges.
// Create snapshot.
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(snap))
}()
snap := t.TempDir()
require.NoError(t, db.Snapshot(snap, true))
require.NoError(t, db.Close())
// Reopen DB from snapshot.
db, err = Open(snap, nil, nil, nil, nil)
db, err := Open(snap, nil, nil, nil, nil)
require.NoError(t, err)
defer db.Close()
@ -1213,17 +1192,12 @@ func TestTombstoneCleanResultEmptyBlock(t *testing.T) {
intervals := tombstones.Intervals{{Mint: 0, Maxt: numSamples}}
// Create snapshot.
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(snap))
}()
snap := t.TempDir()
require.NoError(t, db.Snapshot(snap, true))
require.NoError(t, db.Close())
// Reopen DB from snapshot.
db, err = Open(snap, nil, nil, nil, nil)
db, err := Open(snap, nil, nil, nil, nil)
require.NoError(t, err)
defer db.Close()
@ -1847,11 +1821,7 @@ func TestQuerierWithBoundaryChunks(t *testing.T) {
// - with blocks with WAL: same as above
func TestInitializeHeadTimestamp(t *testing.T) {
t.Run("clean", func(t *testing.T) {
dir, err := ioutil.TempDir("", "test_head_init")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
db, err := Open(dir, nil, nil, nil, nil)
require.NoError(t, err)
@ -1871,11 +1841,7 @@ func TestInitializeHeadTimestamp(t *testing.T) {
require.Equal(t, int64(1000), db.head.MaxTime())
})
t.Run("wal-only", func(t *testing.T) {
dir, err := ioutil.TempDir("", "test_head_init")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
require.NoError(t, os.MkdirAll(path.Join(dir, "wal"), 0o777))
w, err := wal.New(nil, nil, path.Join(dir, "wal"), false)
@ -1903,11 +1869,7 @@ func TestInitializeHeadTimestamp(t *testing.T) {
require.Equal(t, int64(15000), db.head.MaxTime())
})
t.Run("existing-block", func(t *testing.T) {
dir, err := ioutil.TempDir("", "test_head_init")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
createBlock(t, dir, genSeries(1, 1, 1000, 2000))
@ -1919,11 +1881,7 @@ func TestInitializeHeadTimestamp(t *testing.T) {
require.Equal(t, int64(2000), db.head.MaxTime())
})
t.Run("existing-block-and-wal", func(t *testing.T) {
dir, err := ioutil.TempDir("", "test_head_init")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
createBlock(t, dir, genSeries(1, 1, 1000, 6000))
@ -2214,8 +2172,7 @@ func TestBlockRanges(t *testing.T) {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
ctx := context.Background()
dir, err := ioutil.TempDir("", "test_storage")
require.NoError(t, err)
dir := t.TempDir()
// Test that the compactor doesn't create overlapping blocks
// when a non standard block already exists.
@ -2225,9 +2182,7 @@ func TestBlockRanges(t *testing.T) {
require.NoError(t, err)
rangeToTriggerCompaction := db.compactor.(*LeveledCompactor).ranges[0]/2*3 + 1
defer func() {
os.RemoveAll(dir)
}()
app := db.Appender(ctx)
lbl := labels.Labels{{Name: "a", Value: "b"}}
_, err = app.Append(0, lbl, firstBlockMaxT-1, rand.Float64())
@ -2314,12 +2269,7 @@ func TestDBReadOnly(t *testing.T) {
// Bootstrap the db.
{
dbDir, err = ioutil.TempDir("", "test")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dbDir))
}()
dbDir = t.TempDir()
dbBlocks := []*BlockMeta{
// Create three 2-sample blocks.
@ -2408,12 +2358,7 @@ func TestDBReadOnly(t *testing.T) {
// TestDBReadOnlyClosing ensures that after closing the db
// all api methods return an ErrClosed.
func TestDBReadOnlyClosing(t *testing.T) {
dbDir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dbDir))
}()
dbDir := t.TempDir()
db, err := OpenDBReadOnly(dbDir, log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)))
require.NoError(t, err)
require.NoError(t, db.Close())
@ -2435,12 +2380,7 @@ func TestDBReadOnly_FlushWAL(t *testing.T) {
// Bootstrap the db.
{
dbDir, err = ioutil.TempDir("", "test")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dbDir))
}()
dbDir = t.TempDir()
// Append data to the WAL.
db, err := Open(dbDir, logger, nil, nil, nil)
@ -2460,12 +2400,7 @@ func TestDBReadOnly_FlushWAL(t *testing.T) {
db, err := OpenDBReadOnly(dbDir, logger)
require.NoError(t, err)
flush, err := ioutil.TempDir("", "flush")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(flush))
}()
flush := t.TempDir()
require.NoError(t, db.FlushWAL(flush))
require.NoError(t, db.Close())
@ -2503,10 +2438,7 @@ func TestDBCannotSeePartialCommits(t *testing.T) {
t.Skip("skipping test since tsdb isolation is disabled")
}
tmpdir, _ := ioutil.TempDir("", "test")
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
db, err := Open(tmpdir, nil, nil, nil, nil)
require.NoError(t, err)
@ -2577,10 +2509,7 @@ func TestDBQueryDoesntSeeAppendsAfterCreation(t *testing.T) {
t.Skip("skipping test since tsdb isolation is disabled")
}
tmpdir, _ := ioutil.TempDir("", "test")
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
db, err := Open(tmpdir, nil, nil, nil, nil)
require.NoError(t, err)
@ -2767,9 +2696,7 @@ func TestChunkWriter_ReadAfterWrite(t *testing.T) {
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "test_chunk_writer")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(tempDir)) }()
tempDir := t.TempDir()
chunkw, err := chunks.NewWriterWithSegSize(tempDir, chunks.SegmentHeaderSize+int64(test.segmentSize))
require.NoError(t, err)
@ -2856,9 +2783,7 @@ func TestChunkReader_ConcurrentReads(t *testing.T) {
tsdbutil.ChunkFromSamples([]tsdbutil.Sample{sample{1, 5}}),
}
tempDir, err := ioutil.TempDir("", "test_chunk_writer")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(tempDir)) }()
tempDir := t.TempDir()
chunkw, err := chunks.NewWriter(tempDir)
require.NoError(t, err)
@ -2895,9 +2820,7 @@ func TestChunkReader_ConcurrentReads(t *testing.T) {
// * compacts the head; and
// * queries the db to ensure the samples are present from the compacted head.
func TestCompactHead(t *testing.T) {
dbDir, err := ioutil.TempDir("", "testFlush")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dbDir)) }()
dbDir := t.TempDir()
// Open a DB and append data to the WAL.
tsdbCfg := &Options{
@ -2978,11 +2901,7 @@ func deleteNonBlocks(dbDir string) error {
}
func TestOpen_VariousBlockStates(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(tmpDir))
})
tmpDir := t.TempDir()
var (
expectedLoadedDirs = map[string]struct{}{}
@ -3097,11 +3016,7 @@ func TestOneCheckpointPerCompactCall(t *testing.T) {
MaxBlockDuration: blockRange,
}
tmpDir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(tmpDir))
})
tmpDir := t.TempDir()
db, err := Open(tmpDir, log.NewNopLogger(), prometheus.NewRegistry(), tsdbCfg, nil)
require.NoError(t, err)
@ -3210,11 +3125,7 @@ func TestOneCheckpointPerCompactCall(t *testing.T) {
}
func TestNoPanicOnTSDBOpenError(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(tmpdir))
})
tmpdir := t.TempDir()
// Taking the lock will cause a TSDB startup error.
l, err := tsdbutil.NewDirLocker(tmpdir, "tsdb", log.NewNopLogger(), nil)
@ -3499,11 +3410,7 @@ func testChunkQuerierShouldNotPanicIfHeadChunkIsTruncatedWhileReadingQueriedChun
}
func newTestDB(t *testing.T) *DB {
dir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(dir))
})
dir := t.TempDir()
db, err := Open(dir, nil, nil, DefaultOptions(), nil)
require.NoError(t, err)

View File

@ -14,8 +14,6 @@
package tsdb
import (
"io/ioutil"
"os"
"strconv"
"testing"
@ -27,11 +25,7 @@ import (
)
func BenchmarkHeadStripeSeriesCreate(b *testing.B) {
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(chunkDir))
}()
chunkDir := b.TempDir()
// Put a series, select it. GC it and then access it.
opts := DefaultHeadOptions()
opts.ChunkRange = 1000
@ -46,11 +40,7 @@ func BenchmarkHeadStripeSeriesCreate(b *testing.B) {
}
func BenchmarkHeadStripeSeriesCreateParallel(b *testing.B) {
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(chunkDir))
}()
chunkDir := b.TempDir()
// Put a series, select it. GC it and then access it.
opts := DefaultHeadOptions()
opts.ChunkRange = 1000
@ -70,11 +60,7 @@ func BenchmarkHeadStripeSeriesCreateParallel(b *testing.B) {
}
func BenchmarkHeadStripeSeriesCreate_PreCreationFailure(b *testing.B) {
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(chunkDir))
}()
chunkDir := b.TempDir()
// Put a series, select it. GC it and then access it.
opts := DefaultHeadOptions()
opts.ChunkRange = 1000

View File

@ -51,8 +51,7 @@ import (
)
func newTestHead(t testing.TB, chunkRange int64, compressWAL bool) (*Head, *wal.WAL) {
dir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
dir := t.TempDir()
wlog, err := wal.NewSize(nil, nil, filepath.Join(dir, "wal"), 32768, compressWAL)
require.NoError(t, err)
@ -67,9 +66,6 @@ func newTestHead(t testing.TB, chunkRange int64, compressWAL bool) (*Head, *wal.
require.NoError(t, h.chunkDiskMapper.IterateAllChunks(func(_ chunks.HeadSeriesRef, _ chunks.ChunkDiskMapperRef, _, _ int64, _ uint16) error { return nil }))
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(dir))
})
return h, wlog
}
@ -183,11 +179,7 @@ func BenchmarkLoadWAL(b *testing.B) {
// fmt.Println("exemplars per series: ", exemplarsPerSeries)
b.Run(fmt.Sprintf("batches=%d,seriesPerBatch=%d,samplesPerSeries=%d,exemplarsPerSeries=%d,mmappedChunkT=%d", c.batches, c.seriesPerBatch, c.samplesPerSeries, exemplarsPerSeries, c.mmappedChunkT),
func(b *testing.B) {
dir, err := ioutil.TempDir("", "test_load_wal")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(dir))
}()
dir := b.TempDir()
w, err := wal.New(nil, nil, dir, false)
require.NoError(b, err)
@ -724,11 +716,7 @@ func TestHead_Truncate(t *testing.T) {
// Validate various behaviors brought on by firstChunkID accounting for
// garbage collected chunks.
func TestMemSeries_truncateChunks(t *testing.T) {
dir, err := ioutil.TempDir("", "truncate_chunks")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
// This is usually taken from the Head, but passing manually here.
chunkDiskMapper, err := chunks.NewChunkDiskMapper(nil, dir, chunkenc.NewPool(), chunks.DefaultWriteBufferSize, chunks.DefaultWriteQueueSize)
require.NoError(t, err)
@ -1268,11 +1256,7 @@ func TestComputeChunkEndTime(t *testing.T) {
}
func TestMemSeries_append(t *testing.T) {
dir, err := ioutil.TempDir("", "append")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
// This is usually taken from the Head, but passing manually here.
chunkDiskMapper, err := chunks.NewChunkDiskMapper(nil, dir, chunkenc.NewPool(), chunks.DefaultWriteBufferSize, chunks.DefaultWriteQueueSize)
require.NoError(t, err)
@ -1556,11 +1540,7 @@ func TestWalRepair_DecodingError(t *testing.T) {
} {
for _, compress := range []bool{false, true} {
t.Run(fmt.Sprintf("%s,compress=%t", name, compress), func(t *testing.T) {
dir, err := ioutil.TempDir("", "wal_repair")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
// Fill the wal and corrupt it.
{
@ -1620,11 +1600,7 @@ func TestWalRepair_DecodingError(t *testing.T) {
}
func TestHeadReadWriterRepair(t *testing.T) {
dir, err := ioutil.TempDir("", "head_read_writer_repair")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
const chunkRange = 1000
@ -2030,11 +2006,7 @@ func TestIsolationWithoutAdd(t *testing.T) {
}
func TestOutOfOrderSamplesMetric(t *testing.T) {
dir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
db, err := Open(dir, nil, nil, DefaultOptions(), nil)
require.NoError(t, err)
@ -2456,11 +2428,7 @@ func BenchmarkHeadLabelValuesWithMatchers(b *testing.B) {
}
func TestMemSafeIteratorSeekIntoBuffer(t *testing.T) {
dir, err := ioutil.TempDir("", "iterator_seek")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
// This is usually taken from the Head, but passing manually here.
chunkDiskMapper, err := chunks.NewChunkDiskMapper(nil, dir, chunkenc.NewPool(), chunks.DefaultWriteBufferSize, chunks.DefaultWriteQueueSize)
require.NoError(t, err)

View File

@ -137,11 +137,7 @@ func (m mockIndex) Series(ref storage.SeriesRef, lset *labels.Labels, chks *[]ch
}
func TestIndexRW_Create_Open(t *testing.T) {
dir, err := ioutil.TempDir("", "test_index_create")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
fn := filepath.Join(dir, indexFilename)
@ -166,11 +162,7 @@ func TestIndexRW_Create_Open(t *testing.T) {
}
func TestIndexRW_Postings(t *testing.T) {
dir, err := ioutil.TempDir("", "test_index_postings")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
fn := filepath.Join(dir, indexFilename)
@ -250,11 +242,7 @@ func TestIndexRW_Postings(t *testing.T) {
}
func TestPostingsMany(t *testing.T) {
dir, err := ioutil.TempDir("", "test_postings_many")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
fn := filepath.Join(dir, indexFilename)
@ -344,11 +332,7 @@ func TestPostingsMany(t *testing.T) {
}
func TestPersistence_index_e2e(t *testing.T) {
dir, err := ioutil.TempDir("", "test_persistence_e2e")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
lbls, err := labels.ReadLabels(filepath.Join("..", "testdata", "20kseries.json"), 20000)
require.NoError(t, err)

View File

@ -16,8 +16,6 @@ package tsdb
import (
"context"
"fmt"
"io/ioutil"
"os"
"strconv"
"testing"
@ -32,11 +30,7 @@ const (
)
func BenchmarkQuerier(b *testing.B) {
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(chunkDir))
}()
chunkDir := b.TempDir()
opts := DefaultHeadOptions()
opts.ChunkRange = 1000
opts.ChunkDirRoot = chunkDir
@ -74,11 +68,7 @@ func BenchmarkQuerier(b *testing.B) {
})
})
tmpdir, err := ioutil.TempDir("", "test_benchpostingsformatchers")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(tmpdir))
}()
tmpdir := b.TempDir()
blockdir := createBlockFromHead(b, tmpdir, h)
block, err := OpenBlock(nil, blockdir, nil)
@ -186,11 +176,7 @@ func benchmarkLabelValuesWithMatchers(b *testing.B, ir IndexReader) {
}
func BenchmarkQuerierSelect(b *testing.B) {
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(chunkDir))
}()
chunkDir := b.TempDir()
opts := DefaultHeadOptions()
opts.ChunkRange = 1000
opts.ChunkDirRoot = chunkDir
@ -230,11 +216,7 @@ func BenchmarkQuerierSelect(b *testing.B) {
bench(b, h, true)
})
tmpdir, err := ioutil.TempDir("", "test_benchquerierselect")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(tmpdir))
}()
tmpdir := b.TempDir()
blockdir := createBlockFromHead(b, tmpdir, h)
block, err := OpenBlock(nil, blockdir, nil)

View File

@ -16,10 +16,8 @@ package tsdb
import (
"context"
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
"path/filepath"
"sort"
"strconv"
@ -1329,11 +1327,7 @@ func BenchmarkQueryIterator(b *testing.B) {
c.numBlocks, c.numSeries, c.numSamplesPerSeriesPerBlock, overlapPercentage)
b.Run(benchMsg, func(b *testing.B) {
dir, err := ioutil.TempDir("", "bench_query_iterator")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(dir))
}()
dir := b.TempDir()
var (
blocks []*Block
@ -1396,11 +1390,7 @@ func BenchmarkQuerySeek(b *testing.B) {
c.numBlocks, c.numSeries, c.numSamplesPerSeriesPerBlock, overlapPercentage)
b.Run(benchMsg, func(b *testing.B) {
dir, err := ioutil.TempDir("", "bench_query_iterator")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(dir))
}()
dir := b.TempDir()
var (
blocks []*Block
@ -1451,7 +1441,6 @@ func BenchmarkQuerySeek(b *testing.B) {
require.NoError(b, it.Err())
}
require.NoError(b, ss.Err())
require.NoError(b, err)
require.Equal(b, 0, len(ss.Warnings()))
})
}
@ -1537,11 +1526,7 @@ func BenchmarkSetMatcher(b *testing.B) {
}
for _, c := range cases {
dir, err := ioutil.TempDir("", "bench_postings_for_matchers")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(dir))
}()
dir := b.TempDir()
var (
blocks []*Block
@ -1654,11 +1639,7 @@ func TestFindSetMatches(t *testing.T) {
}
func TestPostingsForMatchers(t *testing.T) {
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(chunkDir))
}()
chunkDir := t.TempDir()
opts := DefaultHeadOptions()
opts.ChunkRange = 1000
opts.ChunkDirRoot = chunkDir
@ -1915,13 +1896,7 @@ func TestPostingsForMatchers(t *testing.T) {
// TestClose ensures that calling Close more than once doesn't block and doesn't panic.
func TestClose(t *testing.T) {
dir, err := ioutil.TempDir("", "test_storage")
if err != nil {
t.Fatalf("Opening test dir failed: %s", err)
}
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
createBlock(t, dir, genSeries(1, 1, 0, 10))
createBlock(t, dir, genSeries(1, 1, 10, 20))
@ -1982,11 +1957,7 @@ func BenchmarkQueries(b *testing.B) {
for title, selectors := range cases {
for _, nSeries := range []int{10} {
for _, nSamples := range []int64{1000, 10000, 100000} {
dir, err := ioutil.TempDir("", "test_persisted_query")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(dir))
}()
dir := b.TempDir()
series := genSeries(nSeries, 5, 1, nSamples)
@ -2024,11 +1995,7 @@ func BenchmarkQueries(b *testing.B) {
queryTypes["_3-Blocks"] = storage.NewMergeQuerier(qs[0:3], nil, storage.ChainedSeriesMerge)
queryTypes["_10-Blocks"] = storage.NewMergeQuerier(qs, nil, storage.ChainedSeriesMerge)
chunkDir, err := ioutil.TempDir("", "chunk_dir")
require.NoError(b, err)
defer func() {
require.NoError(b, os.RemoveAll(chunkDir))
}()
chunkDir := b.TempDir()
head := createHead(b, nil, series, chunkDir)
qHead, err := NewBlockQuerier(head, 1, nSamples)
require.NoError(b, err)

View File

@ -14,7 +14,6 @@
package tsdb
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -28,11 +27,7 @@ import (
)
func TestRepairBadIndexVersion(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(tmpDir))
})
tmpDir := t.TempDir()
// The broken index used in this test was written by the following script
// at a broken revision.
@ -74,7 +69,7 @@ func TestRepairBadIndexVersion(t *testing.T) {
// Check the current db.
// In its current state, lookups should fail with the fixed code.
_, _, err = readMetaFile(tmpDbDir)
_, _, err := readMetaFile(tmpDbDir)
require.Error(t, err)
// Touch chunks dir in block to imitate them.

View File

@ -14,10 +14,8 @@
package tombstones
import (
"io/ioutil"
"math"
"math/rand"
"os"
"sync"
"testing"
"time"
@ -34,10 +32,7 @@ func TestMain(m *testing.M) {
}
func TestWriteAndReadbackTombstones(t *testing.T) {
tmpdir, _ := ioutil.TempDir("", "test")
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
ref := uint64(0)

View File

@ -39,11 +39,7 @@ import (
)
func TestSegmentWAL_cut(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test_wal_cut")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(tmpdir))
}()
tmpdir := t.TempDir()
// This calls cut() implicitly the first time without a previous tail.
w, err := OpenSegmentWAL(tmpdir, nil, 0, nil)
@ -89,11 +85,7 @@ func TestSegmentWAL_Truncate(t *testing.T) {
series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics)
require.NoError(t, err)
dir, err := ioutil.TempDir("", "test_wal_log_truncate")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
w, err := OpenSegmentWAL(dir, nil, 0, nil)
require.NoError(t, err)
@ -172,11 +164,7 @@ func TestSegmentWAL_Log_Restore(t *testing.T) {
series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics)
require.NoError(t, err)
dir, err := ioutil.TempDir("", "test_wal_log_restore")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
var (
recordedSeries [][]record.RefSeries
@ -281,11 +269,7 @@ func TestSegmentWAL_Log_Restore(t *testing.T) {
}
func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
dir, err := ioutil.TempDir("", "test_wal_log_restore")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
wal, err := OpenSegmentWAL(dir, nil, 0, nil)
require.NoError(t, err)
@ -386,11 +370,7 @@ func TestWALRestoreCorrupted(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
// Generate testing data. It does not make semantic sense but
// for the purpose of this test.
dir, err := ioutil.TempDir("", "test_corrupted")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
w, err := OpenSegmentWAL(dir, nil, 0, nil)
require.NoError(t, err)
@ -466,11 +446,7 @@ func TestWALRestoreCorrupted(t *testing.T) {
func TestMigrateWAL_Empty(t *testing.T) {
// The migration procedure must properly deal with a zero-length segment,
// which is valid in the new format.
dir, err := ioutil.TempDir("", "walmigrate")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
wdir := path.Join(dir, "wal")
@ -483,11 +459,7 @@ func TestMigrateWAL_Empty(t *testing.T) {
}
func TestMigrateWAL_Fuzz(t *testing.T) {
dir, err := ioutil.TempDir("", "walmigrate")
require.NoError(t, err)
defer func() {
require.NoError(t, os.RemoveAll(dir))
}()
dir := t.TempDir()
wdir := path.Join(dir, "wal")