mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/promrelabel: add test for IfExpression.String() function
While at it, simplify this function a bit after the commit 861852f262
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6462
This commit is contained in:
parent
63dfeec0de
commit
c1e32f4517
2 changed files with 44 additions and 6 deletions
|
@ -1,7 +1,6 @@
|
||||||
package promrelabel
|
package promrelabel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
@ -144,13 +143,13 @@ func (ie *IfExpression) String() string {
|
||||||
if len(ie.ies) == 1 {
|
if len(ie.ies) == 1 {
|
||||||
return ie.ies[0].String()
|
return ie.ies[0].String()
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
|
||||||
buf.WriteString(ie.ies[0].String())
|
b := append([]byte{}, ie.ies[0].String()...)
|
||||||
for _, e := range ie.ies[1:] {
|
for _, e := range ie.ies[1:] {
|
||||||
buf.WriteString(",")
|
b = append(b, ',')
|
||||||
buf.WriteString(e.String())
|
b = append(b, e.String()...)
|
||||||
}
|
}
|
||||||
return buf.String()
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ifExpression struct {
|
type ifExpression struct {
|
||||||
|
|
|
@ -12,11 +12,13 @@ import (
|
||||||
func TestIfExpressionParseFailure(t *testing.T) {
|
func TestIfExpressionParseFailure(t *testing.T) {
|
||||||
f := func(s string) {
|
f := func(s string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var ie IfExpression
|
var ie IfExpression
|
||||||
if err := ie.Parse(s); err == nil {
|
if err := ie.Parse(s); err == nil {
|
||||||
t.Fatalf("expecting non-nil error when parsing %q", s)
|
t.Fatalf("expecting non-nil error when parsing %q", s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f(`{`)
|
f(`{`)
|
||||||
f(`{foo`)
|
f(`{foo`)
|
||||||
f(`foo{`)
|
f(`foo{`)
|
||||||
|
@ -26,11 +28,13 @@ func TestIfExpressionParseFailure(t *testing.T) {
|
||||||
func TestIfExpressionParseSuccess(t *testing.T) {
|
func TestIfExpressionParseSuccess(t *testing.T) {
|
||||||
f := func(s string) {
|
f := func(s string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var ie IfExpression
|
var ie IfExpression
|
||||||
if err := ie.Parse(s); err != nil {
|
if err := ie.Parse(s); err != nil {
|
||||||
t.Fatalf("unexpected error: %s", err)
|
t.Fatalf("unexpected error: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f(`foo`)
|
f(`foo`)
|
||||||
f(`{foo="bar"}`)
|
f(`{foo="bar"}`)
|
||||||
f(`foo{bar=~"baz", x!="y"}`)
|
f(`foo{bar=~"baz", x!="y"}`)
|
||||||
|
@ -45,6 +49,7 @@ func TestIfExpressionParseSuccess(t *testing.T) {
|
||||||
func TestIfExpressionMarshalUnmarshalJSON(t *testing.T) {
|
func TestIfExpressionMarshalUnmarshalJSON(t *testing.T) {
|
||||||
f := func(s, jsonExpected string) {
|
f := func(s, jsonExpected string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var ie IfExpression
|
var ie IfExpression
|
||||||
if err := ie.Parse(s); err != nil {
|
if err := ie.Parse(s); err != nil {
|
||||||
t.Fatalf("cannot parse ifExpression %q: %s", s, err)
|
t.Fatalf("cannot parse ifExpression %q: %s", s, err)
|
||||||
|
@ -68,6 +73,7 @@ func TestIfExpressionMarshalUnmarshalJSON(t *testing.T) {
|
||||||
t.Fatalf("unexpected data after unmarshal/marshal cycle;\ngot\n%s\nwant\n%s", data2, jsonExpected)
|
t.Fatalf("unexpected data after unmarshal/marshal cycle;\ngot\n%s\nwant\n%s", data2, jsonExpected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f("foo", `"foo"`)
|
f("foo", `"foo"`)
|
||||||
f(`{foo="bar",baz=~"x.*"}`, `"{foo=\"bar\",baz=~\"x.*\"}"`)
|
f(`{foo="bar",baz=~"x.*"}`, `"{foo=\"bar\",baz=~\"x.*\"}"`)
|
||||||
f(`{a="b" or c="d",x="z"}`, `"{a=\"b\" or c=\"d\",x=\"z\"}"`)
|
f(`{a="b" or c="d",x="z"}`, `"{a=\"b\" or c=\"d\",x=\"z\"}"`)
|
||||||
|
@ -76,12 +82,14 @@ func TestIfExpressionMarshalUnmarshalJSON(t *testing.T) {
|
||||||
func TestIfExpressionUnmarshalFailure(t *testing.T) {
|
func TestIfExpressionUnmarshalFailure(t *testing.T) {
|
||||||
f := func(s string) {
|
f := func(s string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var ie IfExpression
|
var ie IfExpression
|
||||||
err := yaml.UnmarshalStrict([]byte(s), &ie)
|
err := yaml.UnmarshalStrict([]byte(s), &ie)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expecting non-nil error")
|
t.Fatalf("expecting non-nil error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f(`{`)
|
f(`{`)
|
||||||
f(`{x:y}`)
|
f(`{x:y}`)
|
||||||
f(`[1]`)
|
f(`[1]`)
|
||||||
|
@ -103,6 +111,7 @@ func TestIfExpressionUnmarshalFailure(t *testing.T) {
|
||||||
func TestIfExpressionUnmarshalSuccess(t *testing.T) {
|
func TestIfExpressionUnmarshalSuccess(t *testing.T) {
|
||||||
f := func(s string) {
|
f := func(s string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var ie IfExpression
|
var ie IfExpression
|
||||||
if err := yaml.UnmarshalStrict([]byte(s), &ie); err != nil {
|
if err := yaml.UnmarshalStrict([]byte(s), &ie); err != nil {
|
||||||
t.Fatalf("unexpected error during unmarshal: %s", err)
|
t.Fatalf("unexpected error during unmarshal: %s", err)
|
||||||
|
@ -116,6 +125,7 @@ func TestIfExpressionUnmarshalSuccess(t *testing.T) {
|
||||||
t.Fatalf("unexpected marshaled data;\ngot\n%s\nwant\n%s", b, s)
|
t.Fatalf("unexpected marshaled data;\ngot\n%s\nwant\n%s", b, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f(`'{}'`)
|
f(`'{}'`)
|
||||||
f(`foo`)
|
f(`foo`)
|
||||||
f(`foo{bar="baz"}`)
|
f(`foo{bar="baz"}`)
|
||||||
|
@ -126,9 +136,35 @@ func TestIfExpressionUnmarshalSuccess(t *testing.T) {
|
||||||
- bar{baz="abc"}`)
|
- bar{baz="abc"}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIfExpressionString(t *testing.T) {
|
||||||
|
f := func(s, resultExpected string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var ie IfExpression
|
||||||
|
if err := yaml.UnmarshalStrict([]byte(s), &ie); err != nil {
|
||||||
|
t.Fatalf("cannot unmarshal if expression: %s", err)
|
||||||
|
}
|
||||||
|
result := ie.String()
|
||||||
|
if result != resultExpected {
|
||||||
|
t.Fatalf("unexpected result\ngot\n%s\nwant\n%s", result, resultExpected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// empty filters
|
||||||
|
f(`'{}'`, `{}`)
|
||||||
|
|
||||||
|
// multiple fiters
|
||||||
|
f(`foo{bar="baz",a=~"bc.+",d!="e",g!~".*qwe"}`, `foo{bar="baz",a=~"bc.+",d!="e",g!~".*qwe"}`)
|
||||||
|
|
||||||
|
// multiple if expressions
|
||||||
|
f(`- foo
|
||||||
|
- bar{baz="abc"}`, `foo,bar{baz="abc"}`)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIfExpressionMatch(t *testing.T) {
|
func TestIfExpressionMatch(t *testing.T) {
|
||||||
f := func(ifExpr, metricWithLabels string) {
|
f := func(ifExpr, metricWithLabels string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var ie IfExpression
|
var ie IfExpression
|
||||||
if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil {
|
if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil {
|
||||||
t.Fatalf("unexpected error during unmarshal: %s", err)
|
t.Fatalf("unexpected error during unmarshal: %s", err)
|
||||||
|
@ -138,6 +174,7 @@ func TestIfExpressionMatch(t *testing.T) {
|
||||||
t.Fatalf("unexpected mismatch of ifExpr=%s for %s", ifExpr, metricWithLabels)
|
t.Fatalf("unexpected mismatch of ifExpr=%s for %s", ifExpr, metricWithLabels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f(`foo`, `foo`)
|
f(`foo`, `foo`)
|
||||||
f(`foo`, `foo{bar="baz",a="b"}`)
|
f(`foo`, `foo{bar="baz",a="b"}`)
|
||||||
f(`foo{bar="a"}`, `foo{bar="a"}`)
|
f(`foo{bar="a"}`, `foo{bar="a"}`)
|
||||||
|
@ -165,6 +202,7 @@ func TestIfExpressionMatch(t *testing.T) {
|
||||||
func TestIfExpressionMismatch(t *testing.T) {
|
func TestIfExpressionMismatch(t *testing.T) {
|
||||||
f := func(ifExpr, metricWithLabels string) {
|
f := func(ifExpr, metricWithLabels string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
var ie IfExpression
|
var ie IfExpression
|
||||||
if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil {
|
if err := yaml.UnmarshalStrict([]byte(ifExpr), &ie); err != nil {
|
||||||
t.Fatalf("unexpected error during unmarshal: %s", err)
|
t.Fatalf("unexpected error during unmarshal: %s", err)
|
||||||
|
@ -174,6 +212,7 @@ func TestIfExpressionMismatch(t *testing.T) {
|
||||||
t.Fatalf("unexpected match of ifExpr=%s for %s", ifExpr, metricWithLabels)
|
t.Fatalf("unexpected match of ifExpr=%s for %s", ifExpr, metricWithLabels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f(`foo`, `bar`)
|
f(`foo`, `bar`)
|
||||||
f(`foo`, `a{foo="bar"}`)
|
f(`foo`, `a{foo="bar"}`)
|
||||||
f(`foo{bar="a"}`, `foo`)
|
f(`foo{bar="a"}`, `foo`)
|
||||||
|
|
Loading…
Reference in a new issue