Merge pull request #220 from cstyan/master

Use internal test utils rather than testify package
This commit is contained in:
Fabian Reinartz 2017-12-08 22:52:10 +01:00 committed by GitHub
commit e103f21cca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 470 additions and 468 deletions

View file

@ -17,38 +17,40 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"github.com/prometheus/tsdb/testutil"
) )
func TestSetCompactionFailed(t *testing.T) { func TestSetCompactionFailed(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test-tsdb") tmpdir, err := ioutil.TempDir("", "test-tsdb")
Ok(t, err) testutil.Ok(t, err)
b := createEmptyBlock(t, tmpdir) b := createEmptyBlock(t, tmpdir)
Equals(t, false, b.meta.Compaction.Failed) testutil.Equals(t, false, b.meta.Compaction.Failed)
Ok(t, b.setCompactionFailed()) testutil.Ok(t, b.setCompactionFailed())
Equals(t, true, b.meta.Compaction.Failed) testutil.Equals(t, true, b.meta.Compaction.Failed)
Ok(t, b.Close()) testutil.Ok(t, b.Close())
b, err = OpenBlock(tmpdir, nil) b, err = OpenBlock(tmpdir, nil)
Ok(t, err) testutil.Ok(t, err)
Equals(t, true, b.meta.Compaction.Failed) testutil.Equals(t, true, b.meta.Compaction.Failed)
} }
func createEmptyBlock(t *testing.T, dir string) *Block { func createEmptyBlock(t *testing.T, dir string) *Block {
Ok(t, os.MkdirAll(dir, 0777)) testutil.Ok(t, os.MkdirAll(dir, 0777))
Ok(t, writeMetaFile(dir, &BlockMeta{})) testutil.Ok(t, writeMetaFile(dir, &BlockMeta{}))
ir, err := newIndexWriter(dir) ir, err := newIndexWriter(dir)
Ok(t, err) testutil.Ok(t, err)
Ok(t, ir.Close()) testutil.Ok(t, ir.Close())
Ok(t, os.MkdirAll(chunkDir(dir), 0777)) testutil.Ok(t, os.MkdirAll(chunkDir(dir), 0777))
Ok(t, writeTombstoneFile(dir, EmptyTombstoneReader())) testutil.Ok(t, writeTombstoneFile(dir, EmptyTombstoneReader()))
b, err := OpenBlock(dir, nil) b, err := OpenBlock(dir, nil)
Ok(t, err) testutil.Ok(t, err)
return b return b
} }

View file

@ -20,7 +20,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
type pair struct { type pair struct {
@ -142,7 +142,7 @@ func benchmarkIterator(b *testing.B, newChunk func() Chunk) {
res = append(res, v) res = append(res, v)
} }
if it.Err() != io.EOF { if it.Err() != io.EOF {
require.NoError(b, it.Err()) testutil.Ok(b, it.Err())
} }
res = res[:0] res = res[:0]
} }

View file

@ -19,7 +19,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/tsdb/chunks" "github.com/prometheus/tsdb/chunks"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
type mockChunkReader map[uint64]chunks.Chunk type mockChunkReader map[uint64]chunks.Chunk
@ -40,7 +40,7 @@ func (cr mockChunkReader) Close() error {
func TestDeletedIterator(t *testing.T) { func TestDeletedIterator(t *testing.T) {
chk := chunks.NewXORChunk() chk := chunks.NewXORChunk()
app, err := chk.Appender() app, err := chk.Appender()
require.NoError(t, err) testutil.Ok(t, err)
// Insert random stuff from (0, 1000). // Insert random stuff from (0, 1000).
act := make([]sample, 1000) act := make([]sample, 1000)
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
@ -77,11 +77,11 @@ func TestDeletedIterator(t *testing.T) {
} }
} }
require.True(t, i < 1000) testutil.Assert(t, i < 1000 == true, "")
ts, v := it.At() ts, v := it.At()
require.Equal(t, act[i].t, ts) testutil.Equals(t, act[i].t, ts)
require.Equal(t, act[i].v, v) testutil.Equals(t, act[i].v, v)
} }
// There has been an extra call to Next(). // There has been an extra call to Next().
i++ i++
@ -92,7 +92,7 @@ func TestDeletedIterator(t *testing.T) {
} }
} }
require.False(t, i < 1000) testutil.Assert(t, i < 1000 == false, "")
require.NoError(t, it.Err()) testutil.Ok(t, it.Err())
} }
} }

View file

@ -21,7 +21,7 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func TestSplitByRange(t *testing.T) { func TestSplitByRange(t *testing.T) {
@ -123,7 +123,7 @@ func TestSplitByRange(t *testing.T) {
} }
} }
require.Equal(t, exp, splitByRange(blocks, c.trange)) testutil.Equals(t, exp, splitByRange(blocks, c.trange))
} }
} }
@ -147,7 +147,7 @@ func TestNoPanicFor0Tombstones(t *testing.T) {
} }
c, err := NewLeveledCompactor(nil, nil, []int64{50}, nil) c, err := NewLeveledCompactor(nil, nil, []int64{50}, nil)
require.NoError(t, err) testutil.Ok(t, err)
c.plan(metas) c.plan(metas)
} }
@ -160,7 +160,7 @@ func TestLeveledCompactor_plan(t *testing.T) {
720, 720,
2160, 2160,
}, nil) }, nil)
require.NoError(t, err) testutil.Ok(t, err)
cases := []struct { cases := []struct {
metas []dirMeta metas []dirMeta
@ -261,11 +261,11 @@ func TestLeveledCompactor_plan(t *testing.T) {
}, },
} }
for i, c := range cases { for _, c := range cases {
res, err := compactor.plan(c.metas) res, err := compactor.plan(c.metas)
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, c.expected, res, "test case %d", i) testutil.Equals(t, c.expected, res)
} }
} }
@ -277,7 +277,7 @@ func TestRangeWithFailedCompactionWontGetSelected(t *testing.T) {
720, 720,
2160, 2160,
}, nil) }, nil)
Ok(t, err) testutil.Ok(t, err)
cases := []struct { cases := []struct {
metas []dirMeta metas []dirMeta
@ -310,9 +310,9 @@ func TestRangeWithFailedCompactionWontGetSelected(t *testing.T) {
for _, c := range cases { for _, c := range cases {
c.metas[1].meta.Compaction.Failed = true c.metas[1].meta.Compaction.Failed = true
res, err := compactor.plan(c.metas) res, err := compactor.plan(c.metas)
Ok(t, err) testutil.Ok(t, err)
Equals(t, []string(nil), res) testutil.Equals(t, []string(nil), res)
} }
} }
@ -324,14 +324,14 @@ func TestCompactionFailWillCleanUpTempDir(t *testing.T) {
720, 720,
2160, 2160,
}, nil) }, nil)
Ok(t, err) testutil.Ok(t, err)
tmpdir, err := ioutil.TempDir("", "test") tmpdir, err := ioutil.TempDir("", "test")
Ok(t, err) testutil.Ok(t, err)
NotOk(t, compactor.write(tmpdir, &BlockMeta{}, erringBReader{})) testutil.NotOk(t, compactor.write(tmpdir, &BlockMeta{}, erringBReader{}))
_, err = os.Stat(filepath.Join(tmpdir, BlockMeta{}.ULID.String()) + ".tmp") _, err = os.Stat(filepath.Join(tmpdir, BlockMeta{}.ULID.String()) + ".tmp")
Assert(t, os.IsNotExist(err), "directory is not cleaned up") testutil.Assert(t, os.IsNotExist(err), "directory is not cleaned up")
} }
func metaRange(name string, mint, maxt int64, stats *BlockStats) dirMeta { func metaRange(name string, mint, maxt int64, stats *BlockStats) dirMeta {

View file

@ -23,15 +23,15 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/tsdb/labels" "github.com/prometheus/tsdb/labels"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func openTestDB(t testing.TB, opts *Options) (db *DB, close func()) { func openTestDB(t testing.TB, opts *Options) (db *DB, close func()) {
tmpdir, err := ioutil.TempDir("", "test") tmpdir, err := ioutil.TempDir("", "test")
Ok(t, err) testutil.Ok(t, err)
db, err = Open(tmpdir, nil, nil, opts) db, err = Open(tmpdir, nil, nil, opts)
require.NoError(t, err) testutil.Ok(t, err)
// Do not close the test database by default as it will deadlock on test failures. // Do not close the test database by default as it will deadlock on test failures.
return db, func() { os.RemoveAll(tmpdir) } return db, func() { os.RemoveAll(tmpdir) }
@ -40,7 +40,7 @@ func openTestDB(t testing.TB, opts *Options) (db *DB, close func()) {
// query runs a matcher query against the querier and fully expands its data. // query runs a matcher query against the querier and fully expands its data.
func query(t testing.TB, q Querier, matchers ...labels.Matcher) map[string][]sample { func query(t testing.TB, q Querier, matchers ...labels.Matcher) map[string][]sample {
ss, err := q.Select(matchers...) ss, err := q.Select(matchers...)
Ok(t, err) testutil.Ok(t, err)
result := map[string][]sample{} result := map[string][]sample{}
@ -53,12 +53,12 @@ func query(t testing.TB, q Querier, matchers ...labels.Matcher) map[string][]sam
t, v := it.At() t, v := it.At()
samples = append(samples, sample{t: t, v: v}) samples = append(samples, sample{t: t, v: v})
} }
Ok(t, it.Err()) testutil.Ok(t, it.Err())
name := series.Labels().String() name := series.Labels().String()
result[name] = samples result[name] = samples
} }
Ok(t, ss.Err()) testutil.Ok(t, ss.Err())
return result return result
} }
@ -70,25 +70,25 @@ func TestDataAvailableOnlyAfterCommit(t *testing.T) {
app := db.Appender() app := db.Appender()
_, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0) _, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0)
require.NoError(t, err) testutil.Ok(t, err)
querier, err := db.Querier(0, 1) querier, err := db.Querier(0, 1)
require.NoError(t, err) testutil.Ok(t, err)
seriesSet := query(t, querier, labels.NewEqualMatcher("foo", "bar")) seriesSet := query(t, querier, labels.NewEqualMatcher("foo", "bar"))
require.Equal(t, seriesSet, map[string][]sample{}) testutil.Equals(t, seriesSet, map[string][]sample{})
require.NoError(t, querier.Close()) testutil.Ok(t, querier.Close())
err = app.Commit() err = app.Commit()
require.NoError(t, err) testutil.Ok(t, err)
querier, err = db.Querier(0, 1) querier, err = db.Querier(0, 1)
require.NoError(t, err) testutil.Ok(t, err)
defer querier.Close() defer querier.Close()
seriesSet = query(t, querier, labels.NewEqualMatcher("foo", "bar")) seriesSet = query(t, querier, labels.NewEqualMatcher("foo", "bar"))
require.Equal(t, seriesSet, map[string][]sample{`{foo="bar"}`: []sample{{t: 0, v: 0}}}) testutil.Equals(t, seriesSet, map[string][]sample{`{foo="bar"}`: []sample{{t: 0, v: 0}}})
} }
func TestDataNotAvailableAfterRollback(t *testing.T) { func TestDataNotAvailableAfterRollback(t *testing.T) {
@ -97,18 +97,18 @@ func TestDataNotAvailableAfterRollback(t *testing.T) {
app := db.Appender() app := db.Appender()
_, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0) _, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0)
require.NoError(t, err) testutil.Ok(t, err)
err = app.Rollback() err = app.Rollback()
require.NoError(t, err) testutil.Ok(t, err)
querier, err := db.Querier(0, 1) querier, err := db.Querier(0, 1)
require.NoError(t, err) testutil.Ok(t, err)
defer querier.Close() defer querier.Close()
seriesSet := query(t, querier, labels.NewEqualMatcher("foo", "bar")) seriesSet := query(t, querier, labels.NewEqualMatcher("foo", "bar"))
require.Equal(t, seriesSet, map[string][]sample{}) testutil.Equals(t, seriesSet, map[string][]sample{})
} }
func TestDBAppenderAddRef(t *testing.T) { func TestDBAppenderAddRef(t *testing.T) {
@ -118,41 +118,41 @@ func TestDBAppenderAddRef(t *testing.T) {
app1 := db.Appender() app1 := db.Appender()
ref1, err := app1.Add(labels.FromStrings("a", "b"), 123, 0) ref1, err := app1.Add(labels.FromStrings("a", "b"), 123, 0)
require.NoError(t, err) testutil.Ok(t, err)
// Reference should already work before commit. // Reference should already work before commit.
err = app1.AddFast(ref1, 124, 1) err = app1.AddFast(ref1, 124, 1)
require.NoError(t, err) testutil.Ok(t, err)
err = app1.Commit() err = app1.Commit()
require.NoError(t, err) testutil.Ok(t, err)
app2 := db.Appender() app2 := db.Appender()
// first ref should already work in next transaction. // first ref should already work in next transaction.
err = app2.AddFast(ref1, 125, 0) err = app2.AddFast(ref1, 125, 0)
require.NoError(t, err) testutil.Ok(t, err)
ref2, err := app2.Add(labels.FromStrings("a", "b"), 133, 1) ref2, err := app2.Add(labels.FromStrings("a", "b"), 133, 1)
require.NoError(t, err) testutil.Ok(t, err)
require.True(t, ref1 == ref2) testutil.Assert(t, ref1 == ref2, "")
// Reference must be valid to add another sample. // Reference must be valid to add another sample.
err = app2.AddFast(ref2, 143, 2) err = app2.AddFast(ref2, 143, 2)
require.NoError(t, err) testutil.Ok(t, err)
err = app2.AddFast(9999999, 1, 1) err = app2.AddFast(9999999, 1, 1)
require.EqualError(t, errors.Cause(err), ErrNotFound.Error()) testutil.Equals(t, errors.Cause(err), ErrNotFound)
require.NoError(t, app2.Commit()) testutil.Ok(t, app2.Commit())
q, err := db.Querier(0, 200) q, err := db.Querier(0, 200)
require.NoError(t, err) testutil.Ok(t, err)
res := query(t, q, labels.NewEqualMatcher("a", "b")) res := query(t, q, labels.NewEqualMatcher("a", "b"))
require.Equal(t, map[string][]sample{ testutil.Equals(t, map[string][]sample{
labels.FromStrings("a", "b").String(): []sample{ labels.FromStrings("a", "b").String(): []sample{
{t: 123, v: 0}, {t: 123, v: 0},
{t: 124, v: 1}, {t: 124, v: 1},
@ -162,7 +162,7 @@ func TestDBAppenderAddRef(t *testing.T) {
}, },
}, res) }, res)
require.NoError(t, q.Close()) testutil.Ok(t, q.Close())
} }
func TestDeleteSimple(t *testing.T) { func TestDeleteSimple(t *testing.T) {
@ -179,7 +179,7 @@ func TestDeleteSimple(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i]) app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
} }
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
cases := []struct { cases := []struct {
intervals Intervals intervals Intervals
remaint []int64 remaint []int64
@ -195,15 +195,15 @@ Outer:
// TODO(gouthamve): Reset the tombstones somehow. // TODO(gouthamve): Reset the tombstones somehow.
// Delete the ranges. // Delete the ranges.
for _, r := range c.intervals { for _, r := range c.intervals {
require.NoError(t, db.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b"))) testutil.Ok(t, db.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b")))
} }
// Compare the result. // Compare the result.
q, err := db.Querier(0, numSamples) q, err := db.Querier(0, numSamples)
require.NoError(t, err) testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b")) res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err) testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint)) expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint { for _, ts := range c.remaint {
@ -215,13 +215,13 @@ Outer:
}) })
if len(expSamples) == 0 { if len(expSamples) == 0 {
require.False(t, res.Next()) testutil.Assert(t, res.Next() == false, "")
continue continue
} }
for { for {
eok, rok := expss.Next(), res.Next() eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next") testutil.Equals(t, eok, rok)
if !eok { if !eok {
continue Outer continue Outer
@ -229,13 +229,13 @@ Outer:
sexp := expss.At() sexp := expss.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
} }
} }
@ -246,13 +246,13 @@ func TestAmendDatapointCausesError(t *testing.T) {
app := db.Appender() app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, 0) _, err := app.Add(labels.Labels{}, 0, 0)
require.NoError(t, err, "Failed to add sample") testutil.Ok(t, err)
require.NoError(t, app.Commit(), "Unexpected error committing appender") testutil.Ok(t, app.Commit())
app = db.Appender() app = db.Appender()
_, err = app.Add(labels.Labels{}, 0, 1) _, err = app.Add(labels.Labels{}, 0, 1)
require.Equal(t, ErrAmendSample, err) testutil.Equals(t, ErrAmendSample, err)
require.NoError(t, app.Rollback(), "Unexpected error rolling back appender") testutil.Ok(t, app.Rollback())
} }
func TestDuplicateNaNDatapointNoAmendError(t *testing.T) { func TestDuplicateNaNDatapointNoAmendError(t *testing.T) {
@ -261,12 +261,12 @@ func TestDuplicateNaNDatapointNoAmendError(t *testing.T) {
app := db.Appender() app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, math.NaN()) _, err := app.Add(labels.Labels{}, 0, math.NaN())
require.NoError(t, err, "Failed to add sample") testutil.Ok(t, err)
require.NoError(t, app.Commit(), "Unexpected error committing appender") testutil.Ok(t, app.Commit())
app = db.Appender() app = db.Appender()
_, err = app.Add(labels.Labels{}, 0, math.NaN()) _, err = app.Add(labels.Labels{}, 0, math.NaN())
require.NoError(t, err) testutil.Ok(t, err)
} }
func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) { func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) {
@ -275,12 +275,12 @@ func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) {
app := db.Appender() app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, math.Float64frombits(0x7ff0000000000001)) _, err := app.Add(labels.Labels{}, 0, math.Float64frombits(0x7ff0000000000001))
require.NoError(t, err, "Failed to add sample") testutil.Ok(t, err)
require.NoError(t, app.Commit(), "Unexpected error committing appender") testutil.Ok(t, app.Commit())
app = db.Appender() app = db.Appender()
_, err = app.Add(labels.Labels{}, 0, math.Float64frombits(0x7ff0000000000002)) _, err = app.Add(labels.Labels{}, 0, math.Float64frombits(0x7ff0000000000002))
require.Equal(t, ErrAmendSample, err) testutil.Equals(t, ErrAmendSample, err)
} }
func TestSkippingInvalidValuesInSameTxn(t *testing.T) { func TestSkippingInvalidValuesInSameTxn(t *testing.T) {
@ -290,40 +290,40 @@ func TestSkippingInvalidValuesInSameTxn(t *testing.T) {
// Append AmendedValue. // Append AmendedValue.
app := db.Appender() app := db.Appender()
_, err := app.Add(labels.Labels{{"a", "b"}}, 0, 1) _, err := app.Add(labels.Labels{{"a", "b"}}, 0, 1)
require.NoError(t, err) testutil.Ok(t, err)
_, err = app.Add(labels.Labels{{"a", "b"}}, 0, 2) _, err = app.Add(labels.Labels{{"a", "b"}}, 0, 2)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
// Make sure the right value is stored. // Make sure the right value is stored.
q, err := db.Querier(0, 10) q, err := db.Querier(0, 10)
require.NoError(t, err) testutil.Ok(t, err)
ssMap := query(t, q, labels.NewEqualMatcher("a", "b")) ssMap := query(t, q, labels.NewEqualMatcher("a", "b"))
require.Equal(t, map[string][]sample{ testutil.Equals(t, map[string][]sample{
labels.New(labels.Label{"a", "b"}).String(): []sample{{0, 1}}, labels.New(labels.Label{"a", "b"}).String(): []sample{{0, 1}},
}, ssMap) }, ssMap)
require.NoError(t, q.Close()) testutil.Ok(t, q.Close())
// Append Out of Order Value. // Append Out of Order Value.
app = db.Appender() app = db.Appender()
_, err = app.Add(labels.Labels{{"a", "b"}}, 10, 3) _, err = app.Add(labels.Labels{{"a", "b"}}, 10, 3)
require.NoError(t, err) testutil.Ok(t, err)
_, err = app.Add(labels.Labels{{"a", "b"}}, 7, 5) _, err = app.Add(labels.Labels{{"a", "b"}}, 7, 5)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
q, err = db.Querier(0, 10) q, err = db.Querier(0, 10)
require.NoError(t, err) testutil.Ok(t, err)
ssMap = query(t, q, labels.NewEqualMatcher("a", "b")) ssMap = query(t, q, labels.NewEqualMatcher("a", "b"))
require.Equal(t, map[string][]sample{ testutil.Equals(t, map[string][]sample{
labels.New(labels.Label{"a", "b"}).String(): []sample{{0, 1}, {10, 3}}, labels.New(labels.Label{"a", "b"}).String(): []sample{{0, 1}, {10, 3}},
}, ssMap) }, ssMap)
require.NoError(t, q.Close()) testutil.Ok(t, q.Close())
} }
func TestDB_Snapshot(t *testing.T) { func TestDB_Snapshot(t *testing.T) {
@ -335,28 +335,28 @@ func TestDB_Snapshot(t *testing.T) {
mint := int64(1414141414000) mint := int64(1414141414000)
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
_, err := app.Add(labels.FromStrings("foo", "bar"), mint+int64(i), 1.0) _, err := app.Add(labels.FromStrings("foo", "bar"), mint+int64(i), 1.0)
require.NoError(t, err) testutil.Ok(t, err)
} }
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
require.NoError(t, app.Rollback()) testutil.Ok(t, app.Rollback())
// create snapshot // create snapshot
snap, err := ioutil.TempDir("", "snap") snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, db.Snapshot(snap)) testutil.Ok(t, db.Snapshot(snap))
require.NoError(t, db.Close()) testutil.Ok(t, db.Close())
// reopen DB from snapshot // reopen DB from snapshot
db, err = Open(snap, nil, nil, nil) db, err = Open(snap, nil, nil, nil)
require.NoError(t, err) testutil.Ok(t, err)
querier, err := db.Querier(mint, mint+1000) querier, err := db.Querier(mint, mint+1000)
require.NoError(t, err) testutil.Ok(t, err)
defer querier.Close() defer querier.Close()
// sum values // sum values
seriesSet, err := querier.Select(labels.NewEqualMatcher("foo", "bar")) seriesSet, err := querier.Select(labels.NewEqualMatcher("foo", "bar"))
require.NoError(t, err) testutil.Ok(t, err)
sum := 0.0 sum := 0.0
for seriesSet.Next() { for seriesSet.Next() {
@ -365,10 +365,10 @@ func TestDB_Snapshot(t *testing.T) {
_, v := series.At() _, v := series.At()
sum += v sum += v
} }
require.NoError(t, series.Err()) testutil.Ok(t, series.Err())
} }
require.NoError(t, seriesSet.Err()) testutil.Ok(t, seriesSet.Err())
require.Equal(t, sum, 1000.0) testutil.Equals(t, sum, 1000.0)
} }
func TestDB_SnapshotWithDelete(t *testing.T) { func TestDB_SnapshotWithDelete(t *testing.T) {
@ -385,7 +385,7 @@ func TestDB_SnapshotWithDelete(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i]) app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
} }
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
cases := []struct { cases := []struct {
intervals Intervals intervals Intervals
remaint []int64 remaint []int64
@ -401,25 +401,25 @@ Outer:
// TODO(gouthamve): Reset the tombstones somehow. // TODO(gouthamve): Reset the tombstones somehow.
// Delete the ranges. // Delete the ranges.
for _, r := range c.intervals { for _, r := range c.intervals {
require.NoError(t, db.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b"))) testutil.Ok(t, db.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b")))
} }
// create snapshot // create snapshot
snap, err := ioutil.TempDir("", "snap") snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, db.Snapshot(snap)) testutil.Ok(t, db.Snapshot(snap))
require.NoError(t, db.Close()) testutil.Ok(t, db.Close())
// reopen DB from snapshot // reopen DB from snapshot
db, err = Open(snap, nil, nil, nil) db, err = Open(snap, nil, nil, nil)
require.NoError(t, err) testutil.Ok(t, err)
// Compare the result. // Compare the result.
q, err := db.Querier(0, numSamples) q, err := db.Querier(0, numSamples)
require.NoError(t, err) testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b")) res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err) testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint)) expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint { for _, ts := range c.remaint {
@ -431,13 +431,13 @@ Outer:
}) })
if len(expSamples) == 0 { if len(expSamples) == 0 {
require.False(t, res.Next()) testutil.Assert(t, res.Next() == false, "")
continue continue
} }
for { for {
eok, rok := expss.Next(), res.Next() eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next") testutil.Equals(t, eok, rok)
if !eok { if !eok {
continue Outer continue Outer
@ -445,13 +445,13 @@ Outer:
sexp := expss.At() sexp := expss.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
} }
} }
@ -529,7 +529,7 @@ func TestDB_e2e(t *testing.T) {
series = append(series, sample{ts, v}) series = append(series, sample{ts, v})
_, err := app.Add(lset, ts, v) _, err := app.Add(lset, ts, v)
require.NoError(t, err) testutil.Ok(t, err)
ts += rand.Int63n(timeInterval) + 1 ts += rand.Int63n(timeInterval) + 1
} }
@ -537,7 +537,7 @@ func TestDB_e2e(t *testing.T) {
seriesMap[lset.String()] = series seriesMap[lset.String()] = series
} }
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
// Query each selector on 1000 random time-ranges. // Query each selector on 1000 random time-ranges.
queries := []struct { queries := []struct {
@ -588,10 +588,10 @@ func TestDB_e2e(t *testing.T) {
} }
q, err := db.Querier(mint, maxt) q, err := db.Querier(mint, maxt)
require.NoError(t, err) testutil.Ok(t, err)
ss, err := q.Select(qry.ms...) ss, err := q.Select(qry.ms...)
require.NoError(t, err) testutil.Ok(t, err)
result := map[string][]sample{} result := map[string][]sample{}
@ -599,15 +599,15 @@ func TestDB_e2e(t *testing.T) {
x := ss.At() x := ss.At()
smpls, err := expandSeriesIterator(x.Iterator()) smpls, err := expandSeriesIterator(x.Iterator())
require.NoError(t, err) testutil.Ok(t, err)
if len(smpls) > 0 { if len(smpls) > 0 {
result[x.Labels().String()] = smpls result[x.Labels().String()] = smpls
} }
} }
require.NoError(t, ss.Err()) testutil.Ok(t, ss.Err())
require.Equal(t, expected, result) testutil.Equals(t, expected, result)
q.Close() q.Close()
} }
@ -618,30 +618,30 @@ func TestDB_e2e(t *testing.T) {
func TestWALFlushedOnDBClose(t *testing.T) { func TestWALFlushedOnDBClose(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test") tmpdir, err := ioutil.TempDir("", "test")
Ok(t, err) testutil.Ok(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
db, err := Open(tmpdir, nil, nil, nil) db, err := Open(tmpdir, nil, nil, nil)
require.NoError(t, err) testutil.Ok(t, err)
lbls := labels.Labels{labels.Label{Name: "labelname", Value: "labelvalue"}} lbls := labels.Labels{labels.Label{Name: "labelname", Value: "labelvalue"}}
app := db.Appender() app := db.Appender()
_, err = app.Add(lbls, 0, 1) _, err = app.Add(lbls, 0, 1)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
db.Close() db.Close()
db, err = Open(tmpdir, nil, nil, nil) db, err = Open(tmpdir, nil, nil, nil)
require.NoError(t, err) testutil.Ok(t, err)
q, err := db.Querier(0, 1) q, err := db.Querier(0, 1)
require.NoError(t, err) testutil.Ok(t, err)
values, err := q.LabelValues("labelname") values, err := q.LabelValues("labelname")
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, values, []string{"labelvalue"}) testutil.Equals(t, values, []string{"labelvalue"})
} }
func TestTombstoneClean(t *testing.T) { func TestTombstoneClean(t *testing.T) {
@ -658,7 +658,7 @@ func TestTombstoneClean(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i]) app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
} }
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
cases := []struct { cases := []struct {
intervals Intervals intervals Intervals
remaint []int64 remaint []int64
@ -674,27 +674,27 @@ func TestTombstoneClean(t *testing.T) {
// create snapshot // create snapshot
snap, err := ioutil.TempDir("", "snap") snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, db.Snapshot(snap)) testutil.Ok(t, db.Snapshot(snap))
require.NoError(t, db.Close()) testutil.Ok(t, db.Close())
// reopen DB from snapshot // reopen DB from snapshot
db, err = Open(snap, nil, nil, nil) db, err = Open(snap, nil, nil, nil)
require.NoError(t, err) testutil.Ok(t, err)
for _, r := range c.intervals { for _, r := range c.intervals {
require.NoError(t, db.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b"))) testutil.Ok(t, db.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b")))
} }
// All of the setup for THIS line. // All of the setup for THIS line.
require.NoError(t, db.CleanTombstones()) testutil.Ok(t, db.CleanTombstones())
// Compare the result. // Compare the result.
q, err := db.Querier(0, numSamples) q, err := db.Querier(0, numSamples)
require.NoError(t, err) testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b")) res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err) testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint)) expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint { for _, ts := range c.remaint {
@ -706,13 +706,13 @@ func TestTombstoneClean(t *testing.T) {
}) })
if len(expSamples) == 0 { if len(expSamples) == 0 {
require.False(t, res.Next()) testutil.Assert(t, res.Next() == false, "")
continue continue
} }
for { for {
eok, rok := expss.Next(), res.Next() eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next") testutil.Equals(t, eok, rok)
if !eok { if !eok {
break break
@ -720,17 +720,17 @@ func TestTombstoneClean(t *testing.T) {
sexp := expss.At() sexp := expss.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
for _, b := range db.blocks { for _, b := range db.blocks {
Equals(t, emptyTombstoneReader, b.tombstones) testutil.Equals(t, emptyTombstoneReader, b.tombstones)
} }
} }
} }
@ -740,39 +740,39 @@ func TestDB_Retention(t *testing.T) {
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
db, err := Open(tmpdir, nil, nil, nil) db, err := Open(tmpdir, nil, nil, nil)
require.NoError(t, err) testutil.Ok(t, err)
lbls := labels.Labels{labels.Label{Name: "labelname", Value: "labelvalue"}} lbls := labels.Labels{labels.Label{Name: "labelname", Value: "labelvalue"}}
app := db.Appender() app := db.Appender()
_, err = app.Add(lbls, 0, 1) _, err = app.Add(lbls, 0, 1)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
// create snapshot to make it create a block. // create snapshot to make it create a block.
// TODO(gouthamve): Add a method to compact headblock. // TODO(gouthamve): Add a method to compact headblock.
snap, err := ioutil.TempDir("", "snap") snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, db.Snapshot(snap)) testutil.Ok(t, db.Snapshot(snap))
require.NoError(t, db.Close()) testutil.Ok(t, db.Close())
defer os.RemoveAll(snap) defer os.RemoveAll(snap)
// reopen DB from snapshot // reopen DB from snapshot
db, err = Open(snap, nil, nil, nil) db, err = Open(snap, nil, nil, nil)
require.NoError(t, err) testutil.Ok(t, err)
Equals(t, 1, len(db.blocks)) testutil.Equals(t, 1, len(db.blocks))
app = db.Appender() app = db.Appender()
_, err = app.Add(lbls, 100, 1) _, err = app.Add(lbls, 100, 1)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
// Snapshot again to create another block. // Snapshot again to create another block.
snap, err = ioutil.TempDir("", "snap") snap, err = ioutil.TempDir("", "snap")
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, db.Snapshot(snap)) testutil.Ok(t, db.Snapshot(snap))
require.NoError(t, db.Close()) testutil.Ok(t, db.Close())
defer os.RemoveAll(snap) defer os.RemoveAll(snap)
// reopen DB from snapshot // reopen DB from snapshot
@ -780,14 +780,14 @@ func TestDB_Retention(t *testing.T) {
RetentionDuration: 10, RetentionDuration: 10,
BlockRanges: []int64{50}, BlockRanges: []int64{50},
}) })
require.NoError(t, err) testutil.Ok(t, err)
Equals(t, 2, len(db.blocks)) testutil.Equals(t, 2, len(db.blocks))
// Now call rentention. // Now call rentention.
changes, err := db.retentionCutoff() changes, err := db.retentionCutoff()
Ok(t, err) testutil.Ok(t, err)
Assert(t, changes, "there should be changes") testutil.Assert(t, changes, "there should be changes")
Equals(t, 1, len(db.blocks)) testutil.Equals(t, 1, len(db.blocks))
Equals(t, int64(100), db.blocks[0].meta.MaxTime) // To verify its the right block. testutil.Equals(t, int64(100), db.blocks[0].meta.MaxTime) // To verify its the right block.
} }

