-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
160 lines (149 loc) · 4.04 KB
/
errors_test.go
File metadata and controls
160 lines (149 loc) · 4.04 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
package main
import (
"errors"
"strings"
"testing"
)
func TestUserError(t *testing.T) {
t.Run("error without cause", func(t *testing.T) {
err := &UserError{Message: "test error"}
if err.Error() != "test error" {
t.Errorf("Error() = %q, want %q", err.Error(), "test error")
}
})
t.Run("error with cause", func(t *testing.T) {
cause := errors.New("underlying error")
err := &UserError{Message: "test error", Cause: cause}
expected := "test error: underlying error"
if err.Error() != expected {
t.Errorf("Error() = %q, want %q", err.Error(), expected)
}
})
t.Run("unwrap returns cause", func(t *testing.T) {
cause := errors.New("underlying error")
err := &UserError{Message: "test error", Cause: cause}
if err.Unwrap() != cause {
t.Error("Unwrap() did not return the cause")
}
})
}
func TestFormatUserError(t *testing.T) {
t.Run("formats UserError with suggestion", func(t *testing.T) {
err := &UserError{
Message: "test error",
Suggestion: "try this fix",
}
output := FormatUserError(err)
if !strings.Contains(output, "test error") {
t.Error("output should contain error message")
}
if !strings.Contains(output, "try this fix") {
t.Error("output should contain suggestion")
}
})
t.Run("formats generic error with auto-suggestion", func(t *testing.T) {
err := errors.New("no valid credential sources")
output := FormatUserError(err)
if !strings.Contains(output, "no valid credential") {
t.Error("output should contain error message")
}
if !strings.Contains(output, "aws configure") {
t.Error("output should contain AWS credential suggestion")
}
})
}
func TestGetSuggestionForError(t *testing.T) {
tests := []struct {
name string
errStr string
shouldMatch string
}{
{
name: "AWS credentials error",
errStr: "no valid credential sources",
shouldMatch: "aws configure",
},
{
name: "AWS region error",
errStr: "region not specified",
shouldMatch: "AWS_REGION",
},
{
name: "access denied",
errStr: "Access Denied",
shouldMatch: "IAM",
},
{
name: "throttling",
errStr: "throttled by service",
shouldMatch: "rate-limited",
},
{
name: "podman not found",
errStr: "podman: command not found",
shouldMatch: "podman.io",
},
{
name: "docker permission",
errStr: "docker: permission denied",
shouldMatch: "docker group",
},
{
name: "timeout",
errStr: "context deadline exceeded (timeout)",
shouldMatch: "timed out",
},
{
name: "network error",
errStr: "connection refused",
shouldMatch: "network",
},
{
name: "unknown error",
errStr: "some random error",
shouldMatch: "", // no suggestion
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
suggestion := getSuggestionForError(tt.errStr)
if tt.shouldMatch == "" {
if suggestion != "" {
t.Errorf("expected no suggestion, got %q", suggestion)
}
} else {
if !strings.Contains(strings.ToLower(suggestion), strings.ToLower(tt.shouldMatch)) {
t.Errorf("suggestion %q should contain %q", suggestion, tt.shouldMatch)
}
}
})
}
}
func TestErrorConstructors(t *testing.T) {
t.Run("ErrNoContainerRuntime", func(t *testing.T) {
err := ErrNoContainerRuntime()
if !strings.Contains(err.Message, "container runtime") {
t.Error("should mention container runtime")
}
if !strings.Contains(err.Suggestion, "podman") {
t.Error("should suggest podman")
}
})
t.Run("ErrAWSConfig", func(t *testing.T) {
cause := errors.New("config error")
err := ErrAWSConfig(cause)
if err.Cause != cause {
t.Error("should preserve cause")
}
if !strings.Contains(err.Suggestion, "aws configure") {
t.Error("should suggest aws configure")
}
})
t.Run("ErrContainerPull", func(t *testing.T) {
cause := errors.New("network error")
err := ErrContainerPull("ghcr.io/test:latest", cause)
if !strings.Contains(err.Message, "ghcr.io/test:latest") {
t.Error("should include image name")
}
})
}