-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollector_test.go
239 lines (214 loc) · 5.86 KB
/
collector_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
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
package hdhomerunexporter
import (
"bufio"
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/mdlayher/hdhomerun"
"github.com/prometheus/prometheus/util/promlint"
)
func TestCollector(t *testing.T) {
tests := []struct {
name string
d device
metrics []string
}{
{
name: "no tuners",
d: &testDevice{
model: "hdhomerun_test",
},
metrics: []string{
`hdhomerun_device_info{model="hdhomerun_test"} 1`,
},
},
{
name: "not tuned",
d: &testDevice{
model: "hdhomerun_test",
tuners: []testTuner{{
index: 0,
debug: &hdhomerun.TunerDebug{
Tuner: &hdhomerun.TunerStatus{
Channel: "none",
Lock: "none",
},
Device: &hdhomerun.DeviceStatus{},
CableCARD: &hdhomerun.CableCARDStatus{},
TransportStream: &hdhomerun.TransportStreamStatus{},
Network: &hdhomerun.NetworkStatus{},
},
}},
},
metrics: []string{
`hdhomerun_cablecard_bytes_per_second 0`,
`hdhomerun_cablecard_overflow 0`,
`hdhomerun_cablecard_resync 0`,
`hdhomerun_device_info{model="hdhomerun_test"} 1`,
`hdhomerun_network_errors{tuner="0"} 0`,
`hdhomerun_network_packets_per_second{tuner="0"} 0`,
`hdhomerun_tuner_info{channel="none",lock="none",tuner="0"} 1`,
`hdhomerun_tuner_signal_strength_ratio{tuner="0"} 0`,
`hdhomerun_tuner_signal_to_noise_ratio{tuner="0"} 0`,
`hdhomerun_tuner_symbol_error_ratio{tuner="0"} 0`,
},
},
{
name: "tuned",
d: &testDevice{
model: "hdhomerun_test",
tuners: []testTuner{
{
index: 0,
debug: &hdhomerun.TunerDebug{
Tuner: &hdhomerun.TunerStatus{
Channel: "qam:381000000",
Lock: "qam256:381000000",
SignalStrength: 100,
SignalToNoiseQuality: 100,
SymbolErrorQuality: 100,
},
Device: &hdhomerun.DeviceStatus{
BitsPerSecond: 38809216,
Resync: 1,
Overflow: 1,
},
CableCARD: &hdhomerun.CableCARDStatus{
BitsPerSecond: 38810720,
Resync: 1,
Overflow: 1,
},
TransportStream: &hdhomerun.TransportStreamStatus{
BitsPerSecond: 2534240,
TransportErrors: 1,
CRCErrors: 1,
},
Network: &hdhomerun.NetworkStatus{
PacketsPerSecond: 241,
Errors: 1,
Stop: hdhomerun.StopReasonNotStopped,
},
},
},
{
index: 1,
debug: &hdhomerun.TunerDebug{
Tuner: &hdhomerun.TunerStatus{
Channel: "none",
Lock: "none",
},
Device: &hdhomerun.DeviceStatus{},
CableCARD: &hdhomerun.CableCARDStatus{},
TransportStream: &hdhomerun.TransportStreamStatus{},
Network: &hdhomerun.NetworkStatus{},
},
},
},
},
metrics: []string{
`hdhomerun_cablecard_bytes_per_second 4.85134e+06`,
`hdhomerun_cablecard_overflow 1`,
`hdhomerun_cablecard_resync 1`,
`hdhomerun_device_info{model="hdhomerun_test"} 1`,
`hdhomerun_network_errors{tuner="0"} 1`,
`hdhomerun_network_errors{tuner="1"} 0`,
`hdhomerun_network_packets_per_second{tuner="0"} 241`,
`hdhomerun_network_packets_per_second{tuner="1"} 0`,
`hdhomerun_tuner_info{channel="qam:381000000",lock="qam256:381000000",tuner="0"} 1`,
`hdhomerun_tuner_info{channel="none",lock="none",tuner="1"} 1`,
`hdhomerun_tuner_signal_strength_ratio{tuner="0"} 1`,
`hdhomerun_tuner_signal_strength_ratio{tuner="1"} 0`,
`hdhomerun_tuner_signal_to_noise_ratio{tuner="0"} 1`,
`hdhomerun_tuner_signal_to_noise_ratio{tuner="1"} 0`,
`hdhomerun_tuner_symbol_error_ratio{tuner="0"} 1`,
`hdhomerun_tuner_symbol_error_ratio{tuner="1"} 0`,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body := testCollector(t, tt.d)
s := bufio.NewScanner(bytes.NewReader(body))
for s.Scan() {
// Skip metric HELP and TYPE lines.
text := s.Text()
if strings.HasPrefix(text, "#") {
continue
}
var found bool
for _, m := range tt.metrics {
if text == m {
found = true
break
}
}
if !found {
t.Log(string(body))
t.Fatalf("metric string not matched in whitelist: %s", text)
}
}
if err := s.Err(); err != nil {
t.Fatalf("failed to scan metrics: %v", err)
}
})
}
}
// testCollector uses the input device to generate a blob of Prometheus text
// format metrics.
func testCollector(t *testing.T, d device) []byte {
t.Helper()
s := httptest.NewServer(serveMetrics(d))
defer s.Close()
u, err := url.Parse(s.URL)
if err != nil {
t.Fatalf("failed to parse URL: %v", err)
}
res, err := http.Get(u.String())
if err != nil {
t.Fatalf("failed to perform HTTP request: %v", err)
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
// Ensure best practices are followed by linting the metrics.
problems, err := promlint.New(bytes.NewReader(b)).Lint()
if err != nil {
t.Fatalf("failed to lint metrics: %v", err)
}
if len(problems) > 0 {
for _, p := range problems {
t.Logf("lint: %s: %s", p.Metric, p.Text)
}
t.Fatal("one or more promlint errors found")
}
return b
}
var _ device = &testDevice{}
type testDevice struct {
model string
tuners []testTuner
}
func (d *testDevice) Model() (string, error) {
return d.model, nil
}
func (d *testDevice) ForEachTuner(fn func(t tuner) error) error {
for _, t := range d.tuners {
if err := fn(t); err != nil {
return err
}
}
return nil
}
var _ tuner = &testTuner{}
type testTuner struct {
index int
debug *hdhomerun.TunerDebug
}
func (t testTuner) Index() int { return t.index }
func (t testTuner) Debug() (*hdhomerun.TunerDebug, error) { return t.debug, nil }