-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
126 lines (118 loc) · 3.78 KB
/
Copy patherrors_test.go
File metadata and controls
126 lines (118 loc) · 3.78 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
// SPDX-License-Identifier: AGPL-3.0-only
package pdns
import (
"errors"
"testing"
)
func TestFriendlyMessage(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
want string
}{
{
name: "nil error returns empty string",
err: nil,
want: "",
},
{
name: "non-pdns error returns generic message",
err: errors.New("some internal error"),
want: "Failed to apply DNS record. It will be retried automatically.",
},
{
name: "422 conflict with pre-existing RRset",
err: &pdnsAPIError{Status: 422, Body: `{"error": "RRset www.example.com. IN ALIAS: Conflicts with pre-existing RRset"}`},
want: "A conflicting record already exists for this name. Remove the existing record and try again.",
},
{
name: "422 CNAME conflict",
err: &pdnsAPIError{Status: 422, Body: `{"error": "RRset www.example.com. IN CNAME: Conflicts with pre-existing RRset"}`},
want: "A conflicting record already exists for this name. Remove the existing record and try again.",
},
{
name: "422 invalid character in TXT record content",
err: &pdnsAPIError{Status: 422, Body: `{"error": "Invalid character ';' in record content '\"=DMARC1; p=none; rua=mailto:admin@example.com\"'"}`},
want: "The record content contains an invalid character. TXT records containing semicolons or special characters must be properly quoted.",
},
{
name: "422 not in zone",
err: &pdnsAPIError{Status: 422, Body: `{"error": "Not in zone"}`},
want: "The record name is outside the zone. Check that the name belongs to this DNS zone.",
},
{
name: "422 empty body falls back to generic 422 message",
err: &pdnsAPIError{Status: 422, Body: ""},
want: "The DNS record was rejected as invalid. Check the record type and value.",
},
{
name: "422 unknown body falls back to generic 422 message",
err: &pdnsAPIError{Status: 422, Body: `{"error": "Some other validation error"}`},
want: "The DNS record was rejected as invalid. Check the record type and value.",
},
{
name: "404",
err: &pdnsAPIError{Status: 404, Body: `{"error": "Not found"}`},
want: "The DNS zone could not be found. It may still be provisioning.",
},
{
name: "500",
err: &pdnsAPIError{Status: 500, Body: "internal server error"},
want: "An internal error occurred while applying the record. It will be retried automatically.",
},
{
name: "503 is treated as a server error",
err: &pdnsAPIError{Status: 503, Body: ""},
want: "An internal error occurred while applying the record. It will be retried automatically.",
},
{
name: "unexpected 4xx status code with no matching body",
err: &pdnsAPIError{Status: 409, Body: ""},
want: "Failed to apply DNS record. It will be retried automatically.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := FriendlyMessage(tt.err)
if got != tt.want {
t.Errorf("FriendlyMessage() = %q, want %q", got, tt.want)
}
})
}
}
func TestIsConflict(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
want bool
}{
{"nil", nil, false},
{"non-pdns error", errors.New("boom"), false},
{
"ALIAS coexistence conflict",
&pdnsAPIError{Status: 422, Body: `{"error": "RRset www.ab.dk. IN ALIAS: Conflicts with pre-existing RRset"}`},
true,
},
{
"non-conflict 422 (duplicate record)",
&pdnsAPIError{Status: 422, Body: `{"error": "RRset x. IN NS: duplicate record with content \"ns1.\""}`},
false,
},
{
"non-422 status",
&pdnsAPIError{Status: 500, Body: `{"error": "Conflicts with pre-existing RRset"}`},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := IsConflict(tt.err); got != tt.want {
t.Errorf("IsConflict() = %v, want %v", got, tt.want)
}
})
}
}