mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
promqltest: improve error messages when unexpected type is returned, and fix inverted expected and actual values in error message when scalar value does not match expected
Signed-off-by: Charles Korn <charles.korn@grafana.com>
This commit is contained in:
parent
c2bc6cfe97
commit
6548421f61
|
@ -651,6 +651,7 @@ type evalCmd struct {
|
|||
expectedFailRegexp *regexp.Regexp
|
||||
|
||||
metrics map[uint64]labels.Labels
|
||||
expectScalar bool
|
||||
expected map[uint64]entry
|
||||
}
|
||||
|
||||
|
@ -695,12 +696,15 @@ func (ev *evalCmd) String() string {
|
|||
// expect adds a sequence of values to the set of expected
|
||||
// results for the query.
|
||||
func (ev *evalCmd) expect(pos int, vals ...parser.SequenceValue) {
|
||||
ev.expectScalar = true
|
||||
ev.expected[0] = entry{pos: pos, vals: vals}
|
||||
}
|
||||
|
||||
// expectMetric adds a new metric with a sequence of values to the set of expected
|
||||
// results for the query.
|
||||
func (ev *evalCmd) expectMetric(pos int, m labels.Labels, vals ...parser.SequenceValue) {
|
||||
ev.expectScalar = false
|
||||
|
||||
h := m.Hash()
|
||||
ev.metrics[h] = m
|
||||
ev.expected[h] = entry{pos: pos, vals: vals}
|
||||
|
@ -714,6 +718,10 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
|
|||
return fmt.Errorf("expected ordered result, but query returned a matrix")
|
||||
}
|
||||
|
||||
if ev.expectScalar {
|
||||
return fmt.Errorf("expected scalar result, but got matrix %s", val.String())
|
||||
}
|
||||
|
||||
if err := assertMatrixSorted(val); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -782,6 +790,10 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
|
|||
}
|
||||
|
||||
case promql.Vector:
|
||||
if ev.expectScalar {
|
||||
return fmt.Errorf("expected scalar result, but got vector %s", val.String())
|
||||
}
|
||||
|
||||
seen := map[uint64]bool{}
|
||||
for pos, v := range val {
|
||||
fp := v.Metric.Hash()
|
||||
|
@ -820,15 +832,15 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
|
|||
}
|
||||
|
||||
case promql.Scalar:
|
||||
if len(ev.expected) != 1 {
|
||||
return fmt.Errorf("expected vector result, but got scalar %s", val.String())
|
||||
if !ev.expectScalar {
|
||||
return fmt.Errorf("expected vector or matrix result, but got %s", val.String())
|
||||
}
|
||||
exp0 := ev.expected[0].vals[0]
|
||||
if exp0.Histogram != nil {
|
||||
return fmt.Errorf("expected Histogram %v but got scalar %s", exp0.Histogram.TestExpression(), val.String())
|
||||
return fmt.Errorf("expected histogram %v but got %s", exp0.Histogram.TestExpression(), val.String())
|
||||
}
|
||||
if !almost.Equal(exp0.Value, val.V, defaultEpsilon) {
|
||||
return fmt.Errorf("expected Scalar %v but got %v", val.V, exp0.Value)
|
||||
return fmt.Errorf("expected scalar %v but got %v", exp0.Value, val.V)
|
||||
}
|
||||
|
||||
default:
|
||||
|
|
|
@ -554,6 +554,43 @@ eval range from 0 to 5m step 5m testmetric
|
|||
`,
|
||||
expectedError: `error in eval testmetric (line 5): expected float value at index 0 for {__name__="testmetric"} to have timestamp 300000, but it had timestamp 0 (result has 1 float point [3 @[0]] and 1 histogram point [{count:0, sum:0} @[300000]])`,
|
||||
},
|
||||
"instant query with expected scalar result": {
|
||||
input: `
|
||||
eval instant at 1m 3
|
||||
3
|
||||
`,
|
||||
},
|
||||
"instant query with unexpected scalar result": {
|
||||
input: `
|
||||
eval instant at 1m 3
|
||||
2
|
||||
`,
|
||||
expectedError: `error in eval 3 (line 2): expected scalar 2 but got 3`,
|
||||
},
|
||||
"instant query that returns a scalar but expects a vector": {
|
||||
input: `
|
||||
eval instant at 1m 3
|
||||
{} 3
|
||||
`,
|
||||
expectedError: `error in eval 3 (line 2): expected vector or matrix result, but got scalar: 3 @[60000]`,
|
||||
},
|
||||
"instant query that returns a vector but expects a scalar": {
|
||||
input: `
|
||||
eval instant at 1m vector(3)
|
||||
3
|
||||
`,
|
||||
expectedError: `error in eval vector(3) (line 2): expected scalar result, but got vector {} => 3 @[60000]`,
|
||||
},
|
||||
"range query that returns a matrix but expects a scalar": {
|
||||
input: `
|
||||
eval range from 0 to 1m step 30s vector(3)
|
||||
3
|
||||
`,
|
||||
expectedError: `error in eval vector(3) (line 2): expected scalar result, but got matrix {} =>
|
||||
3 @[0]
|
||||
3 @[30000]
|
||||
3 @[60000]`,
|
||||
},
|
||||
}
|
||||
|
||||
for name, testCase := range testCases {
|
||||
|
|
Loading…
Reference in a new issue