-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathdomain_records_test.go
138 lines (117 loc) · 3.98 KB
/
domain_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
package linodego_test
import (
"context"
"encoding/json"
"testing"
"github.com/linode/linodego"
)
var (
testDomainRecordCreateOpts = linodego.DomainRecordCreateOptions{
Target: "127.0.0.1",
Type: linodego.RecordTypeA,
Name: "a",
}
)
func TestCreateDomainRecord(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
_, _, record, teardown, err := setupDomainRecord(t, "fixtures/TestCreateDomainRecord")
defer teardown()
if err != nil {
t.Errorf("Error creating domain record, got error %v", err)
}
expected := testDomainRecordCreateOpts
// cant compare Target, fixture IPs are sanitized
if record.Name != expected.Name || record.Type != expected.Type {
t.Errorf("DomainRecord did not match CreateOptions")
}
}
func TestUpdateDomainRecord(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestUpdateDomainRecord")
defer teardown()
updateOpts := linodego.DomainRecordUpdateOptions{
Name: "renamed",
}
recordUpdated, err := client.UpdateDomainRecord(context.Background(), domain.ID, record.ID, updateOpts)
if err != nil {
t.Errorf("Error updating domain record, %s", err)
}
if recordUpdated.Name != "renamed" || record.Type != recordUpdated.Type || recordUpdated.Target != record.Target {
t.Errorf("DomainRecord did not match UpdateOptions")
}
}
func TestListDomainRecords(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestListDomainRecords")
defer teardown()
filter, err := json.Marshal(map[string]interface{}{
"name": record.Name,
})
listOpts := linodego.NewListOptions(0, string(filter))
records, err := client.ListDomainRecords(context.Background(), domain.ID, listOpts)
if err != nil {
t.Errorf("Error listing domains records, expected array, got error %v", err)
}
if len(records) != 1 {
t.Errorf("Expected ListDomainRecords to match one result")
}
}
func TestListDomainRecordsMultiplePages(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestListDomainRecordsMultiplePages")
defer teardown()
filter, err := json.Marshal(map[string]interface{}{
"name": record.Name,
})
listOpts := linodego.NewListOptions(0, string(filter))
records, err := client.ListDomainRecords(context.Background(), domain.ID, listOpts)
if err != nil {
t.Errorf("Error listing domains records, expected array, got error %v", err)
}
if len(records) != 2 {
t.Errorf("Expected ListDomainRecords to match one result")
}
}
func TestGetDomainRecord(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestGetDomainRecord")
defer teardown()
recordGot, err := client.GetDomainRecord(context.Background(), domain.ID, record.ID)
if recordGot.Name != record.Name {
t.Errorf("GetDomainRecord did not get the expected record")
}
if err != nil {
t.Errorf("Error getting domain %d, got error %v", domain.ID, err)
}
}
func setupDomainRecord(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.Domain, *linodego.DomainRecord, func(), error) {
t.Helper()
var fixtureTeardown func()
client, domain, fixtureTeardown, err := setupDomain(t, fixturesYaml)
if err != nil {
t.Errorf("Error creating domain, got error %v", err)
}
createOpts := testDomainRecordCreateOpts
record, err := client.CreateDomainRecord(context.Background(), domain.ID, createOpts)
if err != nil {
t.Errorf("Error creating domain record, got error %v", err)
}
teardown := func() {
// delete the DomainRecord to excercise the code
if err := client.DeleteDomainRecord(context.Background(), domain.ID, record.ID); err != nil {
t.Errorf("Expected to delete a domain record, but got %v", err)
}
fixtureTeardown()
}
return client, domain, record, teardown, err
}