From aa922ce3b66af7065a4cef9bece23e6bce7e1357 Mon Sep 17 00:00:00 2001 From: Andrew Hall Date: Tue, 16 Sep 2025 19:28:19 +0800 Subject: [PATCH] Added support for string literals and range results for instant queries in test scripting framework (#17055) Signed-off-by: Andrew Hall Co-authored-by: Charles Korn Co-authored-by: Arve Knudsen --- promql/engine_test.go | 83 --------- promql/promqltest/README.md | 36 ++++ promql/promqltest/test.go | 157 +++++++++++++++--- promql/promqltest/test_test.go | 138 +++++++++++++++ promql/promqltest/testdata/literals.test | 15 ++ promql/promqltest/testdata/range_queries.test | 34 ++++ 6 files changed, 355 insertions(+), 108 deletions(-) diff --git a/promql/engine_test.go b/promql/engine_test.go index f70036e3c0..e2aadd7a5d 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -3195,89 +3195,6 @@ func TestEngine_Close(t *testing.T) { }) } -func TestInstantQueryWithRangeVectorSelector(t *testing.T) { - engine := newTestEngine(t) - - baseT := timestamp.Time(0) - storage := promqltest.LoadedStorage(t, ` - load 1m - some_metric{env="1"} 0+1x4 - some_metric{env="2"} 0+2x4 - some_metric{env="3"} {{count:0}}+{{count:1}}x4 - some_metric_with_stale_marker 0 1 stale 3 - `) - t.Cleanup(func() { require.NoError(t, storage.Close()) }) - - testCases := map[string]struct { - expr string - expected promql.Matrix - ts time.Time - }{ - "matches series with points in range": { - expr: "some_metric[2m]", - ts: baseT.Add(2 * time.Minute), - expected: promql.Matrix{ - { - Metric: labels.FromStrings("__name__", "some_metric", "env", "1"), - Floats: []promql.FPoint{ - {T: timestamp.FromTime(baseT.Add(time.Minute)), F: 1}, - {T: timestamp.FromTime(baseT.Add(2 * time.Minute)), F: 2}, - }, - }, - { - Metric: labels.FromStrings("__name__", "some_metric", "env", "2"), - Floats: []promql.FPoint{ - {T: timestamp.FromTime(baseT.Add(time.Minute)), F: 2}, - {T: timestamp.FromTime(baseT.Add(2 * time.Minute)), F: 4}, - }, - }, - { - Metric: labels.FromStrings("__name__", "some_metric", "env", "3"), - Histograms: []promql.HPoint{ - {T: timestamp.FromTime(baseT.Add(time.Minute)), H: &histogram.FloatHistogram{Count: 1, CounterResetHint: histogram.NotCounterReset}}, - {T: timestamp.FromTime(baseT.Add(2 * time.Minute)), H: &histogram.FloatHistogram{Count: 2, CounterResetHint: histogram.NotCounterReset}}, - }, - }, - }, - }, - "matches no series": { - expr: "some_nonexistent_metric[1m]", - ts: baseT, - expected: promql.Matrix{}, - }, - "no samples in range": { - expr: "some_metric[1m]", - ts: baseT.Add(20 * time.Minute), - expected: promql.Matrix{}, - }, - "metric with stale marker": { - expr: "some_metric_with_stale_marker[3m]", - ts: baseT.Add(3 * time.Minute), - expected: promql.Matrix{ - { - Metric: labels.FromStrings("__name__", "some_metric_with_stale_marker"), - Floats: []promql.FPoint{ - {T: timestamp.FromTime(baseT.Add(time.Minute)), F: 1}, - {T: timestamp.FromTime(baseT.Add(3 * time.Minute)), F: 3}, - }, - }, - }, - }, - } - - for name, testCase := range testCases { - t.Run(name, func(t *testing.T) { - q, err := engine.NewInstantQuery(context.Background(), storage, nil, testCase.expr, testCase.ts) - require.NoError(t, err) - defer q.Close() - - res := q.Exec(context.Background()) - require.NoError(t, res.Err) - testutil.RequireEqual(t, testCase.expected, res.Value) - }) - } -} - func TestQueryLookbackDelta(t *testing.T) { var ( load = `load 5m diff --git a/promql/promqltest/README.md b/promql/promqltest/README.md index 84a0e69f3a..d26c01c6f1 100644 --- a/promql/promqltest/README.md +++ b/promql/promqltest/README.md @@ -106,8 +106,44 @@ eval range from to step * `` and `` specify the time range of the range query, and use the same syntax as `