forked from libdns/hetzner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_test.go
260 lines (226 loc) · 6.17 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
package vercel_test
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
vercel "github.com/fairhat/libdns-vercel"
"github.com/libdns/libdns"
)
var (
envToken = ""
envZone = ""
ttl = time.Duration(120 * time.Second)
)
type testRecordsCleanup = func()
func setupTestRecords(t *testing.T, p *vercel.Provider) ([]libdns.Record, testRecordsCleanup) {
testRecords := []libdns.Record{
{
Type: "TXT",
Name: "test1",
Value: "test1",
TTL: ttl,
}, {
Type: "TXT",
Name: "test2",
Value: "test2",
TTL: ttl,
}, {
Type: "TXT",
Name: "test3",
Value: "test3",
TTL: ttl,
},
}
records, err := p.AppendRecords(context.TODO(), envZone, testRecords)
if err != nil {
t.Fatal(err)
return nil, func() {}
}
return records, func() {
cleanupRecords(t, p, envZone, records)
}
}
func cleanupRecords(t *testing.T, p *vercel.Provider, zone string, r []libdns.Record) {
_, err := p.DeleteRecords(context.TODO(), zone, r)
if err != nil {
t.Fatalf("cleanup failed: %v", err)
}
}
func TestMain(m *testing.M) {
envToken = os.Getenv("LIBDNS_VERCEL_TEST_TOKEN")
envZone = os.Getenv("LIBDNS_VERCEL_TEST_ZONE")
if len(envToken) == 0 || len(envZone) == 0 {
fmt.Println(`Please notice that this test runs agains the public Vercel DNS Api, so you sould
never run the test with a zone, used in production.
To run this test, you have to specify 'LIBDNS_VERCEL_TEST_TOKEN' and 'LIBDNS_VERCEL_TEST_ZONE'.
Example: "LIBDNS_VERCEL_TEST_TOKEN="123" LIBDNS_VERCEL_TEST_ZONE="my-domain.com" go test ./... -v`)
os.Exit(1)
}
os.Exit(m.Run())
}
func Test_AppendRecords(t *testing.T) {
p := &vercel.Provider{
AuthAPIToken: envToken,
}
testCases := []struct {
records []libdns.Record
expected []libdns.Record
}{
{
// multiple records
records: []libdns.Record{
{Type: "TXT", Name: "test_1", Value: "test_1", TTL: ttl},
{Type: "TXT", Name: "test_2", Value: "test_2", TTL: ttl},
{Type: "TXT", Name: "test_3", Value: "test_3", TTL: ttl},
},
expected: []libdns.Record{
{Type: "TXT", Name: "test_1", Value: "test_1", TTL: ttl},
{Type: "TXT", Name: "test_2", Value: "test_2", TTL: ttl},
{Type: "TXT", Name: "test_3", Value: "test_3", TTL: ttl},
},
},
{
// relative name
records: []libdns.Record{
{Type: "TXT", Name: "123.test", Value: "123", TTL: ttl},
},
expected: []libdns.Record{
{Type: "TXT", Name: "123.test", Value: "123", TTL: ttl},
},
},
{
// (fqdn) sans trailing dot
records: []libdns.Record{
{Type: "TXT", Name: fmt.Sprintf("123.test.%s", strings.TrimSuffix(envZone, ".")), Value: "test", TTL: ttl},
},
expected: []libdns.Record{
{Type: "TXT", Name: "123.test", Value: "test", TTL: ttl},
},
},
{
// fqdn with trailing dot
records: []libdns.Record{
{Type: "TXT", Name: fmt.Sprintf("123.test.%s.", strings.TrimSuffix(envZone, ".")), Value: "test", TTL: ttl},
},
expected: []libdns.Record{
{Type: "TXT", Name: "123.test", Value: "test", TTL: ttl},
},
},
}
for _, c := range testCases {
func() {
result, err := p.AppendRecords(context.TODO(), envZone+".", c.records)
if err != nil {
t.Fatal(err)
}
defer cleanupRecords(t, p, envZone, result)
if len(result) != len(c.records) {
t.Fatalf("len(resilt) != len(c.records) => %d != %d", len(c.records), len(result))
}
for k, r := range result {
if len(result[k].ID) == 0 {
t.Fatalf("len(result[%d].ID) == 0", k)
}
if r.Type != c.expected[k].Type {
t.Fatalf("r.Type != c.exptected[%d].Type => %s != %s", k, r.Type, c.expected[k].Type)
}
if r.Name != c.expected[k].Name {
t.Fatalf("r.Name != c.exptected[%d].Name => %s != %s", k, r.Name, c.expected[k].Name)
}
if r.Value != c.expected[k].Value {
t.Fatalf("r.Value != c.exptected[%d].Value => %s != %s", k, r.Value, c.expected[k].Value)
}
// cant check for TTL because vercel does not return any ttl values
// if r.TTL != c.expected[k].TTL {
// t.Fatalf("r.TTL != c.exptected[%d].TTL => %s != %s", k, r.TTL, c.expected[k].TTL)
// }
}
}()
}
}
func Test_DeleteRecords(t *testing.T) {
p := &vercel.Provider{
AuthAPIToken: envToken,
}
testRecords, cleanupFunc := setupTestRecords(t, p)
defer cleanupFunc()
records, err := p.GetRecords(context.TODO(), envZone)
if err != nil {
t.Fatal(err)
}
if len(records) < len(testRecords) {
t.Fatalf("len(records) < len(testRecords) => %d < %d", len(records), len(testRecords))
}
for _, testRecord := range testRecords {
var foundRecord *libdns.Record
for _, record := range records {
if testRecord.ID == record.ID {
foundRecord = &testRecord
}
}
if foundRecord == nil {
t.Fatalf("Record not found => %s", testRecord.ID)
}
}
}
func Test_GetRecords(t *testing.T) {
p := &vercel.Provider{
AuthAPIToken: envToken,
}
testRecords, cleanupFunc := setupTestRecords(t, p)
defer cleanupFunc()
records, err := p.GetRecords(context.TODO(), envZone)
if err != nil {
t.Fatal(err)
}
if len(records) < len(testRecords) {
t.Fatalf("len(records) < len(testRecords) => %d < %d", len(records), len(testRecords))
}
for _, testRecord := range testRecords {
var foundRecord *libdns.Record
for _, record := range records {
if testRecord.ID == record.ID {
foundRecord = &testRecord
}
}
if foundRecord == nil {
t.Fatalf("Record not found => %s", testRecord.ID)
}
}
}
func Test_SetRecords(t *testing.T) {
p := &vercel.Provider{
AuthAPIToken: envToken,
}
existingRecords, _ := setupTestRecords(t, p)
newTestRecords := []libdns.Record{
{
Type: "TXT",
Name: "new_test1",
Value: "new_test1",
TTL: ttl,
},
{
Type: "TXT",
Name: "new_test2",
Value: "new_test2",
TTL: ttl,
},
}
allRecords := append(existingRecords, newTestRecords...)
allRecords[0].Value = "new_value"
records, err := p.SetRecords(context.TODO(), envZone, allRecords)
if err != nil {
t.Fatal(err)
}
defer cleanupRecords(t, p, envZone, records)
if len(records) != len(allRecords) {
t.Fatalf("len(records) != len(allRecords) => %d != %d", len(records), len(allRecords))
}
if records[0].Value != "new_value" {
t.Fatalf(`records[0].Value != "new_value" => %s != "new_value"`, records[0].Value)
}
}