forked from libdns/libdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider_test.go
155 lines (135 loc) · 3.26 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
package dnsexit
import (
"context"
"log"
"os"
"reflect"
"testing"
"github.com/joho/godotenv"
"github.com/libdns/libdns"
)
var (
apiKey string
zone string
)
var (
provider Provider
)
// It is best to run these tests against a throwaway domain to prevent mistakes (DNSExit provides free domains). There is no cleanup after each test, so they do not work well as a suite, and are best run individually. Because the getRecords functionality uses Google DNS, there need to be records in the domain, and they need to have replicated to Google's DNS servers before this test will pass. (You can use https://toolbox.googleapps.com/apps/dig/ to verify before running.)
func init() {
envErr := godotenv.Load()
if envErr != nil {
log.Fatalf("Unable to load environment variables from file")
}
apiKey = os.Getenv("LIBDNS_DNSEXIT_API_KEY")
zone = os.Getenv("LIBDNS_DNSEXIT_ZONE")
debug = os.Getenv("LIBDNS_DNSEXIT_DEBUG") == "TRUE"
if apiKey == "" {
log.Fatalf("API key needs to be provided in env var LIBDNS_DNSEXIT_API_KEY")
}
if zone == "" {
log.Fatalf("DNS zone needs to be provided in env var LIBDNS_DNSEXIT_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")
}
if !(reflect.DeepEqual(records, createdRecords)) {
t.Errorf("Appended records do not match")
}
}
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")
}
}
func TestDeleteRecords(t *testing.T) {
ctx := context.Background()
records := []libdns.Record{
{
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")
}
}