Skip to content

Commit 84f387e

Browse files
authored
test(epp): add unit tests for error package (#1052)
* test(epp): add unit tests for error package - Add comprehensive tests for Error.Error() method - Add tests for CanonicalCode() function with edge cases - Test all error code constants - Achieve 100% test coverage for pkg/epp/util/error Signed-off-by: Naoki Sega <nsegaster@gmail.com> * test(epp): refactor TestErrorConstants to use single map --------- Signed-off-by: Naoki Sega <nsegaster@gmail.com>
1 parent 9c9abd5 commit 84f387e

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed

pkg/epp/util/error/error_test.go

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package error
18+
19+
import (
20+
"errors"
21+
"testing"
22+
)
23+
24+
func TestError_Error(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
err Error
28+
want string
29+
}{
30+
{
31+
name: "BadRequest error",
32+
err: Error{
33+
Code: BadRequest,
34+
Msg: "invalid model name",
35+
},
36+
want: "inference gateway: BadRequest - invalid model name",
37+
},
38+
{
39+
name: "Internal error",
40+
err: Error{
41+
Code: Internal,
42+
Msg: "unexpected condition",
43+
},
44+
want: "inference gateway: Internal - unexpected condition",
45+
},
46+
{
47+
name: "ModelServerError",
48+
err: Error{
49+
Code: ModelServerError,
50+
Msg: "connection timeout",
51+
},
52+
want: "inference gateway: ModelServerError - connection timeout",
53+
},
54+
{
55+
name: "BadConfiguration error",
56+
err: Error{
57+
Code: BadConfiguration,
58+
Msg: "missing required field",
59+
},
60+
want: "inference gateway: BadConfiguration - missing required field",
61+
},
62+
{
63+
name: "InferencePoolResourceExhausted error",
64+
err: Error{
65+
Code: InferencePoolResourceExhausted,
66+
Msg: "no available pods",
67+
},
68+
want: "inference gateway: InferencePoolResourceExhausted - no available pods",
69+
},
70+
{
71+
name: "Unknown error",
72+
err: Error{
73+
Code: Unknown,
74+
Msg: "something went wrong",
75+
},
76+
want: "inference gateway: Unknown - something went wrong",
77+
},
78+
{
79+
name: "Empty message",
80+
err: Error{
81+
Code: BadRequest,
82+
Msg: "",
83+
},
84+
want: "inference gateway: BadRequest - ",
85+
},
86+
{
87+
name: "Empty code",
88+
err: Error{
89+
Code: "",
90+
Msg: "error occurred",
91+
},
92+
want: "inference gateway: - error occurred",
93+
},
94+
}
95+
96+
for _, tt := range tests {
97+
t.Run(tt.name, func(t *testing.T) {
98+
if got := tt.err.Error(); got != tt.want {
99+
t.Errorf("Error.Error() = %v, want %v", got, tt.want)
100+
}
101+
})
102+
}
103+
}
104+
105+
func TestCanonicalCode(t *testing.T) {
106+
tests := []struct {
107+
name string
108+
err error
109+
want string
110+
}{
111+
{
112+
name: "Error type with BadRequest code",
113+
err: Error{
114+
Code: BadRequest,
115+
Msg: "invalid input",
116+
},
117+
want: BadRequest,
118+
},
119+
{
120+
name: "Error type with Internal code",
121+
err: Error{
122+
Code: Internal,
123+
Msg: "server error",
124+
},
125+
want: Internal,
126+
},
127+
{
128+
name: "Error type with ModelServerError code",
129+
err: Error{
130+
Code: ModelServerError,
131+
Msg: "model unavailable",
132+
},
133+
want: ModelServerError,
134+
},
135+
{
136+
name: "Error type with BadConfiguration code",
137+
err: Error{
138+
Code: BadConfiguration,
139+
Msg: "invalid config",
140+
},
141+
want: BadConfiguration,
142+
},
143+
{
144+
name: "Error type with InferencePoolResourceExhausted code",
145+
err: Error{
146+
Code: InferencePoolResourceExhausted,
147+
Msg: "no resources",
148+
},
149+
want: InferencePoolResourceExhausted,
150+
},
151+
{
152+
name: "Error type with Unknown code",
153+
err: Error{
154+
Code: Unknown,
155+
Msg: "unknown error",
156+
},
157+
want: Unknown,
158+
},
159+
{
160+
name: "Error type with empty code",
161+
err: Error{
162+
Code: "",
163+
Msg: "no code provided",
164+
},
165+
want: "",
166+
},
167+
{
168+
name: "Non-Error type",
169+
err: errors.New("standard go error"),
170+
want: Unknown,
171+
},
172+
{
173+
name: "Nil error",
174+
err: nil,
175+
want: Unknown,
176+
},
177+
{
178+
name: "Custom error type that is not Error",
179+
err: customError{msg: "custom error"},
180+
want: Unknown,
181+
},
182+
}
183+
184+
for _, tt := range tests {
185+
t.Run(tt.name, func(t *testing.T) {
186+
if got := CanonicalCode(tt.err); got != tt.want {
187+
t.Errorf("CanonicalCode() = %v, want %v", got, tt.want)
188+
}
189+
})
190+
}
191+
}
192+
193+
// customError is a helper type for testing non-Error error types
194+
type customError struct {
195+
msg string
196+
}
197+
198+
func (e customError) Error() string {
199+
return e.msg
200+
}
201+
202+
func TestErrorConstants(t *testing.T) {
203+
// Verify that error constants match their expected string values
204+
tests := map[string]string{
205+
Unknown: "Unknown",
206+
BadRequest: "BadRequest",
207+
Internal: "Internal",
208+
ModelServerError: "ModelServerError",
209+
BadConfiguration: "BadConfiguration",
210+
InferencePoolResourceExhausted: "InferencePoolResourceExhausted",
211+
}
212+
213+
for constant, expected := range tests {
214+
if constant != expected {
215+
t.Errorf("Constant value %q != expected %q", constant, expected)
216+
}
217+
}
218+
}

0 commit comments

Comments
 (0)