client: ContainerStop(), ContainerRestart(): support stop-signal

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2022-02-16 11:36:37 +01:00
parent 9060126639
commit e8fa708ae5
12 changed files with 68 additions and 40 deletions

View File

@@ -8,8 +8,8 @@ import (
"net/http"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
)
@@ -17,20 +17,23 @@ func TestContainerRestartError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
timeout := 0 * time.Second
err := client.ContainerRestart(context.Background(), "nothing", &timeout)
err := client.ContainerRestart(context.Background(), "nothing", container.StopOptions{})
if !errdefs.IsSystem(err) {
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
}
}
func TestContainerRestart(t *testing.T) {
expectedURL := "/containers/container_id/restart"
const expectedURL = "/v1.42/containers/container_id/restart"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
s := req.URL.Query().Get("signal")
if s != "SIGKILL" {
return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s)
}
t := req.URL.Query().Get("t")
if t != "100" {
return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
@@ -40,9 +43,13 @@ func TestContainerRestart(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}, nil
}),
version: "1.42",
}
timeout := 100 * time.Second
err := client.ContainerRestart(context.Background(), "container_id", &timeout)
timeout := 100
err := client.ContainerRestart(context.Background(), "container_id", container.StopOptions{
Signal: "SIGKILL",
Timeout: &timeout,
})
if err != nil {
t.Fatal(err)
}