From 622ece6273376838d007e32ee88e166375a2f4a5 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sun, 25 Dec 2016 11:12:57 +0100 Subject: [PATCH] *: fix recording tests, migrate matcher types --- rules/recording_test.go | 21 +++++++++++---------- storage/interface.go | 5 ----- storage/tsdb/tsdb.go | 17 +++++++++-------- util/testutil/storage.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 util/testutil/storage.go diff --git a/rules/recording_test.go b/rules/recording_test.go index 00b9308c1..bbef4903c 100644 --- a/rules/recording_test.go +++ b/rules/recording_test.go @@ -20,12 +20,13 @@ import ( "github.com/prometheus/common/model" "golang.org/x/net/context" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/storage/local" ) func TestRuleEval(t *testing.T) { - storage, closer := local.NewTestStorage(t, 2) + storage, closer := local.NewTestStorage(t) defer closer.Close() engine := promql.NewEngine(storage, nil) ctx, cancelCtx := context.WithCancel(context.Background()) @@ -36,27 +37,27 @@ func TestRuleEval(t *testing.T) { suite := []struct { name string expr promql.Expr - labels model.LabelSet - result model.Vector + labels labels.Labels + result promql.Vector }{ { name: "nolabels", expr: &promql.NumberLiteral{Val: 1}, - labels: model.LabelSet{}, - result: model.Vector{&model.Sample{ + labels: labels.Labels{}, + result: promql.Vector{promql.Sample{ Value: 1, Timestamp: now, - Metric: model.Metric{"__name__": "nolabels"}, + Metric: labels.FromStrings("__name__", "nolabels"), }}, }, { name: "labels", expr: &promql.NumberLiteral{Val: 1}, - labels: model.LabelSet{"foo": "bar"}, - result: model.Vector{&model.Sample{ + labels: labels.FromStrings("foo", "bar"), + result: promql.Vector{promql.Sample{ Value: 1, Timestamp: now, - Metric: model.Metric{"__name__": "labels", "foo": "bar"}, + Metric: labels.FromStrings("__name__", "labels", "foo", "bar"), }}, }, } @@ -78,7 +79,7 @@ func TestRecordingRuleHTMLSnippet(t *testing.T) { if err != nil { t.Fatal(err) } - rule := NewRecordingRule("testrule", expr, model.LabelSet{"html": "BOLD"}) + rule := NewRecordingRule("testrule", expr, labels.FromStrings("html", "BOLD")) const want = `testrule{html="<b>BOLD</b>"} = foo{html="<b>BOLD<b>"}` diff --git a/storage/interface.go b/storage/interface.go index 39b8f6787..2f18563b8 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -17,7 +17,6 @@ import ( "errors" "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) var ( @@ -25,10 +24,6 @@ var ( ErrDuplicateSampleForTimestamp = errors.New("duplicate sample for timestamp") ) -func NewTestStorage(t testutil.T) Storage { - return nil, nil -} - // Storage ingests and manages samples, along with various indexes. All methods // are goroutine-safe. Storage implements storage.SampleAppender. type Storage interface { diff --git a/storage/tsdb/tsdb.go b/storage/tsdb/tsdb.go index 43b98a9f9..f9ec8c806 100644 --- a/storage/tsdb/tsdb.go +++ b/storage/tsdb/tsdb.go @@ -6,10 +6,11 @@ import ( "github.com/fabxc/tsdb" tsdbLabels "github.com/fabxc/tsdb/labels" "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/promql" + "github.com/prometheus/prometheus/storage" ) -type storage struct { +// db implements a storage.Storage around TSDB. +type db struct { db *tsdb.DB } @@ -22,7 +23,7 @@ func Open(path string) (storage.Storage, error) { return &storage{db: db} } -func (db *storage) Querier(mint, maxt int64) (storage.Querier, error) { +func (db *db) Querier(mint, maxt int64) (storage.Querier, error) { q, err := db.db.Querier(mint, maxt) if err != nil { return nil, err @@ -31,7 +32,7 @@ func (db *storage) Querier(mint, maxt int64) (storage.Querier, error) { } // Appender returns a new appender against the storage. -func (db *storage) Appender() (Appender, error) { +func (db *db) Appender() (storage.Appender, error) { a, err := db.db.Appender() if err != nil { return nil, err @@ -40,7 +41,7 @@ func (db *storage) Appender() (Appender, error) { } // Close closes the storage and all its underlying resources. -func (db *storage) Close() error { +func (db *db) Close() error { return db.Close() } @@ -48,7 +49,7 @@ type querier struct { q tsdb.Querier } -func (q *querier) Select(oms ...*promql.LabelMatcher) (storage.SeriesSet, error) { +func (q *querier) Select(oms ...*labels.Matcher) (storage.SeriesSet, error) { ms := make([]tsdbLabels.Matcher, 0, len(oms)) for _, om := range oms { @@ -85,7 +86,7 @@ type appender struct { func (a *appender) Add(lset labels.Labels, t int64, v float64) { a.Add(toTSDBLabels(lset), t, v) } func (a *appender) Commit() error { a.a.Commit() } -func convertMatcher(m *promql.LabelMatcher) tsdbLabels.Matcher { +func convertMatcher(m *labels.Matcher) tsdbLabels.Matcher { switch m.Type { case MatchEqual: return tsdbLabels.NewEqualMatcher(m.Name, m.Value) @@ -107,7 +108,7 @@ func convertMatcher(m *promql.LabelMatcher) tsdbLabels.Matcher { } return tsdbLabels.Not(res) } - panic("promql.LabelMatcher.matcher: invalid matcher type") + panic("storage.convertMatcher: invalid matcher type") } func toTSDBLabels(l labels.Labels) tsdbLabels.Labels { diff --git a/util/testutil/storage.go b/util/testutil/storage.go new file mode 100644 index 000000000..bdb0a06ad --- /dev/null +++ b/util/testutil/storage.go @@ -0,0 +1,35 @@ +package testutil + +import ( + "io/ioutil" + "os" + + "github.com/fabxc/tsdb" + "github.com/prometheus/prometheus/storage" +) + +// NewTestStorage returns a new storage for testing purposes +// that removes all associated files on closing. +func NewTestStorage(t T) storage.Storage { + dir, err := ioutil.TempDir("", "test_storage") + if err != nil { + t.Fatalf("Opening test dir failed: %s", errs) + } + db, err := tsdb.Open(dir) + if err != nil { + t.Fatalf("Opening test storage failed: %s", err) + } + return testStorage{DB: db, dir: dir} +} + +type testStorage struct { + *tsdb.DB + dir string +} + +func (s testStorage) Close() error { + if err := s.db.Close(); err != nil { + return err + } + return os.RemoveAll(s.dir) +}