-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient_test.go
327 lines (315 loc) · 7.1 KB
/
client_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package powerdns
import (
"context"
"fmt"
"os"
"os/exec"
"reflect"
"sort"
"strconv"
"testing"
"time"
"github.com/libdns/libdns"
"github.com/mittwald/go-powerdns/apis/zones"
)
func TestPDNSClient(t *testing.T) {
var dockerCompose string
var ok bool
doRun, _ := strconv.ParseBool(os.Getenv("PDNS_RUN_INTEGRATION_TEST"))
if !doRun {
t.Skip("skipping because PDNS_RUN_INTEGRATION_TEST was not set")
}
if dockerCompose, ok = which("docker-compose"); !ok {
t.Skip("docker-compose is not present, skipping")
}
err := runCmd(dockerCompose, "rm", "-sfv")
if err != nil {
t.Fatalf("docker-compose failed: %s", err)
}
err = runCmd(dockerCompose, "down", "-v")
if err != nil {
t.Fatalf("docker-compose failed: %s", err)
}
err = runCmd(dockerCompose, "up", "-d")
if err != nil {
t.Fatalf("docker-compose failed: %s", err)
}
defer func() {
if skipCleanup, _ := strconv.ParseBool(os.Getenv("PDNS_SKIP_CLEANUP")); !skipCleanup {
runCmd(dockerCompose, "down", "-v")
}
}()
time.Sleep(time.Second * 30) // give everything time to finish coming up
z := zones.Zone{
Name: "example.org.",
Type: zones.ZoneTypeZone,
Kind: zones.ZoneKindNative,
ResourceRecordSets: []zones.ResourceRecordSet{
{
Name: "1.example.org.",
Type: "A",
TTL: 60,
Records: []zones.Record{
{
Content: "127.0.0.1",
},
{
Content: "127.0.0.2",
},
{
Content: "127.0.0.3",
},
},
},
{
Name: "1.example.org.",
Type: "TXT",
TTL: 60,
Records: []zones.Record{
{
Content: "\"This is text\"",
},
},
},
{
Name: "2.example.org.",
Type: "A",
TTL: 60,
Records: []zones.Record{
{
Content: "127.0.0.4",
},
{
Content: "127.0.0.5",
},
{
Content: "127.0.0.6",
},
},
},
},
Serial: 1,
Nameservers: []string{
"ns1.example.org.",
"ns2.example.org.",
},
}
p := &Provider{
ServerURL: "http://localhost:8081",
ServerID: "localhost",
APIToken: "secret",
Debug: os.Getenv("PDNS_DEBUG"),
}
c, err := p.client()
if err != nil {
t.Fatalf("could not create client: %s", err)
}
_, err = c.Client.Zones().CreateZone(context.Background(), c.sID, z)
if err != nil {
t.Fatalf("failed to create test zone: %s", err)
}
for _, table := range []struct {
name string
operation string
zone string
Type string
records []libdns.Record
want []string
}{
{
name: "Test Get Zone",
operation: "records",
zone: "example.org.",
records: nil,
Type: "A",
want: []string{"1:127.0.0.1", "1:127.0.0.2", "1:127.0.0.3", "2:127.0.0.4", "2:127.0.0.5", "2:127.0.0.6"},
},
{
name: "Test Append Zone A record",
operation: "append",
zone: "example.org.",
Type: "A",
records: []libdns.Record{
{
Name: "2",
Type: "A",
Value: "127.0.0.7",
},
},
want: []string{"1:127.0.0.1", "1:127.0.0.2", "1:127.0.0.3",
"2:127.0.0.4", "2:127.0.0.5", "2:127.0.0.6", "2:127.0.0.7"},
},
{
name: "Test Append Zone TXT record",
operation: "append",
zone: "example.org.",
Type: "TXT",
records: []libdns.Record{
{
Name: "1",
Type: "TXT",
Value: "\"This is also some text\"",
},
},
want: []string{
`1:"This is text"`,
`1:"This is also some text"`,
},
},
{
name: "Test Append Zone TXT record with weird formatting",
operation: "append",
zone: "example.org.",
Type: "TXT",
records: []libdns.Record{
{
Name: "1",
Type: "TXT",
Value: "This is some weird text that isn't quoted",
},
},
want: []string{
`1:"This is text"`,
`1:"This is also some text"`,
`1:"This is some weird text that isn't quoted"`,
},
},
{
name: "Test Append Zone TXT record with embedded quotes",
operation: "append",
zone: "example.org.",
Type: "TXT",
records: []libdns.Record{
{
Name: "1",
Type: "TXT",
Value: `This is some weird text that "has embedded quoting"`,
},
},
want: []string{`1:"This is text"`, `1:"This is also some text"`,
`1:"This is some weird text that isn't quoted"`,
`1:"This is some weird text that \"has embedded quoting\""`},
},
{
name: "Test Append Zone TXT record with unicode",
operation: "append",
zone: "example.org.",
Type: "TXT",
records: []libdns.Record{
{
Name: "1",
Type: "TXT",
Value: `ç is equal to \195\167`,
},
},
want: []string{`1:"This is text"`, `1:"This is also some text"`,
`1:"This is some weird text that isn't quoted"`,
`1:"This is some weird text that \"has embedded quoting\""`,
`1:"ç is equal to \195\167"`,
},
},
{
name: "Test Delete Zone",
operation: "delete",
zone: "example.org.",
Type: "A",
records: []libdns.Record{
{
Name: "2",
Type: "A",
Value: "127.0.0.7",
},
},
want: []string{"1:127.0.0.1", "1:127.0.0.2", "1:127.0.0.3", "2:127.0.0.4", "2:127.0.0.5", "2:127.0.0.6"},
},
{
name: "Test Append and Add Zone",
operation: "append",
zone: "example.org.",
Type: "A",
records: []libdns.Record{
{
Name: "2",
Type: "A",
Value: "127.0.0.8",
},
{
Name: "3",
Type: "A",
Value: "127.0.0.9",
},
},
want: []string{"1:127.0.0.1", "1:127.0.0.2", "1:127.0.0.3",
"2:127.0.0.4", "2:127.0.0.5", "2:127.0.0.6", "2:127.0.0.8",
"3:127.0.0.9"},
},
{
name: "Test Set",
operation: "set",
zone: "example.org.",
Type: "A",
records: []libdns.Record{
{
Name: "2",
Type: "A",
Value: "127.0.0.1",
},
{
Name: "1",
Type: "A",
Value: "127.0.0.1",
},
},
want: []string{"1:127.0.0.1", "2:127.0.0.1", "3:127.0.0.9"},
},
} {
t.Run(table.name, func(t *testing.T) {
var err error
switch table.operation {
case "records":
// fetch below
case "append":
_, err = p.AppendRecords(context.Background(), table.zone, table.records)
case "set":
_, err = p.SetRecords(context.Background(), table.zone, table.records)
case "delete":
_, err = p.DeleteRecords(context.Background(), table.zone, table.records)
}
if err != nil {
t.Errorf("failed to %s records: %s", table.operation, err)
return
}
// Fetch the zone
recs, err := p.GetRecords(context.Background(), table.zone)
if err != nil {
t.Errorf("error fetching zone")
return
}
var have []string
for _, rr := range recs {
if rr.Type != table.Type {
continue
}
have = append(have, fmt.Sprintf("%s:%s", rr.Name, rr.Value))
}
sort.Strings(have)
sort.Strings(table.want)
if !reflect.DeepEqual(have, table.want) {
t.Errorf("assertion failed: have: %#v want %#v", have, table.want)
}
})
}
}
func which(cmd string) (string, bool) {
pth, err := exec.LookPath(cmd)
if err != nil {
return "", false
}
return pth, true
}
func runCmd(cmd string, args ...string) error {
c := exec.Command(cmd, args...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}