Merge pull request #13822 from dgl/promtool-test-errors

promtool: Avoid using testify for user rule tests
This commit is contained in:
Bryan Boreham 2024-03-23 09:42:34 +01:00 committed by GitHub
commit 773170f372
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 20 deletions

View file

@ -175,13 +175,18 @@ type testGroup struct {
}
// test performs the unit tests.
func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]int, queryOpts promql.LazyLoaderOpts, diffFlag bool, ruleFiles ...string) []error {
func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]int, queryOpts promql.LazyLoaderOpts, diffFlag bool, ruleFiles ...string) (outErr []error) {
// Setup testing suite.
suite, err := promql.NewLazyLoader(nil, tg.seriesLoadingString(), queryOpts)
suite, err := promql.NewLazyLoader(tg.seriesLoadingString(), queryOpts)
if err != nil {
return []error{err}
}
defer suite.Close()
defer func() {
err := suite.Close()
if err != nil {
outErr = append(outErr, err)
}
}()
suite.SubqueryInterval = evalInterval
// Load the rule files.

View file

@ -704,8 +704,6 @@ func parseNumber(s string) (float64, error) {
// LazyLoader lazily loads samples into storage.
// This is specifically implemented for unit testing of rules.
type LazyLoader struct {
testutil.T
loadCmd *loadCmd
storage storage.Storage
@ -727,13 +725,15 @@ type LazyLoaderOpts struct {
}
// NewLazyLoader returns an initialized empty LazyLoader.
func NewLazyLoader(t testutil.T, input string, opts LazyLoaderOpts) (*LazyLoader, error) {
func NewLazyLoader(input string, opts LazyLoaderOpts) (*LazyLoader, error) {
ll := &LazyLoader{
T: t,
opts: opts,
}
err := ll.parse(input)
ll.clear()
if err != nil {
return nil, err
}
err = ll.clear()
return ll, err
}
@ -761,15 +761,20 @@ func (ll *LazyLoader) parse(input string) error {
}
// clear the current test storage of all inserted samples.
func (ll *LazyLoader) clear() {
func (ll *LazyLoader) clear() error {
if ll.storage != nil {
err := ll.storage.Close()
require.NoError(ll.T, err, "Unexpected error while closing test storage.")
if err := ll.storage.Close(); err != nil {
return fmt.Errorf("closing test storage: %w", err)
}
}
if ll.cancelCtx != nil {
ll.cancelCtx()
}
ll.storage = teststorage.New(ll)
var err error
ll.storage, err = teststorage.NewWithError()
if err != nil {
return err
}
opts := EngineOpts{
Logger: nil,
@ -783,6 +788,7 @@ func (ll *LazyLoader) clear() {
ll.queryEngine = NewEngine(opts)
ll.context, ll.cancelCtx = context.WithCancel(context.Background())
return nil
}
// appendTill appends the defined time series to the storage till the given timestamp (in milliseconds).
@ -836,8 +842,7 @@ func (ll *LazyLoader) Storage() storage.Storage {
}
// Close closes resources associated with the LazyLoader.
func (ll *LazyLoader) Close() {
func (ll *LazyLoader) Close() error {
ll.cancelCtx()
err := ll.storage.Close()
require.NoError(ll.T, err, "Unexpected error while closing test storage.")
return ll.storage.Close()
}

View file

@ -110,7 +110,7 @@ func TestLazyLoader_WithSamplesTill(t *testing.T) {
}
for _, c := range cases {
suite, err := NewLazyLoader(t, c.loadString, LazyLoaderOpts{})
suite, err := NewLazyLoader(c.loadString, LazyLoaderOpts{})
require.NoError(t, err)
defer suite.Close()

View file

@ -14,6 +14,7 @@
package teststorage
import (
"fmt"
"os"
"time"
@ -30,8 +31,18 @@ import (
// New returns a new TestStorage for testing purposes
// that removes all associated files on closing.
func New(t testutil.T) *TestStorage {
stor, err := NewWithError()
require.NoError(t, err)
return stor
}
// NewWithError returns a new TestStorage for user facing tests, which reports
// errors directly.
func NewWithError() (*TestStorage, error) {
dir, err := os.MkdirTemp("", "test_storage")
require.NoError(t, err, "unexpected error while opening test directory")
if err != nil {
return nil, fmt.Errorf("opening test directory: %w", err)
}
// Tests just load data for a series sequentially. Thus we
// need a long appendable window.
@ -41,13 +52,17 @@ func New(t testutil.T) *TestStorage {
opts.RetentionDuration = 0
opts.EnableNativeHistograms = true
db, err := tsdb.Open(dir, nil, nil, opts, tsdb.NewDBStats())
require.NoError(t, err, "unexpected error while opening test storage")
if err != nil {
return nil, fmt.Errorf("opening test storage: %w", err)
}
reg := prometheus.NewRegistry()
eMetrics := tsdb.NewExemplarMetrics(reg)
es, err := tsdb.NewCircularExemplarStorage(10, eMetrics)
require.NoError(t, err, "unexpected error while opening test exemplar storage")
return &TestStorage{DB: db, exemplarStorage: es, dir: dir}
if err != nil {
return nil, fmt.Errorf("opening test exemplar storage: %w", err)
}
return &TestStorage{DB: db, exemplarStorage: es, dir: dir}, nil
}
type TestStorage struct {