forked from sahandhabibi/Ssacli-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
54 lines (42 loc) · 1.39 KB
/
main_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
package main
import (
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
"os"
"testing"
)
func TestDiskStatus_Successful(t *testing.T) {
// given
testOutput, _ := os.ReadFile("test/ssacli_output_ok.txt")
// when
parse(string(testOutput))
// then
metricsChan := make(chan prometheus.Metric, 100)
disk_status.Collect(metricsChan)
if len(metricsChan) != 3 {
t.Fatalf(`Expected 3 metrics, but collected %d`, len(metricsChan))
}
assertValue(t, disk_status, "box 6 bay 1 type SAS", 1.0)
}
func TestDiskStatus_Rebuilding(t *testing.T) {
// given
testOutput, _ := os.ReadFile("test/ssacli_output_rebuilding.txt")
// when
parse(string(testOutput))
// then
assertValue(t, disk_status, "box 6 bay 2 type SAS", 0.0)
}
func assertValue(t *testing.T, gaugeVec *prometheus.GaugeVec, name string, expectedVal float64) {
gaugeVal, gaugeName := extractValue(gaugeVec, name)
if gaugeVal != expectedVal {
t.Fatalf(`Expected %s to be %f, but was %f`, gaugeName, expectedVal, gaugeVal)
}
}
func extractValue(gauge *prometheus.GaugeVec, name string) (float64, string) {
metricsChan := make(chan prometheus.Metric, 1)
gauge.With(prometheus.Labels{"physicaldrive": name}).Collect(metricsChan)
metric := <-metricsChan
metricDto := io_prometheus_client.Metric{}
metric.Write(&metricDto)
return *metricDto.Gauge.Value, *metricDto.Label[0].Value
}