*: fix recording tests, migrate matcher types

This commit is contained in:
Fabian Reinartz 2016-12-25 11:12:57 +01:00
parent 0492ddbd4d
commit 622ece6273
4 changed files with 55 additions and 23 deletions

View file

@ -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": "<b>BOLD</b>"})
rule := NewRecordingRule("testrule", expr, labels.FromStrings("html", "<b>BOLD</b>"))
const want = `<a href="/test/prefix/graph?g0.expr=testrule&g0.tab=0">testrule</a>{html=&#34;&lt;b&gt;BOLD&lt;/b&gt;&#34;} = <a href="/test/prefix/graph?g0.expr=foo%7Bhtml%3D%22%3Cb%3EBOLD%3Cb%3E%22%7D&g0.tab=0">foo{html=&#34;&lt;b&gt;BOLD&lt;b&gt;&#34;}</a>`

View file

@ -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 {

View file

@ -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 {

35
util/testutil/storage.go Normal file
View file

@ -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)
}