2026-01-05 07:46:21 -05:00
|
|
|
// Copyright The Prometheus Authors
|
2017-04-19 08:43:09 -04:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2019-08-08 21:35:39 -04:00
|
|
|
package teststorage
|
2016-12-25 05:12:57 -05:00
|
|
|
|
|
|
|
|
import (
|
2024-03-21 05:23:40 -04:00
|
|
|
"fmt"
|
2016-12-25 05:12:57 -05:00
|
|
|
"os"
|
2026-01-21 08:02:58 -05:00
|
|
|
"testing"
|
2020-02-10 11:19:58 -05:00
|
|
|
"time"
|
2016-12-25 05:12:57 -05:00
|
|
|
|
2021-09-13 15:19:20 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2020-02-06 10:58:38 -05:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
2016-12-25 05:12:57 -05:00
|
|
|
)
|
|
|
|
|
|
2026-01-21 03:21:56 -05:00
|
|
|
type Option func(opt *tsdb.Options)
|
|
|
|
|
|
2020-04-29 12:16:14 -04:00
|
|
|
// New returns a new TestStorage for testing purposes
|
2016-12-25 05:12:57 -05:00
|
|
|
// that removes all associated files on closing.
|
2026-01-23 03:41:35 -05:00
|
|
|
//
|
|
|
|
|
// Caller does not need to close the TestStorage after use, it's deferred via t.Cleanup.
|
2026-01-21 08:02:58 -05:00
|
|
|
func New(t testing.TB, o ...Option) *TestStorage {
|
2026-01-21 03:21:56 -05:00
|
|
|
s, err := NewWithError(o...)
|
2024-03-21 05:23:40 -04:00
|
|
|
require.NoError(t, err)
|
2026-01-23 03:41:35 -05:00
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
_ = s.Close() // Ignore errors, as it could be a double close.
|
|
|
|
|
})
|
2026-01-21 03:21:56 -05:00
|
|
|
return s
|
2024-03-21 05:23:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewWithError returns a new TestStorage for user facing tests, which reports
|
|
|
|
|
// errors directly.
|
2026-01-23 03:41:35 -05:00
|
|
|
//
|
|
|
|
|
// It's a caller responsibility to close the TestStorage after use.
|
2026-01-21 03:21:56 -05:00
|
|
|
func NewWithError(o ...Option) (*TestStorage, error) {
|
2017-05-22 05:53:08 -04:00
|
|
|
// Tests just load data for a series sequentially. Thus we
|
|
|
|
|
// need a long appendable window.
|
2020-02-10 15:43:50 -05:00
|
|
|
opts := tsdb.DefaultOptions()
|
2020-02-11 11:34:09 -05:00
|
|
|
opts.MinBlockDuration = int64(24 * time.Hour / time.Millisecond)
|
|
|
|
|
opts.MaxBlockDuration = int64(24 * time.Hour / time.Millisecond)
|
2022-01-02 17:46:03 -05:00
|
|
|
opts.RetentionDuration = 0
|
2026-01-21 03:21:56 -05:00
|
|
|
opts.OutOfOrderTimeWindow = 0
|
2024-10-23 11:34:28 -04:00
|
|
|
|
2026-01-27 05:07:56 -05:00
|
|
|
// Enable exemplars storage by default.
|
|
|
|
|
opts.EnableExemplarStorage = true
|
|
|
|
|
opts.MaxExemplars = 1e5
|
|
|
|
|
|
2026-01-21 03:21:56 -05:00
|
|
|
for _, opt := range o {
|
|
|
|
|
opt(opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir, err := os.MkdirTemp("", "test_storage")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("opening test directory: %w", err)
|
2024-10-23 11:34:28 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-05 10:29:32 -04:00
|
|
|
db, err := tsdb.Open(dir, nil, nil, opts, tsdb.NewDBStats())
|
2024-03-21 05:23:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("opening test storage: %w", err)
|
|
|
|
|
}
|
2026-01-27 05:07:56 -05:00
|
|
|
return &TestStorage{DB: db, dir: dir}, nil
|
2016-12-25 05:12:57 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 12:16:14 -04:00
|
|
|
type TestStorage struct {
|
|
|
|
|
*tsdb.DB
|
2026-01-27 05:07:56 -05:00
|
|
|
dir string
|
2016-12-25 05:12:57 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 12:16:14 -04:00
|
|
|
func (s TestStorage) Close() error {
|
|
|
|
|
if err := s.DB.Close(); err != nil {
|
2016-12-25 05:12:57 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return os.RemoveAll(s.dir)
|
|
|
|
|
}
|