refine test rewriting regex and add validation

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
This commit is contained in:
Jeanette Tan 2024-06-07 18:50:59 +08:00
parent 9adc1699c3
commit b0e320425c

View file

@ -48,6 +48,9 @@ var (
patLoad = regexp.MustCompile(`^load(?:_(with_nhcb))?\s+(.+?)$`) patLoad = regexp.MustCompile(`^load(?:_(with_nhcb))?\s+(.+?)$`)
patEvalInstant = regexp.MustCompile(`^eval(?:_(with_nhcb))?(?:_(fail|warn|ordered))?\s+instant\s+(?:at\s+(.+?))?\s+(.+)$`) patEvalInstant = regexp.MustCompile(`^eval(?:_(with_nhcb))?(?:_(fail|warn|ordered))?\s+instant\s+(?:at\s+(.+?))?\s+(.+)$`)
patEvalRange = regexp.MustCompile(`^eval(?:_(fail|warn))?\s+range\s+from\s+(.+)\s+to\s+(.+)\s+step\s+(.+?)\s+(.+)$`) patEvalRange = regexp.MustCompile(`^eval(?:_(fail|warn))?\s+range\s+from\s+(.+)\s+to\s+(.+)\s+step\s+(.+?)\s+(.+)$`)
patWhitespace = regexp.MustCompile(`\s+`)
patBucket = regexp.MustCompile(`_bucket\b`)
patLE = regexp.MustCompile(`\ble\b`)
histogramBucketReplacements = []struct { histogramBucketReplacements = []struct {
pattern *regexp.Regexp pattern *regexp.Regexp
repl string repl string
@ -57,19 +60,19 @@ var (
repl: "", repl: "",
}, },
{ {
pattern: regexp.MustCompile(`\s+by\s+\(le\)`), pattern: regexp.MustCompile(`\s+by\s+\(\s*le\s*\)`),
repl: "", repl: "",
}, },
{ {
pattern: regexp.MustCompile(`\(le,\s*`), pattern: regexp.MustCompile(`\(\s*le\s*,\s*`),
repl: "(", repl: "(",
}, },
{ {
pattern: regexp.MustCompile(`,\s*le,\s*`), pattern: regexp.MustCompile(`,\s*le\s*,\s*`),
repl: ", ", repl: ", ",
}, },
{ {
pattern: regexp.MustCompile(`,\s*le\)`), pattern: regexp.MustCompile(`,\s*le\s*\)`),
repl: ")", repl: ")",
}, },
} }
@ -1030,11 +1033,20 @@ func (t *test) execInstantEval(cmd *evalCmd, engine promql.QueryEngine) error {
} }
if cmd.withNHCB { if cmd.withNHCB {
if !strings.Contains(iq.expr, "_bucket") { if !strings.Contains(iq.expr, "_bucket") {
return fmt.Errorf("expected '_bucket' in the expression %q", iq.expr) return fmt.Errorf("expected '_bucket' in the expression '%q'", iq.expr)
} }
origExpr := iq.expr
for _, rep := range histogramBucketReplacements { for _, rep := range histogramBucketReplacements {
iq.expr = rep.pattern.ReplaceAllString(iq.expr, rep.repl) iq.expr = rep.pattern.ReplaceAllString(iq.expr, rep.repl)
} }
switch {
case patWhitespace.ReplaceAllString(iq.expr, "") == patWhitespace.ReplaceAllString(origExpr, ""):
return fmt.Errorf("query rewrite of '%q' had no effect", iq.expr)
case patBucket.MatchString(iq.expr):
return fmt.Errorf("rewritten query '%q' still has '_bucket'", iq.expr)
case patLE.MatchString(iq.expr):
return fmt.Errorf("rewritten query '%q' still has 'le'", iq.expr)
}
if err := t.runInstantQuery(iq, cmd, engine); err != nil { if err := t.runInstantQuery(iq, cmd, engine); err != nil {
return err return err
} }