-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpowerdns_exporter_test.go
158 lines (127 loc) · 3.34 KB
/
powerdns_exporter_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
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"runtime"
"testing"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/client_golang/prometheus"
)
type pdns struct {
*httptest.Server
config []byte
}
func newPowerDNS(config []byte) *pdns {
h := &pdns{config: config}
h.Server = httptest.NewServer(handler(h))
return h
}
func handler(h *pdns) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Write(h.config)
}
}
func handlerStale(exit chan bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
<-exit
}
}
func readCounter(m prometheus.Counter) float64 {
// TODO: Revisit this once client_golang offers better testing tools.
pb := &dto.Metric{}
m.Write(pb)
return pb.GetCounter().GetValue()
}
func readGauge(m prometheus.Gauge) float64 {
// TODO: Revisit this once client_golang offers better testing tools.
pb := &dto.Metric{}
m.Write(pb)
return pb.GetGauge().GetValue()
}
func TestServerWithoutChecks(t *testing.T) {
config, err := ioutil.ReadFile("test/recursor_stats.json")
if err != nil {
t.Fatalf("could not read config file: %v", err.Error())
}
h := newPowerDNS(config)
defer h.Close()
hostURL, _ := url.Parse(h.URL)
e := NewExporter("12345", "recursor", hostURL)
ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
e.Collect(ch)
}()
if expect, got := 1., readGauge((<-ch).(prometheus.Gauge)); expect != got {
// up
t.Errorf("expected %f up, got %f", expect, got)
}
if expect, got := 1., readCounter((<-ch).(prometheus.Counter)); expect != got {
// totalScrapes
t.Errorf("expected %f recorded scrape, got %f", expect, got)
}
if expect, got := 0., readCounter((<-ch).(prometheus.Counter)); expect != got {
// jsonParseFailures
t.Errorf("expected %f csv parse failures, got %f", expect, got)
}
// Suck up the remaining metrics.
for _ = range ch {
}
}
func TestParseServerInfo(t *testing.T) {
config, err := ioutil.ReadFile("test/recursor_info.json")
if err != nil {
t.Fatalf("could not read config file: %v", err.Error())
}
h := newPowerDNS(config)
defer h.Close()
hostURL, _ := url.Parse(h.URL)
got, err := getServerInfo(hostURL, "12345")
if err != nil {
t.Errorf("expected getServerInfo() to return no error, but got %v", err)
}
want := &ServerInfo{
Kind: "Server",
ID: "localhost",
URL: "/servers/localhost",
DaemonType: "recursor",
Version: "3.7.3",
ConfigUrl: "/servers/localhost/config{/config_setting}",
ZonesUrl: "/servers/localhost/zones{/zone}",
}
if !reflect.DeepEqual(want, got) {
t.Errorf("want serverInfo %#v but got %#v",
want,
got,
)
}
}
func BenchmarkExtract(b *testing.B) {
config, err := ioutil.ReadFile("test/recursor_stats.json")
if err != nil {
b.Fatalf("could not read config file: %v", err.Error())
}
h := newPowerDNS(config)
defer h.Close()
hostURL, _ := url.Parse(h.URL)
e := NewExporter("12345", "recursor", hostURL)
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ch := make(chan prometheus.Metric)
go func(ch chan prometheus.Metric) {
for _ = range ch {
}
}(ch)
e.Collect(ch)
close(ch)
}
runtime.GC()
runtime.ReadMemStats(&after)
b.Logf("%d bytes used after %d runs", after.Alloc-before.Alloc, b.N)
}