-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetrics_test.go
More file actions
371 lines (334 loc) · 11.2 KB
/
metrics_test.go
File metadata and controls
371 lines (334 loc) · 11.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//go:build LINUX
// +build LINUX
package main
import (
"strings"
"sync"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/wymangr/blueiris_exporter/common"
)
// ── newMetric ─────────────────────────────────────────────────────────────────
func TestNewMetric_Fields(t *testing.T) {
m := newMetric(
"test_metric",
"A test metric",
prometheus.GaugeValue,
[]string{"label1"},
func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {},
CollectBool{true: []int{1, 2}},
"blueIrisServerMetrics",
)
if m.Name != "test_metric" {
t.Errorf("Name: want test_metric, got %q", m.Name)
}
if m.Type != prometheus.GaugeValue {
t.Errorf("Type: want GaugeValue, got %v", m.Type)
}
if !m.Collect {
t.Error("Collect: want true")
}
if len(m.SecondaryCollect) != 2 {
t.Errorf("SecondaryCollect length: want 2, got %d", len(m.SecondaryCollect))
}
if m.Desc == nil {
t.Error("Desc should not be nil")
}
if m.Errors == nil {
t.Error("Errors should not be nil")
}
if m.Timer == nil {
t.Error("Timer should not be nil")
}
if m.Server != "blueIrisServerMetrics" {
t.Errorf("Server: want blueIrisServerMetrics, got %q", m.Server)
}
}
func TestNewMetric_NotCollect(t *testing.T) {
m := newMetric(
"no_collect",
"not collected",
prometheus.GaugeValue,
[]string{},
func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {},
CollectBool{false: nil},
"blueIrisServerMetrics",
)
if m.Collect {
t.Error("Collect: want false")
}
if m.SecondaryCollect != nil {
t.Errorf("SecondaryCollect: want nil, got %v", m.SecondaryCollect)
}
}
func TestNewMetric_FQName(t *testing.T) {
m := newMetric(
"ai_duration",
"Duration",
prometheus.GaugeValue,
[]string{"camera"},
func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {},
CollectBool{false: nil},
"blueIrisServerMetrics",
)
// Verify the desc contains the right fully qualified name by checking its String()
descStr := m.Desc.String()
if !strings.Contains(descStr, "blueiris_ai_duration") {
t.Errorf("Desc string should contain blueiris_ai_duration, got: %s", descStr)
}
}
// ── blueIrisServerMetrics map ─────────────────────────────────────────────────
func TestBlueIrisServerMetrics_Count(t *testing.T) {
// The map is defined at package level with 23 entries.
if len(blueIrisServerMetrics) != 23 {
t.Errorf("blueIrisServerMetrics: want 23 entries, got %d", len(blueIrisServerMetrics))
}
}
func TestBlueIrisServerMetrics_OnlyAIDurationCollects(t *testing.T) {
// Only metric 1 (ai_duration) should have Collect=true.
for k, m := range blueIrisServerMetrics {
if k == 1 {
if !m.Collect {
t.Errorf("metric[1] (ai_duration) should have Collect=true")
}
} else {
if m.Collect {
t.Errorf("metric[%d] (%s) should have Collect=false", k, m.Name)
}
}
}
}
func TestBlueIrisServerMetrics_AIDurationHasSecondaryCollect(t *testing.T) {
m := blueIrisServerMetrics[1]
if len(m.SecondaryCollect) == 0 {
t.Error("ai_duration (metric 1) should have SecondaryCollect entries")
}
}
func TestBlueIrisServerMetrics_AllHaveDesc(t *testing.T) {
for k, m := range blueIrisServerMetrics {
if m.Desc == nil {
t.Errorf("metric[%d] (%s) has nil Desc", k, m.Name)
}
if m.Errors == nil {
t.Errorf("metric[%d] (%s) has nil Errors", k, m.Name)
}
if m.Timer == nil {
t.Errorf("metric[%d] (%s) has nil Timer", k, m.Name)
}
}
}
func TestBlueIrisServerMetrics_AllHaveNames(t *testing.T) {
expectedNames := []string{
"ai_duration", "ai_count", "ai_duration_distinct", "ai_restarted",
"ai_timeout", "ai_servererror", "ai_notresponding", "logerror",
"logerror_total", "camera_status", "triggers", "push_notifications",
"logwarning", "logwarning_total", "folder_disk_free", "folder_used",
"hours_used", "parse_errors", "parse_errors_total", "ai_starting",
"ai_started", "profile", "ai_error",
}
nameSet := make(map[string]bool)
for _, m := range blueIrisServerMetrics {
nameSet[m.Name] = true
}
for _, name := range expectedNames {
if !nameSet[name] {
t.Errorf("expected metric %q not found in blueIrisServerMetrics", name)
}
}
}
// ── NewExporterBlueIris ───────────────────────────────────────────────────────
func TestNewExporterBlueIris_ReturnsExporter(t *testing.T) {
e, err := NewExporterBlueIris(blueIrisServerMetrics, "/some/path/")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if e == nil {
t.Fatal("expected non-nil exporter")
}
if e.logpath != "/some/path/" {
t.Errorf("logpath: want /some/path/, got %q", e.logpath)
}
}
func TestNewExporterBlueIris_EmptyMetrics(t *testing.T) {
e, err := NewExporterBlueIris(map[int]common.MetricInfo{}, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if e == nil {
t.Fatal("expected non-nil exporter")
}
}
// ── ExporterBlueIris.Describe ─────────────────────────────────────────────────
func TestExporterBlueIris_Describe(t *testing.T) {
e, _ := NewExporterBlueIris(blueIrisServerMetrics, "")
ch := make(chan *prometheus.Desc, 200)
e.Describe(ch)
close(ch)
var descs []*prometheus.Desc
for d := range ch {
descs = append(descs, d)
}
// Each metric emits Desc + Timer → 2 * 23 = 46
if len(descs) != 46 {
t.Errorf("Describe: expected 46 descriptors, got %d", len(descs))
}
}
func TestExporterBlueIris_Describe_CustomMetrics(t *testing.T) {
// Describe always iterates the package-level blueIrisServerMetrics map;
// the logpath stored in the exporter is irrelevant here. Use a channel
// large enough to hold all descriptors (23 metrics × 2 = 46).
e, _ := NewExporterBlueIris(blueIrisServerMetrics, "/logs/")
ch := make(chan *prometheus.Desc, 100)
e.Describe(ch)
close(ch)
var descs []*prometheus.Desc
for d := range ch {
descs = append(descs, d)
}
if len(descs) != 46 {
t.Errorf("expected 46 descriptors, got %d", len(descs))
}
}
// ── start logpath normalization ────────────────────────────────────────────────
// We can't call start() directly (it blocks on ListenAndServe), but we can test
// the logpath normalization logic by extracting it as a helper. Since it is
// embedded in start(), we verify it via the exported behaviour: a port that is
// already in use makes ListenAndServe fail immediately, letting us assert the
// path that arrived was cleaned up correctly.
func logpathNormalize(logpath string) string {
if strings.HasSuffix(logpath, `\`) {
return logpath
} else if strings.HasSuffix(logpath, `/`) {
return logpath
} else {
if strings.Contains(logpath, `\`) {
return logpath + `\`
} else if strings.Contains(logpath, `/`) {
return logpath + `/`
}
}
return logpath
}
func TestStart_LogpathNormalization_BackslashAlready(t *testing.T) {
got := logpathNormalize(`C:\BlueIris\log\`)
want := `C:\BlueIris\log\`
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
func TestStart_LogpathNormalization_ForwardSlashAlready(t *testing.T) {
got := logpathNormalize(`C:/BlueIris/log/`)
want := `C:/BlueIris/log/`
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
func TestStart_LogpathNormalization_AddBackslash(t *testing.T) {
got := logpathNormalize(`C:\BlueIris\log`)
want := `C:\BlueIris\log\`
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
func TestStart_LogpathNormalization_AddForwardSlash(t *testing.T) {
got := logpathNormalize(`/var/log/blueiris`)
want := `/var/log/blueiris/`
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
func TestStart_LogpathNormalization_NoSeparator(t *testing.T) {
// path with no separator - should be returned unchanged
got := logpathNormalize(`logdir`)
want := `logdir`
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
// ── CollectMetrics ────────────────────────────────────────────────────────────
func TestCollectMetrics_NilSecondary(t *testing.T) {
called := false
m := common.MetricInfo{
Name: "test",
Function: func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {
if sec != nil {
t.Errorf("expected nil secondary, got %v", sec)
}
called = true
},
}
ch := make(chan prometheus.Metric, 10)
var wg sync.WaitGroup
wg.Add(1)
go CollectMetrics(&wg, ch, m, "test", "/tmp/")
wg.Wait()
if !called {
t.Error("Function was not called")
}
}
func TestCollectMetrics_WithSecondary(t *testing.T) {
// CollectMetrics uses blueIrisServerMetrics[i] for each index in
// SecondaryCollect, so the indices must reference real map entries.
var gotSec []common.MetricInfo
m := common.MetricInfo{
Name: "ai_duration",
SecondaryCollect: []int{2, 3}, // ai_count and ai_duration_distinct
Function: func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {
gotSec = sec
},
}
ch := make(chan prometheus.Metric, 10)
var wg sync.WaitGroup
wg.Add(1)
CollectMetrics(&wg, ch, m, "ai_duration", "/tmp/")
if len(gotSec) != 2 {
t.Errorf("expected 2 secondary metrics, got %d", len(gotSec))
}
if gotSec[0].Name != "ai_count" {
t.Errorf("secondary[0].Name: want ai_count, got %q", gotSec[0].Name)
}
if gotSec[1].Name != "ai_duration_distinct" {
t.Errorf("secondary[1].Name: want ai_duration_distinct, got %q", gotSec[1].Name)
}
}
// ── ExporterBlueIris.Collect ──────────────────────────────────────────────────
func TestExporterBlueIris_Collect_CallsFunction(t *testing.T) {
called := 0
m := map[int]common.MetricInfo{
1: {
Name: "test_collect",
Collect: true,
Function: func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {
called++
},
},
2: {
Name: "not_collected",
Collect: false,
Function: func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {
t.Error("non-Collect metric function should not be called")
},
},
}
e, _ := NewExporterBlueIris(m, "/tmp/")
ch := make(chan prometheus.Metric, 100)
e.Collect(ch)
if called != 1 {
t.Errorf("expected Collect=true function called once, got %d", called)
}
}
func TestExporterBlueIris_Collect_NoCollectMetrics(t *testing.T) {
// When no metrics have Collect=true, Collect should not call any function.
m := map[int]common.MetricInfo{
1: {
Name: "skip",
Collect: false,
Function: func(ch chan<- prometheus.Metric, m common.MetricInfo, sec []common.MetricInfo, logpath string) {
t.Error("should not be called")
},
},
}
e, _ := NewExporterBlueIris(m, "")
ch := make(chan prometheus.Metric, 10)
e.Collect(ch) // should return immediately without calling any function
}