|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package tests |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | + |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | + "github.com/runfinch/common-tests/command" |
| 15 | + "github.com/runfinch/common-tests/option" |
| 16 | + |
| 17 | + "github.com/runfinch/finch-daemon/api/response" |
| 18 | + "github.com/runfinch/finch-daemon/e2e/client" |
| 19 | +) |
| 20 | + |
| 21 | +func ContainerKill(opt *option.Option) { |
| 22 | + Describe("kill a container", func() { |
| 23 | + var ( |
| 24 | + uClient *http.Client |
| 25 | + version string |
| 26 | + apiUrl string |
| 27 | + ) |
| 28 | + BeforeEach(func() { |
| 29 | + uClient = client.NewClient(GetDockerHostUrl()) |
| 30 | + version = GetDockerApiVersion() |
| 31 | + relativeUrl := fmt.Sprintf("/containers/%s/kill", testContainerName) |
| 32 | + apiUrl = client.ConvertToFinchUrl(version, relativeUrl) |
| 33 | + }) |
| 34 | + AfterEach(func() { |
| 35 | + command.RemoveAll(opt) |
| 36 | + }) |
| 37 | + It("should kill the container with default SIGKILL", func() { |
| 38 | + // start a container that keeps running |
| 39 | + command.Run(opt, "run", "-d", "--name", testContainerName, defaultImage, "sleep", "infinity") |
| 40 | + res, err := uClient.Post(apiUrl, "application/json", nil) |
| 41 | + Expect(err).Should(BeNil()) |
| 42 | + Expect(res.StatusCode).Should(Equal(http.StatusNoContent)) |
| 43 | + containerShouldNotBeRunning(opt, testContainerName) |
| 44 | + }) |
| 45 | + It("should fail to kill a non-existent container", func() { |
| 46 | + res, err := uClient.Post(apiUrl, "application/json", nil) |
| 47 | + Expect(err).Should(BeNil()) |
| 48 | + Expect(res.StatusCode).Should(Equal(http.StatusNotFound)) |
| 49 | + var body response.Error |
| 50 | + err = json.NewDecoder(res.Body).Decode(&body) |
| 51 | + Expect(err).Should(BeNil()) |
| 52 | + }) |
| 53 | + It("should fail to kill a non running container", func() { |
| 54 | + command.Run(opt, "create", "--name", testContainerName, defaultImage, "sleep", "infinity") |
| 55 | + res, err := uClient.Post(apiUrl, "application/json", nil) |
| 56 | + Expect(err).Should(BeNil()) |
| 57 | + Expect(res.StatusCode).Should(Equal(http.StatusConflict)) |
| 58 | + var body response.Error |
| 59 | + err = json.NewDecoder(res.Body).Decode(&body) |
| 60 | + Expect(err).Should(BeNil()) |
| 61 | + containerShouldExist(opt, testContainerName) |
| 62 | + }) |
| 63 | + It("should kill the container with SIGINT", func() { |
| 64 | + relativeUrl := fmt.Sprintf("/containers/%s/kill?signal=SIGINT", testContainerName) |
| 65 | + apiUrl = client.ConvertToFinchUrl(version, relativeUrl) |
| 66 | + // sleep infinity doesnot respond to SIGINT |
| 67 | + command.Run(opt, "run", "-d", "--name", testContainerName, defaultImage, "/bin/sh", "-c", "trap 'exit 0' SIGINT; while true; do sleep 1; done") |
| 68 | + res, err := uClient.Post(apiUrl, "application/json", nil) |
| 69 | + Expect(err).Should(BeNil()) |
| 70 | + Expect(res.StatusCode).Should(Equal(http.StatusNoContent)) |
| 71 | + // This is an async operation as a result we need to wait for the container to exit gracefully before checking the status |
| 72 | + time.Sleep(1 * time.Second) |
| 73 | + containerShouldNotBeRunning(opt, testContainerName) |
| 74 | + }) |
| 75 | + It("should not kill the container and throw error on unrecognized signal", func() { |
| 76 | + relativeUrl := fmt.Sprintf("/containers/%s/kill?signal=SIGRAND", testContainerName) |
| 77 | + apiUrl = client.ConvertToFinchUrl(version, relativeUrl) |
| 78 | + command.Run(opt, "run", "-d", "--name", testContainerName, defaultImage, "sleep", "infinity") |
| 79 | + res, err := uClient.Post(apiUrl, "application/json", nil) |
| 80 | + Expect(err).Should(BeNil()) |
| 81 | + Expect(res.StatusCode).Should(Equal(http.StatusInternalServerError)) |
| 82 | + containerShouldExist(opt, testContainerName) |
| 83 | + containerShouldBeRunning(opt, testContainerName) |
| 84 | + }) |
| 85 | + }) |
| 86 | +} |
0 commit comments