Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: image load should close stream after copy #34

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Shubhranshu153 marked this conversation as resolved.
Show resolved Hide resolved
}()
if err = s.nctlImageSvc.LoadImage(ctx, img, outStream, quiet); err != nil {
s.logger.Errorf("failed to load image %s: %s", img, err)
Expand Down
6 changes: 4 additions & 2 deletions internal/service/image/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,28 @@ var _ = Describe("Image Load API", func() {
Return(name, nil)
ncClient.EXPECT().LoadImage(gomock.Any(), gomock.Any(), nil, gomock.Any()).
Return(nil)
logger.EXPECT().Debugf(gomock.Any(), gomock.Any())

// service should return no error
err := service.Load(ctx, inStream, nil, false)
Expect(err).Should(BeNil())
})
It("should return an error if load image method returns an error", func() {
logger.EXPECT().Errorf(gomock.Any(), gomock.Any())
ncClient.EXPECT().GetDataStore().
Return(name, nil)
ncClient.EXPECT().LoadImage(gomock.Any(), gomock.Any(), nil, gomock.Any()).
Return(errors.New("error message"))
logger.EXPECT().Errorf(gomock.Any(), gomock.Any())
logger.EXPECT().Debugf(gomock.Any(), gomock.Any())

// service should return an error
err := service.Load(ctx, inStream, nil, false)
Expect(err).ShouldNot(BeNil())
})
It("should return an error if get datastore method returns an error", func() {
logger.EXPECT().Errorf(gomock.Any(), gomock.Any())
ncClient.EXPECT().GetDataStore().
Return(name, errors.New("error message"))
logger.EXPECT().Errorf(gomock.Any(), gomock.Any())

// service should return an error
err := service.Load(ctx, inStream, nil, false)
Expand Down
Loading