@@ -3,6 +3,8 @@ package pod
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "os"
7
+ "path/filepath"
6
8
"testing"
7
9
"time"
8
10
@@ -90,8 +92,15 @@ func TestPodSandboxTypes(t *testing.T) {
90
92
})
91
93
}
92
94
93
- // Test the connect function with mock server
95
+ // Test the connect function with a mock server
94
96
func Test_connect (t * testing.T ) {
97
+ t .Run ("empty endpoint" , func (t * testing.T ) {
98
+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
99
+ defer cancel ()
100
+ _ , err := connect (ctx , "" )
101
+ assert .Error (t , err )
102
+ })
103
+
95
104
t .Run ("invalid endpoint" , func (t * testing.T ) {
96
105
ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
97
106
defer cancel ()
@@ -105,6 +114,48 @@ func Test_connect(t *testing.T) {
105
114
_ , err := connect (ctx , "unix:///nonexistent/socket/path" )
106
115
assert .Error (t , err )
107
116
})
117
+
118
+ t .Run ("stat fails with non-IsNotExist error" , func (t * testing.T ) {
119
+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
120
+ defer cancel ()
121
+
122
+ // Create a temporary file
123
+ tmpDir := t .TempDir ()
124
+ filePath := filepath .Join (tmpDir , "a_file" )
125
+ f , err := os .Create (filePath )
126
+ require .NoError (t , err )
127
+ require .NoError (t , f .Close ())
128
+
129
+ // Construct an endpoint where a file is used as a directory path component
130
+ invalidSocketPath := filepath .Join (filePath , "socket.sock" )
131
+ endpoint := "unix://" + invalidSocketPath
132
+
133
+ _ , err = connect (ctx , endpoint )
134
+
135
+ assert .Error (t , err )
136
+ assert .ErrorContains (t , err , "failed to stat socket file:" )
137
+ })
138
+
139
+ t .Run ("context cancellation during dial" , func (t * testing.T ) {
140
+ // Create a temporary directory and a file to act as the socket path
141
+ tempDir := t .TempDir ()
142
+ socketPath := filepath .Join (tempDir , "test_cancel.sock" )
143
+ f , err := os .Create (socketPath ) // Create the file so os.Stat passes
144
+ require .NoError (t , err )
145
+ require .NoError (t , f .Close ())
146
+ // We don't listen on this socket, so Dial should block/retry
147
+
148
+ endpoint := "unix://" + socketPath
149
+
150
+ // Create a context and cancel it calling connect
151
+ ctx , cancel := context .WithCancel (context .Background ())
152
+ cancel () // Immediately cancel
153
+
154
+ _ , err = connect (ctx , endpoint )
155
+
156
+ assert .Error (t , err )
157
+ assert .ErrorIs (t , err , context .Canceled , "Expected context.Canceled error" )
158
+ })
108
159
}
109
160
110
161
// Test listSandboxStatus function with mock connectors
0 commit comments