2015-05-11 13:56:35 +00:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-05-11 13:56:35 +00:00
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2015-05-11 13:56:35 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2015-05-29 11:30:30 +00:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2015-05-11 13:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
minNormal = math.Float64frombits(0x0010000000000000) // The smallest positive normal value of type float64.
|
|
|
|
|
|
|
|
patSpace = regexp.MustCompile("[\t ]+")
|
|
|
|
patLoad = regexp.MustCompile(`^load\s+(.+?)$`)
|
|
|
|
patEvalInstant = regexp.MustCompile(`^eval(?:_(fail|ordered))?\s+instant\s+(?:at\s+(.+?))?\s+(.+)$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-12-23 12:51:59 +00:00
|
|
|
epsilon = 0.000001 // Relative error allowed for sample values.
|
2015-05-11 13:56:35 +00:00
|
|
|
)
|
|
|
|
|
2016-12-28 08:16:48 +00:00
|
|
|
var testStartTime = time.Unix(0, 0)
|
2016-12-23 12:51:59 +00:00
|
|
|
|
2015-05-11 13:56:35 +00:00
|
|
|
// Test is a sequence of read and write commands that are run
|
|
|
|
// against a test storage.
|
|
|
|
type Test struct {
|
2015-05-23 10:23:33 +00:00
|
|
|
testutil.T
|
2015-05-11 13:56:35 +00:00
|
|
|
|
|
|
|
cmds []testCommand
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
storage storage.Storage
|
|
|
|
|
|
|
|
queryEngine *Engine
|
|
|
|
context context.Context
|
|
|
|
cancelCtx context.CancelFunc
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTest returns an initialized empty Test.
|
2015-05-23 10:23:33 +00:00
|
|
|
func NewTest(t testutil.T, input string) (*Test, error) {
|
2015-05-11 13:56:35 +00:00
|
|
|
test := &Test{
|
|
|
|
T: t,
|
|
|
|
cmds: []testCommand{},
|
|
|
|
}
|
|
|
|
err := test.parse(input)
|
|
|
|
test.clear()
|
|
|
|
|
|
|
|
return test, err
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:07:27 +00:00
|
|
|
func newTestFromFile(t testutil.T, filename string) (*Test, error) {
|
2015-05-11 13:56:35 +00:00
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewTest(t, string(content))
|
|
|
|
}
|
|
|
|
|
2015-06-04 16:09:20 +00:00
|
|
|
// QueryEngine returns the test's query engine.
|
|
|
|
func (t *Test) QueryEngine() *Engine {
|
|
|
|
return t.queryEngine
|
|
|
|
}
|
|
|
|
|
2016-09-15 22:58:06 +00:00
|
|
|
// Context returns the test's context.
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
func (t *Test) Context() context.Context {
|
2016-09-15 22:58:06 +00:00
|
|
|
return t.context
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 16:09:20 +00:00
|
|
|
// Storage returns the test's storage.
|
2016-12-25 10:34:22 +00:00
|
|
|
func (t *Test) Storage() storage.Storage {
|
2015-06-04 16:09:20 +00:00
|
|
|
return t.storage
|
|
|
|
}
|
|
|
|
|
2015-05-11 13:56:35 +00:00
|
|
|
func raise(line int, format string, v ...interface{}) error {
|
|
|
|
return &ParseErr{
|
|
|
|
Line: line + 1,
|
|
|
|
Err: fmt.Errorf(format, v...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Test) parseLoad(lines []string, i int) (int, *loadCmd, error) {
|
|
|
|
if !patLoad.MatchString(lines[i]) {
|
|
|
|
return i, nil, raise(i, "invalid load command. (load <step:duration>)")
|
|
|
|
}
|
|
|
|
parts := patLoad.FindStringSubmatch(lines[i])
|
|
|
|
|
2016-01-29 14:23:11 +00:00
|
|
|
gap, err := model.ParseDuration(parts[1])
|
2015-05-11 13:56:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
|
|
|
}
|
2016-01-29 14:23:11 +00:00
|
|
|
cmd := newLoadCmd(time.Duration(gap))
|
2015-05-11 13:56:35 +00:00
|
|
|
for i+1 < len(lines) {
|
|
|
|
i++
|
|
|
|
defLine := lines[i]
|
|
|
|
if len(defLine) == 0 {
|
|
|
|
i--
|
|
|
|
break
|
|
|
|
}
|
|
|
|
metric, vals, err := parseSeriesDesc(defLine)
|
|
|
|
if err != nil {
|
2015-08-02 22:26:21 +00:00
|
|
|
if perr, ok := err.(*ParseErr); ok {
|
|
|
|
perr.Line = i + 1
|
|
|
|
}
|
2015-05-11 13:56:35 +00:00
|
|
|
return i, nil, err
|
|
|
|
}
|
|
|
|
cmd.set(metric, vals...)
|
|
|
|
}
|
|
|
|
return i, cmd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Test) parseEval(lines []string, i int) (int, *evalCmd, error) {
|
|
|
|
if !patEvalInstant.MatchString(lines[i]) {
|
|
|
|
return i, nil, raise(i, "invalid evaluation command. (eval[_fail|_ordered] instant [at <offset:duration>] <query>")
|
|
|
|
}
|
|
|
|
parts := patEvalInstant.FindStringSubmatch(lines[i])
|
|
|
|
var (
|
|
|
|
mod = parts[1]
|
|
|
|
at = parts[2]
|
|
|
|
qry = parts[3]
|
|
|
|
)
|
|
|
|
expr, err := ParseExpr(qry)
|
|
|
|
if err != nil {
|
2015-08-02 22:26:21 +00:00
|
|
|
if perr, ok := err.(*ParseErr); ok {
|
|
|
|
perr.Line = i + 1
|
|
|
|
perr.Pos += strings.Index(lines[i], qry)
|
|
|
|
}
|
|
|
|
return i, nil, err
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 14:23:11 +00:00
|
|
|
offset, err := model.ParseDuration(at)
|
2015-05-11 13:56:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return i, nil, raise(i, "invalid step definition %q: %s", parts[1], err)
|
|
|
|
}
|
2016-01-29 14:23:11 +00:00
|
|
|
ts := testStartTime.Add(time.Duration(offset))
|
2015-05-11 13:56:35 +00:00
|
|
|
|
|
|
|
cmd := newEvalCmd(expr, ts, ts, 0)
|
|
|
|
switch mod {
|
|
|
|
case "ordered":
|
|
|
|
cmd.ordered = true
|
|
|
|
case "fail":
|
|
|
|
cmd.fail = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 1; i+1 < len(lines); j++ {
|
|
|
|
i++
|
|
|
|
defLine := lines[i]
|
|
|
|
if len(defLine) == 0 {
|
|
|
|
i--
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if f, err := parseNumber(defLine); err == nil {
|
2016-12-23 12:51:59 +00:00
|
|
|
cmd.expect(0, nil, sequenceValue{value: f})
|
2015-05-11 13:56:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
metric, vals, err := parseSeriesDesc(defLine)
|
|
|
|
if err != nil {
|
2015-08-02 22:26:21 +00:00
|
|
|
if perr, ok := err.(*ParseErr); ok {
|
|
|
|
perr.Line = i + 1
|
|
|
|
}
|
2015-05-11 13:56:35 +00:00
|
|
|
return i, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently, we are not expecting any matrices.
|
|
|
|
if len(vals) > 1 {
|
|
|
|
return i, nil, raise(i, "expecting multiple values in instant evaluation not allowed")
|
|
|
|
}
|
|
|
|
cmd.expect(j, metric, vals...)
|
|
|
|
}
|
|
|
|
return i, cmd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the given command sequence and appends it to the test.
|
|
|
|
func (t *Test) parse(input string) error {
|
|
|
|
// Trim lines and remove comments.
|
|
|
|
lines := strings.Split(input, "\n")
|
|
|
|
for i, l := range lines {
|
|
|
|
l = strings.TrimSpace(l)
|
|
|
|
if strings.HasPrefix(l, "#") {
|
|
|
|
l = ""
|
|
|
|
}
|
|
|
|
lines[i] = l
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Scan for steps line by line.
|
|
|
|
for i := 0; i < len(lines); i++ {
|
|
|
|
l := lines[i]
|
|
|
|
if len(l) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var cmd testCommand
|
|
|
|
|
|
|
|
switch c := strings.ToLower(patSpace.Split(l, 2)[0]); {
|
|
|
|
case c == "clear":
|
|
|
|
cmd = &clearCmd{}
|
|
|
|
case c == "load":
|
|
|
|
i, cmd, err = t.parseLoad(lines, i)
|
|
|
|
case strings.HasPrefix(c, "eval"):
|
|
|
|
i, cmd, err = t.parseEval(lines, i)
|
|
|
|
default:
|
|
|
|
return raise(i, "invalid command %q", l)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.cmds = append(t.cmds, cmd)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// testCommand is an interface that ensures that only the package internal
|
|
|
|
// types can be a valid command for a test.
|
|
|
|
type testCommand interface {
|
|
|
|
testCmd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*clearCmd) testCmd() {}
|
|
|
|
func (*loadCmd) testCmd() {}
|
|
|
|
func (*evalCmd) testCmd() {}
|
|
|
|
|
|
|
|
// loadCmd is a command that loads sequences of sample values for specific
|
|
|
|
// metrics into the storage.
|
|
|
|
type loadCmd struct {
|
|
|
|
gap time.Duration
|
2016-12-23 12:51:59 +00:00
|
|
|
metrics map[uint64]labels.Labels
|
2016-12-24 10:23:06 +00:00
|
|
|
defs map[uint64][]Point
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLoadCmd(gap time.Duration) *loadCmd {
|
|
|
|
return &loadCmd{
|
|
|
|
gap: gap,
|
2016-12-23 12:51:59 +00:00
|
|
|
metrics: map[uint64]labels.Labels{},
|
2016-12-24 10:23:06 +00:00
|
|
|
defs: map[uint64][]Point{},
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cmd loadCmd) String() string {
|
|
|
|
return "load"
|
|
|
|
}
|
|
|
|
|
|
|
|
// set a sequence of sample values for the given metric.
|
2016-12-23 12:51:59 +00:00
|
|
|
func (cmd *loadCmd) set(m labels.Labels, vals ...sequenceValue) {
|
|
|
|
h := m.Hash()
|
2015-05-11 13:56:35 +00:00
|
|
|
|
2016-12-24 10:23:06 +00:00
|
|
|
samples := make([]Point, 0, len(vals))
|
2015-05-11 13:56:35 +00:00
|
|
|
ts := testStartTime
|
|
|
|
for _, v := range vals {
|
|
|
|
if !v.omitted {
|
2016-12-24 10:23:06 +00:00
|
|
|
samples = append(samples, Point{
|
|
|
|
T: ts.UnixNano() / int64(time.Millisecond/time.Nanosecond),
|
|
|
|
V: v.value,
|
2015-05-11 13:56:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
ts = ts.Add(cmd.gap)
|
|
|
|
}
|
2016-12-23 12:51:59 +00:00
|
|
|
cmd.defs[h] = samples
|
|
|
|
cmd.metrics[h] = m
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// append the defined time series to the storage.
|
2017-01-13 13:48:01 +00:00
|
|
|
func (cmd *loadCmd) append(a storage.Appender) error {
|
2016-12-28 08:16:48 +00:00
|
|
|
for h, smpls := range cmd.defs {
|
|
|
|
m := cmd.metrics[h]
|
|
|
|
|
|
|
|
for _, s := range smpls {
|
2017-01-13 13:48:01 +00:00
|
|
|
ref, err := a.SetSeries(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.Add(ref, s.T, s.V); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-28 08:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-13 13:48:01 +00:00
|
|
|
return nil
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// evalCmd is a command that evaluates an expression for the given time (range)
|
|
|
|
// and expects a specific result.
|
|
|
|
type evalCmd struct {
|
|
|
|
expr Expr
|
2016-12-23 12:51:59 +00:00
|
|
|
start, end time.Time
|
2015-05-11 13:56:35 +00:00
|
|
|
interval time.Duration
|
|
|
|
|
|
|
|
instant bool
|
|
|
|
fail, ordered bool
|
|
|
|
|
2016-12-23 12:51:59 +00:00
|
|
|
metrics map[uint64]labels.Labels
|
|
|
|
expected map[uint64]entry
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type entry struct {
|
|
|
|
pos int
|
|
|
|
vals []sequenceValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e entry) String() string {
|
|
|
|
return fmt.Sprintf("%d: %s", e.pos, e.vals)
|
|
|
|
}
|
|
|
|
|
2016-12-23 12:51:59 +00:00
|
|
|
func newEvalCmd(expr Expr, start, end time.Time, interval time.Duration) *evalCmd {
|
2015-05-11 13:56:35 +00:00
|
|
|
return &evalCmd{
|
|
|
|
expr: expr,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
interval: interval,
|
|
|
|
instant: start == end && interval == 0,
|
|
|
|
|
2016-12-23 12:51:59 +00:00
|
|
|
metrics: map[uint64]labels.Labels{},
|
|
|
|
expected: map[uint64]entry{},
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *evalCmd) String() string {
|
|
|
|
return "eval"
|
|
|
|
}
|
|
|
|
|
|
|
|
// expect adds a new metric with a sequence of values to the set of expected
|
|
|
|
// results for the query.
|
2016-12-23 12:51:59 +00:00
|
|
|
func (ev *evalCmd) expect(pos int, m labels.Labels, vals ...sequenceValue) {
|
2015-05-11 13:56:35 +00:00
|
|
|
if m == nil {
|
|
|
|
ev.expected[0] = entry{pos: pos, vals: vals}
|
|
|
|
return
|
|
|
|
}
|
2016-12-23 12:51:59 +00:00
|
|
|
h := m.Hash()
|
|
|
|
ev.metrics[h] = m
|
|
|
|
ev.expected[h] = entry{pos: pos, vals: vals}
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// compareResult compares the result value with the defined expectation.
|
2016-12-23 12:51:59 +00:00
|
|
|
func (ev *evalCmd) compareResult(result Value) error {
|
2015-05-11 13:56:35 +00:00
|
|
|
switch val := result.(type) {
|
2016-12-24 09:42:54 +00:00
|
|
|
case Matrix:
|
2015-05-11 13:56:35 +00:00
|
|
|
if ev.instant {
|
|
|
|
return fmt.Errorf("received range result on instant evaluation")
|
|
|
|
}
|
2016-12-23 12:51:59 +00:00
|
|
|
seen := map[uint64]bool{}
|
2015-05-11 13:56:35 +00:00
|
|
|
for pos, v := range val {
|
2016-12-23 12:51:59 +00:00
|
|
|
fp := v.Metric.Hash()
|
2015-05-11 13:56:35 +00:00
|
|
|
if _, ok := ev.metrics[fp]; !ok {
|
2015-08-24 16:04:41 +00:00
|
|
|
return fmt.Errorf("unexpected metric %s in result", v.Metric)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
exp := ev.expected[fp]
|
|
|
|
if ev.ordered && exp.pos != pos+1 {
|
2015-08-24 16:04:41 +00:00
|
|
|
return fmt.Errorf("expected metric %s with %v at position %d but was at %d", v.Metric, exp.vals, exp.pos, pos+1)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
for i, expVal := range exp.vals {
|
2016-12-24 10:29:39 +00:00
|
|
|
if !almostEqual(expVal.value, v.Points[i].V) {
|
|
|
|
return fmt.Errorf("expected %v for %s but got %v", expVal, v.Metric, v.Points)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
seen[fp] = true
|
|
|
|
}
|
|
|
|
for fp, expVals := range ev.expected {
|
|
|
|
if !seen[fp] {
|
|
|
|
return fmt.Errorf("expected metric %s with %v not found", ev.metrics[fp], expVals)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 09:40:09 +00:00
|
|
|
case Vector:
|
2015-05-11 13:56:35 +00:00
|
|
|
if !ev.instant {
|
2015-09-18 14:50:13 +00:00
|
|
|
return fmt.Errorf("received instant result on range evaluation")
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
2016-12-28 08:16:48 +00:00
|
|
|
|
2016-12-29 08:27:30 +00:00
|
|
|
fmt.Println("vector result", len(val), ev.expr)
|
|
|
|
for _, ss := range val {
|
|
|
|
fmt.Println(" ", ss.Metric, ss.Point)
|
|
|
|
}
|
|
|
|
|
2016-12-23 12:51:59 +00:00
|
|
|
seen := map[uint64]bool{}
|
2015-05-11 13:56:35 +00:00
|
|
|
for pos, v := range val {
|
2016-12-23 12:51:59 +00:00
|
|
|
fp := v.Metric.Hash()
|
2015-05-11 13:56:35 +00:00
|
|
|
if _, ok := ev.metrics[fp]; !ok {
|
2015-08-24 16:04:41 +00:00
|
|
|
return fmt.Errorf("unexpected metric %s in result", v.Metric)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
exp := ev.expected[fp]
|
|
|
|
if ev.ordered && exp.pos != pos+1 {
|
2015-08-24 16:04:41 +00:00
|
|
|
return fmt.Errorf("expected metric %s with %v at position %d but was at %d", v.Metric, exp.vals, exp.pos, pos+1)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
2016-12-28 08:16:48 +00:00
|
|
|
if !almostEqual(exp.vals[0].value, v.V) {
|
2016-12-24 10:23:06 +00:00
|
|
|
return fmt.Errorf("expected %v for %s but got %v", exp.vals[0].value, v.Metric, v.V)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seen[fp] = true
|
|
|
|
}
|
|
|
|
for fp, expVals := range ev.expected {
|
|
|
|
if !seen[fp] {
|
|
|
|
return fmt.Errorf("expected metric %s with %v not found", ev.metrics[fp], expVals)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 09:44:04 +00:00
|
|
|
case Scalar:
|
2016-12-24 10:23:06 +00:00
|
|
|
if !almostEqual(ev.expected[0].vals[0].value, val.V) {
|
|
|
|
return fmt.Errorf("expected Scalar %v but got %v", val.V, ev.expected[0].vals[0].value)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("promql.Test.compareResult: unexpected result type %T", result))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearCmd is a command that wipes the test's storage state.
|
|
|
|
type clearCmd struct{}
|
|
|
|
|
|
|
|
func (cmd clearCmd) String() string {
|
|
|
|
return "clear"
|
|
|
|
}
|
|
|
|
|
2016-03-10 03:29:02 +00:00
|
|
|
// RunAsBenchmark runs the test in benchmark mode.
|
|
|
|
// This will not count any loads or non eval functions.
|
|
|
|
func (t *Test) RunAsBenchmark(b *Benchmark) error {
|
|
|
|
for _, cmd := range t.cmds {
|
|
|
|
|
|
|
|
switch cmd.(type) {
|
|
|
|
// Only time the "eval" command.
|
|
|
|
case *evalCmd:
|
|
|
|
err := t.exec(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if b.iterCount == 0 {
|
|
|
|
b.b.StopTimer()
|
|
|
|
err := t.exec(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.b.StartTimer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-11 13:56:35 +00:00
|
|
|
// Run executes the command sequence of the test. Until the maximum error number
|
|
|
|
// is reached, evaluation errors do not terminate execution.
|
|
|
|
func (t *Test) Run() error {
|
|
|
|
for _, cmd := range t.cmds {
|
|
|
|
err := t.exec(cmd)
|
|
|
|
// TODO(fabxc): aggregate command errors, yield diffs for result
|
|
|
|
// comparison errors.
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-02 08:16:56 +00:00
|
|
|
// exec processes a single step of the test.
|
2015-05-11 13:56:35 +00:00
|
|
|
func (t *Test) exec(tc testCommand) error {
|
|
|
|
switch cmd := tc.(type) {
|
|
|
|
case *clearCmd:
|
|
|
|
t.clear()
|
|
|
|
|
|
|
|
case *loadCmd:
|
2016-12-25 10:34:22 +00:00
|
|
|
app, err := t.storage.Appender()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-13 13:48:01 +00:00
|
|
|
if err := cmd.append(app); err != nil {
|
|
|
|
app.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2016-12-25 10:34:22 +00:00
|
|
|
|
|
|
|
if err := app.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-11 13:56:35 +00:00
|
|
|
|
|
|
|
case *evalCmd:
|
|
|
|
q := t.queryEngine.newQuery(cmd.expr, cmd.start, cmd.end, cmd.interval)
|
2016-09-15 22:58:06 +00:00
|
|
|
res := q.Exec(t.context)
|
2015-05-11 13:56:35 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
if cmd.fail {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-28 08:16:48 +00:00
|
|
|
return fmt.Errorf("error evaluating query %q: %s", cmd.expr, res.Err)
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
if res.Err == nil && cmd.fail {
|
|
|
|
return fmt.Errorf("expected error evaluating query but got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cmd.compareResult(res.Value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error in %s %s: %s", cmd, cmd.expr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("promql.Test.exec: unknown test command type")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the current test storage of all inserted samples.
|
|
|
|
func (t *Test) clear() {
|
2016-12-25 10:34:22 +00:00
|
|
|
if t.storage != nil {
|
|
|
|
if err := t.storage.Close(); err != nil {
|
|
|
|
t.T.Fatalf("closing test storage: %s", err)
|
|
|
|
}
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
2016-09-15 22:58:06 +00:00
|
|
|
if t.cancelCtx != nil {
|
|
|
|
t.cancelCtx()
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
2016-12-25 10:34:22 +00:00
|
|
|
t.storage = testutil.NewStorage(t)
|
2015-05-11 13:56:35 +00:00
|
|
|
|
2016-12-28 08:16:48 +00:00
|
|
|
t.queryEngine = NewEngine(t.storage, nil)
|
2016-09-15 22:58:06 +00:00
|
|
|
t.context, t.cancelCtx = context.WithCancel(context.Background())
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 13:07:27 +00:00
|
|
|
// Close closes resources associated with the Test.
|
2015-05-11 13:56:35 +00:00
|
|
|
func (t *Test) Close() {
|
2016-09-15 22:58:06 +00:00
|
|
|
t.cancelCtx()
|
2016-12-25 10:34:22 +00:00
|
|
|
|
|
|
|
if err := t.storage.Close(); err != nil {
|
|
|
|
t.T.Fatalf("closing test storage: %s", err)
|
|
|
|
}
|
2015-05-11 13:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// samplesAlmostEqual returns true if the two sample lines only differ by a
|
|
|
|
// small relative error in their sample value.
|
|
|
|
func almostEqual(a, b float64) bool {
|
|
|
|
// NaN has no equality but for testing we still want to know whether both values
|
|
|
|
// are NaN.
|
|
|
|
if math.IsNaN(a) && math.IsNaN(b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cf. http://floating-point-gui.de/errors/comparison/
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := math.Abs(a - b)
|
|
|
|
|
|
|
|
if a == 0 || b == 0 || diff < minNormal {
|
|
|
|
return diff < epsilon*minNormal
|
|
|
|
}
|
|
|
|
return diff/(math.Abs(a)+math.Abs(b)) < epsilon
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNumber(s string) (float64, error) {
|
|
|
|
n, err := strconv.ParseInt(s, 0, 64)
|
|
|
|
f := float64(n)
|
|
|
|
if err != nil {
|
|
|
|
f, err = strconv.ParseFloat(s, 64)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("error parsing number: %s", err)
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|