Skip to content

Commit

Permalink
fix: image load should close stream after copy
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Rata <[email protected]>
  • Loading branch information
cezar-r committed Sep 11, 2024
1 parent 5d9b1e0 commit 25f6e8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/service/image/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ func (s *service) Load(ctx context.Context, inStream io.Reader, outStream io.Wri
return err
}
defer func() {
rw.Close()
os.Remove(img)
}()
go func() {
io.Copy(rw, inStream)
written, err := io.Copy(rw, inStream)
if err != nil {
s.logger.Errorf("failed to copy: %s", err)
} else {
s.logger.Debugf("copied %d bytes", written)
}
rw.Close()
}()
if err = s.nctlImageSvc.LoadImage(ctx, img, outStream, quiet); err != nil {
s.logger.Errorf("failed to load image %s: %s", img, err)
Expand Down
2 changes: 2 additions & 0 deletions internal/service/image/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var _ = Describe("Image Load API", func() {
})
Context("service", func() {
It("should return no errors upon success", func() {
logger.EXPECT().Debugf(gomock.Any(), gomock.Any())
ncClient.EXPECT().GetDataStore().
Return(name, nil)
ncClient.EXPECT().LoadImage(gomock.Any(), gomock.Any(), nil, gomock.Any()).
Expand All @@ -53,6 +54,7 @@ var _ = Describe("Image Load API", func() {
})
It("should return an error if load image method returns an error", func() {
logger.EXPECT().Errorf(gomock.Any(), gomock.Any())
logger.EXPECT().Debugf(gomock.Any(), gomock.Any())
ncClient.EXPECT().GetDataStore().
Return(name, nil)
ncClient.EXPECT().LoadImage(gomock.Any(), gomock.Any(), nil, gomock.Any()).
Expand Down

0 comments on commit 25f6e8a

Please sign in to comment.