-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhelper_test.go
More file actions
314 lines (278 loc) · 6.79 KB
/
Copy pathhelper_test.go
File metadata and controls
314 lines (278 loc) · 6.79 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package stackitprovider
import (
"reflect"
"strings"
"testing"
stackitdnsclient "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api"
"go.uber.org/zap"
"sigs.k8s.io/external-dns/endpoint"
)
func TestAppendDotIfNotExists(t *testing.T) {
t.Parallel()
tests := []struct {
name string
s string
want string
}{
{"No dot at end", "test", "test."},
{"Dot at end", "test.", "test."},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := appendDotIfNotExists(tt.s); got != tt.want {
t.Errorf("appendDotIfNotExists() = %v, want %v", got, tt.want)
}
})
}
}
func TestModifyChange(t *testing.T) {
t.Parallel()
endpointWithTTL := &endpoint.Endpoint{
DNSName: "test",
RecordTTL: endpoint.TTL(400),
}
modifyChange(endpointWithTTL)
if endpointWithTTL.DNSName != "test." {
t.Errorf("modifyChange() did not append dot to DNSName = %v, want test.", endpointWithTTL.DNSName)
}
if endpointWithTTL.RecordTTL != 400 {
t.Errorf("modifyChange() changed existing RecordTTL = %v, want 400", endpointWithTTL.RecordTTL)
}
endpointWithoutTTL := &endpoint.Endpoint{
DNSName: "test",
}
modifyChange(endpointWithoutTTL)
if endpointWithoutTTL.DNSName != "test." {
t.Errorf("modifyChange() did not append dot to DNSName = %v, want test.", endpointWithoutTTL.DNSName)
}
if endpointWithoutTTL.RecordTTL != 300 {
t.Errorf("modifyChange() did not set default RecordTTL = %v, want 300", endpointWithoutTTL.RecordTTL)
}
}
func TestGetStackitRRSetRecordPost(t *testing.T) {
t.Parallel()
change := &endpoint.Endpoint{
DNSName: "test.",
RecordTTL: endpoint.TTL(300),
RecordType: "A",
Targets: endpoint.Targets{
"192.0.2.1",
"192.0.2.2",
},
}
expected := stackitdnsclient.CreateRecordSetPayload{
Name: "test.",
Ttl: new(int32(300)),
Type: "A",
Records: []stackitdnsclient.RecordPayload{
{
Content: "192.0.2.1",
},
{
Content: "192.0.2.2",
},
},
}
got := getStackitRecordSetPayload(change)
if !reflect.DeepEqual(got, expected) {
t.Errorf("getStackitRRSetRecordPost() = %v, want %v", got, expected)
}
}
func TestFindBestMatchingZone(t *testing.T) {
t.Parallel()
zones := []stackitdnsclient.Zone{
{DnsName: "foo.com"},
{DnsName: "bar.com"},
{DnsName: "baz.com"},
}
tests := []struct {
name string
rrSetName string
want *stackitdnsclient.Zone
wantFound bool
}{
{"Matching Zone", "www.foo.com", &zones[0], true},
{"No Matching Zone", "www.test.com", nil, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, found := findBestMatchingZone(tt.rrSetName, zones)
if !reflect.DeepEqual(got, tt.want) || found != tt.wantFound {
t.Errorf("findBestMatchingZone() = %v, %v, want %v, %v", got, found, tt.want, tt.wantFound)
}
})
}
}
func TestFindRRSet(t *testing.T) {
t.Parallel()
rrSets := []stackitdnsclient.RecordSet{
{Name: "www.foo.com", Type: "A"},
{Name: "www.bar.com", Type: "A"},
{Name: "www.baz.com", Type: "A"},
}
tests := []struct {
name string
rrSetName string
recordType string
want *stackitdnsclient.RecordSet
wantFound bool
}{
{"Matching RRSet", "www.foo.com", "A", &rrSets[0], true},
{"No Matching RRSet", "www.test.com", "A", nil, false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, found := findRRSet(tt.rrSetName, tt.recordType, rrSets)
if !reflect.DeepEqual(got, tt.want) || found != tt.wantFound {
t.Errorf("findRRSet() = %v, %v, want %v, %v", got, found, tt.want, tt.wantFound)
}
})
}
}
func TestGetLogFields(t *testing.T) {
t.Parallel()
change := &endpoint.Endpoint{
DNSName: "test.",
RecordTTL: endpoint.TTL(300),
RecordType: "A",
Targets: endpoint.Targets{
"192.0.2.1",
"192.0.2.2",
},
}
expected := []zap.Field{
zap.String("record", "test."),
zap.String("content", "192.0.2.1,192.0.2.2"),
zap.String("type", "A"),
zap.String("action", "create"),
zap.String("id", "123"),
}
got := getLogFields(change, "create", "123")
if !reflect.DeepEqual(got, expected) {
t.Errorf("getLogFields() = %v, want %v", got, expected)
}
}
func TestGetStackitRRSetRecordPatch(t *testing.T) {
t.Parallel()
change := &endpoint.Endpoint{
DNSName: "test.",
RecordTTL: endpoint.TTL(300),
RecordType: "A",
Targets: endpoint.Targets{
"192.0.2.1",
"192.0.2.2",
},
}
expected := stackitdnsclient.PartialUpdateRecordSetPayload{
Name: new("test."),
Ttl: new(int32(300)),
Records: []stackitdnsclient.RecordPayload{
{
Content: "192.0.2.1",
},
{
Content: "192.0.2.2",
},
},
}
got := getStackitPartialUpdateRecordSetPayload(change)
if !reflect.DeepEqual(got, expected) {
t.Errorf("getStackitRRSetRecordPatch() = %v, want %v", got, expected)
}
}
func TestFormatTXTContent(t *testing.T) {
t.Parallel()
// Generate strings of exact lengths for testing
string255 := strings.Repeat("a", 255)
string256 := strings.Repeat("a", 256)
string511 := strings.Repeat("a", 511)
tests := []struct {
name string
content string
want string
}{
{
name: "Short string without quotes",
content: "hello world",
want: "hello world",
},
{
name: "Short string with existing quotes",
content: `"hello world"`,
want: `"hello world"`,
},
{
name: "Exactly 255 characters unquoted",
content: string255,
want: string255,
},
{
name: "Exactly 255 characters quoted",
content: `"` + string255 + `"`,
want: `"` + string255 + `"`,
},
{
name: "256 characters (requires 2 chunks)",
content: string256,
want: `"` + string255 + `" "a"`,
},
{
name: "511 characters (requires 3 chunks)",
content: string511,
want: `"` + string255 + `" "` + string255 + `" "a"`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := formatTXTContent(tt.content); got != tt.want {
t.Errorf("formatTXTContent() = %v, want %v", got, tt.want)
}
})
}
}
func TestUnformatTXTContent(t *testing.T) {
t.Parallel()
tests := []struct {
name string
content string
want string
}{
{
name: "Unquoted short string",
content: "hello world",
want: "hello world",
},
{
name: "Single chunk quoted string",
content: `"hello world"`,
want: `"hello world"`,
},
{
name: "Two chunk string",
content: `"hello" "world"`,
want: `"helloworld"`,
},
{
name: "Three chunk string",
content: `"chunk1" "chunk2" "chunk3"`,
want: `"chunk1chunk2chunk3"`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := unformatTXTContent(tt.content); got != tt.want {
t.Errorf("unformatTXTContent() = %v, want %v", got, tt.want)
}
})
}
}