View file

@ -26,16 +26,16 @@ import (
promlabels "github.com/prometheus/prometheus/pkg/labels" promlabels "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse" "github.com/prometheus/prometheus/pkg/textparse"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func BenchmarkCreateSeries(b *testing.B) { func BenchmarkCreateSeries(b *testing.B) {
lbls, err := readPrometheusLabels("testdata/all.series", b.N) lbls, err := readPrometheusLabels("testdata/all.series", b.N)
require.NoError(b, err) testutil.Ok(b, err)
h, err := NewHead(nil, nil, nil, 10000) h, err := NewHead(nil, nil, nil, 10000)
if err != nil { if err != nil {
require.NoError(b, err) testutil.Ok(b, err)
} }
defer h.Close() defer h.Close()
@ -131,40 +131,40 @@ func TestHead_ReadWAL(t *testing.T) {
wal := &memoryWAL{entries: entries} wal := &memoryWAL{entries: entries}
head, err := NewHead(nil, nil, wal, 1000) head, err := NewHead(nil, nil, wal, 1000)
require.NoError(t, err) testutil.Ok(t, err)
defer head.Close() defer head.Close()
require.NoError(t, head.ReadWAL()) testutil.Ok(t, head.ReadWAL())
require.Equal(t, uint64(100), head.lastSeriesID) testutil.Equals(t, uint64(100), head.lastSeriesID)
s10 := head.series.getByID(10) s10 := head.series.getByID(10)
s11 := head.series.getByID(11) s11 := head.series.getByID(11)
s50 := head.series.getByID(50) s50 := head.series.getByID(50)
s100 := head.series.getByID(100) s100 := head.series.getByID(100)
require.Equal(t, labels.FromStrings("a", "1"), s10.lset) testutil.Equals(t, labels.FromStrings("a", "1"), s10.lset)
require.Equal(t, labels.FromStrings("a", "2"), s11.lset) testutil.Equals(t, labels.FromStrings("a", "2"), s11.lset)
require.Equal(t, labels.FromStrings("a", "4"), s50.lset) testutil.Equals(t, labels.FromStrings("a", "4"), s50.lset)
require.Equal(t, labels.FromStrings("a", "3"), s100.lset) testutil.Equals(t, labels.FromStrings("a", "3"), s100.lset)
expandChunk := func(c chunks.Iterator) (x []sample) { expandChunk := func(c chunks.Iterator) (x []sample) {
for c.Next() { for c.Next() {
t, v := c.At() t, v := c.At()
x = append(x, sample{t: t, v: v}) x = append(x, sample{t: t, v: v})
} }
require.NoError(t, c.Err()) testutil.Ok(t, c.Err())
return x return x
} }
require.Equal(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0))) testutil.Equals(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0)))
require.Equal(t, 0, len(s11.chunks)) testutil.Equals(t, 0, len(s11.chunks))
require.Equal(t, []sample{{101, 6}}, expandChunk(s50.iterator(0))) testutil.Equals(t, []sample{{101, 6}}, expandChunk(s50.iterator(0)))
require.Equal(t, []sample{{100, 3}}, expandChunk(s100.iterator(0))) testutil.Equals(t, []sample{{100, 3}}, expandChunk(s100.iterator(0)))
} }
func TestHead_Truncate(t *testing.T) { func TestHead_Truncate(t *testing.T) {
h, err := NewHead(nil, nil, nil, 1000) h, err := NewHead(nil, nil, nil, 1000)
require.NoError(t, err) testutil.Ok(t, err)
defer h.Close() defer h.Close()
h.initTime(0) h.initTime(0)
@ -191,21 +191,21 @@ func TestHead_Truncate(t *testing.T) {
s4.chunks = []*memChunk{} s4.chunks = []*memChunk{}
// Truncation need not be aligned. // Truncation need not be aligned.
require.NoError(t, h.Truncate(1)) testutil.Ok(t, h.Truncate(1))
h.Truncate(2000) h.Truncate(2000)
require.Equal(t, []*memChunk{ testutil.Equals(t, []*memChunk{
{minTime: 2000, maxTime: 2999}, {minTime: 2000, maxTime: 2999},
}, h.series.getByID(s1.ref).chunks) }, h.series.getByID(s1.ref).chunks)
require.Equal(t, []*memChunk{ testutil.Equals(t, []*memChunk{
{minTime: 2000, maxTime: 2999}, {minTime: 2000, maxTime: 2999},
{minTime: 3000, maxTime: 3999}, {minTime: 3000, maxTime: 3999},
}, h.series.getByID(s2.ref).chunks) }, h.series.getByID(s2.ref).chunks)
require.Nil(t, h.series.getByID(s3.ref)) testutil.Assert(t, h.series.getByID(s3.ref) == nil, "")
require.Nil(t, h.series.getByID(s4.ref)) testutil.Assert(t, h.series.getByID(s4.ref) == nil, "")
postingsA1, _ := expandPostings(h.postings.get("a", "1")) postingsA1, _ := expandPostings(h.postings.get("a", "1"))
postingsA2, _ := expandPostings(h.postings.get("a", "2")) postingsA2, _ := expandPostings(h.postings.get("a", "2"))
@ -214,14 +214,14 @@ func TestHead_Truncate(t *testing.T) {
postingsC1, _ := expandPostings(h.postings.get("c", "1")) postingsC1, _ := expandPostings(h.postings.get("c", "1"))
postingsAll, _ := expandPostings(h.postings.get("", "")) postingsAll, _ := expandPostings(h.postings.get("", ""))
require.Equal(t, []uint64{s1.ref}, postingsA1) testutil.Equals(t, []uint64{s1.ref}, postingsA1)
require.Equal(t, []uint64{s2.ref}, postingsA2) testutil.Equals(t, []uint64{s2.ref}, postingsA2)
require.Equal(t, []uint64{s1.ref, s2.ref}, postingsB1) testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsB1)
require.Equal(t, []uint64{s1.ref, s2.ref}, postingsAll) testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsAll)
require.Nil(t, postingsB2) testutil.Assert(t, postingsB2 == nil, "")
require.Nil(t, postingsC1) testutil.Assert(t, postingsC1 == nil, "")
require.Equal(t, map[string]struct{}{ testutil.Equals(t, map[string]struct{}{
"": struct{}{}, // from 'all' postings list "": struct{}{}, // from 'all' postings list
"a": struct{}{}, "a": struct{}{},
"b": struct{}{}, "b": struct{}{},
@ -229,7 +229,7 @@ func TestHead_Truncate(t *testing.T) {
"2": struct{}{}, "2": struct{}{},
}, h.symbols) }, h.symbols)
require.Equal(t, map[string]stringset{ testutil.Equals(t, map[string]stringset{
"a": stringset{"1": struct{}{}, "2": struct{}{}}, "a": stringset{"1": struct{}{}, "2": struct{}{}},
"b": stringset{"1": struct{}{}}, "b": stringset{"1": struct{}{}},
"": stringset{"": struct{}{}}, "": stringset{"": struct{}{}},
@ -243,7 +243,7 @@ func TestMemSeries_truncateChunks(t *testing.T) {
for i := 0; i < 4000; i += 5 { for i := 0; i < 4000; i += 5 {
ok, _ := s.append(int64(i), float64(i)) ok, _ := s.append(int64(i), float64(i))
require.True(t, ok, "sample appen failed") testutil.Assert(t, ok == true, "sample append failed")
} }
// Check that truncate removes half of the chunks and afterwards // Check that truncate removes half of the chunks and afterwards
@ -252,32 +252,32 @@ func TestMemSeries_truncateChunks(t *testing.T) {
lastID := s.chunkID(countBefore - 1) lastID := s.chunkID(countBefore - 1)
lastChunk := s.chunk(lastID) lastChunk := s.chunk(lastID)
require.NotNil(t, s.chunk(0)) testutil.Assert(t, s.chunk(0) != nil, "")
require.NotNil(t, lastChunk) testutil.Assert(t, lastChunk != nil, "")
s.truncateChunksBefore(2000) s.truncateChunksBefore(2000)
require.Equal(t, int64(2000), s.chunks[0].minTime, "unexpected start time of first chunks") testutil.Equals(t, int64(2000), s.chunks[0].minTime)
require.Nil(t, s.chunk(0), "first chunk not gone") testutil.Assert(t, s.chunk(0) == nil, "first chunks not gone")
require.Equal(t, countBefore/2, len(s.chunks), "chunks not truncated correctly") testutil.Equals(t, countBefore/2, len(s.chunks))
require.Equal(t, lastChunk, s.chunk(lastID), "last chunk does not match") testutil.Equals(t, lastChunk, s.chunk(lastID))
// Validate that the series' sample buffer is applied correctly to the last chunk // Validate that the series' sample buffer is applied correctly to the last chunk
// after truncation. // after truncation.
it1 := s.iterator(s.chunkID(len(s.chunks) - 1)) it1 := s.iterator(s.chunkID(len(s.chunks) - 1))
_, ok := it1.(*memSafeIterator) _, ok := it1.(*memSafeIterator)
require.True(t, ok, "last chunk not wrapped with sample buffer") testutil.Assert(t, ok == true, "")
it2 := s.iterator(s.chunkID(len(s.chunks) - 2)) it2 := s.iterator(s.chunkID(len(s.chunks) - 2))
_, ok = it2.(*memSafeIterator) _, ok = it2.(*memSafeIterator)
require.False(t, ok, "non-last chunk incorrectly wrapped with sample buffer") testutil.Assert(t, ok == false, "non-last chunk incorrectly wrapped with sample buffer")
} }
func TestHeadDeleteSimple(t *testing.T) { func TestHeadDeleteSimple(t *testing.T) {
numSamples := int64(10) numSamples := int64(10)
head, err := NewHead(nil, nil, nil, 1000) head, err := NewHead(nil, nil, nil, 1000)
require.NoError(t, err) testutil.Ok(t, err)
defer head.Close() defer head.Close()
app := head.Appender() app := head.Appender()
@ -288,7 +288,7 @@ func TestHeadDeleteSimple(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i]) app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
} }
require.NoError(t, app.Commit()) testutil.Ok(t, app.Commit())
cases := []struct { cases := []struct {
intervals Intervals intervals Intervals
remaint []int64 remaint []int64
@ -322,14 +322,14 @@ Outer:
// Delete the ranges. // Delete the ranges.
for _, r := range c.intervals { for _, r := range c.intervals {
require.NoError(t, head.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b"))) testutil.Ok(t, head.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b")))
} }
// Compare the result. // Compare the result.
q, err := NewBlockQuerier(head, head.MinTime(), head.MaxTime()) q, err := NewBlockQuerier(head, head.MinTime(), head.MaxTime())
require.NoError(t, err) testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b")) res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err) testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint)) expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint { for _, ts := range c.remaint {
@ -341,13 +341,13 @@ Outer:
}) })
if len(expSamples) == 0 { if len(expSamples) == 0 {
require.False(t, res.Next()) testutil.Assert(t, res.Next() == false, "")
continue continue
} }
for { for {
eok, rok := expss.Next(), res.Next() eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next") testutil.Equals(t, eok, rok)
if !eok { if !eok {
continue Outer continue Outer
@ -355,13 +355,13 @@ Outer:
sexp := expss.At() sexp := expss.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
} }
} }
@ -381,12 +381,12 @@ Outer:
// app.Add(labels.Labels{{"a", "b"}}, i, smpls[i]) // app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
// } // }
// require.NoError(t, app.Commit()) // testutil.Ok(t, app.Commit())
// require.NoError(t, hb.Delete(0, 10000, labels.NewEqualMatcher("a", "b"))) // testutil.Ok(t, hb.Delete(0, 10000, labels.NewEqualMatcher("a", "b")))
// app = hb.Appender() // app = hb.Appender()
// _, err := app.Add(labels.Labels{{"a", "b"}}, 11, 1) // _, err := app.Add(labels.Labels{{"a", "b"}}, 11, 1)
// require.NoError(t, err) // testutil.Ok(t, err)
// require.NoError(t, app.Commit()) // testutil.Ok(t, app.Commit())
// q := hb.Querier(0, 100000) // q := hb.Querier(0, 100000)
// res := q.Select(labels.NewEqualMatcher("a", "b")) // res := q.Select(labels.NewEqualMatcher("a", "b"))
@ -395,8 +395,8 @@ Outer:
// exps := res.At() // exps := res.At()
// it := exps.Iterator() // it := exps.Iterator()
// ressmpls, err := expandSeriesIterator(it) // ressmpls, err := expandSeriesIterator(it)
// require.NoError(t, err) // testutil.Ok(t, err)
// require.Equal(t, []sample{{11, 1}}, ressmpls) // testutil.Equals(t, []sample{{11, 1}}, ressmpls)
// } // }
// func TestDelete_e2e(t *testing.T) { // func TestDelete_e2e(t *testing.T) {
@ -473,9 +473,9 @@ Outer:
// _, err := app.Add(ls, ts, v) // _, err := app.Add(ls, ts, v)
// if ts >= minTime && ts <= maxTime { // if ts >= minTime && ts <= maxTime {
// require.NoError(t, err) // testutil.Ok(t, err)
// } else { // } else {
// require.EqualError(t, err, ErrOutOfBounds.Error()) // testutil.EqualsError(t, err, ErrOutOfBounds.Error())
// } // }
// ts += rand.Int63n(timeInterval) + 1 // ts += rand.Int63n(timeInterval) + 1
@ -484,7 +484,7 @@ Outer:
// seriesMap[labels.New(l...).String()] = series // seriesMap[labels.New(l...).String()] = series
// } // }
// require.NoError(t, app.Commit()) // testutil.Ok(t, app.Commit())
// // Delete a time-range from each-selector. // // Delete a time-range from each-selector.
// dels := []struct { // dels := []struct {
@ -519,7 +519,7 @@ Outer:
// hb.tombstones = newEmptyTombstoneReader() // hb.tombstones = newEmptyTombstoneReader()
// for _, r := range del.drange { // for _, r := range del.drange {
// require.NoError(t, hb.Delete(r.Mint, r.Maxt, del.ms...)) // testutil.Ok(t, hb.Delete(r.Mint, r.Maxt, del.ms...))
// } // }
// matched := labels.Slice{} // matched := labels.Slice{}
@ -570,7 +570,7 @@ Outer:
// } // }
// } // }
// } // }
// require.Equal(t, eok, rok, "next") // testutil.Equals(t, eok, rok, "next")
// if !eok { // if !eok {
// break // break
@ -578,13 +578,13 @@ Outer:
// sexp := expSs.At() // sexp := expSs.At()
// sres := ss.At() // sres := ss.At()
// require.Equal(t, sexp.Labels(), sres.Labels(), "labels") // testutil.Equals(t, sexp.Labels(), sres.Labels(), "labels")
// smplExp, errExp := expandSeriesIterator(sexp.Iterator()) // smplExp, errExp := expandSeriesIterator(sexp.Iterator())
// smplRes, errRes := expandSeriesIterator(sres.Iterator()) // smplRes, errRes := expandSeriesIterator(sres.Iterator())
// require.Equal(t, errExp, errRes, "samples error") // testutil.Equals(t, errExp, errRes, "samples error")
// require.Equal(t, smplExp, smplRes, "samples") // testutil.Equals(t, smplExp, smplRes, "samples")
// } // }
// } // }
// } // }
@ -673,35 +673,35 @@ func TestMemSeries_append(t *testing.T) {
// on and after it. // on and after it.
// New chunk must correctly be cut at 1000. // New chunk must correctly be cut at 1000.
ok, chunkCreated := s.append(998, 1) ok, chunkCreated := s.append(998, 1)
Assert(t, ok, "append failed") testutil.Assert(t, ok, "append failed")
Assert(t, chunkCreated, "first sample created chunk") testutil.Assert(t, chunkCreated, "first sample created chunk")
ok, chunkCreated = s.append(999, 2) ok, chunkCreated = s.append(999, 2)
Assert(t, ok, "append failed") testutil.Assert(t, ok, "append failed")
Assert(t, !chunkCreated, "second sample should use same chunk") testutil.Assert(t, !chunkCreated, "second sample should use same chunk")
ok, chunkCreated = s.append(1000, 3) ok, chunkCreated = s.append(1000, 3)
Assert(t, ok, "append failed") testutil.Assert(t, ok, "append failed")
Assert(t, ok, "expected new chunk on boundary") testutil.Assert(t, ok, "expected new chunk on boundary")
ok, chunkCreated = s.append(1001, 4) ok, chunkCreated = s.append(1001, 4)
Assert(t, ok, "append failed") testutil.Assert(t, ok, "append failed")
Assert(t, !chunkCreated, "second sample should use same chunk") testutil.Assert(t, !chunkCreated, "second sample should use same chunk")
Assert(t, s.chunks[0].minTime == 998 && s.chunks[0].maxTime == 999, "wrong chunk range") testutil.Assert(t, s.chunks[0].minTime == 998 && s.chunks[0].maxTime == 999, "wrong chunk range")
Assert(t, s.chunks[1].minTime == 1000 && s.chunks[1].maxTime == 1001, "wrong chunk range") testutil.Assert(t, s.chunks[1].minTime == 1000 && s.chunks[1].maxTime == 1001, "wrong chunk range")
// Fill the range [1000,2000) with many samples. Intermediate chunks should be cut // Fill the range [1000,2000) with many samples. Intermediate chunks should be cut
// at approximately 120 samples per chunk. // at approximately 120 samples per chunk.
for i := 1; i < 1000; i++ { for i := 1; i < 1000; i++ {
ok, _ := s.append(1001+int64(i), float64(i)) ok, _ := s.append(1001+int64(i), float64(i))
Assert(t, ok, "append failed") testutil.Assert(t, ok, "append failed")
} }
Assert(t, len(s.chunks) > 7, "expected intermediate chunks") testutil.Assert(t, len(s.chunks) > 7, "expected intermediate chunks")
// All chunks but the first and last should now be moderately full. // All chunks but the first and last should now be moderately full.
for i, c := range s.chunks[1 : len(s.chunks)-1] { for i, c := range s.chunks[1 : len(s.chunks)-1] {
Assert(t, c.chunk.NumSamples() > 100, "unexpected small chunk %d of length %d", i, c.chunk.NumSamples()) testutil.Assert(t, c.chunk.NumSamples() > 100, "unexpected small chunk %d of length %d", i, c.chunk.NumSamples())
} }
} }

View file

@ -24,7 +24,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/tsdb/chunks" "github.com/prometheus/tsdb/chunks"
"github.com/prometheus/tsdb/labels" "github.com/prometheus/tsdb/labels"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
type series struct { type series struct {
@ -151,35 +151,35 @@ func (m mockIndex) LabelIndices() ([][]string, error) {
func TestIndexRW_Create_Open(t *testing.T) { func TestIndexRW_Create_Open(t *testing.T) {
dir, err := ioutil.TempDir("", "test_index_create") dir, err := ioutil.TempDir("", "test_index_create")
require.NoError(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
// An empty index must still result in a readable file. // An empty index must still result in a readable file.
iw, err := newIndexWriter(dir) iw, err := newIndexWriter(dir)
require.NoError(t, err, "create index writer") testutil.Ok(t, err)
require.NoError(t, iw.Close(), "close index writer") testutil.Ok(t, iw.Close())
ir, err := NewFileIndexReader(filepath.Join(dir, "index")) ir, err := NewFileIndexReader(filepath.Join(dir, "index"))
require.NoError(t, err, "open index reader") testutil.Ok(t, err)
require.NoError(t, ir.Close(), "close index reader") testutil.Ok(t, ir.Close())
// Modify magic header must cause open to fail. // Modify magic header must cause open to fail.
f, err := os.OpenFile(filepath.Join(dir, "index"), os.O_WRONLY, 0666) f, err := os.OpenFile(filepath.Join(dir, "index"), os.O_WRONLY, 0666)
require.NoError(t, err) testutil.Ok(t, err)
_, err = f.WriteAt([]byte{0, 0}, 0) _, err = f.WriteAt([]byte{0, 0}, 0)
require.NoError(t, err) testutil.Ok(t, err)
_, err = NewFileIndexReader(dir) _, err = NewFileIndexReader(dir)
require.Error(t, err) testutil.NotOk(t, err)
} }
func TestIndexRW_Postings(t *testing.T) { func TestIndexRW_Postings(t *testing.T) {
dir, err := ioutil.TempDir("", "test_index_postings") dir, err := ioutil.TempDir("", "test_index_postings")
require.NoError(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
iw, err := newIndexWriter(dir) iw, err := newIndexWriter(dir)
require.NoError(t, err, "create index writer") testutil.Ok(t, err)
series := []labels.Labels{ series := []labels.Labels{
labels.FromStrings("a", "1", "b", "1"), labels.FromStrings("a", "1", "b", "1"),
@ -196,25 +196,25 @@ func TestIndexRW_Postings(t *testing.T) {
"3": struct{}{}, "3": struct{}{},
"4": struct{}{}, "4": struct{}{},
}) })
require.NoError(t, err) testutil.Ok(t, err)
// Postings lists are only written if a series with the respective // Postings lists are only written if a series with the respective
// reference was added before. // reference was added before.
require.NoError(t, iw.AddSeries(1, series[0])) testutil.Ok(t, iw.AddSeries(1, series[0]))
require.NoError(t, iw.AddSeries(2, series[1])) testutil.Ok(t, iw.AddSeries(2, series[1]))
require.NoError(t, iw.AddSeries(3, series[2])) testutil.Ok(t, iw.AddSeries(3, series[2]))
require.NoError(t, iw.AddSeries(4, series[3])) testutil.Ok(t, iw.AddSeries(4, series[3]))
err = iw.WritePostings("a", "1", newListPostings([]uint64{1, 2, 3, 4})) err = iw.WritePostings("a", "1", newListPostings([]uint64{1, 2, 3, 4}))
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, iw.Close()) testutil.Ok(t, iw.Close())
ir, err := NewFileIndexReader(filepath.Join(dir, "index")) ir, err := NewFileIndexReader(filepath.Join(dir, "index"))
require.NoError(t, err, "open index reader") testutil.Ok(t, err)
p, err := ir.Postings("a", "1") p, err := ir.Postings("a", "1")
require.NoError(t, err) testutil.Ok(t, err)
var l labels.Labels var l labels.Labels
var c []ChunkMeta var c []ChunkMeta
@ -222,22 +222,22 @@ func TestIndexRW_Postings(t *testing.T) {
for i := 0; p.Next(); i++ { for i := 0; p.Next(); i++ {
err := ir.Series(p.At(), &l, &c) err := ir.Series(p.At(), &l, &c)
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, 0, len(c)) testutil.Equals(t, 0, len(c))
require.Equal(t, series[i], l) testutil.Equals(t, series[i], l)
} }
require.NoError(t, p.Err()) testutil.Ok(t, p.Err())
require.NoError(t, ir.Close()) testutil.Ok(t, ir.Close())
} }
func TestPersistence_index_e2e(t *testing.T) { func TestPersistence_index_e2e(t *testing.T) {
dir, err := ioutil.TempDir("", "test_persistence_e2e") dir, err := ioutil.TempDir("", "test_persistence_e2e")
require.NoError(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
lbls, err := readPrometheusLabels("testdata/20k.series", 20000) lbls, err := readPrometheusLabels("testdata/20k.series", 20000)
require.NoError(t, err) testutil.Ok(t, err)
// Sort labels as the index writer expects series in sorted order. // Sort labels as the index writer expects series in sorted order.
sort.Sort(labels.Slice(lbls)) sort.Sort(labels.Slice(lbls))
@ -271,9 +271,9 @@ func TestPersistence_index_e2e(t *testing.T) {
} }
iw, err := newIndexWriter(dir) iw, err := newIndexWriter(dir)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, iw.AddSymbols(symbols)) testutil.Ok(t, iw.AddSymbols(symbols))
// Population procedure as done by compaction. // Population procedure as done by compaction.
var ( var (
@ -286,7 +286,7 @@ func TestPersistence_index_e2e(t *testing.T) {
for i, s := range input { for i, s := range input {
err = iw.AddSeries(uint64(i), s.labels, s.chunks...) err = iw.AddSeries(uint64(i), s.labels, s.chunks...)
require.NoError(t, err) testutil.Ok(t, err)
mi.AddSeries(uint64(i), s.labels, s.chunks...) mi.AddSeries(uint64(i), s.labels, s.chunks...)
for _, l := range s.labels { for _, l := range s.labels {
@ -304,8 +304,8 @@ func TestPersistence_index_e2e(t *testing.T) {
for k, v := range values { for k, v := range values {
vals := v.slice() vals := v.slice()
require.NoError(t, iw.WriteLabelIndex([]string{k}, vals)) testutil.Ok(t, iw.WriteLabelIndex([]string{k}, vals))
require.NoError(t, mi.WriteLabelIndex([]string{k}, vals)) testutil.Ok(t, mi.WriteLabelIndex([]string{k}, vals))
} }
all := make([]uint64, len(lbls)) all := make([]uint64, len(lbls))
@ -313,24 +313,24 @@ func TestPersistence_index_e2e(t *testing.T) {
all[i] = uint64(i) all[i] = uint64(i)
} }
err = iw.WritePostings("", "", newListPostings(all)) err = iw.WritePostings("", "", newListPostings(all))
require.NoError(t, err) testutil.Ok(t, err)
mi.WritePostings("", "", newListPostings(all)) mi.WritePostings("", "", newListPostings(all))
for l := range postings.m { for l := range postings.m {
err = iw.WritePostings(l.Name, l.Value, postings.get(l.Name, l.Value)) err = iw.WritePostings(l.Name, l.Value, postings.get(l.Name, l.Value))
require.NoError(t, err) testutil.Ok(t, err)
mi.WritePostings(l.Name, l.Value, postings.get(l.Name, l.Value)) mi.WritePostings(l.Name, l.Value, postings.get(l.Name, l.Value))
} }
err = iw.Close() err = iw.Close()
require.NoError(t, err) testutil.Ok(t, err)
ir, err := NewFileIndexReader(filepath.Join(dir, "index")) ir, err := NewFileIndexReader(filepath.Join(dir, "index"))
require.NoError(t, err) testutil.Ok(t, err)
for p := range mi.postings.m { for p := range mi.postings.m {
gotp, err := ir.Postings(p.Name, p.Value) gotp, err := ir.Postings(p.Name, p.Value)
require.NoError(t, err) testutil.Ok(t, err)
expp, err := mi.Postings(p.Name, p.Value) expp, err := mi.Postings(p.Name, p.Value)
@ -338,39 +338,39 @@ func TestPersistence_index_e2e(t *testing.T) {
var chks, expchks []ChunkMeta var chks, expchks []ChunkMeta
for gotp.Next() { for gotp.Next() {
require.True(t, expp.Next()) testutil.Assert(t, expp.Next() == true, "")
ref := gotp.At() ref := gotp.At()
err := ir.Series(ref, &lset, &chks) err := ir.Series(ref, &lset, &chks)
require.NoError(t, err) testutil.Ok(t, err)
err = mi.Series(expp.At(), &explset, &expchks) err = mi.Series(expp.At(), &explset, &expchks)
require.Equal(t, explset, lset) testutil.Equals(t, explset, lset)
require.Equal(t, expchks, chks) testutil.Equals(t, expchks, chks)
} }
require.False(t, expp.Next()) testutil.Assert(t, expp.Next() == false, "")
require.NoError(t, gotp.Err()) testutil.Ok(t, gotp.Err())
} }
for k, v := range mi.labelIndex { for k, v := range mi.labelIndex {
tplsExp, err := newStringTuples(v, 1) tplsExp, err := newStringTuples(v, 1)
require.NoError(t, err) testutil.Ok(t, err)
tplsRes, err := ir.LabelValues(k) tplsRes, err := ir.LabelValues(k)
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, tplsExp.Len(), tplsRes.Len()) testutil.Equals(t, tplsExp.Len(), tplsRes.Len())
for i := 0; i < tplsExp.Len(); i++ { for i := 0; i < tplsExp.Len(); i++ {
strsExp, err := tplsExp.At(i) strsExp, err := tplsExp.At(i)
require.NoError(t, err) testutil.Ok(t, err)
strsRes, err := tplsRes.At(i) strsRes, err := tplsRes.At(i)
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, strsExp, strsRes) testutil.Equals(t, strsExp, strsRes)
} }
} }
require.NoError(t, ir.Close()) testutil.Ok(t, ir.Close())
} }

View file

@ -25,7 +25,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
promlabels "github.com/prometheus/prometheus/pkg/labels" promlabels "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse" "github.com/prometheus/prometheus/pkg/textparse"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func TestCompareAndEquals(t *testing.T) { func TestCompareAndEquals(t *testing.T) {
@ -87,14 +87,14 @@ func TestCompareAndEquals(t *testing.T) {
// Use constructor to ensure sortedness. // Use constructor to ensure sortedness.
a, b := New(c.a...), New(c.b...) a, b := New(c.a...), New(c.b...)
require.Equal(t, c.res, Compare(a, b)) testutil.Equals(t, c.res, Compare(a, b))
require.Equal(t, c.res == 0, a.Equals(b)) testutil.Equals(t, c.res == 0, a.Equals(b))
} }
} }
func BenchmarkSliceSort(b *testing.B) { func BenchmarkSliceSort(b *testing.B) {
lbls, err := readPrometheusLabels("../testdata/1m.series", 900000) lbls, err := readPrometheusLabels("../testdata/1m.series", 900000)
require.NoError(b, err) testutil.Ok(b, err)
for len(lbls) < 20e6 { for len(lbls) < 20e6 {
lbls = append(lbls, lbls...) lbls = append(lbls, lbls...)

View file

@ -21,7 +21,7 @@ import (
"testing" "testing"
"github.com/prometheus/tsdb/labels" "github.com/prometheus/tsdb/labels"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func TestMemPostings_addFor(t *testing.T) { func TestMemPostings_addFor(t *testing.T) {
@ -30,7 +30,7 @@ func TestMemPostings_addFor(t *testing.T) {
p.addFor(5, allPostingsKey) p.addFor(5, allPostingsKey)
require.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8}, p.m[allPostingsKey]) testutil.Equals(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8}, p.m[allPostingsKey])
} }
func TestMemPostings_ensureOrder(t *testing.T) { func TestMemPostings_ensureOrder(t *testing.T) {
@ -101,8 +101,8 @@ func TestIntersect(t *testing.T) {
b := newListPostings(c.b) b := newListPostings(c.b)
res, err := expandPostings(Intersect(a, b)) res, err := expandPostings(Intersect(a, b))
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, c.res, res) testutil.Equals(t, c.res, res)
} }
} }
@ -142,8 +142,8 @@ func TestMultiIntersect(t *testing.T) {
res, err := expandPostings(Intersect(ps...)) res, err := expandPostings(Intersect(ps...))
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, c.res, res) testutil.Equals(t, c.res, res)
} }
} }
@ -199,8 +199,8 @@ func TestMultiMerge(t *testing.T) {
i3 := newListPostings(c.c) i3 := newListPostings(c.c)
res, err := expandPostings(Merge(i1, i2, i3)) res, err := expandPostings(Merge(i1, i2, i3))
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, c.res, res) testutil.Equals(t, c.res, res)
} }
} }
@ -231,8 +231,8 @@ func TestMergedPostings(t *testing.T) {
b := newListPostings(c.b) b := newListPostings(c.b)
res, err := expandPostings(newMergedPostings(a, b)) res, err := expandPostings(newMergedPostings(a, b))
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, c.res, res) testutil.Equals(t, c.res, res)
} }
} }
@ -285,16 +285,16 @@ func TestMergedPostingsSeek(t *testing.T) {
p := newMergedPostings(a, b) p := newMergedPostings(a, b)
require.Equal(t, c.success, p.Seek(c.seek)) testutil.Equals(t, c.success, p.Seek(c.seek))
// After Seek(), At() should be called. // After Seek(), At() should be called.
if c.success { if c.success {
start := p.At() start := p.At()
lst, err := expandPostings(p) lst, err := expandPostings(p)
require.NoError(t, err) testutil.Ok(t, err)
lst = append([]uint64{start}, lst...) lst = append([]uint64{start}, lst...)
require.Equal(t, c.res, lst) testutil.Equals(t, c.res, lst)
} }
} }
@ -319,12 +319,12 @@ func TestBigEndian(t *testing.T) {
t.Run("Iteration", func(t *testing.T) { t.Run("Iteration", func(t *testing.T) {
bep := newBigEndianPostings(beLst) bep := newBigEndianPostings(beLst)
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
require.True(t, bep.Next()) testutil.Assert(t, bep.Next() == true, "")
require.Equal(t, uint64(ls[i]), bep.At()) testutil.Equals(t, uint64(ls[i]), bep.At())
} }
require.False(t, bep.Next()) testutil.Assert(t, bep.Next() == false, "")
require.Nil(t, bep.Err()) testutil.Assert(t, bep.Err() == nil, "")
}) })
t.Run("Seek", func(t *testing.T) { t.Run("Seek", func(t *testing.T) {
@ -368,9 +368,9 @@ func TestBigEndian(t *testing.T) {
bep := newBigEndianPostings(beLst) bep := newBigEndianPostings(beLst)
for _, v := range table { for _, v := range table {
require.Equal(t, v.found, bep.Seek(uint64(v.seek))) testutil.Equals(t, v.found, bep.Seek(uint64(v.seek)))
require.Equal(t, uint64(v.val), bep.At()) testutil.Equals(t, uint64(v.val), bep.At())
require.Nil(t, bep.Err()) testutil.Assert(t, bep.Err() == nil, "")
} }
}) })
} }
@ -388,6 +388,6 @@ func TestIntersectWithMerge(t *testing.T) {
p := Intersect(a, b) p := Intersect(a, b)
res, err := expandPostings(p) res, err := expandPostings(p)
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, []uint64{30}, res) testutil.Equals(t, []uint64{30}, res)
} }

