2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-09-06 14:46:37 -04:00
|
|
|
"fmt"
|
2021-08-24 06:10:50 -04:00
|
|
|
"io"
|
2016-09-06 14:46:37 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2019-10-12 18:31:53 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerRemoveError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
|
2019-10-12 18:31:53 -04:00
|
|
|
if !errdefs.IsSystem(err) {
|
|
|
|
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
|
|
|
|
}
|
2017-09-08 12:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerRemoveNotFoundError(t *testing.T) {
|
|
|
|
client := &Client{
|
2022-03-20 11:55:42 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusNotFound, "no such container: container_id")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
2017-09-08 12:04:34 -04:00
|
|
|
err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{})
|
2022-03-20 11:55:42 -04:00
|
|
|
assert.ErrorContains(t, err, "no such container: container_id")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, IsErrNotFound(err))
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerRemove(t *testing.T) {
|
|
|
|
expectedURL := "/containers/container_id"
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 14:46:37 -04: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 06:10:50 -04:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 14:46:37 -04:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.ContainerRemove(context.Background(), "container_id", types.ContainerRemoveOptions{
|
|
|
|
RemoveVolumes: true,
|
|
|
|
Force: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|