2022-07-20 16:53:12 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddExtraLabels(t *testing.T) {
|
|
|
|
f := func(s, extraLabels, expectedResult string) {
|
|
|
|
t.Helper()
|
|
|
|
result := addExtraLabels(nil, []byte(s), extraLabels)
|
|
|
|
if string(result) != expectedResult {
|
|
|
|
t.Fatalf("unexpected result; got\n%s\nwant\n%s", result, expectedResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", `foo="bar"`, "")
|
|
|
|
f("a 123", `foo="bar"`, `a{foo="bar"} 123`+"\n")
|
|
|
|
f(`a{b="c"} 1.3`, `foo="bar"`, `a{foo="bar",b="c"} 1.3`+"\n")
|
|
|
|
f(`a{b="c}{"} 1.3`, `foo="bar",baz="x"`, `a{foo="bar",baz="x",b="c}{"} 1.3`+"\n")
|
|
|
|
f(`foo 1
|
|
|
|
bar{a="x"} 2
|
|
|
|
`, `foo="bar"`, `foo{foo="bar"} 1
|
|
|
|
bar{foo="bar",a="x"} 2
|
2022-07-21 18:35:46 +03:00
|
|
|
`)
|
|
|
|
f(`
|
|
|
|
foo 1
|
|
|
|
# some counter
|
|
|
|
# type foobar counter
|
2022-07-21 18:42:53 +03:00
|
|
|
foobar{a="b",c="d"} 4`, `x="y"`, `foo{x="y"} 1
|
2022-07-21 18:35:46 +03:00
|
|
|
# some counter
|
|
|
|
# type foobar counter
|
|
|
|
foobar{x="y",a="b",c="d"} 4
|
2022-07-20 16:53:12 +03:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitPushFailure(t *testing.T) {
|
2022-07-21 19:45:07 +03:00
|
|
|
f := func(pushURL string, interval time.Duration, extraLabels string) {
|
2022-07-20 16:53:12 +03:00
|
|
|
t.Helper()
|
2022-07-21 19:45:07 +03:00
|
|
|
if err := InitPush(pushURL, interval, extraLabels, false); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2022-07-20 16:53:12 +03:00
|
|
|
}
|
|
|
|
|
2022-07-21 19:45:07 +03:00
|
|
|
// Invalid url
|
|
|
|
f("foobar", time.Second, "")
|
|
|
|
f("aaa://foobar", time.Second, "")
|
|
|
|
f("http:///bar", time.Second, "")
|
|
|
|
|
2022-07-20 16:53:12 +03:00
|
|
|
// Non-positive interval
|
2022-07-21 19:45:07 +03:00
|
|
|
f("http://foobar", 0, "")
|
|
|
|
f("http://foobar", -time.Second, "")
|
2022-07-20 16:53:12 +03:00
|
|
|
|
|
|
|
// Invalid extraLabels
|
2022-07-21 19:45:07 +03:00
|
|
|
f("http://foobar", time.Second, "foo")
|
|
|
|
f("http://foobar", time.Second, "foo{bar")
|
|
|
|
f("http://foobar", time.Second, "foo=bar")
|
|
|
|
f("http://foobar", time.Second, "foo='bar'")
|
|
|
|
f("http://foobar", time.Second, `foo="bar",baz`)
|
|
|
|
f("http://foobar", time.Second, `{foo="bar"}`)
|
|
|
|
f("http://foobar", time.Second, `a{foo="bar"}`)
|
2022-07-20 16:53:12 +03:00
|
|
|
}
|