View file

@ -22,7 +22,7 @@ import (
"github.com/prometheus/tsdb/chunks" "github.com/prometheus/tsdb/chunks"
"github.com/prometheus/tsdb/labels" "github.com/prometheus/tsdb/labels"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
type mockSeriesIterator struct { type mockSeriesIterator struct {
@ -190,7 +190,7 @@ Outer:
for { for {
eok, rok := c.exp.Next(), res.Next() eok, rok := c.exp.Next(), res.Next()
require.Equal(t, eok, rok, "next") testutil.Equals(t, eok, rok)
if !eok { if !eok {
continue Outer continue Outer
@ -198,13 +198,13 @@ Outer:
sexp := c.exp.At() sexp := c.exp.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
} }
} }
@ -449,7 +449,7 @@ func TestBlockQuerier(t *testing.T) {
} }
Outer: Outer:
for i, c := range cases.queries { for _, c := range cases.queries {
ir, cr := createIdxChkReaders(cases.data) ir, cr := createIdxChkReaders(cases.data)
querier := &blockQuerier{ querier := &blockQuerier{
index: ir, index: ir,
@ -461,11 +461,11 @@ Outer:
} }
res, err := querier.Select(c.ms...) res, err := querier.Select(c.ms...)
require.NoError(t, err) testutil.Ok(t, err)
for { for {
eok, rok := c.exp.Next(), res.Next() eok, rok := c.exp.Next(), res.Next()
require.Equal(t, eok, rok, "%d: next", i) testutil.Equals(t, eok, rok)
if !eok { if !eok {
continue Outer continue Outer
@ -473,13 +473,13 @@ Outer:
sexp := c.exp.At() sexp := c.exp.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "%d: labels", i) testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "%d: samples error", i) testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "%d: samples", i) testutil.Equals(t, smplExp, smplRes)
} }
} }
@ -632,11 +632,11 @@ Outer:
} }
res, err := querier.Select(c.ms...) res, err := querier.Select(c.ms...)
require.NoError(t, err) testutil.Ok(t, err)
for { for {
eok, rok := c.exp.Next(), res.Next() eok, rok := c.exp.Next(), res.Next()
require.Equal(t, eok, rok, "next") testutil.Equals(t, eok, rok)
if !eok { if !eok {
continue Outer continue Outer
@ -644,13 +644,13 @@ Outer:
sexp := c.exp.At() sexp := c.exp.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
} }
@ -743,13 +743,13 @@ func TestBaseChunkSeries(t *testing.T) {
idx := tc.expIdxs[i] idx := tc.expIdxs[i]
require.Equal(t, tc.series[idx].lset, lset) testutil.Equals(t, tc.series[idx].lset, lset)
require.Equal(t, tc.series[idx].chunks, chks) testutil.Equals(t, tc.series[idx].chunks, chks)
i++ i++
} }
require.Equal(t, len(tc.expIdxs), i) testutil.Equals(t, len(tc.expIdxs), i)
require.NoError(t, bcs.Err()) testutil.Ok(t, bcs.Err())
} }
return return
@ -959,8 +959,8 @@ func TestSeriesIterator(t *testing.T) {
smplExp, errExp := expandSeriesIterator(exp) smplExp, errExp := expandSeriesIterator(exp)
smplRes, errRes := expandSeriesIterator(res) smplRes, errRes := expandSeriesIterator(res)
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
t.Run("Seek", func(t *testing.T) { t.Run("Seek", func(t *testing.T) {
@ -1027,21 +1027,21 @@ func TestSeriesIterator(t *testing.T) {
} }
exp := newListSeriesIterator(smplValid) exp := newListSeriesIterator(smplValid)
require.Equal(t, tc.success, res.Seek(tc.seek)) testutil.Equals(t, tc.success, res.Seek(tc.seek))
if tc.success { if tc.success {
// Init the list and then proceed to check. // Init the list and then proceed to check.
remaining := exp.Next() remaining := exp.Next()
require.True(t, remaining) testutil.Assert(t, remaining == true, "")
for remaining { for remaining {
sExp, eExp := exp.At() sExp, eExp := exp.At()
sRes, eRes := res.At() sRes, eRes := res.At()
require.Equal(t, eExp, eRes, "samples error") testutil.Equals(t, eExp, eRes)
require.Equal(t, sExp, sRes, "samples") testutil.Equals(t, sExp, sRes)
remaining = exp.Next() remaining = exp.Next()
require.Equal(t, remaining, res.Next()) testutil.Equals(t, remaining, res.Next())
} }
} }
} }
@ -1060,8 +1060,8 @@ func TestSeriesIterator(t *testing.T) {
smplExp, errExp := expandSeriesIterator(exp) smplExp, errExp := expandSeriesIterator(exp)
smplRes, errRes := expandSeriesIterator(res) smplRes, errRes := expandSeriesIterator(res)
require.Equal(t, errExp, errRes, "samples error") testutil.Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") testutil.Equals(t, smplExp, smplRes)
} }
t.Run("Seek", func(t *testing.T) { t.Run("Seek", func(t *testing.T) {
@ -1073,21 +1073,21 @@ func TestSeriesIterator(t *testing.T) {
res := newChainedSeriesIterator(a, b, c) res := newChainedSeriesIterator(a, b, c)
exp := newListSeriesIterator(tc.exp) exp := newListSeriesIterator(tc.exp)
require.Equal(t, tc.success, res.Seek(tc.seek)) testutil.Equals(t, tc.success, res.Seek(tc.seek))
if tc.success { if tc.success {
// Init the list and then proceed to check. // Init the list and then proceed to check.
remaining := exp.Next() remaining := exp.Next()
require.True(t, remaining) testutil.Assert(t, remaining == true, "")
for remaining { for remaining {
sExp, eExp := exp.At() sExp, eExp := exp.At()
sRes, eRes := res.At() sRes, eRes := res.At()
require.Equal(t, eExp, eRes, "samples error") testutil.Equals(t, eExp, eRes)
require.Equal(t, sExp, sRes, "samples") testutil.Equals(t, sExp, sRes)
remaining = exp.Next() remaining = exp.Next()
require.Equal(t, remaining, res.Next()) testutil.Equals(t, remaining, res.Next())
} }
} }
} }
@ -1106,11 +1106,11 @@ func TestChunkSeriesIterator_DoubleSeek(t *testing.T) {
} }
res := newChunkSeriesIterator(chkMetas, nil, 2, 8) res := newChunkSeriesIterator(chkMetas, nil, 2, 8)
require.True(t, res.Seek(1)) testutil.Assert(t, res.Seek(1) == true, "")
require.True(t, res.Seek(2)) testutil.Assert(t, res.Seek(2) == true, "")
ts, v := res.At() ts, v := res.At()
require.Equal(t, int64(2), ts) testutil.Equals(t, int64(2), ts)
require.Equal(t, float64(2), v) testutil.Equals(t, float64(2), v)
} }
// Regression when seeked chunks were still found via binary search and we always // Regression when seeked chunks were still found via binary search and we always
@ -1124,15 +1124,15 @@ func TestChunkSeriesIterator_SeekInCurrentChunk(t *testing.T) {
it := newChunkSeriesIterator(metas, nil, 1, 7) it := newChunkSeriesIterator(metas, nil, 1, 7)
require.True(t, it.Next()) testutil.Assert(t, it.Next() == true, "")
ts, v := it.At() ts, v := it.At()
require.Equal(t, int64(1), ts) testutil.Equals(t, int64(1), ts)
require.Equal(t, float64(2), v) testutil.Equals(t, float64(2), v)
require.True(t, it.Seek(4)) testutil.Assert(t, it.Seek(4) == true, "")
ts, v = it.At() ts, v = it.At()
require.Equal(t, int64(5), ts) testutil.Equals(t, int64(5), ts)
require.Equal(t, float64(6), v) testutil.Equals(t, float64(6), v)
} }
// Regression when calling Next() with a time bounded to fit within two samples. // Regression when calling Next() with a time bounded to fit within two samples.
@ -1143,7 +1143,7 @@ func TestChunkSeriesIterator_NextWithMinTime(t *testing.T) {
} }
it := newChunkSeriesIterator(metas, nil, 2, 4) it := newChunkSeriesIterator(metas, nil, 2, 4)
require.False(t, it.Next()) testutil.Assert(t, it.Next() == false, "")
} }
func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) { func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
@ -1173,11 +1173,11 @@ func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
maxt: 0, maxt: 0,
} }
require.False(t, p.Next()) testutil.Assert(t, p.Next() == false, "")
p.mint = 6 p.mint = 6
p.maxt = 9 p.maxt = 9
require.False(t, p.Next()) testutil.Assert(t, p.Next() == false, "")
// Test the case where 1 chunk could cause an unpopulated chunk to be returned. // Test the case where 1 chunk could cause an unpopulated chunk to be returned.
chunkMetas = [][]ChunkMeta{ chunkMetas = [][]ChunkMeta{
@ -1194,7 +1194,7 @@ func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
mint: 10, mint: 10,
maxt: 15, maxt: 15,
} }
require.False(t, p.Next()) testutil.Assert(t, p.Next() == false, "")
return return
} }
@ -1246,7 +1246,7 @@ func BenchmarkMergedSeriesSet(b *testing.B) {
for _, j := range []int{1, 2, 4, 8, 16, 32} { for _, j := range []int{1, 2, 4, 8, 16, 32} {
b.Run(fmt.Sprintf("series=%d,blocks=%d", k, j), func(b *testing.B) { b.Run(fmt.Sprintf("series=%d,blocks=%d", k, j), func(b *testing.B) {
lbls, err := readPrometheusLabels("testdata/1m.series", k) lbls, err := readPrometheusLabels("testdata/1m.series", k)
require.NoError(b, err) testutil.Ok(b, err)
sort.Sort(labels.Slice(lbls)) sort.Sort(labels.Slice(lbls))
@ -1272,8 +1272,8 @@ func BenchmarkMergedSeriesSet(b *testing.B) {
for ms.Next() { for ms.Next() {
i++ i++
} }
require.NoError(b, ms.Err()) testutil.Ok(b, ms.Err())
require.Equal(b, len(lbls), i) testutil.Equals(b, len(lbls), i)
} }
}) })
} }

View file

@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
package tsdb package testutil
import ( import (
"fmt" "fmt"

View file

@ -20,7 +20,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func TestWriteAndReadbackTombStones(t *testing.T) { func TestWriteAndReadbackTombStones(t *testing.T) {
@ -43,13 +43,13 @@ func TestWriteAndReadbackTombStones(t *testing.T) {
stones[ref] = dranges stones[ref] = dranges
} }
require.NoError(t, writeTombstoneFile(tmpdir, stones)) testutil.Ok(t, writeTombstoneFile(tmpdir, stones))
restr, err := readTombstones(tmpdir) restr, err := readTombstones(tmpdir)
require.NoError(t, err) testutil.Ok(t, err)
// Compare the two readers. // Compare the two readers.
require.Equal(t, stones, restr) testutil.Equals(t, stones, restr)
} }
func TestAddingNewIntervals(t *testing.T) { func TestAddingNewIntervals(t *testing.T) {
@ -117,7 +117,7 @@ func TestAddingNewIntervals(t *testing.T) {
for _, c := range cases { for _, c := range cases {
require.Equal(t, c.exp, c.exist.add(c.new)) testutil.Equals(t, c.exp, c.exist.add(c.new))
} }
return return
} }

View file

@ -5,7 +5,7 @@ import (
"sort" "sort"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func TestSampleRing(t *testing.T) { func TestSampleRing(t *testing.T) {
@ -79,12 +79,12 @@ func TestBufferedSeriesIterator(t *testing.T) {
t, v := bit.At() t, v := bit.At()
b = append(b, sample{t: t, v: v}) b = append(b, sample{t: t, v: v})
} }
require.Equal(t, exp, b, "buffer mismatch") testutil.Equals(t, exp, b)
} }
sampleEq := func(ets int64, ev float64) { sampleEq := func(ets int64, ev float64) {
ts, v := it.At() ts, v := it.At()
require.Equal(t, ets, ts, "timestamp mismatch") testutil.Equals(t, ets, ts)
require.Equal(t, ev, v, "value mismatch") testutil.Equals(t, ev, v)
} }
it = NewBuffer(newListSeriesIterator([]sample{ it = NewBuffer(newListSeriesIterator([]sample{
@ -98,29 +98,29 @@ func TestBufferedSeriesIterator(t *testing.T) {
{t: 101, v: 10}, {t: 101, v: 10},
}), 2) }), 2)
require.True(t, it.Seek(-123), "seek failed") testutil.Assert(t, it.Seek(-123) == true, "seek failed")
sampleEq(1, 2) sampleEq(1, 2)
bufferEq(nil) bufferEq(nil)
require.True(t, it.Next(), "next failed") testutil.Assert(t, it.Next() == true, "next failed")
sampleEq(2, 3) sampleEq(2, 3)
bufferEq([]sample{{t: 1, v: 2}}) bufferEq([]sample{{t: 1, v: 2}})
require.True(t, it.Next(), "next failed") testutil.Assert(t, it.Next() == true, "next failed")
require.True(t, it.Next(), "next failed") testutil.Assert(t, it.Next() == true, "next failed")
require.True(t, it.Next(), "next failed") testutil.Assert(t, it.Next() == true, "next failed")
sampleEq(5, 6) sampleEq(5, 6)
bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}}) bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}})
require.True(t, it.Seek(5), "seek failed") testutil.Assert(t, it.Seek(5) == true, "seek failed")
sampleEq(5, 6) sampleEq(5, 6)
bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}}) bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}})
require.True(t, it.Seek(101), "seek failed") testutil.Assert(t, it.Seek(101) == true, "seek failed")
sampleEq(101, 10) sampleEq(101, 10)
bufferEq([]sample{{t: 99, v: 8}, {t: 100, v: 9}}) bufferEq([]sample{{t: 99, v: 8}, {t: 100, v: 9}})
require.False(t, it.Next(), "next succeeded unexpectedly") testutil.Assert(t, it.Next() == false, "next succeeded unexpectedly")
} }
type listSeriesIterator struct { type listSeriesIterator struct {

View file

@ -22,47 +22,47 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/tsdb/fileutil" "github.com/prometheus/tsdb/fileutil"
"github.com/stretchr/testify/require" "github.com/prometheus/tsdb/testutil"
) )
func TestSegmentWAL_cut(t *testing.T) { func TestSegmentWAL_cut(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test_wal_cut") tmpdir, err := ioutil.TempDir("", "test_wal_cut")
require.NoError(t, err) testutil.Ok(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
// This calls cut() implicitly the first time without a previous tail. // This calls cut() implicitly the first time without a previous tail.
w, err := OpenSegmentWAL(tmpdir, nil, 0, nil) w, err := OpenSegmentWAL(tmpdir, nil, 0, nil)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, w.write(WALEntrySeries, 1, []byte("Hello World!!"))) testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
require.NoError(t, w.cut(), "cut failed") testutil.Ok(t, w.cut())
// Cutting creates a new file. // Cutting creates a new file.
require.Equal(t, 2, len(w.files)) testutil.Equals(t, 2, len(w.files))
require.NoError(t, w.write(WALEntrySeries, 1, []byte("Hello World!!"))) testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
require.NoError(t, w.Close()) testutil.Ok(t, w.Close())
for _, of := range w.files { for _, of := range w.files {
f, err := os.Open(of.Name()) f, err := os.Open(of.Name())
require.NoError(t, err) testutil.Ok(t, err)
// Verify header data. // Verify header data.
metab := make([]byte, 8) metab := make([]byte, 8)
_, err = f.Read(metab) _, err = f.Read(metab)
require.NoError(t, err, "read meta data %s", f.Name()) testutil.Ok(t, err)
require.Equal(t, WALMagic, binary.BigEndian.Uint32(metab[:4]), "verify magic") testutil.Equals(t, WALMagic, binary.BigEndian.Uint32(metab[:4]))
require.Equal(t, WALFormatDefault, metab[4], "verify format flag") testutil.Equals(t, WALFormatDefault, metab[4])
// We cannot actually check for correct pre-allocation as it is // We cannot actually check for correct pre-allocation as it is
// optional per filesystem and handled transparently. // optional per filesystem and handled transparently.
et, flag, b, err := newWALReader(nil, nil).entry(f) et, flag, b, err := newWALReader(nil, nil).entry(f)
require.NoError(t, err) testutil.Ok(t, err)
require.Equal(t, WALEntrySeries, et) testutil.Equals(t, WALEntrySeries, et)
require.Equal(t, flag, byte(walSeriesSimple)) testutil.Equals(t, flag, byte(walSeriesSimple))
require.Equal(t, []byte("Hello World!!"), b) testutil.Equals(t, []byte("Hello World!!"), b)
} }
} }
@ -72,14 +72,14 @@ func TestSegmentWAL_Truncate(t *testing.T) {
batch = 100 batch = 100
) )
series, err := readPrometheusLabels("testdata/20k.series", numMetrics) series, err := readPrometheusLabels("testdata/20k.series", numMetrics)
require.NoError(t, err) testutil.Ok(t, err)
dir, err := ioutil.TempDir("", "test_wal_log_truncate") dir, err := ioutil.TempDir("", "test_wal_log_truncate")
require.NoError(t, err) testutil.Ok(t, err)
// defer os.RemoveAll(dir) // defer os.RemoveAll(dir)
w, err := OpenSegmentWAL(dir, nil, 0, nil) w, err := OpenSegmentWAL(dir, nil, 0, nil)
require.NoError(t, err) testutil.Ok(t, err)
w.segmentSize = 10000 w.segmentSize = 10000
for i := 0; i < numMetrics; i += batch { for i := 0; i < numMetrics; i += batch {
@ -89,7 +89,7 @@ func TestSegmentWAL_Truncate(t *testing.T) {
rs = append(rs, RefSeries{Labels: s, Ref: uint64(i+j) + 1}) rs = append(rs, RefSeries{Labels: s, Ref: uint64(i+j) + 1})
} }
err := w.LogSeries(rs) err := w.LogSeries(rs)
require.NoError(t, err) testutil.Ok(t, err)
} }
// We mark the 2nd half of the files with a min timestamp that should discard // We mark the 2nd half of the files with a min timestamp that should discard
@ -111,7 +111,7 @@ func TestSegmentWAL_Truncate(t *testing.T) {
} }
err = w.Truncate(1000, keepf) err = w.Truncate(1000, keepf)
require.NoError(t, err) testutil.Ok(t, err)
var expected []RefSeries var expected []RefSeries
@ -124,12 +124,12 @@ func TestSegmentWAL_Truncate(t *testing.T) {
// Call Truncate once again to see whether we can read the written file without // Call Truncate once again to see whether we can read the written file without
// creating a new WAL. // creating a new WAL.
err = w.Truncate(1000, keepf) err = w.Truncate(1000, keepf)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, w.Close()) testutil.Ok(t, w.Close())
// The same again with a new WAL. // The same again with a new WAL.
w, err = OpenSegmentWAL(dir, nil, 0, nil) w, err = OpenSegmentWAL(dir, nil, 0, nil)
require.NoError(t, err) testutil.Ok(t, err)
var readSeries []RefSeries var readSeries []RefSeries
r := w.Reader() r := w.Reader()
@ -138,7 +138,7 @@ func TestSegmentWAL_Truncate(t *testing.T) {
readSeries = append(readSeries, s...) readSeries = append(readSeries, s...)
}, nil, nil) }, nil, nil)
require.Equal(t, expected, readSeries) testutil.Equals(t, expected, readSeries)
} }
// Symmetrical test of reading and writing to the WAL via its main interface. // Symmetrical test of reading and writing to the WAL via its main interface.
@ -151,10 +151,10 @@ func TestSegmentWAL_Log_Restore(t *testing.T) {
// Generate testing data. It does not make semantical sense but // Generate testing data. It does not make semantical sense but
// for the purpose of this test. // for the purpose of this test.
series, err := readPrometheusLabels("testdata/20k.series", numMetrics) series, err := readPrometheusLabels("testdata/20k.series", numMetrics)
require.NoError(t, err) testutil.Ok(t, err)
dir, err := ioutil.TempDir("", "test_wal_log_restore") dir, err := ioutil.TempDir("", "test_wal_log_restore")
require.NoError(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
var ( var (
@ -168,7 +168,7 @@ func TestSegmentWAL_Log_Restore(t *testing.T) {
// write more data to it, close it. // write more data to it, close it.
for k := 0; k < numMetrics; k += numMetrics / iterations { for k := 0; k < numMetrics; k += numMetrics / iterations {
w, err := OpenSegmentWAL(dir, nil, 0, nil) w, err := OpenSegmentWAL(dir, nil, 0, nil)
require.NoError(t, err) testutil.Ok(t, err)
// Set smaller segment size so we can actually write several files. // Set smaller segment size so we can actually write several files.
w.segmentSize = 1000 * 1000 w.segmentSize = 1000 * 1000
@ -204,11 +204,11 @@ func TestSegmentWAL_Log_Restore(t *testing.T) {
} }
} }
require.NoError(t, r.Read(serf, smplf, delf)) testutil.Ok(t, r.Read(serf, smplf, delf))
require.Equal(t, recordedSamples, resultSamples) testutil.Equals(t, recordedSamples, resultSamples)
require.Equal(t, recordedSeries, resultSeries) testutil.Equals(t, recordedSeries, resultSeries)
require.Equal(t, recordedDeletes, resultDeletes) testutil.Equals(t, recordedDeletes, resultDeletes)
series := series[k : k+(numMetrics/iterations)] series := series[k : k+(numMetrics/iterations)]
@ -239,9 +239,9 @@ func TestSegmentWAL_Log_Restore(t *testing.T) {
}) })
} }
require.NoError(t, w.LogSeries(series)) testutil.Ok(t, w.LogSeries(series))
require.NoError(t, w.LogSamples(samples)) testutil.Ok(t, w.LogSamples(samples))
require.NoError(t, w.LogDeletes(stones)) testutil.Ok(t, w.LogDeletes(stones))
if len(lbls) > 0 { if len(lbls) > 0 {
recordedSeries = append(recordedSeries, series) recordedSeries = append(recordedSeries, series)
@ -255,39 +255,39 @@ func TestSegmentWAL_Log_Restore(t *testing.T) {
} }
} }
require.NoError(t, w.Close()) testutil.Ok(t, w.Close())
} }
} }
func TestWALRestoreCorrupted_invalidSegment(t *testing.T) { func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
dir, err := ioutil.TempDir("", "test_wal_log_restore") dir, err := ioutil.TempDir("", "test_wal_log_restore")
Ok(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
wal, err := OpenSegmentWAL(dir, nil, 0, nil) wal, err := OpenSegmentWAL(dir, nil, 0, nil)
Ok(t, err) testutil.Ok(t, err)
_, err = wal.createSegmentFile(dir + "/000000") _, err = wal.createSegmentFile(dir + "/000000")
Ok(t, err) testutil.Ok(t, err)
f, err := wal.createSegmentFile(dir + "/000001") f, err := wal.createSegmentFile(dir + "/000001")
Ok(t, err) testutil.Ok(t, err)
f2, err := wal.createSegmentFile(dir + "/000002") f2, err := wal.createSegmentFile(dir + "/000002")
Ok(t, err) testutil.Ok(t, err)
Ok(t, f2.Close()) testutil.Ok(t, f2.Close())
// Make header of second segment invalid. // Make header of second segment invalid.
_, err = f.WriteAt([]byte{1, 2, 3, 4}, 0) _, err = f.WriteAt([]byte{1, 2, 3, 4}, 0)
Ok(t, err) testutil.Ok(t, err)
Ok(t, f.Close()) testutil.Ok(t, f.Close())
Ok(t, wal.Close()) testutil.Ok(t, wal.Close())
wal, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil) wal, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil)
Ok(t, err) testutil.Ok(t, err)
fns, err := fileutil.ReadDir(dir) fns, err := fileutil.ReadDir(dir)
Ok(t, err) testutil.Ok(t, err)
Equals(t, []string{"000000"}, fns) testutil.Equals(t, []string{"000000"}, fns)
} }
// Test reading from a WAL that has been corrupted through various means. // Test reading from a WAL that has been corrupted through various means.
@ -300,56 +300,56 @@ func TestWALRestoreCorrupted(t *testing.T) {
name: "truncate_checksum", name: "truncate_checksum",
f: func(t *testing.T, w *SegmentWAL) { f: func(t *testing.T, w *SegmentWAL) {
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
require.NoError(t, err) testutil.Ok(t, err)
defer f.Close() defer f.Close()
off, err := f.Seek(0, os.SEEK_END) off, err := f.Seek(0, os.SEEK_END)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, f.Truncate(off-1)) testutil.Ok(t, f.Truncate(off-1))
}, },
}, },
{ {
name: "truncate_body", name: "truncate_body",
f: func(t *testing.T, w *SegmentWAL) { f: func(t *testing.T, w *SegmentWAL) {
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
require.NoError(t, err) testutil.Ok(t, err)
defer f.Close() defer f.Close()
off, err := f.Seek(0, os.SEEK_END) off, err := f.Seek(0, os.SEEK_END)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, f.Truncate(off-8)) testutil.Ok(t, f.Truncate(off-8))
}, },
}, },
{ {
name: "body_content", name: "body_content",
f: func(t *testing.T, w *SegmentWAL) { f: func(t *testing.T, w *SegmentWAL) {
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
require.NoError(t, err) testutil.Ok(t, err)
defer f.Close() defer f.Close()
off, err := f.Seek(0, os.SEEK_END) off, err := f.Seek(0, os.SEEK_END)
require.NoError(t, err) testutil.Ok(t, err)
// Write junk before checksum starts. // Write junk before checksum starts.
_, err = f.WriteAt([]byte{1, 2, 3, 4}, off-8) _, err = f.WriteAt([]byte{1, 2, 3, 4}, off-8)
require.NoError(t, err) testutil.Ok(t, err)
}, },
}, },
{ {
name: "checksum", name: "checksum",
f: func(t *testing.T, w *SegmentWAL) { f: func(t *testing.T, w *SegmentWAL) {
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
require.NoError(t, err) testutil.Ok(t, err)
defer f.Close() defer f.Close()
off, err := f.Seek(0, os.SEEK_END) off, err := f.Seek(0, os.SEEK_END)
require.NoError(t, err) testutil.Ok(t, err)
// Write junk into checksum // Write junk into checksum
_, err = f.WriteAt([]byte{1, 2, 3, 4}, off-4) _, err = f.WriteAt([]byte{1, 2, 3, 4}, off-4)
require.NoError(t, err) testutil.Ok(t, err)
}, },
}, },
} }
@ -358,21 +358,21 @@ func TestWALRestoreCorrupted(t *testing.T) {
// Generate testing data. It does not make semantical sense but // Generate testing data. It does not make semantical sense but
// for the purpose of this test. // for the purpose of this test.
dir, err := ioutil.TempDir("", "test_corrupted") dir, err := ioutil.TempDir("", "test_corrupted")
require.NoError(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
w, err := OpenSegmentWAL(dir, nil, 0, nil) w, err := OpenSegmentWAL(dir, nil, 0, nil)
require.NoError(t, err) testutil.Ok(t, err)
require.NoError(t, w.LogSamples([]RefSample{{T: 1, V: 2}})) testutil.Ok(t, w.LogSamples([]RefSample{{T: 1, V: 2}}))
require.NoError(t, w.LogSamples([]RefSample{{T: 2, V: 3}})) testutil.Ok(t, w.LogSamples([]RefSample{{T: 2, V: 3}}))
require.NoError(t, w.cut()) testutil.Ok(t, w.cut())
require.NoError(t, w.LogSamples([]RefSample{{T: 3, V: 4}})) testutil.Ok(t, w.LogSamples([]RefSample{{T: 3, V: 4}}))
require.NoError(t, w.LogSamples([]RefSample{{T: 5, V: 6}})) testutil.Ok(t, w.LogSamples([]RefSample{{T: 5, V: 6}}))
require.NoError(t, w.Close()) testutil.Ok(t, w.Close())
// cut() truncates and fsyncs the first segment async. If it happens after // cut() truncates and fsyncs the first segment async. If it happens after
// the corruption we apply below, the corruption will be overwritten again. // the corruption we apply below, the corruption will be overwritten again.
@ -387,39 +387,39 @@ func TestWALRestoreCorrupted(t *testing.T) {
logger := log.NewLogfmtLogger(os.Stderr) logger := log.NewLogfmtLogger(os.Stderr)
w2, err := OpenSegmentWAL(dir, logger, 0, nil) w2, err := OpenSegmentWAL(dir, logger, 0, nil)
require.NoError(t, err) testutil.Ok(t, err)
r := w2.Reader() r := w2.Reader()
serf := func(l []RefSeries) { serf := func(l []RefSeries) {
require.Equal(t, 0, len(l)) testutil.Equals(t, 0, len(l))
} }
// Weird hack to check order of reads. // Weird hack to check order of reads.
i := 0 i := 0
samplf := func(s []RefSample) { samplf := func(s []RefSample) {
if i == 0 { if i == 0 {
require.Equal(t, []RefSample{{T: 1, V: 2}}, s) testutil.Equals(t, []RefSample{{T: 1, V: 2}}, s)
i++ i++
} else { } else {
require.Equal(t, []RefSample{{T: 99, V: 100}}, s) testutil.Equals(t, []RefSample{{T: 99, V: 100}}, s)
} }
} }
require.NoError(t, r.Read(serf, samplf, nil)) testutil.Ok(t, r.Read(serf, samplf, nil))
require.NoError(t, w2.LogSamples([]RefSample{{T: 99, V: 100}})) testutil.Ok(t, w2.LogSamples([]RefSample{{T: 99, V: 100}}))
require.NoError(t, w2.Close()) testutil.Ok(t, w2.Close())
// We should see the first valid entry and the new one, everything after // We should see the first valid entry and the new one, everything after
// is truncated. // is truncated.
w3, err := OpenSegmentWAL(dir, logger, 0, nil) w3, err := OpenSegmentWAL(dir, logger, 0, nil)
require.NoError(t, err) testutil.Ok(t, err)
r = w3.Reader() r = w3.Reader()
i = 0 i = 0
require.NoError(t, r.Read(serf, samplf, nil)) testutil.Ok(t, r.Read(serf, samplf, nil))
}) })
} }
} }