mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Support for daemon-side auto-remove was added in API v1.25; on older versions of the daemon, the client was responsible for removing the container after it exited (see [moby@6dd8e10]) On API versions < 1.30, it used the events API for this purpose, and would wait for a "die", "detach" or "detroy" events to know the container exited, and could be removed or (when attached, but without a TTY) to get the container's exit-status. (see [cli@38591f2]). API version 1.24 (docker 1.12) is 9 Years old (July 29, 2016), and API 1.30 (docker 17.06) is 8 Years old (Jun 20, 2017), and long EOL. While technically, a CLI could negotiate API 1.30 or older, this would only be in cases where either API version negotiation failed, or the version was explicitly overridden through `DOCKER_API_VERSION` for testing. This patch removes the version-gate for daemon-side AutoRemove; version- specific handling is removed from the client (and a related patch in the CLI). [moby@6dd8e10]:6dd8e10d6e[cli@38591f2]:38591f20d0Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
171 lines
5.1 KiB
Go
171 lines
5.1 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
cerrdefs "github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/container"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestContainerCreateError(t *testing.T) {
|
|
client, err := NewClientWithOpts(
|
|
WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
|
|
assert.Error(t, err, "config is nil")
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
|
|
|
|
_, err = client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "nothing")
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
|
|
|
|
// 404 doesn't automatically means an unknown image
|
|
client, err = NewClientWithOpts(
|
|
WithMockClient(errorMock(http.StatusNotFound, "Server error")),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "nothing")
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
|
|
}
|
|
|
|
func TestContainerCreateImageNotFound(t *testing.T) {
|
|
client, err := NewClientWithOpts(
|
|
WithMockClient(errorMock(http.StatusNotFound, "No such image")),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ContainerCreate(context.Background(), &container.Config{Image: "unknown_image"}, nil, nil, nil, "unknown")
|
|
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
|
|
}
|
|
|
|
func TestContainerCreateWithName(t *testing.T) {
|
|
const expectedURL = "/containers/create"
|
|
client, err := NewClientWithOpts(
|
|
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
if err := assertRequest(req, http.MethodPost, expectedURL); err != nil {
|
|
return nil, err
|
|
}
|
|
name := req.URL.Query().Get("name")
|
|
if name != "container_name" {
|
|
return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name)
|
|
}
|
|
b, err := json.Marshal(container.CreateResponse{
|
|
ID: "container_id",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
|
}, nil
|
|
}),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
r, err := client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "container_name")
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Equal(r.ID, "container_id"))
|
|
}
|
|
|
|
func TestContainerCreateAutoRemove(t *testing.T) {
|
|
client, err := NewClientWithOpts(
|
|
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
var config container.CreateRequest
|
|
if err := json.NewDecoder(req.Body).Decode(&config); err != nil {
|
|
return nil, err
|
|
}
|
|
if !config.HostConfig.AutoRemove {
|
|
return nil, errors.New("expected AutoRemove to be enabled")
|
|
}
|
|
b, err := json.Marshal(container.CreateResponse{
|
|
ID: "container_id",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
|
}, nil
|
|
}),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
resp, err := client.ContainerCreate(context.Background(), &container.Config{}, &container.HostConfig{AutoRemove: true}, nil, nil, "")
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Equal(resp.ID, "container_id"))
|
|
}
|
|
|
|
// TestContainerCreateConnectionError 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 TestContainerCreateConnectionError(t *testing.T) {
|
|
client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid"))
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "")
|
|
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
|
|
}
|
|
|
|
// TestContainerCreateCapabilities verifies that CapAdd and CapDrop capabilities
|
|
// are normalized to their canonical form.
|
|
func TestContainerCreateCapabilities(t *testing.T) {
|
|
inputCaps := []string{
|
|
"all",
|
|
"ALL",
|
|
"capability_b",
|
|
"capability_a",
|
|
"capability_c",
|
|
"CAPABILITY_D",
|
|
"CAP_CAPABILITY_D",
|
|
}
|
|
|
|
expectedCaps := []string{
|
|
"ALL",
|
|
"CAP_CAPABILITY_A",
|
|
"CAP_CAPABILITY_B",
|
|
"CAP_CAPABILITY_C",
|
|
"CAP_CAPABILITY_D",
|
|
}
|
|
|
|
client, err := NewClientWithOpts(
|
|
WithMockClient(func(req *http.Request) (*http.Response, error) {
|
|
var config container.CreateRequest
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&config); err != nil {
|
|
return nil, err
|
|
}
|
|
assert.Check(t, is.DeepEqual(config.HostConfig.CapAdd, expectedCaps))
|
|
assert.Check(t, is.DeepEqual(config.HostConfig.CapDrop, expectedCaps))
|
|
|
|
b, err := json.Marshal(container.CreateResponse{
|
|
ID: "container_id",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
|
}, nil
|
|
}),
|
|
)
|
|
assert.NilError(t, err)
|
|
|
|
_, err = client.ContainerCreate(context.Background(), &container.Config{}, &container.HostConfig{CapAdd: inputCaps, CapDrop: inputCaps}, nil, nil, "")
|
|
assert.NilError(t, err)
|
|
}
|