-
Notifications
You must be signed in to change notification settings - Fork 6
/
provider_test.go
446 lines (425 loc) · 12.9 KB
/
provider_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package googleclouddns
import (
"context"
"testing"
"time"
"github.com/libdns/libdns"
"google.golang.org/api/googleapi"
)
var (
testProject = `test-dev`
testZone = `libdns.io.`
)
func Test_GetRecords(t *testing.T) {
p, rs, err := getTestDNSClient(`./replay/provider_getrecords.json`)
if err != nil {
t.Fatal(err)
}
defer rs.Close()
t.Run("retrieve existing records", func(t *testing.T) {
expectedTXTRecordValue := `Hi there! This is a TXT record!`
p.Project = testProject
records, err := p.GetRecords(context.Background(), testZone)
if err != nil {
t.Fatal("error listing records from the test zone:", err)
}
if len(records) != 6 {
t.Fatal("expected six records back, received", len(records))
}
for _, record := range records {
if record.Type != "TXT" && record.Name != "hello" {
continue
}
if record.Value != expectedTXTRecordValue {
t.Fatalf("Expected TXT record '%s', received '%s'", expectedTXTRecordValue, record.Value)
}
}
})
t.Run("attempt to request non-existent zone", func(t *testing.T) {
p.Project = testProject
records, err := p.GetRecords(context.Background(), `i-do-not-exist.io.`)
if err == nil {
t.Fatal("expected an error back but did not receive one")
}
if records != nil {
t.Fatal("there were records returned for a non-existent zone")
}
})
}
func Test_AppendRecords(t *testing.T) {
p, rs, err := getTestDNSClient(`./replay/provider_appendrecords.json`)
if err != nil {
t.Fatal(err)
}
defer rs.Close()
recordsToAppend := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I SHOULD NOT HAVE EXTRA QUOTES`,
TTL: time.Minute,
},
{
Type: "TXT",
Name: "caddy-validation",
Value: `1234567890abcdef`,
TTL: time.Minute,
},
{
Type: "A",
Name: "www",
Value: "127.0.0.1",
TTL: time.Minute * 5,
},
}
t.Run("appending creates new records", func(t *testing.T) {
p.Project = testProject
records, err := p.AppendRecords(context.Background(), testZone, recordsToAppend)
if err != nil {
t.Fatal("error appending new records to the DNS zone:", err)
}
if len(records) != 3 {
t.Fatal("expected three records back, received", len(records))
}
compareTestData(recordsToAppend, records, t)
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
compareTestData(recordsToAppend[:2], txtRecords, t)
aRecord, err := p.getCloudDNSRecord(context.Background(), testZone, `www`, `A`)
if err != nil {
t.Fatal("error retrieving A record for comparison", err)
}
compareTestData(recordsToAppend[2:], aRecord, t)
})
t.Run("appending creates new entries on existing records", func(t *testing.T) {
p.Project = testProject
recordsToAppend := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
}
records, err := p.AppendRecords(context.Background(), testZone, recordsToAppend)
if err != nil {
t.Fatal("error appending a new record to an existing DNS entry:", err)
}
if len(records) != 1 {
t.Fatal("expected one record back, received", len(records))
}
compareTestData(recordsToAppend, records, t)
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
fullRecord := append(recordsToAppend, libdns.Record{
Type: "TXT",
Name: "caddy-validation",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
)
compareTestData(fullRecord, txtRecords, t)
})
t.Run("appending returns no records where entries exist in existing Cloud DNS entry", func(t *testing.T) {
p.Project = testProject
recordsToAppend := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
}
records, err := p.AppendRecords(context.Background(), testZone, recordsToAppend)
if err != nil {
t.Fatal("received an error when attempting to add an already existing record")
}
if len(records) != 0 {
t.Fatal("expected no records back, received", len(records))
}
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
if len(txtRecords) != 3 {
t.Fatal("Should received 3 records back for TXT entry but received", len(txtRecords))
}
})
}
func Test_SetRecords(t *testing.T) {
p, rs, err := getTestDNSClient(`./replay/provider_setrecords.json`)
if err != nil {
t.Fatal(err)
}
defer rs.Close()
t.Run("setting creates new records", func(t *testing.T) {
p.Project = testProject
recordsToCreate := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation-mark2",
Value: `I SHOULD NOT HAVE EXTRA QUOTES`,
TTL: time.Minute,
},
{
Type: "TXT",
Name: "caddy-validation-mark2",
Value: `1234567890abcdef`,
TTL: time.Minute,
},
}
records, err := p.SetRecords(context.Background(), testZone, recordsToCreate)
if err != nil {
t.Fatal("error setting new records to the DNS zone:", err)
}
if len(records) != 2 {
t.Fatal("expected two records back, received", len(records))
}
compareTestData(recordsToCreate, records, t)
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation-mark2`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
compareTestData(recordsToCreate, txtRecords, t)
})
t.Run("setting overwrites existing records", func(t *testing.T) {
p.Project = testProject
recordToOverwrite := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation-mark2",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
}
records, err := p.SetRecords(context.Background(), testZone, recordToOverwrite)
if err != nil {
t.Fatal("error appending a new record to an existing DNS entry:", err)
}
if len(records) != 1 {
t.Fatal("expected one record back, received", len(records))
}
compareTestData(recordToOverwrite, records, t)
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation-mark2`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
compareTestData(recordToOverwrite, txtRecords, t)
})
}
func Test_DeleteRecords(t *testing.T) {
p, rs, err := getTestDNSClient(`./replay/provider_deleterecords.json`)
if err != nil {
t.Fatal(err)
}
defer rs.Close()
t.Run("delete entire record", func(t *testing.T) {
p.Project = testProject
recordToDelete := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation-mark2",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
}
records, err := p.DeleteRecords(context.Background(), testZone, recordToDelete)
if err != nil {
t.Fatal("error setting new records to the DNS zone:", err)
}
if len(records) != 1 {
t.Fatal("expected one record back, received", len(records))
}
if len(records) == 1 && (records[0].Name != recordToDelete[0].Name || records[0].Value != recordToDelete[0].Value) {
t.Fatalf("The record submitted, %v, does not match the record received %v", recordToDelete[0], records[0])
}
})
t.Run("deletes a single record from a multi record entry", func(t *testing.T) {
p.Project = testProject
recordToDelete := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I SHOULD NOT HAVE EXTRA QUOTES`,
TTL: time.Minute,
},
}
recordCleaned := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
{
Type: "TXT",
Name: "caddy-validation",
Value: `1234567890abcdef`,
TTL: time.Minute,
},
}
records, err := p.DeleteRecords(context.Background(), testZone, recordToDelete)
if err != nil {
t.Fatal("error deleting a single record from an existing DNS entry:", err)
}
if len(records) != 1 {
t.Fatal("expected one record back, received", len(records))
}
compareTestData(recordToDelete, records, t)
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
compareTestData(recordCleaned, txtRecords, t)
})
t.Run("deletes no records when a non-existent one is specified", func(t *testing.T) {
p.Project = testProject
recordToDelete := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I SHOULD NOT HAVE EXTRA QUOTES`,
TTL: time.Minute,
},
}
recordCleaned := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
{
Type: "TXT",
Name: "caddy-validation",
Value: `1234567890abcdef`,
TTL: time.Minute,
},
}
records, err := p.DeleteRecords(context.Background(), testZone, recordToDelete)
if err != nil {
t.Fatal("error attempting to delete a non-existent record:", err)
}
if len(records) != 0 {
t.Fatal("expected no records back, received", len(records))
}
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
compareTestData(recordCleaned, txtRecords, t)
})
t.Run("deletes one record when two are specified", func(t *testing.T) {
p.Project = testProject
recordToDelete := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `I SHOULD NOT HAVE EXTRA QUOTES`,
TTL: time.Minute,
},
{
Type: "TXT",
Name: "caddy-validation",
Value: `I provide new information to the cloud`,
TTL: time.Minute,
},
}
recordCleaned := []libdns.Record{
{
Type: "TXT",
Name: "caddy-validation",
Value: `1234567890abcdef`,
TTL: time.Minute,
},
}
records, err := p.DeleteRecords(context.Background(), testZone, recordToDelete)
if err != nil {
t.Fatal("error attempting to delete record:", err)
}
if len(records) != 1 {
t.Fatal("expected one record back, received", len(records))
}
compareTestData(recordToDelete[1:], records, t)
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `caddy-validation`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
compareTestData(recordCleaned, txtRecords, t)
})
}
func Test_EndToEnd(t *testing.T) {
p, rs, err := getTestDNSClient(`./replay/provider_endtoendrecords.json`)
if err != nil {
t.Fatal(err)
}
defer rs.Close()
p.Project = testProject
requestOne := []libdns.Record{
{
Type: "TXT",
Name: "_acme-challenge",
Value: `1234567890abcdef`,
TTL: 0,
},
}
requestTwo := []libdns.Record{
{
Type: "TXT",
Name: "_acme-challenge",
Value: `fedcba0987654321`,
TTL: 0,
},
}
appendedRecords, err := p.AppendRecords(context.Background(), testZone, requestOne)
if err != nil {
t.Fatal("error setting up first challenge:", err)
}
compareTestData(requestOne, appendedRecords, t)
appendedRecords, err = p.AppendRecords(context.Background(), testZone, requestTwo)
if err != nil {
t.Fatal("error setting up second challenge:", err)
}
compareTestData(requestTwo, appendedRecords, t)
txtRecords, err := p.getCloudDNSRecord(context.Background(), testZone, `_acme-challenge`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
if len(txtRecords) != 2 {
t.Fatal("expectd there to be two records but received", len(txtRecords))
}
deletedRecord, err := p.DeleteRecords(context.Background(), testZone, requestTwo)
if err != nil {
t.Fatal("error deleting single record from Cloud DNS entry", err)
}
if len(deletedRecord) != 1 {
t.Fatal("expected 1 record back but received", len(deletedRecord))
}
txtRecords, err = p.getCloudDNSRecord(context.Background(), testZone, `_acme-challenge`, `TXT`)
if err != nil {
t.Fatal("error retrieving TXT record for comparison", err)
}
if len(txtRecords) != 1 {
t.Fatal("expectd there to be on record but received", len(txtRecords))
}
deletedRecord, err = p.DeleteRecords(context.Background(), testZone, requestOne)
if err != nil {
t.Fatal("error deleting single record from Cloud DNS entry", err)
}
if len(deletedRecord) != 1 {
t.Fatal("expected 1 record back but received", len(deletedRecord))
}
txtRecords, err = p.getCloudDNSRecord(context.Background(), testZone, `_acme-challenge`, `TXT`)
if err != nil {
if googleError, ok := err.(*googleapi.Error); !ok || (ok && googleError.Code != 404) {
t.Fatal("error retrieving TXT record for comparison", err)
}
}
if len(txtRecords) != 0 {
t.Fatal("expectd there to be no records received but received", len(txtRecords))
}
}