Skip to content

Commit 9cd19bc

Browse files
authored
adds tests for packages missing them (core/env/errors/transport/errors/versions) (#1729)
* adds missing tests Signed-off-by: ChrisJBurns <[email protected]> * lint Signed-off-by: ChrisJBurns <[email protected]> --------- Signed-off-by: ChrisJBurns <[email protected]>
1 parent be58632 commit 9cd19bc

File tree

3 files changed

+399
-0
lines changed

3 files changed

+399
-0
lines changed

pkg/env/env_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package env
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestOSReader_Getenv(t *testing.T) { //nolint:paralleltest // Modifies environment variables
9+
// Cannot run in parallel because it modifies environment variables
10+
testKey := "TEST_ENV_VARIABLE_FOR_TESTING"
11+
testValue := "test_value_123"
12+
13+
// Set an environment variable for testing
14+
originalValue, wasSet := os.LookupEnv(testKey)
15+
os.Setenv(testKey, testValue)
16+
t.Cleanup(func() {
17+
if wasSet {
18+
os.Setenv(testKey, originalValue)
19+
} else {
20+
os.Unsetenv(testKey)
21+
}
22+
})
23+
24+
reader := &OSReader{}
25+
26+
tests := []struct {
27+
name string
28+
key string
29+
want string
30+
}{
31+
{
32+
name: "existing environment variable",
33+
key: testKey,
34+
want: testValue,
35+
},
36+
{
37+
name: "non-existing environment variable",
38+
key: "NONEXISTENT_ENV_VAR_TESTING_12345",
39+
want: "",
40+
},
41+
{
42+
name: "empty key",
43+
key: "",
44+
want: "",
45+
},
46+
}
47+
48+
for _, tt := range tests { //nolint:paralleltest // Test modifies environment variables
49+
t.Run(tt.name, func(t *testing.T) {
50+
// Cannot run in parallel because parent test modifies environment variables
51+
got := reader.Getenv(tt.key)
52+
if got != tt.want {
53+
t.Errorf("OSReader.Getenv() = %v, want %v", got, tt.want)
54+
}
55+
})
56+
}
57+
}
58+
59+
// TestReader_InterfaceCompliance ensures OSReader implements the Reader interface
60+
func TestReader_InterfaceCompliance(t *testing.T) {
61+
t.Parallel()
62+
var _ Reader = &OSReader{}
63+
// If this compiles, the test passes
64+
}

pkg/errors/errors_test.go

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
package errors
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func TestError_Error(t *testing.T) {
9+
t.Parallel()
10+
tests := []struct {
11+
name string
12+
err *Error
13+
want string
14+
}{
15+
{
16+
name: "error with cause",
17+
err: &Error{
18+
Type: ErrInvalidArgument,
19+
Message: "test message",
20+
Cause: errors.New("underlying error"),
21+
},
22+
want: "invalid_argument: test message: underlying error",
23+
},
24+
{
25+
name: "error without cause",
26+
err: &Error{
27+
Type: ErrContainerRuntime,
28+
Message: "test message",
29+
Cause: nil,
30+
},
31+
want: "container_runtime: test message",
32+
},
33+
}
34+
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
t.Parallel()
38+
got := tt.err.Error()
39+
if got != tt.want {
40+
t.Errorf("Error.Error() = %v, want %v", got, tt.want)
41+
}
42+
})
43+
}
44+
}
45+
46+
func TestError_Unwrap(t *testing.T) {
47+
t.Parallel()
48+
cause := errors.New("underlying error")
49+
err := &Error{
50+
Type: ErrInternal,
51+
Message: "test message",
52+
Cause: cause,
53+
}
54+
55+
if got := err.Unwrap(); got != cause {
56+
t.Errorf("Error.Unwrap() = %v, want %v", got, cause)
57+
}
58+
59+
errNoCause := &Error{
60+
Type: ErrInternal,
61+
Message: "test message",
62+
Cause: nil,
63+
}
64+
65+
if got := errNoCause.Unwrap(); got != nil {
66+
t.Errorf("Error.Unwrap() = %v, want nil", got)
67+
}
68+
}
69+
70+
func TestNewError(t *testing.T) {
71+
t.Parallel()
72+
cause := errors.New("underlying error")
73+
err := NewError(ErrInvalidArgument, "test message", cause)
74+
75+
if err.Type != ErrInvalidArgument {
76+
t.Errorf("NewError().Type = %v, want %v", err.Type, ErrInvalidArgument)
77+
}
78+
if err.Message != "test message" {
79+
t.Errorf("NewError().Message = %v, want %v", err.Message, "test message")
80+
}
81+
if err.Cause != cause {
82+
t.Errorf("NewError().Cause = %v, want %v", err.Cause, cause)
83+
}
84+
}
85+
86+
func TestNewErrorConstructors(t *testing.T) {
87+
t.Parallel()
88+
cause := errors.New("cause")
89+
90+
tests := []struct {
91+
name string
92+
constructor func(string, error) *Error
93+
wantType string
94+
}{
95+
{
96+
name: "NewInvalidArgumentError",
97+
constructor: NewInvalidArgumentError,
98+
wantType: ErrInvalidArgument,
99+
},
100+
{
101+
name: "NewContainerRuntimeError",
102+
constructor: NewContainerRuntimeError,
103+
wantType: ErrContainerRuntime,
104+
},
105+
{
106+
name: "NewContainerNotFoundError",
107+
constructor: NewContainerNotFoundError,
108+
wantType: ErrContainerNotFound,
109+
},
110+
{
111+
name: "NewContainerAlreadyExistsError",
112+
constructor: NewContainerAlreadyExistsError,
113+
wantType: ErrContainerAlreadyExists,
114+
},
115+
{
116+
name: "NewContainerNotRunningError",
117+
constructor: NewContainerNotRunningError,
118+
wantType: ErrContainerNotRunning,
119+
},
120+
{
121+
name: "NewContainerAlreadyRunningError",
122+
constructor: NewContainerAlreadyRunningError,
123+
wantType: ErrContainerAlreadyRunning,
124+
},
125+
{
126+
name: "NewRunConfigNotFoundError",
127+
constructor: NewRunConfigNotFoundError,
128+
wantType: ErrRunConfigNotFound,
129+
},
130+
{
131+
name: "NewGroupAlreadyExistsError",
132+
constructor: NewGroupAlreadyExistsError,
133+
wantType: ErrGroupAlreadyExists,
134+
},
135+
{
136+
name: "NewGroupNotFoundError",
137+
constructor: NewGroupNotFoundError,
138+
wantType: ErrGroupNotFound,
139+
},
140+
{
141+
name: "NewTransportError",
142+
constructor: NewTransportError,
143+
wantType: ErrTransport,
144+
},
145+
{
146+
name: "NewPermissionsError",
147+
constructor: NewPermissionsError,
148+
wantType: ErrPermissions,
149+
},
150+
{
151+
name: "NewInternalError",
152+
constructor: NewInternalError,
153+
wantType: ErrInternal,
154+
},
155+
}
156+
157+
for _, tt := range tests {
158+
t.Run(tt.name, func(t *testing.T) {
159+
t.Parallel()
160+
err := tt.constructor("test message", cause)
161+
if err.Type != tt.wantType {
162+
t.Errorf("%s().Type = %v, want %v", tt.name, err.Type, tt.wantType)
163+
}
164+
if err.Message != "test message" {
165+
t.Errorf("%s().Message = %v, want %v", tt.name, err.Message, "test message")
166+
}
167+
if err.Cause != cause {
168+
t.Errorf("%s().Cause = %v, want %v", tt.name, err.Cause, cause)
169+
}
170+
})
171+
}
172+
}
173+
174+
func TestErrorTypeCheckers(t *testing.T) {
175+
t.Parallel()
176+
tests := []struct {
177+
name string
178+
err error
179+
checker func(error) bool
180+
want bool
181+
}{
182+
{
183+
name: "IsInvalidArgument with matching error",
184+
err: NewInvalidArgumentError("test", nil),
185+
checker: IsInvalidArgument,
186+
want: true,
187+
},
188+
{
189+
name: "IsInvalidArgument with non-matching error",
190+
err: NewContainerRuntimeError("test", nil),
191+
checker: IsInvalidArgument,
192+
want: false,
193+
},
194+
{
195+
name: "IsInvalidArgument with non-Error type",
196+
err: errors.New("regular error"),
197+
checker: IsInvalidArgument,
198+
want: false,
199+
},
200+
{
201+
name: "IsContainerRuntime with matching error",
202+
err: NewContainerRuntimeError("test", nil),
203+
checker: IsContainerRuntime,
204+
want: true,
205+
},
206+
{
207+
name: "IsContainerNotFound with matching error",
208+
err: NewContainerNotFoundError("test", nil),
209+
checker: IsContainerNotFound,
210+
want: true,
211+
},
212+
{
213+
name: "IsContainerAlreadyExists with matching error",
214+
err: NewContainerAlreadyExistsError("test", nil),
215+
checker: IsContainerAlreadyExists,
216+
want: true,
217+
},
218+
{
219+
name: "IsContainerNotRunning with matching error",
220+
err: NewContainerNotRunningError("test", nil),
221+
checker: IsContainerNotRunning,
222+
want: true,
223+
},
224+
{
225+
name: "IsContainerAlreadyRunning with matching error",
226+
err: NewContainerAlreadyRunningError("test", nil),
227+
checker: IsContainerAlreadyRunning,
228+
want: true,
229+
},
230+
{
231+
name: "IsRunConfigNotFound with matching error",
232+
err: NewRunConfigNotFoundError("test", nil),
233+
checker: IsRunConfigNotFound,
234+
want: true,
235+
},
236+
{
237+
name: "IsGroupAlreadyExists with matching error",
238+
err: NewGroupAlreadyExistsError("test", nil),
239+
checker: IsGroupAlreadyExists,
240+
want: true,
241+
},
242+
{
243+
name: "IsGroupNotFound with matching error",
244+
err: NewGroupNotFoundError("test", nil),
245+
checker: IsGroupNotFound,
246+
want: true,
247+
},
248+
{
249+
name: "IsTransport with matching error",
250+
err: NewTransportError("test", nil),
251+
checker: IsTransport,
252+
want: true,
253+
},
254+
{
255+
name: "IsPermissions with matching error",
256+
err: NewPermissionsError("test", nil),
257+
checker: IsPermissions,
258+
want: true,
259+
},
260+
{
261+
name: "IsInternal with matching error",
262+
err: NewInternalError("test", nil),
263+
checker: IsInternal,
264+
want: true,
265+
},
266+
{
267+
name: "IsInternal with nil error",
268+
err: nil,
269+
checker: IsInternal,
270+
want: false,
271+
},
272+
}
273+
274+
for _, tt := range tests {
275+
t.Run(tt.name, func(t *testing.T) {
276+
t.Parallel()
277+
got := tt.checker(tt.err)
278+
if got != tt.want {
279+
t.Errorf("%s() = %v, want %v", tt.name, got, tt.want)
280+
}
281+
})
282+
}
283+
}

0 commit comments

Comments
 (0)