mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 05:34:05 -08:00
use test utils in head_test
This commit is contained in:
parent
ae8bb5fd16
commit
5aa2af255b
123
head_test.go
123
head_test.go
|
@ -26,16 +26,15 @@ import (
|
|||
|
||||
promlabels "github.com/prometheus/prometheus/pkg/labels"
|
||||
"github.com/prometheus/prometheus/pkg/textparse"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func BenchmarkCreateSeries(b *testing.B) {
|
||||
lbls, err := readPrometheusLabels("testdata/all.series", b.N)
|
||||
require.NoError(b, err)
|
||||
Ok(b, err)
|
||||
|
||||
h, err := NewHead(nil, nil, nil, 10000)
|
||||
if err != nil {
|
||||
require.NoError(b, err)
|
||||
Ok(b, err)
|
||||
}
|
||||
defer h.Close()
|
||||
|
||||
|
@ -131,40 +130,40 @@ func TestHead_ReadWAL(t *testing.T) {
|
|||
wal := &memoryWAL{entries: entries}
|
||||
|
||||
head, err := NewHead(nil, nil, wal, 1000)
|
||||
require.NoError(t, err)
|
||||
Ok(t, err)
|
||||
defer head.Close()
|
||||
|
||||
require.NoError(t, head.ReadWAL())
|
||||
require.Equal(t, uint64(100), head.lastSeriesID)
|
||||
Ok(t, head.ReadWAL())
|
||||
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)
|
||||
Equals(t, labels.FromStrings("a", "1"), s10.lset)
|
||||
Equals(t, labels.FromStrings("a", "2"), s11.lset)
|
||||
Equals(t, labels.FromStrings("a", "4"), s50.lset)
|
||||
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())
|
||||
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)))
|
||||
Equals(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0)))
|
||||
Equals(t, 0, len(s11.chunks))
|
||||
Equals(t, []sample{{101, 6}}, expandChunk(s50.iterator(0)))
|
||||
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)
|
||||
Ok(t, err)
|
||||
defer h.Close()
|
||||
|
||||
h.initTime(0)
|
||||
|
@ -191,21 +190,21 @@ func TestHead_Truncate(t *testing.T) {
|
|||
s4.chunks = []*memChunk{}
|
||||
|
||||
// Truncation need not be aligned.
|
||||
require.NoError(t, h.Truncate(1))
|
||||
Ok(t, h.Truncate(1))
|
||||
|
||||
h.Truncate(2000)
|
||||
|
||||
require.Equal(t, []*memChunk{
|
||||
Equals(t, []*memChunk{
|
||||
{minTime: 2000, maxTime: 2999},
|
||||
}, h.series.getByID(s1.ref).chunks)
|
||||
|
||||
require.Equal(t, []*memChunk{
|
||||
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))
|
||||
Nil(t, h.series.getByID(s3.ref), "")
|
||||
Nil(t, h.series.getByID(s4.ref), "")
|
||||
|
||||
postingsA1, _ := expandPostings(h.postings.get("a", "1"))
|
||||
postingsA2, _ := expandPostings(h.postings.get("a", "2"))
|
||||
|
@ -214,14 +213,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)
|
||||
Equals(t, []uint64{s1.ref}, postingsA1)
|
||||
Equals(t, []uint64{s2.ref}, postingsA2)
|
||||
Equals(t, []uint64{s1.ref, s2.ref}, postingsB1)
|
||||
Equals(t, []uint64{s1.ref, s2.ref}, postingsAll)
|
||||
Nil(t, postingsB2, "")
|
||||
Nil(t, postingsC1, "")
|
||||
|
||||
require.Equal(t, map[string]struct{}{
|
||||
Equals(t, map[string]struct{}{
|
||||
"": struct{}{}, // from 'all' postings list
|
||||
"a": struct{}{},
|
||||
"b": struct{}{},
|
||||
|
@ -229,7 +228,7 @@ func TestHead_Truncate(t *testing.T) {
|
|||
"2": struct{}{},
|
||||
}, h.symbols)
|
||||
|
||||
require.Equal(t, map[string]stringset{
|
||||
Equals(t, map[string]stringset{
|
||||
"a": stringset{"1": struct{}{}, "2": struct{}{}},
|
||||
"b": stringset{"1": struct{}{}},
|
||||
"": stringset{"": struct{}{}},
|
||||
|
@ -243,7 +242,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")
|
||||
Assert(t, ok == true, "sample append failed")
|
||||
}
|
||||
|
||||
// Check that truncate removes half of the chunks and afterwards
|
||||
|
@ -252,32 +251,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)
|
||||
NotNil(t, s.chunk(0), "")
|
||||
NotNil(t, lastChunk, "")
|
||||
|
||||
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")
|
||||
Equals(t, int64(2000), s.chunks[0].minTime)
|
||||
Nil(t, s.chunk(0), "first chunks not gone")
|
||||
Equals(t, countBefore/2, len(s.chunks))
|
||||
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")
|
||||
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")
|
||||
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)
|
||||
Ok(t, err)
|
||||
defer head.Close()
|
||||
|
||||
app := head.Appender()
|
||||
|
@ -288,7 +287,7 @@ func TestHeadDeleteSimple(t *testing.T) {
|
|||
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
|
||||
}
|
||||
|
||||
require.NoError(t, app.Commit())
|
||||
Ok(t, app.Commit())
|
||||
cases := []struct {
|
||||
intervals Intervals
|
||||
remaint []int64
|
||||
|
@ -322,14 +321,14 @@ Outer:
|
|||
|
||||
// Delete the ranges.
|
||||
for _, r := range c.intervals {
|
||||
require.NoError(t, head.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b")))
|
||||
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)
|
||||
Ok(t, err)
|
||||
res, err := q.Select(labels.NewEqualMatcher("a", "b"))
|
||||
require.NoError(t, err)
|
||||
Ok(t, err)
|
||||
|
||||
expSamples := make([]sample, 0, len(c.remaint))
|
||||
for _, ts := range c.remaint {
|
||||
|
@ -341,13 +340,13 @@ Outer:
|
|||
})
|
||||
|
||||
if len(expSamples) == 0 {
|
||||
require.False(t, res.Next())
|
||||
Assert(t, res.Next() == false, "")
|
||||
continue
|
||||
}
|
||||
|
||||
for {
|
||||
eok, rok := expss.Next(), res.Next()
|
||||
require.Equal(t, eok, rok, "next")
|
||||
Equals(t, eok, rok)
|
||||
|
||||
if !eok {
|
||||
continue Outer
|
||||
|
@ -355,13 +354,13 @@ Outer:
|
|||
sexp := expss.At()
|
||||
sres := res.At()
|
||||
|
||||
require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
|
||||
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")
|
||||
Equals(t, errExp, errRes)
|
||||
Equals(t, smplExp, smplRes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -381,12 +380,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")))
|
||||
// Ok(t, app.Commit())
|
||||
// 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())
|
||||
// Ok(t, err)
|
||||
// Ok(t, app.Commit())
|
||||
|
||||
// q := hb.Querier(0, 100000)
|
||||
// res := q.Select(labels.NewEqualMatcher("a", "b"))
|
||||
|
@ -395,8 +394,8 @@ Outer:
|
|||
// exps := res.At()
|
||||
// it := exps.Iterator()
|
||||
// ressmpls, err := expandSeriesIterator(it)
|
||||
// require.NoError(t, err)
|
||||
// require.Equal(t, []sample{{11, 1}}, ressmpls)
|
||||
// Ok(t, err)
|
||||
// Equals(t, []sample{{11, 1}}, ressmpls)
|
||||
// }
|
||||
|
||||
// func TestDelete_e2e(t *testing.T) {
|
||||
|
@ -473,9 +472,9 @@ Outer:
|
|||
|
||||
// _, err := app.Add(ls, ts, v)
|
||||
// if ts >= minTime && ts <= maxTime {
|
||||
// require.NoError(t, err)
|
||||
// Ok(t, err)
|
||||
// } else {
|
||||
// require.EqualError(t, err, ErrOutOfBounds.Error())
|
||||
// EqualsError(t, err, ErrOutOfBounds.Error())
|
||||
// }
|
||||
|
||||
// ts += rand.Int63n(timeInterval) + 1
|
||||
|
@ -484,7 +483,7 @@ Outer:
|
|||
// seriesMap[labels.New(l...).String()] = series
|
||||
// }
|
||||
|
||||
// require.NoError(t, app.Commit())
|
||||
// Ok(t, app.Commit())
|
||||
|
||||
// // Delete a time-range from each-selector.
|
||||
// dels := []struct {
|
||||
|
@ -519,7 +518,7 @@ Outer:
|
|||
// hb.tombstones = newEmptyTombstoneReader()
|
||||
|
||||
// for _, r := range del.drange {
|
||||
// require.NoError(t, hb.Delete(r.Mint, r.Maxt, del.ms...))
|
||||
// Ok(t, hb.Delete(r.Mint, r.Maxt, del.ms...))
|
||||
// }
|
||||
|
||||
// matched := labels.Slice{}
|
||||
|
@ -570,7 +569,7 @@ Outer:
|
|||
// }
|
||||
// }
|
||||
// }
|
||||
// require.Equal(t, eok, rok, "next")
|
||||
// Equals(t, eok, rok, "next")
|
||||
|
||||
// if !eok {
|
||||
// break
|
||||
|
@ -578,13 +577,13 @@ Outer:
|
|||
// sexp := expSs.At()
|
||||
// sres := ss.At()
|
||||
|
||||
// require.Equal(t, sexp.Labels(), sres.Labels(), "labels")
|
||||
// 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")
|
||||
// Equals(t, errExp, errRes, "samples error")
|
||||
// Equals(t, smplExp, smplRes, "samples")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
|
Loading…
Reference in a new issue