From 987fa5c6a2782ebf2c9902b5708245741d007019 Mon Sep 17 00:00:00 2001 From: Charles Korn Date: Mon, 17 Jun 2024 16:43:01 +1000 Subject: [PATCH 1/2] Convert range query test cases to test scripting language Signed-off-by: Charles Korn --- promql/engine_test.go | 161 ------------------ promql/promqltest/testdata/range_queries.test | 73 ++++++++ 2 files changed, 73 insertions(+), 161 deletions(-) create mode 100644 promql/promqltest/testdata/range_queries.test diff --git a/promql/engine_test.go b/promql/engine_test.go index 69d9ea0361f..2d13500b1da 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -3061,167 +3061,6 @@ func TestEngineOptsValidation(t *testing.T) { } } -func TestRangeQuery(t *testing.T) { - cases := []struct { - Name string - Load string - Query string - Result parser.Value - Start time.Time - End time.Time - Interval time.Duration - }{ - { - Name: "sum_over_time with all values", - Load: `load 30s - bar 0 1 10 100 1000`, - Query: "sum_over_time(bar[30s])", - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 0, T: 0}, {F: 11, T: 60000}, {F: 1100, T: 120000}}, - Metric: labels.EmptyLabels(), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(120, 0), - Interval: 60 * time.Second, - }, - { - Name: "sum_over_time with trailing values", - Load: `load 30s - bar 0 1 10 100 1000 0 0 0 0`, - Query: "sum_over_time(bar[30s])", - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 0, T: 0}, {F: 11, T: 60000}, {F: 1100, T: 120000}}, - Metric: labels.EmptyLabels(), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(120, 0), - Interval: 60 * time.Second, - }, - { - Name: "sum_over_time with all values long", - Load: `load 30s - bar 0 1 10 100 1000 10000 100000 1000000 10000000`, - Query: "sum_over_time(bar[30s])", - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 0, T: 0}, {F: 11, T: 60000}, {F: 1100, T: 120000}, {F: 110000, T: 180000}, {F: 11000000, T: 240000}}, - Metric: labels.EmptyLabels(), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(240, 0), - Interval: 60 * time.Second, - }, - { - Name: "sum_over_time with all values random", - Load: `load 30s - bar 5 17 42 2 7 905 51`, - Query: "sum_over_time(bar[30s])", - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 5, T: 0}, {F: 59, T: 60000}, {F: 9, T: 120000}, {F: 956, T: 180000}}, - Metric: labels.EmptyLabels(), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(180, 0), - Interval: 60 * time.Second, - }, - { - Name: "metric query", - Load: `load 30s - metric 1+1x4`, - Query: "metric", - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 1, T: 0}, {F: 3, T: 60000}, {F: 5, T: 120000}}, - Metric: labels.FromStrings("__name__", "metric"), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(120, 0), - Interval: 1 * time.Minute, - }, - { - Name: "metric query with trailing values", - Load: `load 30s - metric 1+1x8`, - Query: "metric", - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 1, T: 0}, {F: 3, T: 60000}, {F: 5, T: 120000}}, - Metric: labels.FromStrings("__name__", "metric"), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(120, 0), - Interval: 1 * time.Minute, - }, - { - Name: "short-circuit", - Load: `load 30s - foo{job="1"} 1+1x4 - bar{job="2"} 1+1x4`, - Query: `foo > 2 or bar`, - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 1, T: 0}, {F: 3, T: 60000}, {F: 5, T: 120000}}, - Metric: labels.FromStrings( - "__name__", "bar", - "job", "2", - ), - }, - promql.Series{ - Floats: []promql.FPoint{{F: 3, T: 60000}, {F: 5, T: 120000}}, - Metric: labels.FromStrings( - "__name__", "foo", - "job", "1", - ), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(120, 0), - Interval: 1 * time.Minute, - }, - { - Name: "drop-metric-name", - Load: `load 30s - requests{job="1", __address__="bar"} 100`, - Query: `requests * 2`, - Result: promql.Matrix{ - promql.Series{ - Floats: []promql.FPoint{{F: 200, T: 0}, {F: 200, T: 60000}, {F: 200, T: 120000}}, - Metric: labels.FromStrings( - "__address__", "bar", - "job", "1", - ), - }, - }, - Start: time.Unix(0, 0), - End: time.Unix(120, 0), - Interval: 1 * time.Minute, - }, - } - for _, c := range cases { - t.Run(c.Name, func(t *testing.T) { - engine := newTestEngine() - storage := promqltest.LoadedStorage(t, c.Load) - t.Cleanup(func() { storage.Close() }) - - qry, err := engine.NewRangeQuery(context.Background(), storage, nil, c.Query, c.Start, c.End, c.Interval) - require.NoError(t, err) - - res := qry.Exec(context.Background()) - require.NoError(t, res.Err) - testutil.RequireEqual(t, c.Result, res.Value) - }) - } -} - func TestInstantQueryWithRangeVectorSelector(t *testing.T) { engine := newTestEngine() diff --git a/promql/promqltest/testdata/range_queries.test b/promql/promqltest/testdata/range_queries.test new file mode 100644 index 00000000000..e6951096026 --- /dev/null +++ b/promql/promqltest/testdata/range_queries.test @@ -0,0 +1,73 @@ +# sum_over_time with all values +load 30s + bar 0 1 10 100 1000 + +eval range from 0 to 2m step 1m sum_over_time(bar[30s]) + {} 0 11 1100 + +clear + +# sum_over_time with trailing values +load 30s + bar 0 1 10 100 1000 0 0 0 0 + +eval range from 0 to 2m step 1m sum_over_time(bar[30s]) + {} 0 11 1100 + +clear + +# sum_over_time with all values long +load 30s + bar 0 1 10 100 1000 10000 100000 1000000 10000000 + +eval range from 0 to 4m step 1m sum_over_time(bar[30s]) + {} 0 11 1100 110000 11000000 + +clear + +# sum_over_time with all values random +load 30s + bar 5 17 42 2 7 905 51 + +eval range from 0 to 3m step 1m sum_over_time(bar[30s]) + {} 5 59 9 956 + +clear + +# metric query +load 30s + metric 1+1x4 + +eval range from 0 to 2m step 1m metric + metric 1 3 5 + +clear + +# metric query with trailing values +load 30s + metric 1+1x8 + +eval range from 0 to 2m step 1m metric + metric 1 3 5 + +clear + +# short-circuit +load 30s + foo{job="1"} 1+1x4 + bar{job="2"} 1+1x4 + +eval range from 0 to 2m step 1m foo > 2 or bar + foo{job="1"} _ 3 5 + bar{job="2"} 1 3 5 + +clear + +# Drop metric name +load 30s + requests{job="1", __address__="bar"} 100 + +eval range from 0 to 2m step 1m requests * 2 + {job="1", __address__="bar"} 200 200 200 + +clear From aeec30f082012b3f3445d51ecdc30dfb15790a45 Mon Sep 17 00:00:00 2001 From: Charles Korn Date: Mon, 17 Jun 2024 16:56:56 +1000 Subject: [PATCH 2/2] Convert `TestTimestampFunction_StepsMoreOftenThanSamples` Signed-off-by: Charles Korn --- promql/engine_test.go | 41 ----------------------- promql/promqltest/testdata/functions.test | 8 +++++ 2 files changed, 8 insertions(+), 41 deletions(-) diff --git a/promql/engine_test.go b/promql/engine_test.go index 2d13500b1da..4e321a6c334 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -2015,47 +2015,6 @@ func TestSubquerySelector(t *testing.T) { } } -func TestTimestampFunction_StepsMoreOftenThanSamples(t *testing.T) { - engine := newTestEngine() - storage := promqltest.LoadedStorage(t, ` -load 1m - metric 0+1x1000 -`) - t.Cleanup(func() { storage.Close() }) - - query := "timestamp(metric)" - start := time.Unix(0, 0) - end := time.Unix(61, 0) - interval := time.Second - - // We expect the value to be 0 for t=0s to t=59s (inclusive), then 60 for t=60s and t=61s. - expectedPoints := []promql.FPoint{} - - for t := 0; t <= 59; t++ { - expectedPoints = append(expectedPoints, promql.FPoint{F: 0, T: int64(t * 1000)}) - } - - expectedPoints = append( - expectedPoints, - promql.FPoint{F: 60, T: 60_000}, - promql.FPoint{F: 60, T: 61_000}, - ) - - expectedResult := promql.Matrix{ - promql.Series{ - Floats: expectedPoints, - Metric: labels.EmptyLabels(), - }, - } - - qry, err := engine.NewRangeQuery(context.Background(), storage, nil, query, start, end, interval) - require.NoError(t, err) - - res := qry.Exec(context.Background()) - require.NoError(t, res.Err) - testutil.RequireEqual(t, expectedResult, res.Value) -} - type FakeQueryLogger struct { closed bool logs []interface{} diff --git a/promql/promqltest/testdata/functions.test b/promql/promqltest/testdata/functions.test index 2c198374acb..7e741e9956f 100644 --- a/promql/promqltest/testdata/functions.test +++ b/promql/promqltest/testdata/functions.test @@ -1213,3 +1213,11 @@ eval instant at 5m log10(exp_root_log - 20) {l="y"} -Inf clear + +# Test that timestamp() handles the scenario where there are more steps than samples. +load 1m + metric 0+1x1000 + +# We expect the value to be 0 for t=0s to t=59s (inclusive), then 60 for t=60s and t=61s. +eval range from 0 to 61s step 1s timestamp(metric) + {} 0x59 60 60