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"
|
|
|
|
|
2023-08-25 18:25:58 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2019-10-12 22:31:53 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2023-05-10 11:17:40 +00:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerRemoveError(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
|
|
|
}
|
2023-08-25 18:25:58 +00:00
|
|
|
err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2017-09-08 16:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerRemoveNotFoundError(t *testing.T) {
|
|
|
|
client := &Client{
|
2022-03-20 15:55:42 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusNotFound, "no such container: container_id")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2023-08-25 18:25:58 +00:00
|
|
|
err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "no such container: container_id"))
|
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerRemove(t *testing.T) {
|
|
|
|
expectedURL := "/containers/container_id"
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
query := req.URL.Query()
|
|
|
|
volume := query.Get("v")
|
|
|
|
if volume != "1" {
|
|
|
|
return nil, fmt.Errorf("v (volume) not set in URL query properly. Expected '1', got %s", volume)
|
|
|
|
}
|
|
|
|
force := query.Get("force")
|
|
|
|
if force != "1" {
|
|
|
|
return nil, fmt.Errorf("force not set in URL query properly. Expected '1', got %s", force)
|
|
|
|
}
|
|
|
|
link := query.Get("link")
|
|
|
|
if link != "" {
|
|
|
|
return nil, fmt.Errorf("link should have not be present in query, go %s", link)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:25:58 +00:00
|
|
|
err := client.ContainerRemove(context.Background(), "container_id", container.RemoveOptions{
|
2016-09-06 18:46:37 +00:00
|
|
|
RemoveVolumes: true,
|
|
|
|
Force: true,
|
|
|
|
})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|