-
Notifications
You must be signed in to change notification settings - Fork 4
/
dns_records_test.go
430 lines (359 loc) · 12.7 KB
/
dns_records_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
package cloudns
import (
"errors"
"github.com/stretchr/testify/assert"
"math/rand"
"net/url"
"strings"
"testing"
"time"
)
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
var randomCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func createRandomString(length int) string {
result := make([]byte, length)
for i := range result {
result[i] = randomCharset[seededRand.Intn(len(randomCharset))]
}
return string(result)
}
func createTestRecord(t *testing.T) Record {
recordName := createRandomString(16)
record := NewRecord(RecordTypeA, recordName, "127.0.0.1", testTTL)
_, err := client.Records.Create(ctx, testDomain, record)
if err != nil {
t.Fatalf("could not create test record: %v", err)
}
records, err := client.Records.Search(ctx, testDomain, recordName, RecordTypeA)
if err != nil {
t.Fatalf("could not list records for determining test record: %v", err)
}
if len(records) < 1 {
t.Fatalf("could not find created test record")
}
return records.AsSlice()[0]
}
func setupWithRecord(t *testing.T) (Record, func()) {
baseTeardown := setup(t)
testRecord := createTestRecord(t)
return testRecord, func() {
_, _ = client.Records.Delete(ctx, testDomain, testRecord.ID)
baseTeardown()
}
}
func TestRecordService_GetSOA(t *testing.T) {
teardown := setup(t)
defer teardown()
_, err := client.Records.GetSOA(ctx, testDomain)
if err != nil {
t.Fatalf("Records.GetSOA() returned error: %v", err)
}
}
func TestRecordService_UpdateSOA(t *testing.T) {
teardown := setup(t)
defer teardown()
oldSOA, err := client.Records.GetSOA(ctx, testDomain)
if err != nil {
t.Fatalf("Records.GetSOA() returned error: %v", err)
}
newSOA := oldSOA
newSOA.AdminMail = createRandomString(16) + "@" + testDomain
_, err = client.Records.UpdateSOA(ctx, testDomain, newSOA)
if err != nil {
t.Fatalf("Records.UpdateSOA() returned error: %v", err)
}
}
func TestRecordService_List(t *testing.T) {
_, teardown := setupWithRecord(t)
defer teardown()
records, err := client.Records.List(ctx, testDomain)
if err != nil {
t.Fatalf("Records.List() returned error: %v", err)
}
if len(records) < 1 {
t.Fatalf("Records.List() returned %d elements, expected >= 1", len(records))
}
}
func TestRecordService_Create(t *testing.T) {
teardown := setup(t)
defer teardown()
record := NewRecord(RecordTypeA, "localhost", "127.0.0.1", testTTL)
_, err := client.Records.Create(ctx, testDomain, record)
if err != nil {
t.Fatalf("Records.Create() returned error: %v", err)
}
}
func TestRecordService_Update(t *testing.T) {
record, teardown := setupWithRecord(t)
defer teardown()
record.Record = "127.0.0.2"
_, err := client.Records.Update(ctx, testDomain, record.ID, record)
if err != nil {
t.Fatalf("Records.Update() returned error: %v", err)
}
}
func TestRecordService_Delete(t *testing.T) {
record, teardown := setupWithRecord(t)
defer teardown()
_, err := client.Records.Delete(ctx, testDomain, record.ID)
if err != nil {
t.Fatalf("Records.Delete() returned error: %v", err)
}
}
func TestRecordService_SetActive(t *testing.T) {
record, teardown := setupWithRecord(t)
defer teardown()
_, err := client.Records.SetActive(ctx, testDomain, record.ID, false)
if err != nil {
t.Fatalf("Records.SetActive() returned error: %v", err)
}
}
func TestRecordService_Import_BIND(t *testing.T) {
teardown := setup(t)
defer teardown()
_, err := client.Records.Import(ctx, testDomain, RecordFormatBIND, "@ 3600 IN A 1.2.3.4", false)
if err != nil {
t.Fatalf("Records.Import() returned error: %v", err)
}
}
func TestRecordService_Import_Overwrite_BIND(t *testing.T) {
_, teardown := setupWithRecord(t)
defer teardown()
_, err := client.Records.Import(ctx, testDomain, RecordFormatBIND, "@ 3600 IN A 1.2.3.4", true)
if err != nil {
t.Fatalf("Records.Import() returned error: %v", err)
}
records, err := client.Records.List(ctx, testDomain)
if err != nil {
t.Fatalf("Records.List() returned error: %v", err)
}
if len(records) != 1 {
t.Fatalf("Expected single result from Records.List(), got: %d", len(records))
}
}
func TestRecordService_Import_TinyDNS(t *testing.T) {
teardown := setup(t)
defer teardown()
_, err := client.Records.Import(ctx, testDomain, RecordFormatTinyDNS, "=:1.2.3.4", false)
if err != nil {
t.Fatalf("Records.Import() returned error: %v", err)
}
}
func TestRecordService_Import_Overwrite_TinyDNS(t *testing.T) {
_, teardown := setupWithRecord(t)
defer teardown()
_, err := client.Records.Import(ctx, testDomain, RecordFormatTinyDNS, "=:1.2.3.4", true)
if err != nil {
t.Fatalf("Records.Import() returned error: %v", err)
}
records, err := client.Records.List(ctx, testDomain)
if err != nil {
t.Fatalf("Records.List() returned error: %v", err)
}
if len(records) != 1 {
t.Fatalf("Expected single result from Records.List(), got: %d", len(records))
}
}
func TestRecordService_Import_Invalid(t *testing.T) {
teardown := setup(t)
defer teardown()
_, err := client.Records.Import(ctx, testDomain, -1, "", false)
if err == nil || !errors.Is(err, ErrIllegalArgument) {
t.Fatalf("Expected ErrIllegalArgument from Records.Import() with invalid format, got: %v", err)
}
}
func TestRecordService_Export(t *testing.T) {
teardown := setup(t)
defer teardown()
_, err := client.Records.Export(ctx, testDomain)
if err != nil {
t.Fatalf("Records.Export() returned error: %v", err)
}
}
func TestRecordService_GetDynamicURL(t *testing.T) {
record, teardown := setupWithRecord(t)
defer teardown()
result, err := client.Records.GetDynamicURL(ctx, testDomain, record.ID)
if err != nil {
t.Fatalf("Records.GetDynamicURL() returned error: %v", err)
}
expectedHost := record.Host + "." + testDomain
if result.Host != expectedHost {
t.Fatalf("Records.GetDynamicURL() returned host [%s], expected [%s]", result.Host, expectedHost)
}
if _, err := url.Parse(result.URL); err != nil {
t.Fatalf("could not parse URL from Records.GetDynamicURL(): %s", result.URL)
}
}
func TestRecordService_ChangeDynamicURL(t *testing.T) {
record, teardown := setupWithRecord(t)
defer teardown()
oldResult, err := client.Records.GetDynamicURL(ctx, testDomain, record.ID)
if err != nil {
t.Fatalf("Records.GetDynamicURL() returned error: %v", err)
}
newResult, err := client.Records.ChangeDynamicURL(ctx, testDomain, record.ID)
if err != nil {
t.Fatalf("Records.ChangeDynamicURL() returned error: %v", err)
}
if oldResult.Host != newResult.Host {
t.Fatalf("Unexpected host diffence, got [%s], expected [%s]", oldResult.Host, newResult.Host)
}
if oldResult.URL == newResult.URL {
t.Fatalf("URL has not changed after Records.ChangeDynamicURL(), still got [%s]", newResult.URL)
}
}
func TestRecordService_DisableDynamicURL(t *testing.T) {
record, teardown := setupWithRecord(t)
defer teardown()
_, err := client.Records.GetDynamicURL(ctx, testDomain, record.ID)
if err != nil {
t.Fatalf("Records.GetDynamicURL() returned error: %v", err)
}
_, err = client.Records.DisableDynamicURL(ctx, testDomain, record.ID)
if err != nil {
t.Fatalf("Records.DisableDynamicURL() returned error: %v", err)
}
}
func TestRecordService_AvailableTTLs(t *testing.T) {
teardown := setup(t)
defer teardown()
ttlValues, err := client.Records.AvailableTTLs(ctx, testDomain)
if err != nil {
t.Fatalf("Records.AvailableTTLs() returned error: %v", err)
}
if len(ttlValues) < 1 {
t.Fatalf("Expected at least one result from Records.AvailableTTLs()")
}
}
func TestRecordService_AvailableRecordTypes_Valid(t *testing.T) {
teardown := setup(t)
defer teardown()
test := func(zt ZoneType, zk ZoneKind) {
recordTypes, err := client.Records.AvailableRecordTypes(ctx, zt, zk)
if err != nil {
t.Fatalf("Records.AvailableRecordTypes(%d, %d) returned error: %v", zt, zk, err)
}
if len(recordTypes) < 1 {
t.Fatalf("Expected at least one result from Records.AvailableTTLs(%d, %d)", zt, zk)
}
}
test(ZoneTypeMaster, ZoneKindDomain)
test(ZoneTypeMaster, ZoneKindIPv4)
test(ZoneTypeMaster, ZoneKindIPv6)
test(ZoneTypeGeoDNS, ZoneKindDomain)
test(ZoneTypeParked, ZoneKindDomain)
}
func TestRecordService_AvailableRecordTypes_Invalid(t *testing.T) {
teardown := setup(t)
defer teardown()
_, err := client.Records.AvailableRecordTypes(ctx, ZoneTypeSlave, ZoneKindDomain)
if err == nil || !errors.Is(err, ErrIllegalArgument) {
t.Fatalf("Expected ErrIllegalArgument from Records.AvailableRecordTypes() with invalid zone type/kind, got: %v", err)
}
}
func TestRecordService_RecordTypes(t *testing.T) {
teardown := setup(t)
defer teardown()
testRecordUpdate := func(initial, updated Record) {
var err error
// Override test-specific values for initial and updated record
initial.TTL = 60
initial.Host = initial.Host + strings.ToLower(createRandomString(16))
updated.Host = initial.Host
updated.TTL = 300
// Create the test record using the ClouDNS API
_, err = client.Records.Create(ctx, testDomain, initial)
assert.NoError(t, err, "creating test record should not fail")
// Search for the created test record
initialRecords, err := client.Records.Search(ctx, testDomain, initial.Host, initial.RecordType)
assert.NoError(t, err, "searching for initial test record should not fail")
assert.Len(t, initialRecords, 1, "result should contain a single record")
initialRecord := initialRecords.AsSlice()[0]
// Ensure to cleanup test record when test ends
defer func() {
_, err := client.Records.Delete(ctx, testDomain, initialRecord.ID)
assert.NoError(t, err, "deleting test record should not fail during cleanup")
}()
// Update the provided initial and record structs with API-provided values
initial.ID = initialRecord.ID
initial.IsActive = initialRecord.IsActive
initial.Host = initialRecord.Host
updated.ID = initialRecord.ID
updated.IsActive = initialRecord.IsActive
updated.Host = initialRecord.Host
// Ensure the current record matches the provided initial record
assert.EqualValues(t, initial, initialRecord, "created test record should match provided initial data")
// Update the record using the provided struct
_, err = client.Records.Update(ctx, testDomain, initialRecord.ID, updated)
assert.NoError(t, err, "updating test record should not fail")
// Search for the updated test record
updatedRecords, err := client.Records.Search(ctx, testDomain, updated.Host, updated.RecordType)
assert.NoError(t, err, "searching for updated test record should not fail")
assert.Len(t, updatedRecords, 1, "result should contain a single record")
updatedRecord := updatedRecords.AsSlice()[0]
// Ensure the current record matches the provided updated record
assert.EqualValues(t, initialRecord.ID, updatedRecord.ID, "updated test record should have same ID as initial test record")
assert.EqualValues(t, updated, updatedRecord, "updated test record should match provided update data")
}
testRecordUpdate(
NewRecordA("", "192.0.2.100", 0),
NewRecordA("", "192.0.2.200", 0),
)
testRecordUpdate(
NewRecordAAAA("", "2001:db8::100", 0),
NewRecordAAAA("", "2001:db8::200", 0),
)
testRecordUpdate(
NewRecordMX("", 100, "mx1.local", 0),
NewRecordMX("", 200, "mx2.local", 0),
)
testRecordUpdate(
NewRecordCNAME("", "server1.local", 0),
NewRecordCNAME("", "server2.local", 0),
)
testRecordUpdate(
NewRecordTXT("", "Hello", 0),
NewRecordTXT("", "World", 0),
)
testRecordUpdate(
NewRecordNS("", "ns1.local", 0),
NewRecordNS("", "ns2.local", 0),
)
testRecordUpdate(
NewRecordSRV("_test._tcp.", 10, 20, 30, "srv1.local", 0),
NewRecordSRV("_test._tcp.", 40, 50, 60, "srv2.local", 0),
)
testRecordUpdate(
NewRecordWebRedirect("", "http://www1.local", WebRedirect{
IsFrame: true, FrameTitle: "T", FrameKeywords: "K", FrameDescription: "D",
}, 0),
NewRecordWebRedirect("", "http://www2.local", WebRedirect{
IsFrame: false, SavePath: true, RedirectType: 302,
}, 0),
)
testRecordUpdate(
NewRecordALIAS("", "www1.local", 0),
NewRecordALIAS("", "www2.local", 0),
)
testRecordUpdate(
NewRecordRP("", "admin1@mail.local", "txt1.local", 0),
NewRecordRP("", "admin2@mail.local", "txt2.local", 0),
)
testRecordUpdate(
NewRecordSSHFP("", 1, 1, "4fca1fe60ec4fca4053504f4fcab0d5d7c99bd0f", 0),
NewRecordSSHFP("", 3, 2, "1357acf64348f3f7bd0942ba75878ebd3a75af979007f059741d29f95c4a0b80", 0),
)
testRecordUpdate(
NewRecordNAPTR("", 10, 20, "U", "svc1.local", "Hello", "", 0),
NewRecordNAPTR("", 30, 40, "S", "svc2.local", "", "World", 0),
)
testRecordUpdate(
NewRecordCAA("", 0, "issue", "ca1.local", 0),
NewRecordCAA("", 128, "issuewild", "ca2.local", 0),
)
testRecordUpdate(
NewRecordTLSA("_443._tcp.", 0, 1, 2, "53472ce4477a2b2f17085ff9f0b07ecad2091d25a9ec02dda622c741e62c75e4ee8dea577db822caa32935a86e52827c51bf000a65506d4f760dab7712ed9a25", 0),
NewRecordTLSA("_443._tcp.", 2, 0, 1, "078a656e3670499c991bb0274682058af7bdc05fc462c605f0f8958179816cd7", 0),
)
}