Skip to content

Commit

Permalink
add tests for all stores
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <yb532204897@gmail.com>
  • Loading branch information
yeya24 committed Dec 18, 2019
1 parent b36d6e9 commit e5cca30
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 3 deletions.
45 changes: 42 additions & 3 deletions pkg/store/bucket_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {

// TODO(bwplotka): Add those test cases to TSDB querier_test.go as well, there are no tests for matching.
for i, tcase := range []struct {
req *storepb.SeriesRequest
expected [][]storepb.Label
req *storepb.SeriesRequest
expected [][]storepb.Label
expectedChunkLen int
}{
{
req: &storepb.SeriesRequest{
Expand All @@ -183,7 +184,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -202,7 +205,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -217,7 +222,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -232,7 +239,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -251,7 +260,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -270,7 +281,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "2"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -285,7 +298,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "c", Value: "1"}, {Name: "ext2", Value: "value2"}},
{{Name: "a", Value: "1"}, {Name: "c", Value: "2"}, {Name: "ext2", Value: "value2"}},
Expand All @@ -299,6 +314,7 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
},
{
Expand All @@ -308,7 +324,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -323,7 +341,9 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
expectedChunkLen: 3,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
Expand All @@ -345,6 +365,25 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {
},
MinTime: mint,
MaxTime: maxt,
NoChunk: false,
},
},
// test no-chunk option
{
req: &storepb.SeriesRequest{
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "a", Value: "1"},
},
MinTime: mint,
MaxTime: maxt,
NoChunk: true,
},
expectedChunkLen: 0,
expected: [][]storepb.Label{
{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "b", Value: "2"}, {Name: "ext1", Value: "value1"}},
{{Name: "a", Value: "1"}, {Name: "c", Value: "1"}, {Name: "ext2", Value: "value2"}},
{{Name: "a", Value: "1"}, {Name: "c", Value: "2"}, {Name: "ext2", Value: "value2"}},
},
},
} {
Expand All @@ -357,7 +396,7 @@ func testBucketStore_e2e(t testing.TB, ctx context.Context, s *storeSuite) {

for i, s := range srv.SeriesSet {
testutil.Equals(t, tcase.expected[i], s.Labels)
testutil.Equals(t, 3, len(s.Chunks))
testutil.Equals(t, tcase.expectedChunkLen, len(s.Chunks))
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/store/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"context"
"fmt"
"io"
"math"
"os"
Expand Down Expand Up @@ -106,6 +107,7 @@ func TestProxyStore_Series(t *testing.T) {
MinTime: 301,
MaxTime: 400,
Matchers: []storepb.LabelMatcher{{Name: "a", Value: "a", Type: storepb.LabelMatcher_EQ}},
NoChunk: false,
},
expectedWarningsLen: 1, // No store matched for this query.
},
Expand All @@ -127,6 +129,7 @@ func TestProxyStore_Series(t *testing.T) {
MinTime: 1,
MaxTime: 300,
Matchers: []storepb.LabelMatcher{{Name: "ext", Value: "2", Type: storepb.LabelMatcher_EQ}},
NoChunk: false,
},
expectedWarningsLen: 1, // No store matched for this query.
},
Expand All @@ -148,6 +151,7 @@ func TestProxyStore_Series(t *testing.T) {
MinTime: 1,
MaxTime: 300,
Matchers: []storepb.LabelMatcher{{Name: "ext", Value: "1", Type: storepb.LabelMatcher_EQ}},
NoChunk: false,
},
expectedSeries: []rawSeries{
{
Expand All @@ -173,6 +177,7 @@ func TestProxyStore_Series(t *testing.T) {
MinTime: 1,
MaxTime: 300,
Matchers: []storepb.LabelMatcher{{Name: "ext", Value: "1", Type: storepb.LabelMatcher_EQ}},
NoChunk: false,
},
expectedSeries: []rawSeries{
{
Expand All @@ -199,6 +204,7 @@ func TestProxyStore_Series(t *testing.T) {
MinTime: 1,
MaxTime: 300,
Matchers: []storepb.LabelMatcher{{Name: "ext", Value: "1", Type: storepb.LabelMatcher_EQ}},
NoChunk: false,
},
},
{
Expand All @@ -218,6 +224,7 @@ func TestProxyStore_Series(t *testing.T) {
MinTime: 1,
MaxTime: 300,
Matchers: []storepb.LabelMatcher{{Name: "a", Value: "b", Type: storepb.LabelMatcher_EQ}},
NoChunk: false,
},
expectedSeries: []rawSeries{
{
Expand Down Expand Up @@ -284,6 +291,7 @@ func TestProxyStore_Series(t *testing.T) {
MinTime: 1,
MaxTime: 300,
Matchers: []storepb.LabelMatcher{{Name: "ext", Value: "1", Type: storepb.LabelMatcher_EQ}},
NoChunk: false,
},
expectedSeries: []rawSeries{
{
Expand Down Expand Up @@ -408,6 +416,32 @@ func TestProxyStore_Series(t *testing.T) {
},
expectedErr: errors.New("fetch series for [name:\"ext\" value:\"1\" ] test: error!"),
},
{
title: "use no chunk to only get labels",
storeAPIs: []Client{
&testClient{
StoreClient: &mockedStoreAPI{
RespSeries: []*storepb.SeriesResponse{
storeSeriesResponse(t, labels.FromStrings("a", "a")),
},
},
minTime: 1,
maxTime: 300,
labelSets: []storepb.LabelSet{{Labels: []storepb.Label{{Name: "ext", Value: "1"}}}},
},
},
req: &storepb.SeriesRequest{
MinTime: 1,
MaxTime: 300,
Matchers: []storepb.LabelMatcher{{Name: "ext", Value: "1", Type: storepb.LabelMatcher_EQ}},
NoChunk: true,
},
expectedSeries: []rawSeries{
{
lset: []storepb.Label{{Name: "a", Value: "a"}},
},
},
},
} {

if ok := t.Run(tc.title, func(t *testing.T) {
Expand All @@ -420,6 +454,9 @@ func TestProxyStore_Series(t *testing.T) {

s := newStoreSeriesServer(context.Background())

if tc.req.NoChunk == true {
fmt.Println(1)
}
err := q.Series(tc.req, s)
if tc.expectedErr != nil {
testutil.NotOk(t, err)
Expand Down
131 changes: 131 additions & 0 deletions pkg/store/tsdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,137 @@ func TestTSDBStore_Info(t *testing.T) {
testutil.Equals(t, int64(math.MaxInt64), resp.MaxTime)
}

func TestTSDBStore_Series(t *testing.T) {
defer leaktest.CheckTimeout(t, 10*time.Second)()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

db, err := testutil.NewTSDB()
defer func() { testutil.Ok(t, db.Close()) }()
testutil.Ok(t, err)

tsdbStore := NewTSDBStore(nil, nil, db, component.Rule, labels.FromStrings("region", "eu-west"))

appender := db.Appender()

for i := 1; i <= 3; i++ {
_, err = appender.Add(labels.FromStrings("a", "1"), int64(i), float64(i))
testutil.Ok(t, err)
}
err = appender.Commit()
testutil.Ok(t, err)

for _, tc := range []struct {
title string
req *storepb.SeriesRequest
expectedSeries []rawSeries
expectedError string
}{
{
title: "total match series",
req: &storepb.SeriesRequest{
MinTime: 1,
MaxTime: 3,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "a", Value: "1"},
},
NoChunk: false,
},
expectedSeries: []rawSeries{
{
lset: []storepb.Label{{Name: "a", Value: "1"}, {Name: "region", Value: "eu-west"}},
chunks: [][]sample{{{1, 1}, {2, 2}, {3, 3}}},
},
},
},
{
title: "partially match time range series",
req: &storepb.SeriesRequest{
MinTime: 1,
MaxTime: 2,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "a", Value: "1"},
},
NoChunk: false,
},
expectedSeries: []rawSeries{
{
lset: []storepb.Label{{Name: "a", Value: "1"}, {Name: "region", Value: "eu-west"}},
chunks: [][]sample{{{1, 1}, {2, 2}}},
},
},
},
{
title: "dont't match time range series",
req: &storepb.SeriesRequest{
MinTime: 4,
MaxTime: 6,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "a", Value: "1"},
},
NoChunk: false,
},
expectedSeries: []rawSeries{},
},
{
title: "only match external label",
req: &storepb.SeriesRequest{
MinTime: 1,
MaxTime: 3,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "region", Value: "eu-west"},
},
NoChunk: false,
},
expectedError: "rpc error: code = InvalidArgument desc = no matchers specified (excluding external labels)",
},
{
title: "dont't match labels",
req: &storepb.SeriesRequest{
MinTime: 1,
MaxTime: 3,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "b", Value: "1"},
},
NoChunk: false,
},
expectedSeries: []rawSeries{},
},
{
title: "no chunk",
req: &storepb.SeriesRequest{
MinTime: 1,
MaxTime: 3,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "a", Value: "1"},
},
NoChunk: true,
},
expectedSeries: []rawSeries{
{
lset: []storepb.Label{{Name: "a", Value: "1"}, {Name: "region", Value: "eu-west"}},
},
},
},
} {
if ok := t.Run(tc.title, func(t *testing.T) {
srv := newStoreSeriesServer(ctx)
err := tsdbStore.Series(tc.req, srv)
if len(tc.expectedError) > 0 {
testutil.NotOk(t, err)
testutil.Equals(t, tc.expectedError, err.Error())
} else {
testutil.Ok(t, err)
seriesEquals(t, tc.expectedSeries, srv.SeriesSet)
}
}); !ok {
return
}
}

}

func TestTSDBStore_LabelNames(t *testing.T) {
var err error
defer leaktest.CheckTimeout(t, 10*time.Second)()
Expand Down

0 comments on commit e5cca30

Please sign in to comment.