test: PromQL: stop using internal fields of engine

* set enablePerStepStats and lookback duration via
  `NewTestEngine` parameters.
* check maxSamples by recreating query engine
* check lookback without modifying internals

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2024-05-04 14:05:11 +01:00
parent 0dbfd20b69
commit 786e0e706c
3 changed files with 7 additions and 13 deletions

View File

@ -1289,13 +1289,10 @@ load 10s
},
}
engine := newTestEngine()
engine.enablePerStepStats = true
origMaxSamples := engine.maxSamplesPerQuery
for _, c := range cases {
t.Run(c.Query, func(t *testing.T) {
opts := promql.NewPrometheusQueryOpts(true, 0)
engine.maxSamplesPerQuery = origMaxSamples
engine := promqltest.NewTestEngine(true, 0, promqltest.DefaultMaxSamplesPerQuery)
runQuery := func(expErr error) *stats.Statistics {
var err error
@ -1322,7 +1319,7 @@ load 10s
if c.SkipMaxCheck {
return
}
engine.maxSamplesPerQuery = stats.Samples.PeakSamples - 1
engine = promqltest.NewTestEngine(true, 0, stats.Samples.PeakSamples-1)
runQuery(promql.ErrTooManySamples(env))
})
}
@ -1496,11 +1493,11 @@ load 10s
}
// Within limit.
engine.maxSamplesPerQuery = c.MaxSamples
engine = promqltest.NewTestEngine(false, 0, c.MaxSamples)
testFunc(nil)
// Exceeding limit.
engine.maxSamplesPerQuery = c.MaxSamples - 1
engine = promqltest.NewTestEngine(false, 0, c.MaxSamples-1)
testFunc(promql.ErrTooManySamples(env))
})
}
@ -4956,13 +4953,10 @@ metric 0 1 2
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
engine := newTestEngine()
engine := promqltest.NewTestEngine(false, c.engineLookback, promqltest.DefaultMaxSamplesPerQuery)
storage := promqltest.LoadedStorage(t, load)
t.Cleanup(func() { storage.Close() })
if c.engineLookback != 0 {
engine.lookbackDelta = c.engineLookback
}
opts := promql.NewPrometheusQueryOpts(false, c.queryLookback)
qry, err := engine.NewInstantQuery(context.Background(), storage, opts, query, c.ts)
require.NoError(t, err)

View File

@ -32,7 +32,7 @@ func newTestEngine() *promql.Engine {
}
func TestEvaluations(t *testing.T) {
RunBuiltinTests(t, newTestEngine())
promqltest.RunBuiltinTests(t, newTestEngine())
}
// Run a lot of queries at the same time, to check for race conditions.

View File

@ -76,7 +76,7 @@ func NewTestEngine(enablePerStepStats bool, lookbackDelta time.Duration, maxSamp
return promql.NewEngine(promql.EngineOpts{
Logger: nil,
Reg: nil,
MaxSamples: DefaultMaxSamplesPerQuery,
MaxSamples: maxSamples,
Timeout: 100 * time.Second,
NoStepSubqueryIntervalFn: func(int64) int64 { return durationMilliseconds(1 * time.Minute) },
EnableAtModifier: true,