forked from joeig/go-powerdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics_test.go
77 lines (65 loc) · 2.26 KB
/
statistics_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
package powerdns
import (
"net/http"
"testing"
"github.com/jarcoal/httpmock"
)
func registerStatisticsMockResponder() {
httpmock.RegisterResponder("GET", generateTestAPIVHostURL()+"/statistics",
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
return res, nil
}
statisticsMock := "[{\"name\": \"corrupt-packets\", \"type\": \"StatisticItem\", \"value\": \"0\"}, {\"name\": \"response-by-rcode\", \"type\": \"MapStatisticItem\", \"value\": [{\"name\": \"foo1\", \"value\": \"bar1\"}, {\"name\": \"foo2\", \"value\": \"bar2\"}]}, {\"name\": \"logmessages\", \"size\": \"10000\", \"type\": \"RingStatisticItem\", \"value\": [{\"name\": \"gmysql Connection successful. Connected to database 'powerdns' on 'mariadb'.\", \"value\": \"235\"}]}]"
statisticQueryString := req.URL.Query().Get("statistic")
if statisticQueryString != "" {
if statisticQueryString == "corrupt-packets" {
statisticsMock = "[{\"name\": \"corrupt-packets\", \"type\": \"StatisticItem\", \"value\": \"0\"}]"
} else {
return httpmock.NewStringResponse(http.StatusUnprocessableEntity, "Unprocessable Entity"), nil
}
}
return httpmock.NewStringResponse(http.StatusOK, statisticsMock), nil
},
)
}
func TestListStatistics(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
registerStatisticsMockResponder()
p := initialisePowerDNSTestClient()
statistics, err := p.Statistics.List()
if err != nil {
t.Errorf("%s", err)
}
if len(statistics) == 0 {
t.Error("Received amount of statistics is 0")
}
}
func TestListStatisticsError(t *testing.T) {
p := initialisePowerDNSTestClient()
p.Port = "x"
if _, err := p.Statistics.List(); err == nil {
t.Error("error is nil")
}
}
func TestGetStatistics(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
registerStatisticsMockResponder()
p := initialisePowerDNSTestClient()
statistics, err := p.Statistics.Get("corrupt-packets")
if err != nil {
t.Errorf("%s", err)
}
if len(statistics) != 1 {
t.Error("Received amount of statistics is not 1")
}
}
func TestGetStatisticsError(t *testing.T) {
p := initialisePowerDNSTestClient()
p.Port = "x"
if _, err := p.Statistics.Get("corrupt-packets"); err == nil {
t.Error("error is nil")
}
}