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
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-11-24 18:04:31 +00:00
|
|
|
"github.com/docker/docker/api/types/image"
|
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 TestImageRemoveError(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-07-07 11:40:24 +00:00
|
|
|
_, err := client.ImageRemove(context.Background(), "image_id", image.RemoveOptions{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 21:16:02 +00:00
|
|
|
func TestImageRemoveImageNotFound(t *testing.T) {
|
|
|
|
client := &Client{
|
2022-03-20 15:55:42 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusNotFound, "no such image: unknown")),
|
2017-08-15 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:40:24 +00:00
|
|
|
_, err := client.ImageRemove(context.Background(), "unknown", image.RemoveOptions{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "no such image: unknown"))
|
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2017-08-15 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
func TestImageRemove(t *testing.T) {
|
|
|
|
expectedURL := "/images/image_id"
|
|
|
|
removeCases := []struct {
|
|
|
|
force bool
|
|
|
|
pruneChildren bool
|
|
|
|
expectedQueryParams map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
force: false,
|
|
|
|
pruneChildren: false,
|
|
|
|
expectedQueryParams: map[string]string{
|
|
|
|
"force": "",
|
|
|
|
"noprune": "1",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
force: true,
|
|
|
|
pruneChildren: true,
|
|
|
|
expectedQueryParams: map[string]string{
|
|
|
|
"force": "1",
|
|
|
|
"noprune": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, removeCase := range removeCases {
|
|
|
|
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)
|
|
|
|
}
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodDelete {
|
2016-09-06 18:46:37 +00:00
|
|
|
return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
|
|
|
|
}
|
|
|
|
query := req.URL.Query()
|
|
|
|
for key, expected := range removeCase.expectedQueryParams {
|
|
|
|
actual := query.Get(key)
|
|
|
|
if actual != expected {
|
|
|
|
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 18:04:31 +00:00
|
|
|
b, err := json.Marshal([]image.DeleteResponse{
|
2016-09-06 18:46:37 +00:00
|
|
|
{
|
|
|
|
Untagged: "image_id1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Deleted: "image_id",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
2023-07-07 11:40:24 +00:00
|
|
|
imageDeletes, err := client.ImageRemove(context.Background(), "image_id", image.RemoveOptions{
|
2016-09-06 18:46:37 +00:00
|
|
|
Force: removeCase.force,
|
|
|
|
PruneChildren: removeCase.pruneChildren,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(imageDeletes) != 2 {
|
|
|
|
t.Fatalf("expected 2 deleted images, got %v", imageDeletes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|