mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
Merge pull request #13822 from dgl/promtool-test-errors
promtool: Avoid using testify for user rule tests
This commit is contained in:
commit
773170f372
|
@ -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.
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue