|
| 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 | + |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + "github.com/runfinch/common-tests/command" |
| 14 | + "github.com/runfinch/common-tests/option" |
| 15 | + |
| 16 | + "github.com/runfinch/finch-daemon/api/response" |
| 17 | + "github.com/runfinch/finch-daemon/e2e/client" |
| 18 | +) |
| 19 | + |
| 20 | +func ContainerUnpause(opt *option.Option) { |
| 21 | + Describe("unpause a container", func() { |
| 22 | + var ( |
| 23 | + uClient *http.Client |
| 24 | + version string |
| 25 | + unpauseUrl string |
| 26 | + pauseUrl string |
| 27 | + ) |
| 28 | + |
| 29 | + BeforeEach(func() { |
| 30 | + uClient = client.NewClient(GetDockerHostUrl()) |
| 31 | + version = GetDockerApiVersion() |
| 32 | + relativeUrl_pause := fmt.Sprintf("/containers/%s/pause", testContainerName) |
| 33 | + relativeUrl_unpause := fmt.Sprintf("/containers/%s/unpause", testContainerName) |
| 34 | + unpauseUrl = client.ConvertToFinchUrl(version, relativeUrl_unpause) |
| 35 | + pauseUrl = client.ConvertToFinchUrl(version, relativeUrl_pause) |
| 36 | + }) |
| 37 | + |
| 38 | + AfterEach(func() { |
| 39 | + command.RemoveAll(opt) |
| 40 | + }) |
| 41 | + |
| 42 | + It("should unpause a paused container", func() { |
| 43 | + // Start a container that keeps running |
| 44 | + command.Run(opt, "run", "-d", "--name", testContainerName, defaultImage, "sleep", "infinity") |
| 45 | + |
| 46 | + // Pause this running container |
| 47 | + res, err := uClient.Post(pauseUrl, "application/json", nil) |
| 48 | + Expect(err).Should(BeNil()) |
| 49 | + Expect(res.StatusCode).Should(Equal(http.StatusNoContent)) |
| 50 | + |
| 51 | + // Verify container is paused |
| 52 | + output := command.StdoutStr(opt, "inspect", "--format", "{{.State.Status}}", testContainerName) |
| 53 | + Expect(output).Should(Equal("paused")) |
| 54 | + |
| 55 | + // Unpause the paused container |
| 56 | + res, err = uClient.Post(unpauseUrl, "application/json", nil) |
| 57 | + Expect(err).Should(BeNil()) |
| 58 | + Expect(res.StatusCode).Should(Equal(http.StatusNoContent)) |
| 59 | + |
| 60 | + // Verify container is running again |
| 61 | + output = command.StdoutStr(opt, "inspect", "--format", "{{.State.Status}}", testContainerName) |
| 62 | + Expect(output).Should(Equal("running")) |
| 63 | + }) |
| 64 | + |
| 65 | + It("should fail to unpause a non-existent container", func() { |
| 66 | + res, err := uClient.Post(unpauseUrl, "application/json", nil) |
| 67 | + Expect(err).Should(BeNil()) |
| 68 | + Expect(res.StatusCode).Should(Equal(http.StatusNotFound)) |
| 69 | + |
| 70 | + var body response.Error |
| 71 | + err = json.NewDecoder(res.Body).Decode(&body) |
| 72 | + Expect(err).Should(BeNil()) |
| 73 | + }) |
| 74 | + |
| 75 | + It("should fail to unpause a running container", func() { |
| 76 | + // Start a container that keeps running |
| 77 | + command.Run(opt, "run", "-d", "--name", testContainerName, defaultImage, "sleep", "infinity") |
| 78 | + |
| 79 | + // Try to unpause the running container |
| 80 | + res, err := uClient.Post(unpauseUrl, "application/json", nil) |
| 81 | + Expect(err).Should(BeNil()) |
| 82 | + Expect(res.StatusCode).Should(Equal(http.StatusConflict)) |
| 83 | + |
| 84 | + var body response.Error |
| 85 | + err = json.NewDecoder(res.Body).Decode(&body) |
| 86 | + Expect(err).Should(BeNil()) |
| 87 | + Expect(body.Message).Should(Equal(fmt.Sprintf("container %s is already running", testContainerName))) |
| 88 | + |
| 89 | + // Verify container is still running |
| 90 | + output := command.StdoutStr(opt, "inspect", "--format", "{{.State.Status}}", testContainerName) |
| 91 | + Expect(output).Should(Equal("running")) |
| 92 | + }) |
| 93 | + |
| 94 | + It("should fail to unpause a stopped container", func() { |
| 95 | + // Create and start a container |
| 96 | + command.Run(opt, "run", "-d", "--name", testContainerName, defaultImage, "sleep", "infinity") |
| 97 | + |
| 98 | + // Verify container is running |
| 99 | + output := command.StdoutStr(opt, "inspect", "--format", "{{.State.Status}}", testContainerName) |
| 100 | + Expect(output).Should(Equal("running")) |
| 101 | + |
| 102 | + // Stop the container with a timeout to ensure it stops |
| 103 | + command.Run(opt, "stop", "-t", "1", testContainerName) |
| 104 | + |
| 105 | + // Try to unpause the stopped container |
| 106 | + res, err := uClient.Post(unpauseUrl, "application/json", nil) |
| 107 | + Expect(err).Should(BeNil()) |
| 108 | + Expect(res.StatusCode).Should(Equal(http.StatusConflict)) |
| 109 | + |
| 110 | + var body response.Error |
| 111 | + err = json.NewDecoder(res.Body).Decode(&body) |
| 112 | + Expect(err).Should(BeNil()) |
| 113 | + Expect(body.Message).Should(Equal(fmt.Sprintf("container %s is not paused", testContainerName))) |
| 114 | + }) |
| 115 | + }) |
| 116 | +} |
0 commit comments