-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesc_test.go
More file actions
82 lines (70 loc) · 2 KB
/
desc_test.go
File metadata and controls
82 lines (70 loc) · 2 KB
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
package openmetrics_test
import (
"testing"
. "github.com/bsm/openmetrics"
)
// https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#metricfamily
func TestDesc_Validate(t *testing.T) {
t.Run("valid", func(t *testing.T) {
examples := []Desc{
{Name: "foo", Unit: "seconds", Help: "short and useful", Labels: []string{"one", "two"}},
{Name: "foo123"},
{Name: "foo_bar"},
{Name: "foo_123"},
{Name: "foo_"},
{Name: ":foo:"},
}
for i, ls := range examples {
if err := ls.Validate(); err != nil {
t.Errorf("[%d] expected %v to be valid, but %v", i, ls, err)
}
}
})
t.Run("bad names", func(t *testing.T) {
examples := []Desc{
{Name: "with space"},
{Name: "with-hyphen"},
{Name: "1leading_digit"},
{Name: "ambiguous_suffix_total"},
{Name: "_reserved"},
{Name: "with_unit", Unit: "unit"},
}
for i, ls := range examples {
if err := ls.Validate(); err == nil {
t.Errorf("[%d] expected %v to be invalid", i, ls)
}
}
})
t.Run("bad unit", func(t *testing.T) {
examples := []Desc{
{Name: "foo", Unit: "m/s"},
}
for i, ls := range examples {
if err := ls.Validate(); err == nil {
t.Errorf("[%d] expected %v to be invalid", i, ls)
}
}
})
t.Run("bad help", func(t *testing.T) {
examples := []Desc{
{Name: "foo", Help: "not \xff\xfe\xfd helpful"},
{Name: "foo", Help: "Help is a string and SHOULD be non-empty. It is used to give a brief description of the MetricFamily for human consumption and SHOULD be short enough to be used as a tooltip."},
}
for i, ls := range examples {
if err := ls.Validate(); err == nil {
t.Errorf("[%d] expected %v to be invalid", i, ls)
}
}
})
t.Run("bad label names", func(t *testing.T) {
examples := []Desc{
{Name: "foo", Labels: []string{"_reserved"}},
{Name: "foo", Labels: []string{"one", "two", "one"}},
}
for i, ls := range examples {
if err := ls.Validate(); err == nil {
t.Errorf("[%d] expected %v to be invalid", i, ls)
}
}
})
}