-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprovider_test.go
More file actions
138 lines (114 loc) · 3.13 KB
/
provider_test.go
File metadata and controls
138 lines (114 loc) · 3.13 KB
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
// Integration tests for the netcup provider
package netcup
import (
"context"
"fmt"
"os"
"testing"
"github.com/libdns/libdns"
)
var (
customerNumber = ""
apiKey = ""
apiPassword = ""
zone = ""
testRecords = []libdns.Record{
libdns.TXT{
Name: "test",
Text: "testval1",
},
}
)
func TestMain(m *testing.M) {
fmt.Println("Loading environment variables to set up provider")
customerNumber = os.Getenv("LIBDNS_NETCUP_CUSTOMER_NUMBER")
apiKey = os.Getenv("LIBDNS_NETCUP_API_KEY")
apiPassword = os.Getenv("LIBDNS_NETCUP_API_PASSWORD")
zone = os.Getenv("LIBDNS_NETCUP_ZONE")
os.Exit(m.Run())
}
func setupTestRecords(t *testing.T, p *Provider) []libdns.Record {
fmt.Println("Appending test records")
records, err := p.AppendRecords(context.TODO(), zone, testRecords)
if err != nil {
t.Fatalf("Setup failed: %v", err)
return nil
}
return records
}
func cleanupRecords(t *testing.T, p *Provider, records []libdns.Record) {
fmt.Println("Cleaning up test records")
if _, err := p.DeleteRecords(context.TODO(), zone, records); err != nil {
t.Fatalf("Cleanup failed: %v", err)
}
}
func TestProvider_GetRecords(t *testing.T) {
fmt.Println("Test GetRecords")
p := &Provider{
CustomerNumber: customerNumber,
APIKey: apiKey,
APIPassword: apiPassword,
}
setupRecords := setupTestRecords(t, p)
defer cleanupRecords(t, p, setupRecords)
records, err := p.GetRecords(context.TODO(), zone)
if err != nil {
t.Fatal(err)
}
if len(records) < len(setupRecords) {
t.Fatalf("Number of records found should have been at least %v", len(setupRecords))
}
// this is actually more a test of AppendRecords
for _, setupRecord := range setupRecords {
var foundRecord *libdns.Record
for _, record := range records {
// NOTE: Here we can do a full equals
if rrEqualsRR(setupRecord.RR(), record.RR()) {
foundRecord = &setupRecord
}
}
if foundRecord == nil {
t.Fatalf("Record %v not found", setupRecord)
}
}
}
func TestProvider_SetRecords(t *testing.T) {
fmt.Println("Test SetRecords")
p := &Provider{
CustomerNumber: customerNumber,
APIKey: apiKey,
APIPassword: apiPassword,
}
setupRecords := setupTestRecords(t, p)
defer cleanupRecords(t, p, setupRecords)
var updateRecords []libdns.Record
// test, if records without IDs update the correct records
for _, record := range testRecords {
rr := record.RR()
updateRecords = append(updateRecords, libdns.RR{
Type: rr.Type,
Name: rr.Name,
Data: rr.Data + "edit",
})
}
records, err := p.SetRecords(context.TODO(), zone, updateRecords)
if err != nil {
t.Fatal(err)
}
if len(records) < len(setupRecords) {
t.Fatalf("Number of records set should have been at least %v", len(setupRecords))
}
for _, setupRecord := range setupRecords {
var foundRecord *libdns.Record
for _, record := range records {
// NOTE: Due to updating the record value, we can only compare the ID.
// As we don't have an ID, we only compare Name and Type.
if rrMatchID(record.RR(), setupRecord.RR()) {
foundRecord = &setupRecord
}
}
if foundRecord == nil {
t.Fatalf("Record %v not found", setupRecord)
}
}
}