forked from jayme-github/fritzbox_smarthome_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
71 lines (64 loc) · 1.74 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
package main
import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/mock"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestCollector(t *testing.T) {
deviceLists, _ := filepath.Glob(path.Join("test", "*.xml"))
for _, devicelistPath := range deviceLists {
t.Run(devicelistPath, func(t *testing.T) {
metricsPath := strings.TrimSuffix(devicelistPath, filepath.Ext(devicelistPath)) + ".metrics"
exp, err := os.Open(metricsPath)
if err != nil {
t.Skipf("Error opening fixture file: %q: %v", metricsPath, err)
}
m := mock.New()
m.DeviceList = devicelistPath
m.Start()
defer m.Close()
fbURL, err := url.Parse(m.Server.URL)
if err != nil {
t.Errorf("Failed to parse mock server url: %v", err)
}
fritzClient = NewClient(fritz.URL(fbURL))
fc := NewFritzCollector()
if err := testutil.CollectAndCompare(fc, exp); err != nil {
t.Error("Unexpected metrics returned:", err)
}
})
}
}
func Test_parseSwitchStrings(t *testing.T) {
tests := []struct {
arg string
want float64
wantErr bool
}{
{arg: "manuell", want: 1.0, wantErr: false},
{arg: "1", want: 1.0, wantErr: false},
{arg: "0", want: 0.0, wantErr: false},
{arg: "auto", want: 0.0, wantErr: false},
{arg: "", want: -1.0, wantErr: true},
{arg: "9", want: -1.0, wantErr: true},
}
for idx, tt := range tests {
t.Run(fmt.Sprint(idx), func(t *testing.T) {
got, err := parseSwitchStrings(tt.arg)
if (err != nil) != tt.wantErr {
t.Errorf("parseSwitchStrings() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("parseSwitchStrings() = %v, want %v", got, tt.want)
}
})
}
}