Skip to content

Commit 8b155ff

Browse files
committed
add more tests in ut
Signed-off-by: Ben Ye <[email protected]>
1 parent adf8965 commit 8b155ff

File tree

7 files changed

+847
-766
lines changed

7 files changed

+847
-766
lines changed

integration/querier_remote_read_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integration
66
import (
77
"bytes"
88
"context"
9+
"fmt"
910
"io"
1011
"net/http"
1112
"strconv"
@@ -26,21 +27,14 @@ import (
2627
)
2728

2829
func TestQuerierRemoteRead(t *testing.T) {
29-
tests := map[string]struct {
30-
thanosEngine bool
31-
}{
32-
"old engine": {thanosEngine: false},
33-
"new engine": {thanosEngine: true},
34-
}
35-
36-
for testName, tc := range tests {
37-
t.Run(testName, func(t *testing.T) {
30+
for _, thanosEngine := range []bool{false, true} {
31+
t.Run(fmt.Sprintf("thanosEngine=%t", thanosEngine), func(t *testing.T) {
3832
s, err := e2e.NewScenario(networkName)
3933
require.NoError(t, err)
4034
defer s.Close()
4135

4236
flags := mergeFlags(BlocksStorageFlags(), map[string]string{
43-
"-querier.thanos-engine": strconv.FormatBool(tc.thanosEngine),
37+
"-querier.thanos-engine": strconv.FormatBool(thanosEngine),
4438
})
4539

4640
// Start dependencies.

integration/querier_test.go

Lines changed: 307 additions & 331 deletions
Large diffs are not rendered by default.

integration/ruler_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,8 @@ func TestRulerAPI(t *testing.T) {
3939
)
4040
ruleGroup := createTestRuleGroup(t)
4141

42-
tests := map[string]struct {
43-
thanosEngine bool
44-
}{
45-
"old engine": {thanosEngine: false},
46-
"new engine": {thanosEngine: true},
47-
}
48-
for testName, tc := range tests {
49-
t.Run(testName, func(t *testing.T) {
42+
for _, thanosEngine := range []bool{false, true} {
43+
t.Run(fmt.Sprintf("thanosEngine=%t", thanosEngine), func(t *testing.T) {
5044
s, err := e2e.NewScenario(networkName)
5145
require.NoError(t, err)
5246
defer s.Close()
@@ -57,7 +51,7 @@ func TestRulerAPI(t *testing.T) {
5751
require.NoError(t, s.StartAndWaitReady(consul, minio))
5852

5953
flags := mergeFlags(BlocksStorageFlags(), RulerFlags(), map[string]string{
60-
"-querier.thanos-engine": strconv.FormatBool(tc.thanosEngine),
54+
"-querier.thanos-engine": strconv.FormatBool(thanosEngine),
6155
})
6256

6357
// Start Cortex components.

pkg/querier/blocks_store_queryable_test.go

Lines changed: 103 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ import (
2222
"github.com/prometheus/prometheus/promql"
2323
"github.com/prometheus/prometheus/storage"
2424
"github.com/prometheus/prometheus/tsdb/chunkenc"
25+
v1 "github.com/prometheus/prometheus/web/api/v1"
2526
"github.com/stretchr/testify/assert"
2627
"github.com/stretchr/testify/mock"
2728
"github.com/stretchr/testify/require"
29+
"github.com/thanos-community/promql-engine/engine"
30+
"github.com/thanos-community/promql-engine/logicalplan"
2831
"github.com/thanos-io/thanos/pkg/store/hintspb"
2932
"github.com/thanos-io/thanos/pkg/store/labelpb"
3033
"github.com/thanos-io/thanos/pkg/store/storepb"
@@ -1319,6 +1322,13 @@ func TestBlocksStoreQuerier_SelectSortedShouldHonorQueryStoreAfter(t *testing.T)
13191322
}
13201323

13211324
func TestBlocksStoreQuerier_PromQLExecution(t *testing.T) {
1325+
logger := log.NewNopLogger()
1326+
opts := promql.EngineOpts{
1327+
Logger: logger,
1328+
Timeout: 10 * time.Second,
1329+
MaxSamples: 1e6,
1330+
}
1331+
13221332
block1 := ulid.MustNew(1, nil)
13231333
block2 := ulid.MustNew(2, nil)
13241334
series1 := []labelpb.ZLabel{{Name: "__name__", Value: "metric_1"}}
@@ -1341,103 +1351,109 @@ func TestBlocksStoreQuerier_PromQLExecution(t *testing.T) {
13411351
{T: 1589760015000, V: 2},
13421352
{T: 1589760030000, V: 2},
13431353
}
1354+
for _, thanosEngine := range []bool{false, true} {
1355+
var queryEngine v1.QueryEngine
1356+
if thanosEngine {
1357+
queryEngine = engine.New(engine.Opts{
1358+
EngineOpts: opts,
1359+
LogicalOptimizers: logicalplan.AllOptimizers,
1360+
})
1361+
} else {
1362+
queryEngine = promql.NewEngine(opts)
1363+
}
13441364

1345-
// Mock the finder to simulate we need to query two blocks.
1346-
finder := &blocksFinderMock{
1347-
Service: services.NewIdleService(nil, nil),
1348-
}
1349-
finder.On("GetBlocks", mock.Anything, "user-1", mock.Anything, mock.Anything).Return(bucketindex.Blocks{
1350-
{ID: block1},
1351-
{ID: block2},
1352-
}, map[ulid.ULID]*bucketindex.BlockDeletionMark(nil), error(nil))
1353-
1354-
// Mock the store to simulate each block is queried from a different store-gateway.
1355-
gateway1 := &storeGatewayClientMock{remoteAddr: "1.1.1.1", mockedSeriesResponses: []*storepb.SeriesResponse{
1356-
{
1357-
Result: &storepb.SeriesResponse_Series{
1358-
Series: &storepb.Series{
1359-
Labels: series1,
1360-
Chunks: []storepb.AggrChunk{
1361-
createAggrChunkWithSamples(series1Samples[:3]...), // First half.
1365+
t.Run(fmt.Sprintf("thanos engine enabled=%t", thanosEngine), func(t *testing.T) {
1366+
// Mock the finder to simulate we need to query two blocks.
1367+
finder := &blocksFinderMock{
1368+
Service: services.NewIdleService(nil, nil),
1369+
}
1370+
finder.On("GetBlocks", mock.Anything, "user-1", mock.Anything, mock.Anything).Return(bucketindex.Blocks{
1371+
{ID: block1},
1372+
{ID: block2},
1373+
}, map[ulid.ULID]*bucketindex.BlockDeletionMark(nil), error(nil))
1374+
1375+
// Mock the store to simulate each block is queried from a different store-gateway.
1376+
gateway1 := &storeGatewayClientMock{remoteAddr: "1.1.1.1", mockedSeriesResponses: []*storepb.SeriesResponse{
1377+
{
1378+
Result: &storepb.SeriesResponse_Series{
1379+
Series: &storepb.Series{
1380+
Labels: series1,
1381+
Chunks: []storepb.AggrChunk{
1382+
createAggrChunkWithSamples(series1Samples[:3]...), // First half.
1383+
},
1384+
},
13621385
},
1363-
},
1364-
},
1365-
}, {
1366-
Result: &storepb.SeriesResponse_Series{
1367-
Series: &storepb.Series{
1368-
Labels: series2,
1369-
Chunks: []storepb.AggrChunk{
1370-
createAggrChunkWithSamples(series2Samples[:3]...),
1386+
}, {
1387+
Result: &storepb.SeriesResponse_Series{
1388+
Series: &storepb.Series{
1389+
Labels: series2,
1390+
Chunks: []storepb.AggrChunk{
1391+
createAggrChunkWithSamples(series2Samples[:3]...),
1392+
},
1393+
},
13711394
},
13721395
},
1373-
},
1374-
},
1375-
mockHintsResponse(block1),
1376-
}}
1377-
1378-
gateway2 := &storeGatewayClientMock{remoteAddr: "2.2.2.2", mockedSeriesResponses: []*storepb.SeriesResponse{
1379-
{
1380-
Result: &storepb.SeriesResponse_Series{
1381-
Series: &storepb.Series{
1382-
Labels: series1,
1383-
Chunks: []storepb.AggrChunk{
1384-
createAggrChunkWithSamples(series1Samples[3:]...), // Second half.
1396+
mockHintsResponse(block1),
1397+
}}
1398+
1399+
gateway2 := &storeGatewayClientMock{remoteAddr: "2.2.2.2", mockedSeriesResponses: []*storepb.SeriesResponse{
1400+
{
1401+
Result: &storepb.SeriesResponse_Series{
1402+
Series: &storepb.Series{
1403+
Labels: series1,
1404+
Chunks: []storepb.AggrChunk{
1405+
createAggrChunkWithSamples(series1Samples[3:]...), // Second half.
1406+
},
1407+
},
1408+
},
1409+
}, {
1410+
Result: &storepb.SeriesResponse_Series{
1411+
Series: &storepb.Series{
1412+
Labels: series2,
1413+
Chunks: []storepb.AggrChunk{
1414+
createAggrChunkWithSamples(series2Samples[3:]...),
1415+
},
1416+
},
13851417
},
13861418
},
1387-
},
1388-
}, {
1389-
Result: &storepb.SeriesResponse_Series{
1390-
Series: &storepb.Series{
1391-
Labels: series2,
1392-
Chunks: []storepb.AggrChunk{
1393-
createAggrChunkWithSamples(series2Samples[3:]...),
1419+
mockHintsResponse(block2),
1420+
}}
1421+
1422+
stores := &blocksStoreSetMock{
1423+
Service: services.NewIdleService(nil, nil),
1424+
mockedResponses: []interface{}{
1425+
map[BlocksStoreClient][]ulid.ULID{
1426+
gateway1: {block1},
1427+
gateway2: {block2},
13941428
},
13951429
},
1396-
},
1397-
},
1398-
mockHintsResponse(block2),
1399-
}}
1430+
}
14001431

1401-
stores := &blocksStoreSetMock{
1402-
Service: services.NewIdleService(nil, nil),
1403-
mockedResponses: []interface{}{
1404-
map[BlocksStoreClient][]ulid.ULID{
1405-
gateway1: {block1},
1406-
gateway2: {block2},
1407-
},
1408-
},
1432+
// Instance the querier that will be executed to run the query.
1433+
queryable, err := NewBlocksStoreQueryable(stores, finder, NewBlocksConsistencyChecker(0, 0, logger, nil), &blocksStoreLimitsMock{}, 0, logger, nil)
1434+
require.NoError(t, err)
1435+
require.NoError(t, services.StartAndAwaitRunning(context.Background(), queryable))
1436+
defer services.StopAndAwaitTerminated(context.Background(), queryable) // nolint:errcheck
1437+
1438+
// Run a query.
1439+
q, err := queryEngine.NewRangeQuery(queryable, nil, `{__name__=~"metric.*"}`, time.Unix(1589759955, 0), time.Unix(1589760030, 0), 15*time.Second)
1440+
require.NoError(t, err)
1441+
1442+
ctx := user.InjectOrgID(context.Background(), "user-1")
1443+
res := q.Exec(ctx)
1444+
require.NoError(t, err)
1445+
require.NoError(t, res.Err)
1446+
1447+
matrix, err := res.Matrix()
1448+
require.NoError(t, err)
1449+
require.Len(t, matrix, 2)
1450+
1451+
assert.Equal(t, labelpb.ZLabelsToPromLabels(series1), matrix[0].Metric)
1452+
assert.Equal(t, labelpb.ZLabelsToPromLabels(series2), matrix[1].Metric)
1453+
assert.Equal(t, series1Samples, matrix[0].Points)
1454+
assert.Equal(t, series2Samples, matrix[1].Points)
1455+
})
14091456
}
1410-
1411-
// Instance the querier that will be executed to run the query.
1412-
logger := log.NewNopLogger()
1413-
queryable, err := NewBlocksStoreQueryable(stores, finder, NewBlocksConsistencyChecker(0, 0, logger, nil), &blocksStoreLimitsMock{}, 0, logger, nil)
1414-
require.NoError(t, err)
1415-
require.NoError(t, services.StartAndAwaitRunning(context.Background(), queryable))
1416-
defer services.StopAndAwaitTerminated(context.Background(), queryable) // nolint:errcheck
1417-
1418-
engine := promql.NewEngine(promql.EngineOpts{
1419-
Logger: logger,
1420-
Timeout: 10 * time.Second,
1421-
MaxSamples: 1e6,
1422-
})
1423-
1424-
// Run a query.
1425-
q, err := engine.NewRangeQuery(queryable, nil, `{__name__=~"metric.*"}`, time.Unix(1589759955, 0), time.Unix(1589760030, 0), 15*time.Second)
1426-
require.NoError(t, err)
1427-
1428-
ctx := user.InjectOrgID(context.Background(), "user-1")
1429-
res := q.Exec(ctx)
1430-
require.NoError(t, err)
1431-
require.NoError(t, res.Err)
1432-
1433-
matrix, err := res.Matrix()
1434-
require.NoError(t, err)
1435-
require.Len(t, matrix, 2)
1436-
1437-
assert.Equal(t, labelpb.ZLabelsToPromLabels(series1), matrix[0].Metric)
1438-
assert.Equal(t, labelpb.ZLabelsToPromLabels(series2), matrix[1].Metric)
1439-
assert.Equal(t, series1Samples, matrix[0].Points)
1440-
assert.Equal(t, series2Samples, matrix[1].Points)
14411457
}
14421458

14431459
type blocksStoreSetMock struct {

pkg/querier/chunk_store_queryable_test.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/go-kit/log"
1011
"github.com/prometheus/common/model"
1112
"github.com/prometheus/prometheus/model/labels"
13+
"github.com/prometheus/prometheus/promql"
14+
v1 "github.com/prometheus/prometheus/web/api/v1"
1215
"github.com/stretchr/testify/require"
16+
"github.com/thanos-community/promql-engine/engine"
17+
"github.com/thanos-community/promql-engine/logicalplan"
1318

1419
"github.com/cortexproject/cortex/pkg/chunk"
1520
promchunk "github.com/cortexproject/cortex/pkg/chunk/encoding"
@@ -19,14 +24,31 @@ import (
1924
var _ SeriesWithChunks = &chunkSeries{}
2025

2126
func TestChunkQueryable(t *testing.T) {
22-
for _, testcase := range testcases {
23-
for _, encoding := range encodings {
24-
for _, query := range queries {
25-
t.Run(fmt.Sprintf("%s/%s/%s", testcase.name, encoding.name, query.query), func(t *testing.T) {
26-
store, from := makeMockChunkStore(t, 24, encoding.e)
27-
queryable := newMockStoreQueryable(store, testcase.f)
28-
testRangeQuery(t, queryable, from, query)
29-
})
27+
opts := promql.EngineOpts{
28+
Logger: log.NewNopLogger(),
29+
ActiveQueryTracker: promql.NewActiveQueryTracker(t.TempDir(), 10, log.NewNopLogger()),
30+
MaxSamples: 1e6,
31+
Timeout: 1 * time.Minute,
32+
}
33+
for _, thanosEngine := range []bool{false, true} {
34+
var queryEngine v1.QueryEngine
35+
if thanosEngine {
36+
queryEngine = engine.New(engine.Opts{
37+
EngineOpts: opts,
38+
LogicalOptimizers: logicalplan.AllOptimizers,
39+
})
40+
} else {
41+
queryEngine = promql.NewEngine(opts)
42+
}
43+
for _, testcase := range testcases {
44+
for _, encoding := range encodings {
45+
for _, query := range queries {
46+
t.Run(fmt.Sprintf("%s/%s/%s/ thanos engine enabled = %t", testcase.name, encoding.name, query.query, thanosEngine), func(t *testing.T) {
47+
store, from := makeMockChunkStore(t, 24, encoding.e)
48+
queryable := newMockStoreQueryable(store, testcase.f)
49+
testRangeQuery(t, queryable, queryEngine, from, query)
50+
})
51+
}
3052
}
3153
}
3254
}

0 commit comments

Comments
 (0)