-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_test.go
122 lines (104 loc) · 3.07 KB
/
metrics_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
package main
import (
"log/slog"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
)
var observationCount = map[string]int{
"minute": 0,
"hour": 0,
"day_of_week": 0,
"day_of_month": 0,
"month": 0,
}
func verifyMetricDescription(d *prometheus.Desc, t *testing.T) (bool, string) {
_, after, ok := strings.Cut(d.String(), "tou_exporter_localized_")
if !ok {
slog.Error(`Could not find "tou_exporter_localized_" in metric description`, "desc", d)
t.Fail()
return false, ""
}
n := strings.Split(after, `"`)[0]
if _, ok := observationCount[n]; ok {
return true, n
}
return false, ""
}
func TestDescribeLocalizedTimezones(t *testing.T) {
testCh := make(chan *prometheus.Desc)
go describeLocalizedTimezones(testCh)
oc := observationCount
for k, _ := range oc {
oc[k] = 0
}
for i := 0; i < len(oc); i++ {
ok, n := verifyMetricDescription(<-testCh, t)
if ok {
oc[n]++
}
}
select {
case d := <-testCh:
assert.Equal(t, nil, d, "Channel should be empty, more metric descriptors than expected")
default:
}
for k, v := range oc {
assert.Equal(t, 1, v, k+" should have been observed exactly once")
}
}
func TestCollectLocalizedTimezones(t *testing.T) {
testCollectCh := make(chan prometheus.Metric)
liveConfig = config{LocalizedTimezones: []string{
"Pacific/Chatham", // UTC+13:45 - tests minute offsets too
}}
tTime := time.Date(2023, 1, 31, 20, 3, 4, 0, time.UTC)
go collectLocalizedTimezones(testCollectCh, tTime)
oc := observationCount
for k, _ := range oc {
oc[k] = 0
}
for i := 0; i < len(oc); i++ {
m := <-testCollectCh
ok, n := verifyMetricDescription(m.Desc(), t)
if ok {
oc[n]++
var actual = &dto.Metric{}
if err := m.Write(actual); err != nil {
slog.Error("error writing metric", "err", err, "metric", m, "metricDesc", m.Desc())
t.Fail()
}
actualValue := actual.GetGauge().GetValue()
var labelMap = map[string]string{}
for _, l := range actual.GetLabel() {
labelMap[l.GetName()] = l.GetValue()
}
assert.Equal(t, "Pacific/Chatham", labelMap["tz"], "minute label should be Pacific/Chatham")
switch n {
case "minute":
assert.Equal(t, float64(48), actualValue, "minute should be 48")
case "hour":
assert.Equal(t, float64(9), actualValue, "hour should be 9")
case "day_of_week":
assert.Equal(t, float64(3), actualValue, "day_of_week should be 3 (wednesday)")
assert.Equal(t, "Wednesday", labelMap["day"], "day_of_week label should be Wednesday")
case "day_of_month":
assert.Equal(t, float64(1), actualValue, "day_of_month should be 1st")
case "month":
assert.Equal(t, float64(2), actualValue, "month should be 2 (feb)")
assert.Equal(t, "February", labelMap["month"], "day_of_month label should be February")
}
}
}
select {
case m := <-testCollectCh:
assert.Equal(t, nil, m, "Channel should be empty, more metrics than expected")
default:
}
for k, v := range oc {
assert.Equal(t, 1, v, k+" should have been observed exactly once")
}
}