2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-12-31 17:22:43 +00:00
|
|
|
|
2022-02-16 10:36:37 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2018-12-31 17:22:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2023-05-10 11:17:40 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerStopError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2022-02-16 10:36:37 +00:00
|
|
|
err := client.ContainerStop(context.Background(), "nothing", container.StopOptions{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 11:20:06 +00:00
|
|
|
// TestContainerStopConnectionError verifies that connection errors occurring
|
|
|
|
// during API-version negotiation are not shadowed by API-version errors.
|
|
|
|
//
|
|
|
|
// Regression test for https://github.com/docker/cli/issues/4890
|
|
|
|
func TestContainerStopConnectionError(t *testing.T) {
|
|
|
|
client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = client.ContainerStop(context.Background(), "nothing", container.StopOptions{})
|
|
|
|
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
func TestContainerStop(t *testing.T) {
|
2022-02-16 10:36:37 +00:00
|
|
|
const expectedURL = "/v1.42/containers/container_id/stop"
|
2016-09-06 18:46:37 +00:00
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
2022-02-16 10:36:37 +00:00
|
|
|
s := req.URL.Query().Get("signal")
|
|
|
|
if s != "SIGKILL" {
|
|
|
|
return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s)
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
2022-02-16 10:36:37 +00:00
|
|
|
version: "1.42",
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2022-02-16 10:36:37 +00:00
|
|
|
timeout := 100
|
|
|
|
err := client.ContainerStop(context.Background(), "container_id", container.StopOptions{
|
|
|
|
Signal: "SIGKILL",
|
|
|
|
Timeout: &timeout,
|
|
|
|
})
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|