|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package container |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + |
| 12 | + ncTypes "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 13 | + "github.com/containerd/nerdctl/v2/pkg/config" |
| 14 | + "github.com/golang/mock/gomock" |
| 15 | + "github.com/gorilla/mux" |
| 16 | + . "github.com/onsi/ginkgo/v2" |
| 17 | + . "github.com/onsi/gomega" |
| 18 | + "github.com/runfinch/finch-daemon/pkg/errdefs" |
| 19 | + |
| 20 | + "github.com/runfinch/finch-daemon/mocks/mocks_container" |
| 21 | + "github.com/runfinch/finch-daemon/mocks/mocks_logger" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("Container Pause API", func() { |
| 25 | + var ( |
| 26 | + mockCtrl *gomock.Controller |
| 27 | + logger *mocks_logger.Logger |
| 28 | + service *mocks_container.MockService |
| 29 | + h *handler |
| 30 | + rr *httptest.ResponseRecorder |
| 31 | + _ ncTypes.GlobalCommandOptions |
| 32 | + _ error |
| 33 | + ) |
| 34 | + |
| 35 | + BeforeEach(func() { |
| 36 | + mockCtrl = gomock.NewController(GinkgoT()) |
| 37 | + defer mockCtrl.Finish() |
| 38 | + logger = mocks_logger.NewLogger(mockCtrl) |
| 39 | + service = mocks_container.NewMockService(mockCtrl) |
| 40 | + c := config.Config{} |
| 41 | + h = newHandler(service, &c, logger) |
| 42 | + rr = httptest.NewRecorder() |
| 43 | + }) |
| 44 | + |
| 45 | + Context("pause handler", func() { |
| 46 | + It("should return 204 No Content on successful pause", func() { |
| 47 | + req, err := http.NewRequest(http.MethodPost, "/containers/id1/pause", nil) |
| 48 | + Expect(err).Should(BeNil()) |
| 49 | + req = mux.SetURLVars(req, map[string]string{"id": "id1"}) |
| 50 | + |
| 51 | + service.EXPECT().Pause(gomock.Any(), "id1", gomock.Any()).DoAndReturn( |
| 52 | + func(ctx context.Context, cid string, opts ncTypes.ContainerPauseOptions) error { |
| 53 | + return nil |
| 54 | + }) |
| 55 | + |
| 56 | + h.pause(rr, req) |
| 57 | + Expect(rr.Body.String()).Should(BeEmpty()) |
| 58 | + Expect(rr).Should(HaveHTTPStatus(http.StatusNoContent)) |
| 59 | + }) |
| 60 | + |
| 61 | + It("should return 400 when container ID is missing", func() { |
| 62 | + req, err := http.NewRequest(http.MethodPost, "/containers//pause", nil) |
| 63 | + Expect(err).Should(BeNil()) |
| 64 | + req = mux.SetURLVars(req, map[string]string{"id": ""}) |
| 65 | + |
| 66 | + h.pause(rr, req) |
| 67 | + Expect(rr.Body).Should(MatchJSON(`{"message": "must specify a container ID"}`)) |
| 68 | + Expect(rr).Should(HaveHTTPStatus(http.StatusBadRequest)) |
| 69 | + }) |
| 70 | + |
| 71 | + It("should return 404 when service returns a not found error", func() { |
| 72 | + req, err := http.NewRequest(http.MethodPost, "/containers/id1/pause", nil) |
| 73 | + Expect(err).Should(BeNil()) |
| 74 | + req = mux.SetURLVars(req, map[string]string{"id": "id1"}) |
| 75 | + |
| 76 | + service.EXPECT().Pause(gomock.Any(), "id1", gomock.Any()).Return( |
| 77 | + errdefs.NewNotFound(fmt.Errorf("container not found"))) |
| 78 | + |
| 79 | + h.pause(rr, req) |
| 80 | + Expect(rr.Body).Should(MatchJSON(`{"message": "container not found"}`)) |
| 81 | + Expect(rr).Should(HaveHTTPStatus(http.StatusNotFound)) |
| 82 | + }) |
| 83 | + |
| 84 | + It("should return 409 when service returns a conflict error", func() { |
| 85 | + req, err := http.NewRequest(http.MethodPost, "/containers/id1/pause", nil) |
| 86 | + Expect(err).Should(BeNil()) |
| 87 | + req = mux.SetURLVars(req, map[string]string{"id": "id1"}) |
| 88 | + |
| 89 | + service.EXPECT().Pause(gomock.Any(), "id1", gomock.Any()).Return( |
| 90 | + errdefs.NewConflict(fmt.Errorf("container already paused"))) |
| 91 | + |
| 92 | + h.pause(rr, req) |
| 93 | + Expect(rr.Body).Should(MatchJSON(`{"message": "container already paused"}`)) |
| 94 | + Expect(rr).Should(HaveHTTPStatus(http.StatusConflict)) |
| 95 | + }) |
| 96 | + |
| 97 | + It("should return 500 when service returns an internal error", func() { |
| 98 | + req, err := http.NewRequest(http.MethodPost, "/containers/id1/pause", nil) |
| 99 | + Expect(err).Should(BeNil()) |
| 100 | + req = mux.SetURLVars(req, map[string]string{"id": "id1"}) |
| 101 | + |
| 102 | + service.EXPECT().Pause(gomock.Any(), "id1", gomock.Any()).Return( |
| 103 | + fmt.Errorf("unexpected internal error")) |
| 104 | + |
| 105 | + h.pause(rr, req) |
| 106 | + Expect(rr.Body).Should(MatchJSON(`{"message": "unexpected internal error"}`)) |
| 107 | + Expect(rr).Should(HaveHTTPStatus(http.StatusInternalServerError)) |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments