-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_test.go
159 lines (137 loc) · 2.71 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
package he
import (
"context"
"log"
"os"
"testing"
"github.com/libdns/libdns"
)
var (
apiKey = os.Getenv("LIBDNS_HE_KEY")
zone = os.Getenv("LIBDNS_HE_ZONE")
)
var (
provider Provider
)
func init() {
if apiKey == "" {
log.Fatalf("API key needs to be provided in env var LIBDNS_HE_KEY")
}
if zone == "" {
log.Fatalf("DNS zone needs to be provided in env var LIBDNS_HE_ZONE")
}
if zone[len(zone)-1:] != "." {
// Zone names come from caddy with trailing period
zone += "."
}
provider = Provider{APIKey: apiKey}
}
func TestAppendRecords(t *testing.T) {
ctx := context.Background()
records := []libdns.Record{
{
Type: "A",
Name: "test001",
Value: "192.0.2.1",
},
{
Type: "AAAA",
Name: "test001",
Value: "2001:0db8:2::1",
},
{
Type: "TXT",
Name: "test001",
Value: "ZYXWVUTSRQPONMLKJIHGFEDCBA",
},
}
createdRecords, err := provider.AppendRecords(ctx, zone, records)
if err != nil {
t.Errorf("%v", err)
}
if len(records) != len(createdRecords) {
t.Errorf("Number of appended records does not match number of records")
}
}
func TestGetRecords(t *testing.T) {
ctx := context.Background()
records, err := provider.GetRecords(ctx, zone)
if err != nil {
t.Errorf("%v", err)
}
if len(records) == 0 {
t.Errorf("No records")
}
}
func TestSetRecords(t *testing.T) {
ctx := context.Background()
goodRecords := []libdns.Record{
{
Type: "A",
Name: "test001",
Value: "198.51.100.1",
},
{
Type: "AAAA",
Name: "test001",
Value: "2001:0db8::1",
},
{
Type: "TXT",
Name: "test001",
Value: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
},
{
Type: "A",
Name: "test002",
Value: "198.51.100.2",
},
{
Type: "A",
Name: "test003",
Value: "198.51.100.3",
},
}
createdRecords, err := provider.SetRecords(ctx, zone, goodRecords)
if err != nil {
t.Fatalf("adding records failed: %v", err)
}
if len(goodRecords) != len(createdRecords) {
t.Fatalf("Number of added records does not match number of records")
}
badRecords := []libdns.Record{
{
Type: "CNAME",
Name: "test000",
Value: "example.org",
},
}
_, err = provider.SetRecords(ctx, zone, badRecords)
if err == nil {
t.Fatalf("unsupported records should return error")
}
}
func TestDeleteRecords(t *testing.T) {
ctx := context.Background()
records := []libdns.Record{
{
Type: "A",
Name: "test001",
},
{
Type: "AAAA",
Name: "test001",
},
{
Type: "TXT",
Name: "test001",
},
}
deletedRecords, err := provider.DeleteRecords(ctx, zone, records)
if err != nil {
t.Errorf("deleting records failed: %v", err)
}
if len(records) != len(deletedRecords) {
t.Errorf("Number of deleted records does not match number of records")
}
}