small refactor of openTestDB to handle errors properly.

Signed-off-by: Krasi Georgiev <kgeorgie@redhat.com>
This commit is contained in:
Krasi Georgiev 2019-01-30 11:40:12 +02:00
parent 7245c6dc33
commit 1603222bbc
2 changed files with 124 additions and 80 deletions

View file

@ -693,9 +693,11 @@ func TestCompaction_populateBlock(t *testing.T) {
// This is needed for unit tests that rely on
// checking state before and after a compaction.
func TestDisableAutoCompactions(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
blockRange := DefaultOptions.BlockRanges[0]
label := labels.FromStrings("foo", "bar")
@ -783,11 +785,13 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) {
for title, bootStrap := range tests {
t.Run(title, func(t *testing.T) {
db, close := openTestDB(t, &Options{
db, delete := openTestDB(t, &Options{
BlockRanges: []int64{1, 100},
})
defer close()
defer db.Close()
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
db.DisableCompactions()
expBlocks := bootStrap(db)

View file

@ -46,7 +46,9 @@ func openTestDB(t testing.TB, opts *Options) (db *DB, close func()) {
testutil.Ok(t, err)
// Do not close the test database by default as it will deadlock on test failures.
return db, func() { os.RemoveAll(tmpdir) }
return db, func() {
testutil.Ok(t, os.RemoveAll(tmpdir))
}
}
// query runs a matcher query against the querier and fully expands its data.
@ -78,9 +80,11 @@ func query(t testing.TB, q Querier, matchers ...labels.Matcher) map[string][]sam
// Ensure that blocks are held in memory in their time order
// and not in ULID order as they are read from the directory.
func TestDB_reloadOrder(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
metas := []BlockMeta{
{MinTime: 90, MaxTime: 100},
@ -106,9 +110,11 @@ func TestDB_reloadOrder(t *testing.T) {
}
func TestDataAvailableOnlyAfterCommit(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
@ -135,9 +141,11 @@ func TestDataAvailableOnlyAfterCommit(t *testing.T) {
}
func TestDataNotAvailableAfterRollback(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
_, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0)
@ -156,9 +164,11 @@ func TestDataNotAvailableAfterRollback(t *testing.T) {
}
func TestDBAppenderAddRef(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app1 := db.Appender()
@ -213,9 +223,11 @@ func TestDBAppenderAddRef(t *testing.T) {
func TestDeleteSimple(t *testing.T) {
numSamples := int64(10)
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
@ -287,9 +299,11 @@ Outer:
}
func TestAmendDatapointCausesError(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, 0)
@ -303,9 +317,11 @@ func TestAmendDatapointCausesError(t *testing.T) {
}
func TestDuplicateNaNDatapointNoAmendError(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, math.NaN())
@ -318,10 +334,11 @@ func TestDuplicateNaNDatapointNoAmendError(t *testing.T) {
}
func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, math.Float64frombits(0x7ff0000000000001))
testutil.Ok(t, err)
@ -333,9 +350,11 @@ func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) {
}
func TestSkippingInvalidValuesInSameTxn(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
// Append AmendedValue.
app := db.Appender()
@ -377,8 +396,8 @@ func TestSkippingInvalidValuesInSameTxn(t *testing.T) {
}
func TestDB_Snapshot(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
db, delete := openTestDB(t, nil)
defer delete()
// append data
app := db.Appender()
@ -401,11 +420,11 @@ func TestDB_Snapshot(t *testing.T) {
// reopen DB from snapshot
db, err = Open(snap, nil, nil, nil)
testutil.Ok(t, err)
defer db.Close()
defer func() { testutil.Ok(t, db.Close()) }()
querier, err := db.Querier(mint, mint+1000)
testutil.Ok(t, err)
defer querier.Close()
defer func() { querier.Close() }()
// sum values
seriesSet, err := querier.Select(labels.NewEqualMatcher("foo", "bar"))
@ -427,8 +446,8 @@ func TestDB_Snapshot(t *testing.T) {
func TestDB_SnapshotWithDelete(t *testing.T) {
numSamples := int64(10)
db, close := openTestDB(t, nil)
defer close()
db, delete := openTestDB(t, nil)
defer delete()
app := db.Appender()
@ -468,12 +487,12 @@ Outer:
// reopen DB from snapshot
db, err = Open(snap, nil, nil, nil)
testutil.Ok(t, err)
defer db.Close()
defer func() { testutil.Ok(t, db.Close()) }()
// Compare the result.
q, err := db.Querier(0, numSamples)
testutil.Ok(t, err)
defer q.Close()
defer func() { testutil.Ok(t, q.Close()) }()
res, err := q.Select(labels.NewEqualMatcher("a", "b"))
testutil.Ok(t, err)
@ -570,9 +589,11 @@ func TestDB_e2e(t *testing.T) {
seriesMap[labels.New(l...).String()] = []sample{}
}
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
@ -675,8 +696,8 @@ func TestDB_e2e(t *testing.T) {
}
func TestWALFlushedOnDBClose(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
db, delete := openTestDB(t, nil)
defer delete()
dirDb := db.Dir()
@ -691,7 +712,7 @@ func TestWALFlushedOnDBClose(t *testing.T) {
db, err = Open(dirDb, nil, nil, nil)
testutil.Ok(t, err)
defer db.Close()
defer func() { testutil.Ok(t, db.Close()) }()
q, err := db.Querier(0, 1)
testutil.Ok(t, err)
@ -704,8 +725,8 @@ func TestWALFlushedOnDBClose(t *testing.T) {
func TestWALSegmentSizeOption(t *testing.T) {
options := *DefaultOptions
options.WALSegmentSize = 2 * 32 * 1024
db, close := openTestDB(t, &options)
defer close()
db, delete := openTestDB(t, &options)
defer delete()
app := db.Appender()
for i := int64(0); i < 155; i++ {
_, err := app.Add(labels.Labels{labels.Label{Name: "wal", Value: "size"}}, i, rand.Float64())
@ -730,8 +751,8 @@ func TestWALSegmentSizeOption(t *testing.T) {
func TestTombstoneClean(t *testing.T) {
numSamples := int64(10)
db, close := openTestDB(t, nil)
defer close()
db, delete := openTestDB(t, nil)
defer delete()
app := db.Appender()
@ -827,9 +848,11 @@ func TestTombstoneClean(t *testing.T) {
// if TombstoneClean leaves any blocks behind these will overlap.
func TestTombstoneCleanFail(t *testing.T) {
db, close := openTestDB(t, nil)
defer db.Close()
defer close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
var expectedBlockDirs []string
@ -906,11 +929,13 @@ func (*mockCompactorFailing) Compact(dest string, dirs []string, open []*Block)
}
func TestTimeRetention(t *testing.T) {
db, close := openTestDB(t, &Options{
db, delete := openTestDB(t, &Options{
BlockRanges: []int64{1000},
})
defer close()
defer db.Close()
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
blocks := []*BlockMeta{
{MinTime: 500, MaxTime: 900}, // Oldest block
@ -938,11 +963,13 @@ func TestTimeRetention(t *testing.T) {
}
func TestSizeRetention(t *testing.T) {
db, close := openTestDB(t, &Options{
db, delete := openTestDB(t, &Options{
BlockRanges: []int64{100},
})
defer close()
defer db.Close()
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
blocks := []*BlockMeta{
{MinTime: 100, MaxTime: 200}, // Oldest block
@ -1000,8 +1027,11 @@ func dbDiskSize(dir string) int64 {
}
func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
labelpairs := []labels.Labels{
labels.FromStrings("a", "abcd", "b", "abcde"),
@ -1059,7 +1089,7 @@ func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) {
q, err := db.Querier(0, 10)
testutil.Ok(t, err)
defer q.Close()
defer func() { testutil.Ok(t, q.Close()) }()
for _, c := range cases {
ss, err := q.Select(c.selector...)
@ -1175,9 +1205,11 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
// Regression test for https://github.com/prometheus/tsdb/issues/347
func TestChunkAtBlockBoundary(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
@ -1229,9 +1261,11 @@ func TestChunkAtBlockBoundary(t *testing.T) {
}
func TestQuerierWithBoundaryChunks(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
app := db.Appender()
@ -1366,11 +1400,13 @@ func TestInitializeHeadTimestamp(t *testing.T) {
}
func TestNoEmptyBlocks(t *testing.T) {
db, close := openTestDB(t, &Options{
db, delete := openTestDB(t, &Options{
BlockRanges: []int64{100},
})
defer close()
defer db.Close()
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
db.DisableCompactions()
rangeToTriggerCompaction := db.opts.BlockRanges[0]/2*3 - 1
@ -1524,9 +1560,11 @@ func TestDB_LabelNames(t *testing.T) {
testutil.Ok(t, err)
}
for _, tst := range tests {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
appendSamples(db, 0, 4, tst.sampleLabels1)
@ -1567,9 +1605,11 @@ func TestDB_LabelNames(t *testing.T) {
}
func TestCorrectNumTombstones(t *testing.T) {
db, close := openTestDB(t, nil)
defer close()
defer db.Close()
db, delete := openTestDB(t, nil)
defer func() {
testutil.Ok(t, db.Close())
delete()
}()
blockRange := DefaultOptions.BlockRanges[0]
defaultLabel := labels.FromStrings("foo", "bar")