Skip to content

Commit f9d6f7c

Browse files
committed
Add x-increase range tests
Signed-off-by: Filip Petkovski <[email protected]>
1 parent 5f8558c commit f9d6f7c

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

engine/engine_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,128 @@ func TestXFunctions(t *testing.T) {
28802880
}
28812881
}
28822882

2883+
func TestXFunctionsRangeQuery(t *testing.T) {
2884+
// Negative offset and at modifier are enabled by default
2885+
// since Prometheus v2.33.0, so we also enable them.
2886+
opts := promql.EngineOpts{
2887+
Timeout: 1 * time.Hour,
2888+
MaxSamples: 1e10,
2889+
EnableNegativeOffset: true,
2890+
EnableAtModifier: true,
2891+
}
2892+
2893+
cases := []struct {
2894+
name string
2895+
load string
2896+
query string
2897+
startTime time.Time
2898+
endTime time.Time
2899+
step time.Duration
2900+
2901+
expected promql.Matrix
2902+
}{
2903+
{
2904+
name: "gaps between steps",
2905+
load: `load 10s
2906+
http_requests 1 5 10 20 _ 40`,
2907+
query: "xincrease(http_requests[10s])",
2908+
2909+
startTime: time.Unix(0, 0),
2910+
endTime: time.Unix(60, 0),
2911+
step: 20 * time.Second,
2912+
2913+
expected: promql.Matrix{
2914+
promql.Series{
2915+
Metric: labels.New(),
2916+
Floats: []promql.FPoint{
2917+
{T: 00_000, F: 1},
2918+
{T: 20_000, F: 5},
2919+
{T: 40_000, F: 0},
2920+
{T: 60_000, F: 0},
2921+
},
2922+
},
2923+
},
2924+
},
2925+
{
2926+
name: "back to back steps",
2927+
load: `load 10s
2928+
http_requests 1 5 10 20 _ 40`,
2929+
query: "xincrease(http_requests[10s])",
2930+
2931+
startTime: time.Unix(0, 0),
2932+
endTime: time.Unix(60, 0),
2933+
step: 10 * time.Second,
2934+
2935+
expected: promql.Matrix{
2936+
promql.Series{
2937+
Metric: labels.New(),
2938+
Floats: []promql.FPoint{
2939+
{T: 00_000, F: 1},
2940+
{T: 10_000, F: 4},
2941+
{T: 20_000, F: 5},
2942+
{T: 30_000, F: 10},
2943+
{T: 40_000, F: 0},
2944+
{T: 50_000, F: 20},
2945+
{T: 60_000, F: 0},
2946+
},
2947+
},
2948+
},
2949+
},
2950+
{
2951+
name: "overlapping steps",
2952+
load: `load 10s
2953+
http_requests 1 5 10 20 _ 40`,
2954+
query: "xincrease(http_requests[20s])",
2955+
2956+
startTime: time.Unix(0, 0),
2957+
endTime: time.Unix(60, 0),
2958+
step: 10 * time.Second,
2959+
2960+
expected: promql.Matrix{
2961+
promql.Series{
2962+
Metric: labels.New(),
2963+
Floats: []promql.FPoint{
2964+
{T: 00_000, F: 1},
2965+
{T: 10_000, F: 4},
2966+
{T: 20_000, F: 9},
2967+
{T: 30_000, F: 15},
2968+
{T: 40_000, F: 10},
2969+
{T: 50_000, F: 20},
2970+
{T: 60_000, F: 20},
2971+
},
2972+
},
2973+
},
2974+
},
2975+
}
2976+
2977+
for _, tc := range cases {
2978+
t.Run(tc.name, func(t *testing.T) {
2979+
storage := promqltest.LoadedStorage(t, tc.load)
2980+
defer storage.Close()
2981+
2982+
ctx := context.Background()
2983+
newEngine := engine.New(engine.Opts{
2984+
EngineOpts: opts,
2985+
LogicalOptimizers: logicalplan.AllOptimizers,
2986+
EnableXFunctions: true,
2987+
})
2988+
query, err := newEngine.NewRangeQuery(ctx, storage, nil, tc.query, tc.startTime, tc.endTime, tc.step)
2989+
testutil.Ok(t, err)
2990+
defer query.Close()
2991+
2992+
engineResult := query.Exec(ctx)
2993+
testutil.Ok(t, engineResult.Err)
2994+
2995+
gotMatrix, err := engineResult.Matrix()
2996+
require.NoError(t, err)
2997+
2998+
for i := range tc.expected {
2999+
testutil.WithGoCmp(comparer).Equals(t, tc.expected[i].Floats, gotMatrix[i].Floats, queryExplanation(query))
3000+
}
3001+
})
3002+
}
3003+
}
3004+
28833005
func TestXFunctionsWhenDisabled(t *testing.T) {
28843006
var (
28853007
query = "xincrease(http_requests[50s])"

0 commit comments

Comments
 (0)