mirror of https://github.com/prometheus/prometheus
Merge pull request #14680 from charleskorn/promqltest-scalar
promqltest: improve error messages when unexpected type is returned, and fix inverted expected and actual values in error messagepull/14734/head
commit
70bb219d33
|
@ -652,6 +652,7 @@ type evalCmd struct {
|
||||||
expectedFailRegexp *regexp.Regexp
|
expectedFailRegexp *regexp.Regexp
|
||||||
|
|
||||||
metrics map[uint64]labels.Labels
|
metrics map[uint64]labels.Labels
|
||||||
|
expectScalar bool
|
||||||
expected map[uint64]entry
|
expected map[uint64]entry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -696,12 +697,15 @@ func (ev *evalCmd) String() string {
|
||||||
// expect adds a sequence of values to the set of expected
|
// expect adds a sequence of values to the set of expected
|
||||||
// results for the query.
|
// results for the query.
|
||||||
func (ev *evalCmd) expect(pos int, vals ...parser.SequenceValue) {
|
func (ev *evalCmd) expect(pos int, vals ...parser.SequenceValue) {
|
||||||
|
ev.expectScalar = true
|
||||||
ev.expected[0] = entry{pos: pos, vals: vals}
|
ev.expected[0] = entry{pos: pos, vals: vals}
|
||||||
}
|
}
|
||||||
|
|
||||||
// expectMetric adds a new metric with a sequence of values to the set of expected
|
// expectMetric adds a new metric with a sequence of values to the set of expected
|
||||||
// results for the query.
|
// results for the query.
|
||||||
func (ev *evalCmd) expectMetric(pos int, m labels.Labels, vals ...parser.SequenceValue) {
|
func (ev *evalCmd) expectMetric(pos int, m labels.Labels, vals ...parser.SequenceValue) {
|
||||||
|
ev.expectScalar = false
|
||||||
|
|
||||||
h := m.Hash()
|
h := m.Hash()
|
||||||
ev.metrics[h] = m
|
ev.metrics[h] = m
|
||||||
ev.expected[h] = entry{pos: pos, vals: vals}
|
ev.expected[h] = entry{pos: pos, vals: vals}
|
||||||
|
@ -715,6 +719,10 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
|
||||||
return fmt.Errorf("expected ordered result, but query returned a matrix")
|
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 {
|
if err := assertMatrixSorted(val); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -783,6 +791,10 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
case promql.Vector:
|
case promql.Vector:
|
||||||
|
if ev.expectScalar {
|
||||||
|
return fmt.Errorf("expected scalar result, but got vector %s", val.String())
|
||||||
|
}
|
||||||
|
|
||||||
seen := map[uint64]bool{}
|
seen := map[uint64]bool{}
|
||||||
for pos, v := range val {
|
for pos, v := range val {
|
||||||
fp := v.Metric.Hash()
|
fp := v.Metric.Hash()
|
||||||
|
@ -821,15 +833,15 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
case promql.Scalar:
|
case promql.Scalar:
|
||||||
if len(ev.expected) != 1 {
|
if !ev.expectScalar {
|
||||||
return fmt.Errorf("expected vector result, but got scalar %s", val.String())
|
return fmt.Errorf("expected vector or matrix result, but got %s", val.String())
|
||||||
}
|
}
|
||||||
exp0 := ev.expected[0].vals[0]
|
exp0 := ev.expected[0].vals[0]
|
||||||
if exp0.Histogram != nil {
|
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) {
|
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:
|
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]])`,
|
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 {
|
for name, testCase := range testCases {
|
||||||
|
|
Loading…
Reference in New Issue