-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_test.go
More file actions
250 lines (221 loc) · 7 KB
/
mock_test.go
File metadata and controls
250 lines (221 loc) · 7 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package httpnet_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sort"
"strings"
"testing"
"time"
"github.com/libdns/httpnet"
"github.com/libdns/libdns"
)
// mockServer stands in for the http.net API and returns scripted responses.
// It records the sequence of recordsUpdate payloads for assertions.
type mockServer struct {
t *testing.T
// zone contains the apiZoneConfig row the mock returns for zoneConfigsFind.
zoneID, zoneName string
// records is the current set of records in the zone.
records []mockRecord
// updateCalls captures every recordsUpdate request body for inspection.
updateCalls []recordsUpdateReq
}
type mockRecord struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
TTL int `json:"ttl,omitempty"`
}
type recordsUpdateReq struct {
AuthToken string `json:"authToken"`
ZoneName string `json:"zoneName"`
RecordsToAdd []mockRecord `json:"recordsToAdd"`
RecordsToModify []mockRecord `json:"recordsToModify"`
RecordsToDelete []mockRecord `json:"recordsToDelete"`
}
func (m *mockServer) handler(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
method := strings.TrimPrefix(r.URL.Path, "/")
switch method {
case "zoneConfigsFind":
resp := map[string]any{
"status": "success",
"response": map[string]any{
"data": []map[string]any{{"id": m.zoneID, "name": m.zoneName}},
"totalEntries": 1,
"totalPages": 1,
},
}
_ = json.NewEncoder(w).Encode(resp)
case "recordsFind":
resp := map[string]any{
"status": "success",
"response": map[string]any{
"data": m.records,
"totalEntries": len(m.records),
"totalPages": 1,
},
}
_ = json.NewEncoder(w).Encode(resp)
case "recordsUpdate":
var req recordsUpdateReq
if err := json.Unmarshal(body, &req); err != nil {
m.t.Fatalf("mock: decoding recordsUpdate: %v", err)
}
m.updateCalls = append(m.updateCalls, req)
resp := map[string]any{
"status": "success",
"response": map[string]any{"records": m.records},
}
_ = json.NewEncoder(w).Encode(resp)
default:
m.t.Fatalf("mock: unexpected API method %q", method)
}
}
func newMockProvider(t *testing.T, existing []mockRecord) (*httpnet.Provider, *mockServer) {
t.Helper()
mock := &mockServer{
t: t,
zoneID: "zone-1",
zoneName: "example.com",
records: existing,
}
srv := httptest.NewServer(http.HandlerFunc(mock.handler))
t.Cleanup(srv.Close)
return &httpnet.Provider{
AuthToken: "token",
BaseURL: srv.URL,
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
}, mock
}
// TestSetRecords_ReplacesRRset verifies that SetRecords deletes extra existing
// records in the same RRset — the core libdns v1 contract previously broken.
func TestSetRecords_ReplacesRRset(t *testing.T) {
existing := []mockRecord{
{ID: "r1", Name: "www.example.com", Type: "A", Content: "1.1.1.1", TTL: 3600},
{ID: "r2", Name: "www.example.com", Type: "A", Content: "2.2.2.2", TTL: 3600},
{ID: "r3", Name: "www.example.com", Type: "A", Content: "3.3.3.3", TTL: 3600},
// Different RRset; must NOT be touched.
{ID: "r4", Name: "mail.example.com", Type: "A", Content: "9.9.9.9", TTL: 3600},
}
p, mock := newMockProvider(t, existing)
_, err := p.SetRecords(context.Background(), "example.com.", []libdns.Record{
libdns.RR{Name: "www", Type: "A", TTL: 3600 * time.Second, Data: "1.1.1.1"},
})
if err != nil {
t.Fatalf("SetRecords: %v", err)
}
if len(mock.updateCalls) != 1 {
t.Fatalf("expected 1 recordsUpdate call, got %d", len(mock.updateCalls))
}
call := mock.updateCalls[0]
deletedIDs := idsOf(call.RecordsToDelete)
sort.Strings(deletedIDs)
// r1 was reused by content match; r2 and r3 must be deleted. r4 untouched.
want := []string{"r2", "r3"}
if !equalStrings(deletedIDs, want) {
t.Errorf("deleted IDs: got %v, want %v", deletedIDs, want)
}
for _, d := range call.RecordsToDelete {
if d.Name == "mail.example.com" {
t.Errorf("SetRecords touched unrelated RRset: %+v", d)
}
}
if len(call.RecordsToAdd) != 0 {
t.Errorf("expected 0 adds, got %d: %+v", len(call.RecordsToAdd), call.RecordsToAdd)
}
}
// TestSetRecords_MultipleNewRecordsSameRRset verifies that the provider can
// set multiple records under the same (name, type) without collapsing IDs —
// the previous implementation would assign the same ID to all of them.
func TestSetRecords_MultipleNewRecordsSameRRset(t *testing.T) {
existing := []mockRecord{
{ID: "r1", Name: "www.example.com", Type: "A", Content: "1.1.1.1", TTL: 3600},
}
p, mock := newMockProvider(t, existing)
_, err := p.SetRecords(context.Background(), "example.com.", []libdns.Record{
libdns.RR{Name: "www", Type: "A", TTL: 3600 * time.Second, Data: "1.1.1.1"},
libdns.RR{Name: "www", Type: "A", TTL: 3600 * time.Second, Data: "2.2.2.2"},
libdns.RR{Name: "www", Type: "A", TTL: 3600 * time.Second, Data: "3.3.3.3"},
})
if err != nil {
t.Fatalf("SetRecords: %v", err)
}
call := mock.updateCalls[0]
if len(call.RecordsToAdd) != 2 {
t.Errorf("expected 2 adds, got %d", len(call.RecordsToAdd))
}
// No ID collision in the modify list.
seen := map[string]bool{}
for _, m := range call.RecordsToModify {
if m.ID != "" && seen[m.ID] {
t.Errorf("duplicate ID in recordsToModify: %s", m.ID)
}
seen[m.ID] = true
}
}
// TestDeleteRecords_WholeRRset verifies that a record with empty Data deletes
// every record at the matching (name, type).
func TestDeleteRecords_WholeRRset(t *testing.T) {
existing := []mockRecord{
{ID: "r1", Name: "www.example.com", Type: "A", Content: "1.1.1.1"},
{ID: "r2", Name: "www.example.com", Type: "A", Content: "2.2.2.2"},
{ID: "r3", Name: "www.example.com", Type: "AAAA", Content: "::1"},
}
p, mock := newMockProvider(t, existing)
deleted, err := p.DeleteRecords(context.Background(), "example.com.", []libdns.Record{
libdns.RR{Name: "www", Type: "A"},
})
if err != nil {
t.Fatalf("DeleteRecords: %v", err)
}
if len(deleted) != 2 {
t.Errorf("expected 2 deleted, got %d", len(deleted))
}
call := mock.updateCalls[0]
deletedIDs := idsOf(call.RecordsToDelete)
sort.Strings(deletedIDs)
want := []string{"r1", "r2"}
if !equalStrings(deletedIDs, want) {
t.Errorf("deleted IDs: got %v, want %v", deletedIDs, want)
}
}
// TestListZones verifies the ZoneLister implementation and the trailing-dot fixup.
func TestListZones(t *testing.T) {
p, mock := newMockProvider(t, nil)
mock.zoneID = "z-42"
zones, err := p.ListZones(context.Background())
if err != nil {
t.Fatalf("ListZones: %v", err)
}
if len(zones) != 1 {
t.Fatalf("expected 1 zone, got %d", len(zones))
}
if zones[0].Name != "example.com." {
t.Errorf("Name: got %q, want %q", zones[0].Name, "example.com.")
}
}
func idsOf(rs []mockRecord) []string {
out := make([]string, 0, len(rs))
for _, r := range rs {
out = append(out, r.ID)
}
return out
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}