Skip to content

Commit 937ea4a

Browse files
committed
add unit test and better help messages
1 parent 49a9901 commit 937ea4a

File tree

2 files changed

+209
-2
lines changed

2 files changed

+209
-2
lines changed

internal/services/domain/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func getRecordFromTypeAndData(dnsType domain.RecordType, data string, records []
3838
if dnsType == domain.RecordTypeSRV {
3939
if flattedData == flattenCurrentData {
4040
if currentRecord != nil {
41-
return nil, errors.New("multiple records found with same type and data")
41+
return nil, fmt.Errorf("multiple records found with same type and data: existing record %s (ID: %s) conflicts with new record data %s", currentRecord.Data, currentRecord.ID, data)
4242
}
4343

4444
currentRecord = r
@@ -48,7 +48,7 @@ func getRecordFromTypeAndData(dnsType domain.RecordType, data string, records []
4848
} else {
4949
if strings.HasPrefix(flattedData, flattenCurrentData) && r.Type == dnsType {
5050
if currentRecord != nil {
51-
return nil, errors.New("multiple records found with same type and data")
51+
return nil, fmt.Errorf("multiple records found with same type and data: existing record %s (ID: %s) conflicts with new record data %s", currentRecord.Data, currentRecord.ID, data)
5252
}
5353

5454
currentRecord = r
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package domain_test
2+
3+
import (
4+
"testing"
5+
6+
domain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
7+
)
8+
9+
func TestFlattenDomainData(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
data string
13+
recordType domain.RecordType
14+
expected string
15+
}{
16+
{
17+
name: "SRV record with domain duplication",
18+
data: "0 0 1234 foo.noet-ia.com.noet-ia.com.",
19+
recordType: domain.RecordTypeSRV,
20+
expected: "0 0 1234 foo.noet-ia.",
21+
},
22+
{
23+
name: "SRV record without domain duplication",
24+
data: "0 0 1234 foo.noet-ia.com",
25+
recordType: domain.RecordTypeSRV,
26+
expected: "0 0 1234 foo.noet-ia.com",
27+
},
28+
{
29+
name: "SRV record with complex domain duplication",
30+
data: "10 5 8080 service.example.com.example.com.",
31+
recordType: domain.RecordTypeSRV,
32+
expected: "10 5 8080 service.example.",
33+
},
34+
{
35+
name: "SRV record with no duplication pattern",
36+
data: "0 0 1234 foo.bar.com",
37+
recordType: domain.RecordTypeSRV,
38+
expected: "0 0 1234 foo.bar.com",
39+
},
40+
{
41+
name: "SRV record with trailing dot only",
42+
data: "0 0 1234 foo.example.com.",
43+
recordType: domain.RecordTypeSRV,
44+
expected: "0 0 1234 foo.example.com.",
45+
},
46+
{
47+
name: "SRV record with real test case",
48+
data: "0 0 1234 foo.example.com.test-srv-duplication.scaleway-terraform.com.",
49+
recordType: domain.RecordTypeSRV,
50+
expected: "0 0 1234 foo.example.com.",
51+
},
52+
{
53+
name: "SRV record with Terraform data (3 parts)",
54+
data: "0 1234 foo.example.com",
55+
recordType: domain.RecordTypeSRV,
56+
expected: "0 0 1234 foo.example.com",
57+
},
58+
{
59+
name: "MX record",
60+
data: "10 mail.example.com",
61+
recordType: domain.RecordTypeMX,
62+
expected: "mail.example.com",
63+
},
64+
{
65+
name: "TXT record",
66+
data: "\"v=spf1 include:_spf.example.com ~all\"",
67+
recordType: domain.RecordTypeTXT,
68+
expected: "v=spf1 include:_spf.example.com ~all",
69+
},
70+
{
71+
name: "A record",
72+
data: "192.168.1.1",
73+
recordType: domain.RecordTypeA,
74+
expected: "192.168.1.1",
75+
},
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
result := flattenDomainData(tt.data, tt.recordType)
81+
if result != tt.expected {
82+
t.Errorf("flattenDomainData(%q, %v) = %q, want %q", tt.data, tt.recordType, result, tt.expected)
83+
}
84+
})
85+
}
86+
}
87+
88+
func TestNormalizeSRVData(t *testing.T) {
89+
tests := []struct {
90+
name string
91+
input string
92+
expected string
93+
}{
94+
{
95+
name: "SRV with weight 0",
96+
input: "0 0 8080 server.example.com",
97+
expected: "0 0 8080 server.example.com",
98+
},
99+
{
100+
name: "SRV with weight 1",
101+
input: "0 1 8080 server.example.com",
102+
expected: "0 1 8080 server.example.com",
103+
},
104+
{
105+
name: "SRV with high weight",
106+
input: "0 100 8080 server.example.com",
107+
expected: "0 100 8080 server.example.com",
108+
},
109+
{
110+
name: "SRV with priority 10",
111+
input: "10 0 8080 server.example.com",
112+
expected: "10 0 8080 server.example.com",
113+
},
114+
{
115+
name: "SRV with port 443",
116+
input: "0 0 443 server.example.com",
117+
expected: "0 0 443 server.example.com",
118+
},
119+
{
120+
name: "SRV with complex target",
121+
input: "0 0 8080 server.subdomain.example.com",
122+
expected: "0 0 8080 server.subdomain",
123+
},
124+
{
125+
name: "SRV with trailing dot",
126+
input: "0 0 8080 server.example.com.",
127+
expected: "0 0 8080 server.example.com.",
128+
},
129+
}
130+
131+
for _, tt := range tests {
132+
t.Run(tt.name, func(t *testing.T) {
133+
result := normalizeSRVData(tt.input)
134+
if result != tt.expected {
135+
t.Errorf("normalizeSRVData(%q) = %q, want %q", tt.input, result, tt.expected)
136+
}
137+
})
138+
}
139+
}
140+
141+
func TestRemoveZoneDomainSuffix(t *testing.T) {
142+
tests := []struct {
143+
name string
144+
input string
145+
expected string
146+
}{
147+
{
148+
name: "simple domain",
149+
input: "server.example.com",
150+
expected: "server.example.com",
151+
},
152+
{
153+
name: "with trailing dot",
154+
input: "server.example.com.",
155+
expected: "server.example.com.",
156+
},
157+
{
158+
name: "with zone domain duplication",
159+
input: "server.example.com.zone.tld",
160+
expected: "server.example",
161+
},
162+
{
163+
name: "with zone domain duplication and trailing dot",
164+
input: "server.example.com.zone.tld.",
165+
expected: "server.example.",
166+
},
167+
{
168+
name: "with complex zone domain duplication",
169+
input: "server.example.com.subdomain.zone.tld",
170+
expected: "server.example.com",
171+
},
172+
{
173+
name: "with complex zone domain duplication and trailing dot",
174+
input: "server.example.com.subdomain.zone.tld.",
175+
expected: "server.example.com.",
176+
},
177+
{
178+
name: "no domain duplication pattern",
179+
input: "server.example.com.other.tld",
180+
expected: "server.example",
181+
},
182+
{
183+
name: "no domain duplication pattern with trailing dot",
184+
input: "server.example.com.other.tld.",
185+
expected: "server.example.",
186+
},
187+
{
188+
name: "single word",
189+
input: "server",
190+
expected: "server",
191+
},
192+
{
193+
name: "single word with trailing dot",
194+
input: "server.",
195+
expected: "server.",
196+
},
197+
}
198+
199+
for _, tt := range tests {
200+
t.Run(tt.name, func(t *testing.T) {
201+
result := removeZoneDomainSuffix(tt.input)
202+
if result != tt.expected {
203+
t.Errorf("removeZoneDomainSuffix(%q) = %q, want %q", tt.input, result, tt.expected)
204+
}
205+
})
206+
}
207+
}

0 commit comments

Comments
 (0)