Refactor testutil.NewStorage into a new package

This avoids a circular dependency between the testutil and storage
packages.

Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
This commit is contained in:
Chris Marchbanks 2019-08-08 19:35:39 -06:00
parent ff40de7ca6
commit 0685eb5395
No known key found for this signature in database
GPG key ID: B7FD940BC86A8E7A
9 changed files with 29 additions and 21 deletions

View file

@ -22,11 +22,11 @@ import (
"time" "time"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/teststorage"
) )
func BenchmarkRangeQuery(b *testing.B) { func BenchmarkRangeQuery(b *testing.B) {
storage := testutil.NewStorage(b) storage := teststorage.New(b)
defer storage.Close() defer storage.Close()
opts := EngineOpts{ opts := EngineOpts{
Logger: nil, Logger: nil,

View file

@ -20,6 +20,7 @@ import (
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/util/teststorage"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
@ -27,7 +28,7 @@ func TestDeriv(t *testing.T) {
// https://github.com/prometheus/prometheus/issues/2674#issuecomment-315439393 // https://github.com/prometheus/prometheus/issues/2674#issuecomment-315439393
// This requires more precision than the usual test system offers, // This requires more precision than the usual test system offers,
// so we test it by hand. // so we test it by hand.
storage := testutil.NewStorage(t) storage := teststorage.New(t)
defer storage.Close() defer storage.Close()
opts := EngineOpts{ opts := EngineOpts{
Logger: nil, Logger: nil,

View file

@ -28,6 +28,7 @@ import (
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/teststorage"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
@ -509,7 +510,7 @@ func (t *Test) clear() {
if t.cancelCtx != nil { if t.cancelCtx != nil {
t.cancelCtx() t.cancelCtx()
} }
t.storage = testutil.NewStorage(t) t.storage = teststorage.New(t)
opts := EngineOpts{ opts := EngineOpts{
Logger: nil, Logger: nil,
@ -623,7 +624,7 @@ func (ll *LazyLoader) clear() {
if ll.cancelCtx != nil { if ll.cancelCtx != nil {
ll.cancelCtx() ll.cancelCtx()
} }
ll.storage = testutil.NewStorage(ll) ll.storage = teststorage.New(ll)
opts := EngineOpts{ opts := EngineOpts{
Logger: nil, Logger: nil,

View file

@ -28,6 +28,7 @@ import (
"github.com/prometheus/prometheus/pkg/value" "github.com/prometheus/prometheus/pkg/value"
"github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/teststorage"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
@ -492,7 +493,7 @@ func TestForStateRestore(t *testing.T) {
} }
func TestStaleness(t *testing.T) { func TestStaleness(t *testing.T) {
storage := testutil.NewStorage(t) storage := teststorage.New(t)
defer storage.Close() defer storage.Close()
engineOpts := promql.EngineOpts{ engineOpts := promql.EngineOpts{
Logger: nil, Logger: nil,
@ -633,7 +634,7 @@ func TestCopyState(t *testing.T) {
} }
func TestDeletedRuleMarkedStale(t *testing.T) { func TestDeletedRuleMarkedStale(t *testing.T) {
storage := testutil.NewStorage(t) storage := teststorage.New(t)
defer storage.Close() defer storage.Close()
oldGroup := &Group{ oldGroup := &Group{
rules: []Rule{ rules: []Rule{
@ -679,7 +680,7 @@ func TestUpdate(t *testing.T) {
expected := map[string]labels.Labels{ expected := map[string]labels.Labels{
"test": labels.FromStrings("name", "value"), "test": labels.FromStrings("name", "value"),
} }
storage := testutil.NewStorage(t) storage := teststorage.New(t)
defer storage.Close() defer storage.Close()
opts := promql.EngineOpts{ opts := promql.EngineOpts{
Logger: nil, Logger: nil,
@ -718,7 +719,7 @@ func TestUpdate(t *testing.T) {
} }
func TestNotify(t *testing.T) { func TestNotify(t *testing.T) {
storage := testutil.NewStorage(t) storage := teststorage.New(t)
defer storage.Close() defer storage.Close()
engineOpts := promql.EngineOpts{ engineOpts := promql.EngineOpts{
Logger: nil, Logger: nil,

View file

@ -21,11 +21,12 @@ import (
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/util/teststorage"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestRuleEval(t *testing.T) { func TestRuleEval(t *testing.T) {
storage := testutil.NewStorage(t) storage := teststorage.New(t)
defer storage.Close() defer storage.Close()
opts := promql.EngineOpts{ opts := promql.EngineOpts{

View file

@ -42,6 +42,7 @@ import (
"github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/pkg/value" "github.com/prometheus/prometheus/pkg/value"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/teststorage"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
@ -663,7 +664,7 @@ test_metric 1
func TestScrapeLoopSeriesAdded(t *testing.T) { func TestScrapeLoopSeriesAdded(t *testing.T) {
// Need a full storage for correct Add/AddFast semantics. // Need a full storage for correct Add/AddFast semantics.
s := testutil.NewStorage(t) s := teststorage.New(t)
defer s.Close() defer s.Close()
app, err := s.Appender() app, err := s.Appender()
@ -818,7 +819,7 @@ func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) {
} }
func TestScrapeLoopCache(t *testing.T) { func TestScrapeLoopCache(t *testing.T) {
s := testutil.NewStorage(t) s := teststorage.New(t)
defer s.Close() defer s.Close()
sapp, err := s.Appender() sapp, err := s.Appender()
@ -897,7 +898,7 @@ func TestScrapeLoopCache(t *testing.T) {
} }
func TestScrapeLoopCacheMemoryExhaustionProtection(t *testing.T) { func TestScrapeLoopCacheMemoryExhaustionProtection(t *testing.T) {
s := testutil.NewStorage(t) s := teststorage.New(t)
defer s.Close() defer s.Close()
sapp, err := s.Appender() sapp, err := s.Appender()
@ -1106,7 +1107,7 @@ func TestScrapeLoop_ChangingMetricString(t *testing.T) {
// This is a regression test for the scrape loop cache not properly maintaining // This is a regression test for the scrape loop cache not properly maintaining
// IDs when the string representation of a metric changes across a scrape. Thus // IDs when the string representation of a metric changes across a scrape. Thus
// we use a real storage appender here. // we use a real storage appender here.
s := testutil.NewStorage(t) s := teststorage.New(t)
defer s.Close() defer s.Close()
app, err := s.Appender() app, err := s.Appender()
@ -1546,7 +1547,7 @@ func (ts *testScraper) scrape(ctx context.Context, w io.Writer) (string, error)
} }
func TestScrapeLoop_RespectTimestamps(t *testing.T) { func TestScrapeLoop_RespectTimestamps(t *testing.T) {
s := testutil.NewStorage(t) s := teststorage.New(t)
defer s.Close() defer s.Close()
app, err := s.Appender() app, err := s.Appender()
@ -1583,7 +1584,7 @@ func TestScrapeLoop_RespectTimestamps(t *testing.T) {
} }
func TestScrapeLoop_DiscardTimestamps(t *testing.T) { func TestScrapeLoop_DiscardTimestamps(t *testing.T) {
s := testutil.NewStorage(t) s := teststorage.New(t)
defer s.Close() defer s.Close()
app, err := s.Appender() app, err := s.Appender()

View file

@ -20,11 +20,12 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage/tsdb" "github.com/prometheus/prometheus/storage/tsdb"
"github.com/prometheus/prometheus/util/teststorage"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestMetrics(t *testing.T) { func TestMetrics(t *testing.T) {
db := testutil.NewStorage(t) db := teststorage.New(t)
defer db.Close() defer db.Close()
metrics := &dto.Metric{} metrics := &dto.Metric{}

View file

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package testutil package teststorage
import ( import (
"io/ioutil" "io/ioutil"
@ -21,11 +21,12 @@ import (
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/storage/tsdb" "github.com/prometheus/prometheus/storage/tsdb"
"github.com/prometheus/prometheus/util/testutil"
) )
// NewStorage returns a new storage for testing purposes // New returns a new storage for testing purposes
// that removes all associated files on closing. // that removes all associated files on closing.
func NewStorage(t T) storage.Storage { func New(t testutil.T) storage.Storage {
dir, err := ioutil.TempDir("", "test_storage") dir, err := ioutil.TempDir("", "test_storage")
if err != nil { if err != nil {
t.Fatalf("Opening test dir failed: %s", err) t.Fatalf("Opening test dir failed: %s", err)

View file

@ -50,6 +50,7 @@ import (
"github.com/prometheus/prometheus/scrape" "github.com/prometheus/prometheus/scrape"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/storage/remote" "github.com/prometheus/prometheus/storage/remote"
"github.com/prometheus/prometheus/util/teststorage"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
@ -165,7 +166,7 @@ func (m rulesRetrieverMock) AlertingRules() []*rules.AlertingRule {
func (m rulesRetrieverMock) RuleGroups() []*rules.Group { func (m rulesRetrieverMock) RuleGroups() []*rules.Group {
var ar rulesRetrieverMock var ar rulesRetrieverMock
arules := ar.AlertingRules() arules := ar.AlertingRules()
storage := testutil.NewStorage(m.testing) storage := teststorage.New(m.testing)
defer storage.Close() defer storage.Close()
engineOpts := promql.EngineOpts{ engineOpts := promql.EngineOpts{