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"
"os"
"testing"
"github.com/prometheus/tsdb/testutil"
)
func TestSetCompactionFailed(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test-tsdb")
Ok(t, err)
testutil.Ok(t, err)
b := createEmptyBlock(t, tmpdir)
Equals(t, false, b.meta.Compaction.Failed)
Ok(t, b.setCompactionFailed())
Equals(t, true, b.meta.Compaction.Failed)
Ok(t, b.Close())
testutil.Equals(t, false, b.meta.Compaction.Failed)
testutil.Ok(t, b.setCompactionFailed())
testutil.Equals(t, true, b.meta.Compaction.Failed)
testutil.Ok(t, b.Close())
b, err = OpenBlock(tmpdir, nil)
Ok(t, err)
Equals(t, true, b.meta.Compaction.Failed)
testutil.Ok(t, err)
testutil.Equals(t, true, b.meta.Compaction.Failed)
}
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)
Ok(t, err)
Ok(t, ir.Close())
testutil.Ok(t, err)
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)
Ok(t, err)
testutil.Ok(t, err)
return b
}

View file

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

View file

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

View file

@ -21,7 +21,7 @@ import (
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/prometheus/tsdb/testutil"
)
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)
require.NoError(t, err)
testutil.Ok(t, err)
c.plan(metas)
}
@ -160,7 +160,7 @@ func TestLeveledCompactor_plan(t *testing.T) {
720,
2160,
}, nil)
require.NoError(t, err)
testutil.Ok(t, err)
cases := []struct {
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)
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,
2160,
}, nil)
Ok(t, err)
testutil.Ok(t, err)
cases := []struct {
metas []dirMeta
@ -310,9 +310,9 @@ func TestRangeWithFailedCompactionWontGetSelected(t *testing.T) {
for _, c := range cases {
c.metas[1].meta.Compaction.Failed = true
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,
2160,
}, nil)
Ok(t, err)
testutil.Ok(t, err)
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")
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 {

View file

@ -23,15 +23,15 @@ import (
"github.com/pkg/errors"
"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()) {
tmpdir, err := ioutil.TempDir("", "test")
Ok(t, err)
testutil.Ok(t, err)
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.
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.
func query(t testing.TB, q Querier, matchers ...labels.Matcher) map[string][]sample {
ss, err := q.Select(matchers...)
Ok(t, err)
testutil.Ok(t, err)
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()
samples = append(samples, sample{t: t, v: v})
}
Ok(t, it.Err())
testutil.Ok(t, it.Err())
name := series.Labels().String()
result[name] = samples
}
Ok(t, ss.Err())
testutil.Ok(t, ss.Err())
return result
}
@ -70,25 +70,25 @@ func TestDataAvailableOnlyAfterCommit(t *testing.T) {
app := db.Appender()
_, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0)
require.NoError(t, err)
testutil.Ok(t, err)
querier, err := db.Querier(0, 1)
require.NoError(t, err)
testutil.Ok(t, err)
seriesSet := query(t, querier, labels.NewEqualMatcher("foo", "bar"))
require.Equal(t, seriesSet, map[string][]sample{})
require.NoError(t, querier.Close())
testutil.Equals(t, seriesSet, map[string][]sample{})
testutil.Ok(t, querier.Close())
err = app.Commit()
require.NoError(t, err)
testutil.Ok(t, err)
querier, err = db.Querier(0, 1)
require.NoError(t, err)
testutil.Ok(t, err)
defer querier.Close()
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) {
@ -97,18 +97,18 @@ func TestDataNotAvailableAfterRollback(t *testing.T) {
app := db.Appender()
_, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0)
require.NoError(t, err)
testutil.Ok(t, err)
err = app.Rollback()
require.NoError(t, err)
testutil.Ok(t, err)
querier, err := db.Querier(0, 1)
require.NoError(t, err)
testutil.Ok(t, err)
defer querier.Close()
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) {
@ -118,41 +118,41 @@ func TestDBAppenderAddRef(t *testing.T) {
app1 := db.Appender()
ref1, err := app1.Add(labels.FromStrings("a", "b"), 123, 0)
require.NoError(t, err)
testutil.Ok(t, err)
// Reference should already work before commit.
err = app1.AddFast(ref1, 124, 1)
require.NoError(t, err)
testutil.Ok(t, err)
err = app1.Commit()
require.NoError(t, err)
testutil.Ok(t, err)
app2 := db.Appender()
// first ref should already work in next transaction.
err = app2.AddFast(ref1, 125, 0)
require.NoError(t, err)
testutil.Ok(t, err)
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.
err = app2.AddFast(ref2, 143, 2)
require.NoError(t, err)
testutil.Ok(t, err)
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)
require.NoError(t, err)
testutil.Ok(t, err)
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{
{t: 123, v: 0},
{t: 124, v: 1},
@ -162,7 +162,7 @@ func TestDBAppenderAddRef(t *testing.T) {
},
}, res)
require.NoError(t, q.Close())
testutil.Ok(t, q.Close())
}
func TestDeleteSimple(t *testing.T) {
@ -179,7 +179,7 @@ func TestDeleteSimple(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
}
require.NoError(t, app.Commit())
testutil.Ok(t, app.Commit())
cases := []struct {
intervals Intervals
remaint []int64
@ -195,15 +195,15 @@ Outer:
// TODO(gouthamve): Reset the tombstones somehow.
// Delete the ranges.
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.
q, err := db.Querier(0, numSamples)
require.NoError(t, err)
testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err)
testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint {
@ -215,13 +215,13 @@ Outer:
})
if len(expSamples) == 0 {
require.False(t, res.Next())
testutil.Assert(t, res.Next() == false, "")
continue
}
for {
eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next")
testutil.Equals(t, eok, rok)
if !eok {
continue Outer
@ -229,13 +229,13 @@ Outer:
sexp := expss.At()
sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
}
}
@ -246,13 +246,13 @@ func TestAmendDatapointCausesError(t *testing.T) {
app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, 0)
require.NoError(t, err, "Failed to add sample")
require.NoError(t, app.Commit(), "Unexpected error committing appender")
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
app = db.Appender()
_, err = app.Add(labels.Labels{}, 0, 1)
require.Equal(t, ErrAmendSample, err)
require.NoError(t, app.Rollback(), "Unexpected error rolling back appender")
testutil.Equals(t, ErrAmendSample, err)
testutil.Ok(t, app.Rollback())
}
func TestDuplicateNaNDatapointNoAmendError(t *testing.T) {
@ -261,12 +261,12 @@ func TestDuplicateNaNDatapointNoAmendError(t *testing.T) {
app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, math.NaN())
require.NoError(t, err, "Failed to add sample")
require.NoError(t, app.Commit(), "Unexpected error committing appender")
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
app = db.Appender()
_, err = app.Add(labels.Labels{}, 0, math.NaN())
require.NoError(t, err)
testutil.Ok(t, err)
}
func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) {
@ -275,12 +275,12 @@ func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) {
app := db.Appender()
_, err := app.Add(labels.Labels{}, 0, math.Float64frombits(0x7ff0000000000001))
require.NoError(t, err, "Failed to add sample")
require.NoError(t, app.Commit(), "Unexpected error committing appender")
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
app = db.Appender()
_, err = app.Add(labels.Labels{}, 0, math.Float64frombits(0x7ff0000000000002))
require.Equal(t, ErrAmendSample, err)
testutil.Equals(t, ErrAmendSample, err)
}
func TestSkippingInvalidValuesInSameTxn(t *testing.T) {
@ -290,40 +290,40 @@ func TestSkippingInvalidValuesInSameTxn(t *testing.T) {
// Append AmendedValue.
app := db.Appender()
_, 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)
require.NoError(t, err)
require.NoError(t, app.Commit())
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
// Make sure the right value is stored.
q, err := db.Querier(0, 10)
require.NoError(t, err)
testutil.Ok(t, err)
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}},
}, ssMap)
require.NoError(t, q.Close())
testutil.Ok(t, q.Close())
// Append Out of Order Value.
app = db.Appender()
_, 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)
require.NoError(t, err)
require.NoError(t, app.Commit())
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
q, err = db.Querier(0, 10)
require.NoError(t, err)
testutil.Ok(t, err)
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}},
}, ssMap)
require.NoError(t, q.Close())
testutil.Ok(t, q.Close())
}
func TestDB_Snapshot(t *testing.T) {
@ -335,28 +335,28 @@ func TestDB_Snapshot(t *testing.T) {
mint := int64(1414141414000)
for i := 0; i < 1000; i++ {
_, 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())
require.NoError(t, app.Rollback())
testutil.Ok(t, app.Commit())
testutil.Ok(t, app.Rollback())
// create snapshot
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
require.NoError(t, db.Snapshot(snap))
require.NoError(t, db.Close())
testutil.Ok(t, err)
testutil.Ok(t, db.Snapshot(snap))
testutil.Ok(t, db.Close())
// reopen DB from snapshot
db, err = Open(snap, nil, nil, nil)
require.NoError(t, err)
testutil.Ok(t, err)
querier, err := db.Querier(mint, mint+1000)
require.NoError(t, err)
testutil.Ok(t, err)
defer querier.Close()
// sum values
seriesSet, err := querier.Select(labels.NewEqualMatcher("foo", "bar"))
require.NoError(t, err)
testutil.Ok(t, err)
sum := 0.0
for seriesSet.Next() {
@ -365,10 +365,10 @@ func TestDB_Snapshot(t *testing.T) {
_, v := series.At()
sum += v
}
require.NoError(t, series.Err())
testutil.Ok(t, series.Err())
}
require.NoError(t, seriesSet.Err())
require.Equal(t, sum, 1000.0)
testutil.Ok(t, seriesSet.Err())
testutil.Equals(t, sum, 1000.0)
}
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])
}
require.NoError(t, app.Commit())
testutil.Ok(t, app.Commit())
cases := []struct {
intervals Intervals
remaint []int64
@ -401,25 +401,25 @@ Outer:
// TODO(gouthamve): Reset the tombstones somehow.
// Delete the ranges.
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
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
require.NoError(t, db.Snapshot(snap))
require.NoError(t, db.Close())
testutil.Ok(t, err)
testutil.Ok(t, db.Snapshot(snap))
testutil.Ok(t, db.Close())
// reopen DB from snapshot
db, err = Open(snap, nil, nil, nil)
require.NoError(t, err)
testutil.Ok(t, err)
// Compare the result.
q, err := db.Querier(0, numSamples)
require.NoError(t, err)
testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err)
testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint {
@ -431,13 +431,13 @@ Outer:
})
if len(expSamples) == 0 {
require.False(t, res.Next())
testutil.Assert(t, res.Next() == false, "")
continue
}
for {
eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next")
testutil.Equals(t, eok, rok)
if !eok {
continue Outer
@ -445,13 +445,13 @@ Outer:
sexp := expss.At()
sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
}
}
@ -529,7 +529,7 @@ func TestDB_e2e(t *testing.T) {
series = append(series, sample{ts, v})
_, err := app.Add(lset, ts, v)
require.NoError(t, err)
testutil.Ok(t, err)
ts += rand.Int63n(timeInterval) + 1
}
@ -537,7 +537,7 @@ func TestDB_e2e(t *testing.T) {
seriesMap[lset.String()] = series
}
require.NoError(t, app.Commit())
testutil.Ok(t, app.Commit())
// Query each selector on 1000 random time-ranges.
queries := []struct {
@ -588,10 +588,10 @@ func TestDB_e2e(t *testing.T) {
}
q, err := db.Querier(mint, maxt)
require.NoError(t, err)
testutil.Ok(t, err)
ss, err := q.Select(qry.ms...)
require.NoError(t, err)
testutil.Ok(t, err)
result := map[string][]sample{}
@ -599,15 +599,15 @@ func TestDB_e2e(t *testing.T) {
x := ss.At()
smpls, err := expandSeriesIterator(x.Iterator())
require.NoError(t, err)
testutil.Ok(t, err)
if len(smpls) > 0 {
result[x.Labels().String()] = smpls
}
}
require.NoError(t, ss.Err())
require.Equal(t, expected, result)
testutil.Ok(t, ss.Err())
testutil.Equals(t, expected, result)
q.Close()
}
@ -618,30 +618,30 @@ func TestDB_e2e(t *testing.T) {
func TestWALFlushedOnDBClose(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test")
Ok(t, err)
testutil.Ok(t, err)
defer os.RemoveAll(tmpdir)
db, err := Open(tmpdir, nil, nil, nil)
require.NoError(t, err)
testutil.Ok(t, err)
lbls := labels.Labels{labels.Label{Name: "labelname", Value: "labelvalue"}}
app := db.Appender()
_, err = app.Add(lbls, 0, 1)
require.NoError(t, err)
require.NoError(t, app.Commit())
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
db.Close()
db, err = Open(tmpdir, nil, nil, nil)
require.NoError(t, err)
testutil.Ok(t, err)
q, err := db.Querier(0, 1)
require.NoError(t, err)
testutil.Ok(t, err)
values, err := q.LabelValues("labelname")
require.NoError(t, err)
require.Equal(t, values, []string{"labelvalue"})
testutil.Ok(t, err)
testutil.Equals(t, values, []string{"labelvalue"})
}
func TestTombstoneClean(t *testing.T) {
@ -658,7 +658,7 @@ func TestTombstoneClean(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
}
require.NoError(t, app.Commit())
testutil.Ok(t, app.Commit())
cases := []struct {
intervals Intervals
remaint []int64
@ -674,27 +674,27 @@ func TestTombstoneClean(t *testing.T) {
// create snapshot
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
require.NoError(t, db.Snapshot(snap))
require.NoError(t, db.Close())
testutil.Ok(t, err)
testutil.Ok(t, db.Snapshot(snap))
testutil.Ok(t, db.Close())
// reopen DB from snapshot
db, err = Open(snap, nil, nil, nil)
require.NoError(t, err)
testutil.Ok(t, err)
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.
require.NoError(t, db.CleanTombstones())
testutil.Ok(t, db.CleanTombstones())
// Compare the result.
q, err := db.Querier(0, numSamples)
require.NoError(t, err)
testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err)
testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint {
@ -706,13 +706,13 @@ func TestTombstoneClean(t *testing.T) {
})
if len(expSamples) == 0 {
require.False(t, res.Next())
testutil.Assert(t, res.Next() == false, "")
continue
}
for {
eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next")
testutil.Equals(t, eok, rok)
if !eok {
break
@ -720,17 +720,17 @@ func TestTombstoneClean(t *testing.T) {
sexp := expss.At()
sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
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)
db, err := Open(tmpdir, nil, nil, nil)
require.NoError(t, err)
testutil.Ok(t, err)
lbls := labels.Labels{labels.Label{Name: "labelname", Value: "labelvalue"}}
app := db.Appender()
_, err = app.Add(lbls, 0, 1)
require.NoError(t, err)
require.NoError(t, app.Commit())
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
// create snapshot to make it create a block.
// TODO(gouthamve): Add a method to compact headblock.
snap, err := ioutil.TempDir("", "snap")
require.NoError(t, err)
require.NoError(t, db.Snapshot(snap))
require.NoError(t, db.Close())
testutil.Ok(t, err)
testutil.Ok(t, db.Snapshot(snap))
testutil.Ok(t, db.Close())
defer os.RemoveAll(snap)
// reopen DB from snapshot
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()
_, err = app.Add(lbls, 100, 1)
require.NoError(t, err)
require.NoError(t, app.Commit())
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
// Snapshot again to create another block.
snap, err = ioutil.TempDir("", "snap")
require.NoError(t, err)
require.NoError(t, db.Snapshot(snap))
require.NoError(t, db.Close())
testutil.Ok(t, err)
testutil.Ok(t, db.Snapshot(snap))
testutil.Ok(t, db.Close())
defer os.RemoveAll(snap)
// reopen DB from snapshot
@ -780,14 +780,14 @@ func TestDB_Retention(t *testing.T) {
RetentionDuration: 10,
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.
changes, err := db.retentionCutoff()
Ok(t, err)
Assert(t, changes, "there should be changes")
Equals(t, 1, len(db.blocks))
Equals(t, int64(100), db.blocks[0].meta.MaxTime) // To verify its the right block.
testutil.Ok(t, err)
testutil.Assert(t, changes, "there should be changes")
testutil.Equals(t, 1, len(db.blocks))
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"
"github.com/prometheus/prometheus/pkg/textparse"
"github.com/stretchr/testify/require"
"github.com/prometheus/tsdb/testutil"
)
func BenchmarkCreateSeries(b *testing.B) {
lbls, err := readPrometheusLabels("testdata/all.series", b.N)
require.NoError(b, err)
testutil.Ok(b, err)
h, err := NewHead(nil, nil, nil, 10000)
if err != nil {
require.NoError(b, err)
testutil.Ok(b, err)
}
defer h.Close()
@ -131,40 +131,40 @@ func TestHead_ReadWAL(t *testing.T) {
wal := &memoryWAL{entries: entries}
head, err := NewHead(nil, nil, wal, 1000)
require.NoError(t, err)
testutil.Ok(t, err)
defer head.Close()
require.NoError(t, head.ReadWAL())
require.Equal(t, uint64(100), head.lastSeriesID)
testutil.Ok(t, head.ReadWAL())
testutil.Equals(t, uint64(100), head.lastSeriesID)
s10 := head.series.getByID(10)
s11 := head.series.getByID(11)
s50 := head.series.getByID(50)
s100 := head.series.getByID(100)
require.Equal(t, labels.FromStrings("a", "1"), s10.lset)
require.Equal(t, labels.FromStrings("a", "2"), s11.lset)
require.Equal(t, labels.FromStrings("a", "4"), s50.lset)
require.Equal(t, labels.FromStrings("a", "3"), s100.lset)
testutil.Equals(t, labels.FromStrings("a", "1"), s10.lset)
testutil.Equals(t, labels.FromStrings("a", "2"), s11.lset)
testutil.Equals(t, labels.FromStrings("a", "4"), s50.lset)
testutil.Equals(t, labels.FromStrings("a", "3"), s100.lset)
expandChunk := func(c chunks.Iterator) (x []sample) {
for c.Next() {
t, v := c.At()
x = append(x, sample{t: t, v: v})
}
require.NoError(t, c.Err())
testutil.Ok(t, c.Err())
return x
}
require.Equal(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0)))
require.Equal(t, 0, len(s11.chunks))
require.Equal(t, []sample{{101, 6}}, expandChunk(s50.iterator(0)))
require.Equal(t, []sample{{100, 3}}, expandChunk(s100.iterator(0)))
testutil.Equals(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0)))
testutil.Equals(t, 0, len(s11.chunks))
testutil.Equals(t, []sample{{101, 6}}, expandChunk(s50.iterator(0)))
testutil.Equals(t, []sample{{100, 3}}, expandChunk(s100.iterator(0)))
}
func TestHead_Truncate(t *testing.T) {
h, err := NewHead(nil, nil, nil, 1000)
require.NoError(t, err)
testutil.Ok(t, err)
defer h.Close()
h.initTime(0)
@ -191,21 +191,21 @@ func TestHead_Truncate(t *testing.T) {
s4.chunks = []*memChunk{}
// Truncation need not be aligned.
require.NoError(t, h.Truncate(1))
testutil.Ok(t, h.Truncate(1))
h.Truncate(2000)
require.Equal(t, []*memChunk{
testutil.Equals(t, []*memChunk{
{minTime: 2000, maxTime: 2999},
}, h.series.getByID(s1.ref).chunks)
require.Equal(t, []*memChunk{
testutil.Equals(t, []*memChunk{
{minTime: 2000, maxTime: 2999},
{minTime: 3000, maxTime: 3999},
}, h.series.getByID(s2.ref).chunks)
require.Nil(t, h.series.getByID(s3.ref))
require.Nil(t, h.series.getByID(s4.ref))
testutil.Assert(t, h.series.getByID(s3.ref) == nil, "")
testutil.Assert(t, h.series.getByID(s4.ref) == nil, "")
postingsA1, _ := expandPostings(h.postings.get("a", "1"))
postingsA2, _ := expandPostings(h.postings.get("a", "2"))
@ -214,14 +214,14 @@ func TestHead_Truncate(t *testing.T) {
postingsC1, _ := expandPostings(h.postings.get("c", "1"))
postingsAll, _ := expandPostings(h.postings.get("", ""))
require.Equal(t, []uint64{s1.ref}, postingsA1)
require.Equal(t, []uint64{s2.ref}, postingsA2)
require.Equal(t, []uint64{s1.ref, s2.ref}, postingsB1)
require.Equal(t, []uint64{s1.ref, s2.ref}, postingsAll)
require.Nil(t, postingsB2)
require.Nil(t, postingsC1)
testutil.Equals(t, []uint64{s1.ref}, postingsA1)
testutil.Equals(t, []uint64{s2.ref}, postingsA2)
testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsB1)
testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsAll)
testutil.Assert(t, postingsB2 == nil, "")
testutil.Assert(t, postingsC1 == nil, "")
require.Equal(t, map[string]struct{}{
testutil.Equals(t, map[string]struct{}{
"": struct{}{}, // from 'all' postings list
"a": struct{}{},
"b": struct{}{},
@ -229,7 +229,7 @@ func TestHead_Truncate(t *testing.T) {
"2": struct{}{},
}, h.symbols)
require.Equal(t, map[string]stringset{
testutil.Equals(t, map[string]stringset{
"a": stringset{"1": struct{}{}, "2": struct{}{}},
"b": stringset{"1": struct{}{}},
"": stringset{"": struct{}{}},
@ -243,7 +243,7 @@ func TestMemSeries_truncateChunks(t *testing.T) {
for i := 0; i < 4000; i += 5 {
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
@ -252,32 +252,32 @@ func TestMemSeries_truncateChunks(t *testing.T) {
lastID := s.chunkID(countBefore - 1)
lastChunk := s.chunk(lastID)
require.NotNil(t, s.chunk(0))
require.NotNil(t, lastChunk)
testutil.Assert(t, s.chunk(0) != nil, "")
testutil.Assert(t, lastChunk != nil, "")
s.truncateChunksBefore(2000)
require.Equal(t, int64(2000), s.chunks[0].minTime, "unexpected start time of first chunks")
require.Nil(t, s.chunk(0), "first chunk not gone")
require.Equal(t, countBefore/2, len(s.chunks), "chunks not truncated correctly")
require.Equal(t, lastChunk, s.chunk(lastID), "last chunk does not match")
testutil.Equals(t, int64(2000), s.chunks[0].minTime)
testutil.Assert(t, s.chunk(0) == nil, "first chunks not gone")
testutil.Equals(t, countBefore/2, len(s.chunks))
testutil.Equals(t, lastChunk, s.chunk(lastID))
// Validate that the series' sample buffer is applied correctly to the last chunk
// after truncation.
it1 := s.iterator(s.chunkID(len(s.chunks) - 1))
_, 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))
_, 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) {
numSamples := int64(10)
head, err := NewHead(nil, nil, nil, 1000)
require.NoError(t, err)
testutil.Ok(t, err)
defer head.Close()
app := head.Appender()
@ -288,7 +288,7 @@ func TestHeadDeleteSimple(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
}
require.NoError(t, app.Commit())
testutil.Ok(t, app.Commit())
cases := []struct {
intervals Intervals
remaint []int64
@ -322,14 +322,14 @@ Outer:
// Delete the ranges.
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.
q, err := NewBlockQuerier(head, head.MinTime(), head.MaxTime())
require.NoError(t, err)
testutil.Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err)
testutil.Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint {
@ -341,13 +341,13 @@ Outer:
})
if len(expSamples) == 0 {
require.False(t, res.Next())
testutil.Assert(t, res.Next() == false, "")
continue
}
for {
eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next")
testutil.Equals(t, eok, rok)
if !eok {
continue Outer
@ -355,13 +355,13 @@ Outer:
sexp := expss.At()
sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
}
}
@ -381,12 +381,12 @@ Outer:
// app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
// }
// require.NoError(t, app.Commit())
// require.NoError(t, hb.Delete(0, 10000, labels.NewEqualMatcher("a", "b")))
// testutil.Ok(t, app.Commit())
// testutil.Ok(t, hb.Delete(0, 10000, labels.NewEqualMatcher("a", "b")))
// app = hb.Appender()
// _, err := app.Add(labels.Labels{{"a", "b"}}, 11, 1)
// require.NoError(t, err)
// require.NoError(t, app.Commit())
// testutil.Ok(t, err)
// testutil.Ok(t, app.Commit())
// q := hb.Querier(0, 100000)
// res := q.Select(labels.NewEqualMatcher("a", "b"))
@ -395,8 +395,8 @@ Outer:
// exps := res.At()
// it := exps.Iterator()
// ressmpls, err := expandSeriesIterator(it)
// require.NoError(t, err)
// require.Equal(t, []sample{{11, 1}}, ressmpls)
// testutil.Ok(t, err)
// testutil.Equals(t, []sample{{11, 1}}, ressmpls)
// }
// func TestDelete_e2e(t *testing.T) {
@ -473,9 +473,9 @@ Outer:
// _, err := app.Add(ls, ts, v)
// if ts >= minTime && ts <= maxTime {
// require.NoError(t, err)
// testutil.Ok(t, err)
// } else {
// require.EqualError(t, err, ErrOutOfBounds.Error())
// testutil.EqualsError(t, err, ErrOutOfBounds.Error())
// }
// ts += rand.Int63n(timeInterval) + 1
@ -484,7 +484,7 @@ Outer:
// seriesMap[labels.New(l...).String()] = series
// }
// require.NoError(t, app.Commit())
// testutil.Ok(t, app.Commit())
// // Delete a time-range from each-selector.
// dels := []struct {
@ -519,7 +519,7 @@ Outer:
// hb.tombstones = newEmptyTombstoneReader()
// 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{}
@ -570,7 +570,7 @@ Outer:
// }
// }
// }
// require.Equal(t, eok, rok, "next")
// testutil.Equals(t, eok, rok, "next")
// if !eok {
// break
@ -578,13 +578,13 @@ Outer:
// sexp := expSs.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())
// smplRes, errRes := expandSeriesIterator(sres.Iterator())
// require.Equal(t, errExp, errRes, "samples error")
// require.Equal(t, smplExp, smplRes, "samples")
// testutil.Equals(t, errExp, errRes, "samples error")
// testutil.Equals(t, smplExp, smplRes, "samples")
// }
// }
// }
@ -673,35 +673,35 @@ func TestMemSeries_append(t *testing.T) {
// on and after it.
// New chunk must correctly be cut at 1000.
ok, chunkCreated := s.append(998, 1)
Assert(t, ok, "append failed")
Assert(t, chunkCreated, "first sample created chunk")
testutil.Assert(t, ok, "append failed")
testutil.Assert(t, chunkCreated, "first sample created chunk")
ok, chunkCreated = s.append(999, 2)
Assert(t, ok, "append failed")
Assert(t, !chunkCreated, "second sample should use same chunk")
testutil.Assert(t, ok, "append failed")
testutil.Assert(t, !chunkCreated, "second sample should use same chunk")
ok, chunkCreated = s.append(1000, 3)
Assert(t, ok, "append failed")
Assert(t, ok, "expected new chunk on boundary")
testutil.Assert(t, ok, "append failed")
testutil.Assert(t, ok, "expected new chunk on boundary")
ok, chunkCreated = s.append(1001, 4)
Assert(t, ok, "append failed")
Assert(t, !chunkCreated, "second sample should use same chunk")
testutil.Assert(t, ok, "append failed")
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")
Assert(t, s.chunks[1].minTime == 1000 && s.chunks[1].maxTime == 1001, "wrong chunk range")
testutil.Assert(t, s.chunks[0].minTime == 998 && s.chunks[0].maxTime == 999, "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
// at approximately 120 samples per chunk.
for i := 1; i < 1000; 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.
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/prometheus/tsdb/chunks"
"github.com/prometheus/tsdb/labels"
"github.com/stretchr/testify/require"
"github.com/prometheus/tsdb/testutil"
)
type series struct {
@ -151,35 +151,35 @@ func (m mockIndex) LabelIndices() ([][]string, error) {
func TestIndexRW_Create_Open(t *testing.T) {
dir, err := ioutil.TempDir("", "test_index_create")
require.NoError(t, err)
testutil.Ok(t, err)
defer os.RemoveAll(dir)
// An empty index must still result in a readable file.
iw, err := newIndexWriter(dir)
require.NoError(t, err, "create index writer")
require.NoError(t, iw.Close(), "close index writer")
testutil.Ok(t, err)
testutil.Ok(t, iw.Close())
ir, err := NewFileIndexReader(filepath.Join(dir, "index"))
require.NoError(t, err, "open index reader")
require.NoError(t, ir.Close(), "close index reader")
testutil.Ok(t, err)
testutil.Ok(t, ir.Close())
// Modify magic header must cause open to fail.
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)
require.NoError(t, err)
testutil.Ok(t, err)
_, err = NewFileIndexReader(dir)
require.Error(t, err)
testutil.NotOk(t, err)
}
func TestIndexRW_Postings(t *testing.T) {
dir, err := ioutil.TempDir("", "test_index_postings")
require.NoError(t, err)
testutil.Ok(t, err)
defer os.RemoveAll(dir)
iw, err := newIndexWriter(dir)
require.NoError(t, err, "create index writer")
testutil.Ok(t, err)
series := []labels.Labels{
labels.FromStrings("a", "1", "b", "1"),
@ -196,25 +196,25 @@ func TestIndexRW_Postings(t *testing.T) {
"3": struct{}{},
"4": struct{}{},
})
require.NoError(t, err)
testutil.Ok(t, err)
// Postings lists are only written if a series with the respective
// reference was added before.
require.NoError(t, iw.AddSeries(1, series[0]))
require.NoError(t, iw.AddSeries(2, series[1]))
require.NoError(t, iw.AddSeries(3, series[2]))
require.NoError(t, iw.AddSeries(4, series[3]))
testutil.Ok(t, iw.AddSeries(1, series[0]))
testutil.Ok(t, iw.AddSeries(2, series[1]))
testutil.Ok(t, iw.AddSeries(3, series[2]))
testutil.Ok(t, iw.AddSeries(4, series[3]))
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"))
require.NoError(t, err, "open index reader")
testutil.Ok(t, err)
p, err := ir.Postings("a", "1")
require.NoError(t, err)
testutil.Ok(t, err)
var l labels.Labels
var c []ChunkMeta
@ -222,22 +222,22 @@ func TestIndexRW_Postings(t *testing.T) {
for i := 0; p.Next(); i++ {
err := ir.Series(p.At(), &l, &c)
require.NoError(t, err)
require.Equal(t, 0, len(c))
require.Equal(t, series[i], l)
testutil.Ok(t, err)
testutil.Equals(t, 0, len(c))
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) {
dir, err := ioutil.TempDir("", "test_persistence_e2e")
require.NoError(t, err)
testutil.Ok(t, err)
defer os.RemoveAll(dir)
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.Sort(labels.Slice(lbls))
@ -271,9 +271,9 @@ func TestPersistence_index_e2e(t *testing.T) {
}
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.
var (
@ -286,7 +286,7 @@ func TestPersistence_index_e2e(t *testing.T) {
for i, s := range input {
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...)
for _, l := range s.labels {
@ -304,8 +304,8 @@ func TestPersistence_index_e2e(t *testing.T) {
for k, v := range values {
vals := v.slice()
require.NoError(t, iw.WriteLabelIndex([]string{k}, vals))
require.NoError(t, mi.WriteLabelIndex([]string{k}, vals))
testutil.Ok(t, iw.WriteLabelIndex([]string{k}, vals))
testutil.Ok(t, mi.WriteLabelIndex([]string{k}, vals))
}
all := make([]uint64, len(lbls))
@ -313,24 +313,24 @@ func TestPersistence_index_e2e(t *testing.T) {
all[i] = uint64(i)
}
err = iw.WritePostings("", "", newListPostings(all))
require.NoError(t, err)
testutil.Ok(t, err)
mi.WritePostings("", "", newListPostings(all))
for l := range postings.m {
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))
}
err = iw.Close()
require.NoError(t, err)
testutil.Ok(t, err)
ir, err := NewFileIndexReader(filepath.Join(dir, "index"))
require.NoError(t, err)
testutil.Ok(t, err)
for p := range mi.postings.m {
gotp, err := ir.Postings(p.Name, p.Value)
require.NoError(t, err)
testutil.Ok(t, err)
expp, err := mi.Postings(p.Name, p.Value)
@ -338,39 +338,39 @@ func TestPersistence_index_e2e(t *testing.T) {
var chks, expchks []ChunkMeta
for gotp.Next() {
require.True(t, expp.Next())
testutil.Assert(t, expp.Next() == true, "")
ref := gotp.At()
err := ir.Series(ref, &lset, &chks)
require.NoError(t, err)
testutil.Ok(t, err)
err = mi.Series(expp.At(), &explset, &expchks)
require.Equal(t, explset, lset)
require.Equal(t, expchks, chks)
testutil.Equals(t, explset, lset)
testutil.Equals(t, expchks, chks)
}
require.False(t, expp.Next())
require.NoError(t, gotp.Err())
testutil.Assert(t, expp.Next() == false, "")
testutil.Ok(t, gotp.Err())
}
for k, v := range mi.labelIndex {
tplsExp, err := newStringTuples(v, 1)
require.NoError(t, err)
testutil.Ok(t, err)
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++ {
strsExp, err := tplsExp.At(i)
require.NoError(t, err)
testutil.Ok(t, err)
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"
promlabels "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse"
"github.com/stretchr/testify/require"
"github.com/prometheus/tsdb/testutil"
)
func TestCompareAndEquals(t *testing.T) {
@ -87,14 +87,14 @@ func TestCompareAndEquals(t *testing.T) {
// Use constructor to ensure sortedness.
a, b := New(c.a...), New(c.b...)
require.Equal(t, c.res, Compare(a, b))
require.Equal(t, c.res == 0, a.Equals(b))
testutil.Equals(t, c.res, Compare(a, b))
testutil.Equals(t, c.res == 0, a.Equals(b))
}
}
func BenchmarkSliceSort(b *testing.B) {
lbls, err := readPrometheusLabels("../testdata/1m.series", 900000)
require.NoError(b, err)
testutil.Ok(b, err)
for len(lbls) < 20e6 {
lbls = append(lbls, lbls...)

View file

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

View file

@ -22,7 +22,7 @@ import (
"github.com/prometheus/tsdb/chunks"
"github.com/prometheus/tsdb/labels"
"github.com/stretchr/testify/require"
"github.com/prometheus/tsdb/testutil"
)
type mockSeriesIterator struct {
@ -190,7 +190,7 @@ Outer:
for {
eok, rok := c.exp.Next(), res.Next()
require.Equal(t, eok, rok, "next")
testutil.Equals(t, eok, rok)
if !eok {
continue Outer
@ -198,13 +198,13 @@ Outer:
sexp := c.exp.At()
sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
}
}
@ -449,7 +449,7 @@ func TestBlockQuerier(t *testing.T) {
}
Outer:
for i, c := range cases.queries {
for _, c := range cases.queries {
ir, cr := createIdxChkReaders(cases.data)
querier := &blockQuerier{
index: ir,
@ -461,11 +461,11 @@ Outer:
}
res, err := querier.Select(c.ms...)
require.NoError(t, err)
testutil.Ok(t, err)
for {
eok, rok := c.exp.Next(), res.Next()
require.Equal(t, eok, rok, "%d: next", i)
testutil.Equals(t, eok, rok)
if !eok {
continue Outer
@ -473,13 +473,13 @@ Outer:
sexp := c.exp.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())
smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "%d: samples error", i)
require.Equal(t, smplExp, smplRes, "%d: samples", i)
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
}
@ -632,11 +632,11 @@ Outer:
}
res, err := querier.Select(c.ms...)
require.NoError(t, err)
testutil.Ok(t, err)
for {
eok, rok := c.exp.Next(), res.Next()
require.Equal(t, eok, rok, "next")
testutil.Equals(t, eok, rok)
if !eok {
continue Outer
@ -644,13 +644,13 @@ Outer:
sexp := c.exp.At()
sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
testutil.Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
}
@ -743,13 +743,13 @@ func TestBaseChunkSeries(t *testing.T) {
idx := tc.expIdxs[i]
require.Equal(t, tc.series[idx].lset, lset)
require.Equal(t, tc.series[idx].chunks, chks)
testutil.Equals(t, tc.series[idx].lset, lset)
testutil.Equals(t, tc.series[idx].chunks, chks)
i++
}
require.Equal(t, len(tc.expIdxs), i)
require.NoError(t, bcs.Err())
testutil.Equals(t, len(tc.expIdxs), i)
testutil.Ok(t, bcs.Err())
}
return
@ -959,8 +959,8 @@ func TestSeriesIterator(t *testing.T) {
smplExp, errExp := expandSeriesIterator(exp)
smplRes, errRes := expandSeriesIterator(res)
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
t.Run("Seek", func(t *testing.T) {
@ -1027,21 +1027,21 @@ func TestSeriesIterator(t *testing.T) {
}
exp := newListSeriesIterator(smplValid)
require.Equal(t, tc.success, res.Seek(tc.seek))
testutil.Equals(t, tc.success, res.Seek(tc.seek))
if tc.success {
// Init the list and then proceed to check.
remaining := exp.Next()
require.True(t, remaining)
testutil.Assert(t, remaining == true, "")
for remaining {
sExp, eExp := exp.At()
sRes, eRes := res.At()
require.Equal(t, eExp, eRes, "samples error")
require.Equal(t, sExp, sRes, "samples")
testutil.Equals(t, eExp, eRes)
testutil.Equals(t, sExp, sRes)
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)
smplRes, errRes := expandSeriesIterator(res)
require.Equal(t, errExp, errRes, "samples error")
require.Equal(t, smplExp, smplRes, "samples")
testutil.Equals(t, errExp, errRes)
testutil.Equals(t, smplExp, smplRes)
}
t.Run("Seek", func(t *testing.T) {
@ -1073,21 +1073,21 @@ func TestSeriesIterator(t *testing.T) {
res := newChainedSeriesIterator(a, b, c)
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 {
// Init the list and then proceed to check.
remaining := exp.Next()
require.True(t, remaining)
testutil.Assert(t, remaining == true, "")
for remaining {
sExp, eExp := exp.At()
sRes, eRes := res.At()
require.Equal(t, eExp, eRes, "samples error")
require.Equal(t, sExp, sRes, "samples")
testutil.Equals(t, eExp, eRes)
testutil.Equals(t, sExp, sRes)
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)
require.True(t, res.Seek(1))
require.True(t, res.Seek(2))
testutil.Assert(t, res.Seek(1) == true, "")
testutil.Assert(t, res.Seek(2) == true, "")
ts, v := res.At()
require.Equal(t, int64(2), ts)
require.Equal(t, float64(2), v)
testutil.Equals(t, int64(2), ts)
testutil.Equals(t, float64(2), v)
}
// 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)
require.True(t, it.Next())
testutil.Assert(t, it.Next() == true, "")
ts, v := it.At()
require.Equal(t, int64(1), ts)
require.Equal(t, float64(2), v)
testutil.Equals(t, int64(1), ts)
testutil.Equals(t, float64(2), v)
require.True(t, it.Seek(4))
testutil.Assert(t, it.Seek(4) == true, "")
ts, v = it.At()
require.Equal(t, int64(5), ts)
require.Equal(t, float64(6), v)
testutil.Equals(t, int64(5), ts)
testutil.Equals(t, float64(6), v)
}
// 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)
require.False(t, it.Next())
testutil.Assert(t, it.Next() == false, "")
}
func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
@ -1173,11 +1173,11 @@ func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
maxt: 0,
}
require.False(t, p.Next())
testutil.Assert(t, p.Next() == false, "")
p.mint = 6
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.
chunkMetas = [][]ChunkMeta{
@ -1194,7 +1194,7 @@ func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
mint: 10,
maxt: 15,
}
require.False(t, p.Next())
testutil.Assert(t, p.Next() == false, "")
return
}
@ -1246,7 +1246,7 @@ func BenchmarkMergedSeriesSet(b *testing.B) {
for _, j := range []int{1, 2, 4, 8, 16, 32} {
b.Run(fmt.Sprintf("series=%d,blocks=%d", k, j), func(b *testing.B) {
lbls, err := readPrometheusLabels("testdata/1m.series", k)
require.NoError(b, err)
testutil.Ok(b, err)
sort.Sort(labels.Slice(lbls))
@ -1272,8 +1272,8 @@ func BenchmarkMergedSeriesSet(b *testing.B) {
for ms.Next() {
i++
}
require.NoError(b, ms.Err())
require.Equal(b, len(lbls), i)
testutil.Ok(b, ms.Err())
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
// SOFTWARE.
package tsdb
package testutil
import (
"fmt"

View file

@ -20,7 +20,7 @@ import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/prometheus/tsdb/testutil"
)
func TestWriteAndReadbackTombStones(t *testing.T) {
@ -43,13 +43,13 @@ func TestWriteAndReadbackTombStones(t *testing.T) {
stones[ref] = dranges
}
require.NoError(t, writeTombstoneFile(tmpdir, stones))
testutil.Ok(t, writeTombstoneFile(tmpdir, stones))
restr, err := readTombstones(tmpdir)
require.NoError(t, err)
testutil.Ok(t, err)
// Compare the two readers.
require.Equal(t, stones, restr)
testutil.Equals(t, stones, restr)
}
func TestAddingNewIntervals(t *testing.T) {
@ -117,7 +117,7 @@ func TestAddingNewIntervals(t *testing.T) {
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
}

View file

@ -5,7 +5,7 @@ import (
"sort"
"testing"
"github.com/stretchr/testify/require"
"github.com/prometheus/tsdb/testutil"
)
func TestSampleRing(t *testing.T) {
@ -79,12 +79,12 @@ func TestBufferedSeriesIterator(t *testing.T) {
t, v := bit.At()
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) {
ts, v := it.At()
require.Equal(t, ets, ts, "timestamp mismatch")
require.Equal(t, ev, v, "value mismatch")
testutil.Equals(t, ets, ts)
testutil.Equals(t, ev, v)
}
it = NewBuffer(newListSeriesIterator([]sample{
@ -98,29 +98,29 @@ func TestBufferedSeriesIterator(t *testing.T) {
{t: 101, v: 10},
}), 2)
require.True(t, it.Seek(-123), "seek failed")
testutil.Assert(t, it.Seek(-123) == true, "seek failed")
sampleEq(1, 2)
bufferEq(nil)
require.True(t, it.Next(), "next failed")
testutil.Assert(t, it.Next() == true, "next failed")
sampleEq(2, 3)
bufferEq([]sample{{t: 1, v: 2}})
require.True(t, it.Next(), "next failed")
require.True(t, it.Next(), "next failed")
require.True(t, it.Next(), "next failed")
testutil.Assert(t, it.Next() == true, "next failed")
testutil.Assert(t, it.Next() == true, "next failed")
testutil.Assert(t, it.Next() == true, "next failed")
sampleEq(5, 6)
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)
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)
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 {

View file

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