forked from joeig/go-powerdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers_test.go
143 lines (124 loc) · 3.63 KB
/
servers_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
package powerdns
import (
"fmt"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
)
func registerServersMockResponder() {
httpmock.RegisterResponder("GET", generateTestAPIURL()+"/servers",
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
return res, nil
}
serversMock := []Server{
{
Type: String("Server"),
ID: String(testVHost),
DaemonType: String("authoritative"),
Version: String("4.1.2"),
URL: String("/api/v1/servers/" + testVHost),
ConfigURL: String("/api/v1/servers/" + testVHost + "/config{/config_setting}"),
ZonesURL: String("/api/v1/servers/" + testVHost + "/zones{/zone}"),
},
}
return httpmock.NewJsonResponse(http.StatusOK, serversMock)
},
)
httpmock.RegisterResponder("GET", generateTestAPIVHostURL(),
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
return res, nil
}
serverMock := Server{
Type: String("Server"),
ID: String(testVHost),
DaemonType: String("authoritative"),
Version: String("4.1.2"),
URL: String("/api/v1/servers/" + testVHost),
ConfigURL: String("/api/v1/servers/" + testVHost + "/config{/config_setting}"),
ZonesURL: String("/api/v1/servers/" + testVHost + "/zones{/zone}"),
}
return httpmock.NewJsonResponse(http.StatusOK, serverMock)
},
)
}
func registerCacheFlushMockResponder(testDomain string) {
httpmock.RegisterResponder("PUT", fmt.Sprintf("%s/cache/flush", generateTestAPIVHostURL()),
func(req *http.Request) (*http.Response, error) {
if res := verifyAPIKey(req); res != nil {
return res, nil
}
if req.URL.Query().Get("domain") != makeDomainCanonical(testDomain) {
return httpmock.NewStringResponse(http.StatusUnprocessableEntity, "Unprocessable Eneity"), nil
}
cacheFlushResultMock := CacheFlushResult{
Count: Uint32(1),
Result: String("foo"),
}
return httpmock.NewJsonResponse(http.StatusOK, cacheFlushResultMock)
},
)
}
func TestListServers(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
registerServersMockResponder()
p := initialisePowerDNSTestClient()
servers, err := p.Servers.List()
if err != nil {
t.Errorf("%s", err)
}
if len(servers) == 0 {
t.Error("Received amount of servers is 0")
}
}
func TestListServersError(t *testing.T) {
p := initialisePowerDNSTestClient()
p.Port = "x"
if _, err := p.Servers.List(); err == nil {
t.Error("error is nil")
}
}
func TestGetServer(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
registerServersMockResponder()
p := initialisePowerDNSTestClient()
server, err := p.Servers.Get(testVHost)
if err != nil {
t.Errorf("%s", err)
}
if *server.ID != testVHost {
t.Error("Received no server")
}
}
func TestGetServerError(t *testing.T) {
p := initialisePowerDNSTestClient()
p.Port = "x"
if _, err := p.Servers.Get(testVHost); err == nil {
t.Error("error is nil")
}
}
func TestCacheFlush(t *testing.T) {
testDomain := generateTestZone(true)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
registerCacheFlushMockResponder(testDomain)
p := initialisePowerDNSTestClient()
cacheFlushResult, err := p.Servers.CacheFlush(testVHost, testDomain)
if err != nil {
t.Errorf("%s", err)
}
if *cacheFlushResult.Count != 1 {
t.Error("Received cache flush result is invalid")
}
}
func TestCacheFlushResultError(t *testing.T) {
testDomain := generateTestZone(false)
p := initialisePowerDNSTestClient()
p.Port = "x"
if _, err := p.Servers.CacheFlush(testVHost, testDomain); err == nil {
t.Error("error is nil")
}
}