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"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2016-09-06 14:46:37 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestImageRemoveError(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.ImageRemove(context.Background(), "image_id", types.ImageRemoveOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Error(err, "Error response from daemon: Server error"))
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:16:02 -04:00
|
|
|
func TestImageRemoveImageNotFound(t *testing.T) {
|
|
|
|
client := &Client{
|
2017-09-08 12:04:34 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusNotFound, "missing")),
|
2017-08-15 17:16:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.ImageRemove(context.Background(), "unknown", types.ImageRemoveOptions{})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Error(err, "Error: No such image: unknown"))
|
|
|
|
assert.Check(t, IsErrNotFound(err))
|
2017-08-15 17:16:02 -04:00
|
|
|
}
|
|
|
|
|
2016-09-06 14:46:37 -04: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-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)
|
|
|
|
}
|
|
|
|
if req.Method != "DELETE" {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 11:27:56 -05:00
|
|
|
b, err := json.Marshal([]types.ImageDeleteResponseItem{
|
2016-09-06 14:46:37 -04:00
|
|
|
{
|
|
|
|
Untagged: "image_id1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Deleted: "image_id",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(b)),
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
imageDeletes, err := client.ImageRemove(context.Background(), "image_id", types.ImageRemoveOptions{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|