-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_test.go
More file actions
158 lines (138 loc) · 4.14 KB
/
provider_test.go
File metadata and controls
158 lines (138 loc) · 4.14 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package httpnet_test
import (
"context"
"os"
"sort"
"testing"
"time"
"github.com/libdns/httpnet"
"github.com/libdns/libdns"
)
// Integration tests that run against the live http.net API.
// Set the following environment variables to run these tests:
//
// HTTPNET_AUTH_TOKEN – your http.net API token
// HTTPNET_TEST_ZONE – a zone you control (e.g. "example.com.")
func newProvider(t *testing.T) (*httpnet.Provider, string) {
t.Helper()
token := os.Getenv("HTTPNET_AUTH_TOKEN")
zone := os.Getenv("HTTPNET_TEST_ZONE")
if token == "" || zone == "" {
t.Skip("HTTPNET_AUTH_TOKEN and HTTPNET_TEST_ZONE must be set to run integration tests")
}
return &httpnet.Provider{AuthToken: token}, zone
}
// deleteByName fetches the zone and deletes all records matching the given
// relative name. Used for cleanup so that content changes don't break matching.
func deleteByName(t *testing.T, p *httpnet.Provider, zone, name string) {
t.Helper()
ctx := context.Background()
all, err := p.GetRecords(ctx, zone)
if err != nil {
t.Logf("cleanup GetRecords: %v", err)
return
}
var toDelete []libdns.Record
for _, r := range all {
if r.RR().Name == name {
toDelete = append(toDelete, r)
}
}
if len(toDelete) == 0 {
return
}
if _, err := p.DeleteRecords(ctx, zone, toDelete); err != nil {
t.Logf("cleanup DeleteRecords: %v", err)
}
}
func TestGetRecords(t *testing.T) {
p, zone := newProvider(t)
records, err := p.GetRecords(context.Background(), zone)
if err != nil {
t.Fatalf("GetRecords: %v", err)
}
t.Logf("found %d records in zone %s", len(records), zone)
}
func TestAppendAndDeleteRecords(t *testing.T) {
p, zone := newProvider(t)
ctx := context.Background()
// Remove any leftovers from a previously interrupted run.
deleteByName(t, p, zone, "_libdns_test")
t.Cleanup(func() { deleteByName(t, p, zone, "_libdns_test") })
toCreate := []libdns.Record{
libdns.TXT{
Name: "_libdns_test",
TTL: 60 * time.Second,
Text: "libdns-httpnet-test-value",
},
}
created, err := p.AppendRecords(ctx, zone, toCreate)
if err != nil {
t.Fatalf("AppendRecords: %v", err)
}
t.Logf("created: %+v", created)
deleted, err := p.DeleteRecords(ctx, zone, created)
if err != nil {
t.Fatalf("DeleteRecords: %v", err)
}
t.Logf("deleted: %+v", deleted)
}
func TestSetRecords(t *testing.T) {
p, zone := newProvider(t)
ctx := context.Background()
// Remove any leftovers from a previously interrupted run.
deleteByName(t, p, zone, "_libdns_set_test")
t.Cleanup(func() { deleteByName(t, p, zone, "_libdns_set_test") })
initial := libdns.TXT{
Name: "_libdns_set_test",
TTL: 60 * time.Second,
Text: "initial",
}
created, err := p.AppendRecords(ctx, zone, []libdns.Record{initial})
if err != nil {
t.Fatalf("AppendRecords: %v", err)
}
updated := libdns.TXT{
Name: "_libdns_set_test",
TTL: 60 * time.Second,
Text: "updated",
}
set, err := p.SetRecords(ctx, zone, []libdns.Record{updated})
if err != nil {
_, _ = p.DeleteRecords(ctx, zone, created)
t.Fatalf("SetRecords: %v", err)
}
t.Logf("set: %+v", set)
}
// TestMultipleTXTSameName verifies that http.net accepts multiple TXT records
// at the same (name, type) -- which is what ACME DNS-01 requires for a
// wildcard + apex certificate (two TXT records at _acme-challenge.<zone>).
func TestMultipleTXTSameName(t *testing.T) {
p, zone := newProvider(t)
ctx := context.Background()
const name = "_libdns_multitxt_test"
deleteByName(t, p, zone, name)
t.Cleanup(func() { deleteByName(t, p, zone, name) })
if _, err := p.AppendRecords(ctx, zone, []libdns.Record{
libdns.TXT{Name: name, TTL: 60 * time.Second, Text: "value-one"},
libdns.TXT{Name: name, TTL: 60 * time.Second, Text: "value-two"},
}); err != nil {
t.Fatalf("AppendRecords: %v", err)
}
all, err := p.GetRecords(ctx, zone)
if err != nil {
t.Fatalf("GetRecords: %v", err)
}
var values []string
for _, r := range all {
rr := r.RR()
if rr.Name == name && rr.Type == "TXT" {
values = append(values, rr.Data)
}
}
sort.Strings(values)
want := []string{"value-one", "value-two"}
if len(values) != len(want) || values[0] != want[0] || values[1] != want[1] {
t.Errorf("got %v, want %v", values, want)
}
}