File: validator_test.go

package info (click to toggle)
golang-github-victoriametrics-metrics 1.35.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 308 kB
  • sloc: makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,011 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package metrics

import (
	"testing"
)

func TestValidateMetricSuccess(t *testing.T) {
	f := func(s string) {
		t.Helper()
		if err := validateMetric(s); err != nil {
			t.Fatalf("cannot validate %q: %s", s, err)
		}
	}
	f("a")
	f("_9:8")
	f("a{}")
	f(`a{foo="bar"}`)
	f(`foo{bar="baz", x="y\"z"}`)
	f(`foo{bar="b}az"}`)
	f(`:foo:bar{bar="a",baz="b"}`)
	f(`some.foo{bar="baz"}`)
}

func TestValidateMetricError(t *testing.T) {
	f := func(s string) {
		t.Helper()
		if err := validateMetric(s); err == nil {
			t.Fatalf("expecting non-nil error when validating %q", s)
		}
	}
	f("")
	f("{}")

	// superflouos space
	f("a ")
	f(" a")
	f(" a ")
	f("a {}")
	f("a{} ")
	f("a{ }")
	f(`a{foo ="bar"}`)
	f(`a{ foo="bar"}`)
	f(`a{foo= "bar"}`)
	f(`a{foo="bar" }`)
	f(`a{foo="bar" ,baz="a"}`)

	// invalid tags
	f("a{foo}")
	f("a{=}")
	f(`a{=""}`)
	f(`a{`)
	f(`a}`)
	f(`a{foo=}`)
	f(`a{foo="`)
	f(`a{foo="}`)
	f(`a{foo="bar",}`)
	f(`a{foo="bar", x`)
	f(`a{foo="bar", x=`)
	f(`a{foo="bar", x="`)
	f(`a{foo="bar", x="}`)
}