Skip to content

Commit dce0cf5

Browse files
authored
Improve connect test coverage (#667)
Progress towards #472
1 parent c3a6fb0 commit dce0cf5

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

components/containerd/pod/cri_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package pod
33
import (
44
"context"
55
"encoding/json"
6+
"os"
7+
"path/filepath"
68
"testing"
79
"time"
810

@@ -90,8 +92,15 @@ func TestPodSandboxTypes(t *testing.T) {
9092
})
9193
}
9294

93-
// Test the connect function with mock server
95+
// Test the connect function with a mock server
9496
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+
95104
t.Run("invalid endpoint", func(t *testing.T) {
96105
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
97106
defer cancel()
@@ -105,6 +114,48 @@ func Test_connect(t *testing.T) {
105114
_, err := connect(ctx, "unix:///nonexistent/socket/path")
106115
assert.Error(t, err)
107116
})
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+
})
108159
}
109160

110161
// Test listSandboxStatus function with mock connectors

0 commit comments

Comments
 (0)