-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparse_test.go
89 lines (85 loc) · 1.97 KB
/
parse_test.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestParsePrometheus(t *testing.T) {
fp := func(f float64) (p *float64) {
p = new(float64)
*p = f
return p
}
for name, testcase := range map[string]struct {
input string
obs observation
err bool
}{
"only spaces": {
input: ` `,
err: true,
},
"empty": {
input: ``,
err: true,
},
"no braces": {
input: `foo 1`,
err: true,
},
"leading space": {
input: ` foo{} 1`,
obs: observation{Name: "foo", Value: fp(1.00), Labels: map[string]string{}},
},
"trailing space": {
input: `foo{} 1 `,
obs: observation{Name: "foo", Value: fp(1.00), Labels: map[string]string{}},
},
"ascii value": {
input: `foo{} A`,
err: true,
},
"most basic": {
input: `foo{} 1`,
obs: observation{Name: "foo", Value: fp(1.00), Labels: map[string]string{}},
},
"with label": {
input: `foo{code="200"} 2.34`,
obs: observation{Name: "foo", Value: fp(2.34), Labels: map[string]string{"code": "200"}},
},
"missing quotes": {
input: `foo{code=200} 2.34`,
err: true,
},
"missing closing brace": {
input: `foo{code="200" 7`,
err: true,
},
"double closing brace": {
input: `foo{code="200"}} 7`,
err: true,
},
"two labels": {
input: `foo{code="200",err="false"} 7`,
obs: observation{Name: "foo", Value: fp(7.00), Labels: map[string]string{"code": "200", "err": "false"}},
},
"space between labels": {
input: `foo{code="200", err="false"} 7`,
err: true,
},
"space instead of comma": {
input: `foo{code="200" err="false"} 7`,
err: true,
},
} {
t.Run(name, func(t *testing.T) {
var obs observation
err := prometheusUnmarshal([]byte(testcase.input), &obs)
if want, have := testcase.err, err != nil; want != have {
t.Fatalf("err: want %v, have %v (%v)", want, have, err)
}
if want, have := testcase.obs, obs; !cmp.Equal(want, have) {
t.Fatal(cmp.Diff(want, have))
}
})
}
}