-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Cezar Rata <[email protected]>
Signed-off-by: Cezar Rata <[email protected]> Signed-off-by: Cezar Rata <[email protected]> Signed-off-by: Cezar Rata <[email protected]> Signed-off-by: Cezar Rata <[email protected]> Signed-off-by: Cezar Rata <[email protected]> porting patch #1 Signed-off-by: Cezar Rata <[email protected]>
- Loading branch information
Showing
11 changed files
with
269 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package image | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/containerd/containerd/namespaces" | ||
"github.com/runfinch/finch-daemon/api/response" | ||
) | ||
|
||
func (h *handler) load(w http.ResponseWriter, r *http.Request) { | ||
ctx := namespaces.WithNamespace(r.Context(), h.Config.Namespace) | ||
quiet, err := strconv.ParseBool(r.URL.Query().Get("quiet")) | ||
if err != nil { | ||
quiet = false | ||
} | ||
out := response.NewStreamWriter(w) | ||
err = h.service.Load(ctx, r.Body, out, quiet) | ||
if err != nil { | ||
out.WriteError(http.StatusInternalServerError, err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package image | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/containerd/nerdctl/pkg/config" | ||
"github.com/golang/mock/gomock" | ||
"github.com/gorilla/mux" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/runfinch/finch-daemon/mocks/mocks_image" | ||
"github.com/runfinch/finch-daemon/mocks/mocks_logger" | ||
) | ||
|
||
var _ = Describe("Image Load API", func() { | ||
var ( | ||
mockCtrl *gomock.Controller | ||
logger *mocks_logger.Logger | ||
service *mocks_image.MockService | ||
h *handler | ||
rr *httptest.ResponseRecorder | ||
name string | ||
req *http.Request | ||
) | ||
BeforeEach(func() { | ||
mockCtrl = gomock.NewController(GinkgoT()) | ||
defer mockCtrl.Finish() | ||
logger = mocks_logger.NewLogger(mockCtrl) | ||
service = mocks_image.NewMockService(mockCtrl) | ||
c := config.Config{} | ||
h = newHandler(service, &c, logger) | ||
rr = httptest.NewRecorder() | ||
name = "test-image" | ||
var err error | ||
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("/images/%s/json", name), nil) | ||
Expect(err).Should(BeNil()) | ||
req = mux.SetURLVars(req, map[string]string{"name": name}) | ||
|
||
logger.EXPECT().Debugf(gomock.Any(), gomock.Any()).AnyTimes() | ||
}) | ||
Context("handler", func() { | ||
It("should return 200 status code upon success", func() { | ||
service.EXPECT().Load( | ||
gomock.Any(), | ||
gomock.Any(), | ||
gomock.Any(), | ||
gomock.Any(), | ||
).Return(nil) | ||
|
||
// handler should return 200 status code | ||
h.load(rr, req) | ||
Expect(rr).Should(HaveHTTPStatus(http.StatusOK)) | ||
}) | ||
It("should return 500 status code if service returns an error message", func() { | ||
service.EXPECT().Load(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("error")) | ||
logger.EXPECT().Debugf(gomock.Any(), gomock.Any()) | ||
|
||
// handler should return error message | ||
h.load(rr, req) | ||
Expect(rr.Body).Should(MatchJSON(`{"message": "error"}`)) | ||
Expect(rr).Should(HaveHTTPStatus(http.StatusInternalServerError)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package image | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/containerd/fifo" | ||
) | ||
|
||
func (s *service) Load(ctx context.Context, inStream io.Reader, outStream io.Writer, quiet bool) error { | ||
root, err := s.nctlImageSvc.GetDataStore() | ||
if err != nil { | ||
s.logger.Errorf("failed to get data store dir: %s", err) | ||
return err | ||
} | ||
d := filepath.Join(root, "fifo") | ||
if err = os.Mkdir(d, 0700); err != nil && !os.IsExist(err) { | ||
fmt.Println(err) | ||
s.logger.Errorf("failed to create fifo dir %s: %s", d, err) | ||
return err | ||
} | ||
t := time.Now() | ||
var b [3]byte | ||
rand.Read(b[:]) | ||
img := filepath.Join(d, fmt.Sprintf("%d%s", t.Nanosecond(), base64.URLEncoding.EncodeToString(b[:]))) | ||
rw, err := fifo.OpenFifo(ctx, img, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700) | ||
if err != nil { | ||
s.logger.Errorf("failed to open fifo %s: %s", img, err) | ||
return err | ||
} | ||
defer func() { | ||
rw.Close() | ||
os.Remove(img) | ||
}() | ||
go func() { | ||
io.Copy(rw, inStream) | ||
}() | ||
if err = s.nctlImageSvc.LoadImage(ctx, img, outStream, quiet); err != nil { | ||
s.logger.Errorf("failed to load image %s: %s", img, err) | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package image | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"strings" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/runfinch/finch-daemon/api/handlers/image" | ||
"github.com/runfinch/finch-daemon/mocks/mocks_backend" | ||
"github.com/runfinch/finch-daemon/mocks/mocks_logger" | ||
) | ||
|
||
// Unit tests related to image load API | ||
var _ = Describe("Image Load API", func() { | ||
var ( | ||
ctx context.Context | ||
mockCtrl *gomock.Controller | ||
logger *mocks_logger.Logger | ||
cdClient *mocks_backend.MockContainerdClient | ||
ncClient *mocks_backend.MockNerdctlImageSvc | ||
name string | ||
inStream io.Reader | ||
service image.Service | ||
) | ||
BeforeEach(func() { | ||
ctx = context.Background() | ||
mockCtrl = gomock.NewController(GinkgoT()) | ||
logger = mocks_logger.NewLogger(mockCtrl) | ||
cdClient = mocks_backend.NewMockContainerdClient(mockCtrl) | ||
ncClient = mocks_backend.NewMockNerdctlImageSvc(mockCtrl) | ||
name = "tmp" | ||
inStream = strings.NewReader("") | ||
service = NewService(cdClient, ncClient, logger) | ||
}) | ||
Context("service", func() { | ||
It("should return no errors upon success", func() { | ||
// search image method returns one image | ||
ncClient.EXPECT().GetDataStore().Return(name, nil) | ||
ncClient.EXPECT().LoadImage(gomock.Any(), gomock.Any(), nil, gomock.Any()).Return(nil) | ||
|
||
// service should return no error | ||
err := service.Load(ctx, inStream, nil, false) | ||
Expect(err).Should(BeNil()) | ||
}) | ||
It("should return an error if no image was found", func() { | ||
logger.EXPECT().Errorf(gomock.Any(), gomock.Any()) | ||
ncClient.EXPECT().GetDataStore().Return("", errors.New("error message")) | ||
|
||
// service should return a error | ||
err := service.Load(ctx, inStream, nil, false) | ||
Expect(err).ShouldNot(BeNil()) | ||
}) | ||
}) | ||
}) |
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,3 @@ sudo containerd & | |
sudo buildkitd & | ||
|
||
sleep 2 | ||
|