Skip to content

Commit 36ff2e8

Browse files
fix image pull stream error handling
Signed-off-by: puneeth_aditya_5656 <myakampuneeth@gmail.com>
1 parent c01f511 commit 36ff2e8

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

pkg/connectors/container_client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ func (cli *containerClient) CreateContainer(opts ContainerOpts) (string, error)
112112
return "", err
113113
}
114114
defer out.Close()
115-
io.Copy(os.Stdout, out)
115+
if err := copyImagePullOutput(out); err != nil {
116+
return "", err
117+
}
116118

117119
resp, err := cli.cli.ContainerCreate(
118120
ctx,
@@ -148,3 +150,11 @@ func (cli *containerClient) StopContainer(containerId string) error {
148150
func (cli *containerClient) CloseClient() error {
149151
return cli.cli.Close()
150152
}
153+
154+
func copyImagePullOutput(out io.Reader) error {
155+
_, err := io.Copy(os.Stdout, out)
156+
if err != nil {
157+
return fmt.Errorf("failed to read image pull output: %w", err)
158+
}
159+
return nil
160+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package connectors
2+
3+
import (
4+
"errors"
5+
"io"
6+
"strings"
7+
"testing"
8+
)
9+
10+
type failingReader struct{}
11+
12+
func (f failingReader) Read(p []byte) (int, error) {
13+
return 0, errors.New("read failed")
14+
}
15+
16+
func TestCopyImagePullOutputReturnsReadError(t *testing.T) {
17+
err := copyImagePullOutput(failingReader{})
18+
19+
if err == nil {
20+
t.Fatal("expected error")
21+
}
22+
if !strings.Contains(err.Error(), "failed to read image pull output") {
23+
t.Fatalf("unexpected error: %v", err)
24+
}
25+
}
26+
27+
func TestCopyImagePullOutputSucceeds(t *testing.T) {
28+
err := copyImagePullOutput(strings.NewReader(""))
29+
30+
if err != nil {
31+
t.Fatalf("unexpected error: %v", err)
32+
}
33+
}
34+
35+
var _ io.Reader = failingReader{}

0 commit comments

Comments
 (0)