-
Notifications
You must be signed in to change notification settings - Fork 2
/
init_test.go
175 lines (162 loc) · 4.67 KB
/
init_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gotoprom
import (
"testing"
"github.com/cabify/gotoprom/prometheusvanilla"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
func TestInitializer_MustAddBuilder(t *testing.T) {
t.Run("adds builder", func(t *testing.T) {
initializer := NewInitializer(prometheus.NewRegistry())
initializer.MustAddBuilder(prometheusvanilla.GaugeType, prometheusvanilla.BuildGauge)
err := initializer.Init(&struct {
Metric func() prometheus.Gauge `name:"gauge" help:"help"`
}{}, "namespace")
assert.NoError(t, err)
})
t.Run("same builder twice panics", func(t *testing.T) {
initializer := NewInitializer(prometheus.NewRegistry())
initializer.MustAddBuilder(prometheusvanilla.GaugeType, prometheusvanilla.BuildGauge)
assert.Panics(t, func() {
initializer.MustAddBuilder(prometheusvanilla.GaugeType, prometheusvanilla.BuildGauge)
})
})
}
func TestInitializer_AddBuilder(t *testing.T) {
t.Run("same builder twice fails", func(t *testing.T) {
initializer := NewInitializer(prometheus.NewRegistry())
err := initializer.AddBuilder(prometheusvanilla.GaugeType, prometheusvanilla.BuildGauge)
assert.NoError(t, err)
err = initializer.AddBuilder(prometheusvanilla.GaugeType, prometheusvanilla.BuildGauge)
assert.Error(t, err)
})
}
func TestInitializer_MustInit(t *testing.T) {
t.Run("happy case", func(t *testing.T) {
initializer := NewInitializer(prometheus.NewRegistry())
structPtr := &struct{}{}
assert.NotPanics(t, func() {
initializer.MustInit(structPtr, "namespace")
})
})
t.Run("panics when can't init", func(t *testing.T) {
initializer := NewInitializer(prometheus.NewRegistry())
notAPointer := struct{}{}
assert.Panics(t, func() {
initializer.MustInit(notAPointer, "namespace")
})
})
}
func TestInitializer_Init(t *testing.T) {
someInt := 3
type someLabels struct {
Label string `label:"label"`
}
type someLabelsWithoutLabelTag struct {
LabelWithoutLabelTag string
}
t.Run("fails", func(t *testing.T) {
for _, tc := range []struct {
desc string
metrics interface{}
}{
{
desc: "not a pointer to struct",
metrics: struct{}{},
},
{
desc: "pointer to a non struct",
metrics: &someInt,
},
{
desc: "contains a non-supported field, like a string",
metrics: &struct {
Something string
}{},
},
{
desc: "sub-struct doesn't define its namespace",
metrics: &struct {
Inner struct{}
}{},
},
{
desc: "sub-struct can't be initialized",
metrics: &struct {
Inner struct {
Deeper struct{} `thisdoesnothaveanamespace:"nothing"`
} `namespace:"thishasanamespace"`
}{},
},
{
desc: "non-exported field",
metrics: &struct {
foo func() prometheus.Gauge `name:"unexported" help:"this can't be set"`
}{},
},
{
desc: "missing name",
metrics: &struct {
Foo func() prometheus.Gauge `help:"missing name tag"`
}{},
},
{
desc: "missing help",
metrics: &struct {
Foo func() prometheus.Gauge `name:"nohelp"`
}{},
},
{
desc: "more than one return argument",
metrics: &struct {
Foo func() (prometheus.Gauge, error) `name:"toomanyreturnargs" help:"what is this?"`
}{},
},
{
desc: "no builders defined",
metrics: &struct {
Foo func() prometheus.Counter `name:"unknowntype" help:"Counter is not registered"`
}{},
},
{
desc: "builder fails",
metrics: &struct {
Foo func() prometheus.Summary `name:"unknowntype" help:"max_age is malformed" max_age:"bar"`
}{},
},
{
desc: "builder fails",
metrics: &struct {
Foo func() prometheus.Summary `name:"unknowntype" help:"max_age is malformed" max_age:"bar"`
}{},
},
{
desc: "too many params",
metrics: &struct {
Foo func(someLabels, string) prometheus.Gauge `name:"too many input params" help:"only labels are accepted"`
}{},
},
{
desc: "labels without label tag",
metrics: &struct {
Foo func(someLabelsWithoutLabelTag) prometheus.Gauge `name:"nolabeltag" help:"This labels are missing the label tag"`
}{},
},
{
desc: "metric registration fails",
metrics: &struct {
Foo func() prometheus.Gauge `name:"metric" help:"first gauge"`
Bar func() prometheus.Gauge `name:"metric" help:"name duplicates the previous one"`
}{},
},
} {
t.Run(tc.desc, func(t *testing.T) {
initializer := NewInitializer(prometheus.NewRegistry())
initializer.MustAddBuilder(prometheusvanilla.GaugeType, prometheusvanilla.BuildGauge)
initializer.MustAddBuilder(prometheusvanilla.SummaryType, prometheusvanilla.BuildSummary)
err := initializer.Init(tc.metrics, "namespace")
assert.Error(t, err)
})
}
})
}