2015-04-08 01:57:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2015-04-20 21:03:56 +00:00
|
|
|
"net/http"
|
2017-04-14 23:01:56 +00:00
|
|
|
"net/http/httptest"
|
2015-04-14 03:02:29 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-04-08 01:57:54 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-24 03:56:26 +00:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/client"
|
2017-03-23 17:35:22 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2015-04-08 01:57:54 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPIImagesFilter(c *testing.T) {
|
2019-01-03 21:49:00 +00:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
|
2015-04-10 21:51:40 +00:00
|
|
|
name := "utest:tag1"
|
|
|
|
name2 := "utest/docker:tag2"
|
|
|
|
name3 := "utest:5000/docker:tag3"
|
2015-04-18 16:46:47 +00:00
|
|
|
for _, n := range []string{name, name2, name3} {
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "tag", "busybox", n)
|
2015-04-18 16:46:47 +00:00
|
|
|
}
|
2017-05-24 03:56:26 +00:00
|
|
|
getImages := func(filter string) []types.ImageSummary {
|
|
|
|
filters := filters.NewArgs()
|
|
|
|
filters.Add("reference", filter)
|
|
|
|
options := types.ImageListOptions{
|
|
|
|
All: false,
|
|
|
|
Filters: filters,
|
|
|
|
}
|
|
|
|
images, err := cli.ImageList(context.Background(), options)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-10 21:51:40 +00:00
|
|
|
|
|
|
|
return images
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:36:45 +00:00
|
|
|
// incorrect number of matches returned
|
2015-11-18 13:06:29 +00:00
|
|
|
images := getImages("utest*/*")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 2)
|
2015-11-18 13:06:29 +00:00
|
|
|
|
|
|
|
images = getImages("utest")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 1)
|
2015-11-18 13:06:29 +00:00
|
|
|
|
|
|
|
images = getImages("utest*")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 1)
|
2015-11-18 13:06:29 +00:00
|
|
|
|
|
|
|
images = getImages("*5000*/*")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 1)
|
2015-04-10 21:51:40 +00:00
|
|
|
}
|
2015-04-14 03:02:29 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPIImagesSaveAndLoad(c *testing.T) {
|
2015-04-18 16:46:47 +00:00
|
|
|
testRequires(c, Network)
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "saveandload", build.WithDockerfile("FROM busybox\nENV FOO bar"))
|
2017-01-16 10:30:14 +00:00
|
|
|
id := getIDByName(c, "saveandload")
|
2015-04-14 03:02:29 +00:00
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
res, body, err := request.Get("/images/" + id + "/get")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-14 03:02:29 +00:00
|
|
|
defer body.Close()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-04-14 03:02:29 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "rmi", id)
|
2015-04-14 03:02:29 +00:00
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
res, loadBody, err := request.Post("/images/load", request.RawContent(body), request.ContentType("application/x-tar"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-14 03:02:29 +00:00
|
|
|
defer loadBody.Close()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-04-14 03:02:29 +00:00
|
|
|
|
2017-03-23 17:35:22 +00:00
|
|
|
inspectOut := cli.InspectCmd(c, id, cli.Format(".Id")).Combined()
|
2019-08-05 15:54:15 +00:00
|
|
|
assert.Equal(c, strings.TrimSpace(inspectOut), id, "load did not work properly")
|
2015-04-14 03:02:29 +00:00
|
|
|
}
|
2015-04-29 11:56:45 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPIImagesDelete(c *testing.T) {
|
2019-01-03 21:49:00 +00:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
|
2018-01-15 14:32:06 +00:00
|
|
|
if testEnv.OSType != "windows" {
|
2016-01-23 05:13:38 +00:00
|
|
|
testRequires(c, Network)
|
|
|
|
}
|
2015-04-29 11:56:45 +00:00
|
|
|
name := "test-api-images-delete"
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
|
2017-01-16 10:30:14 +00:00
|
|
|
id := getIDByName(c, name)
|
2015-04-29 11:56:45 +00:00
|
|
|
|
2015-07-14 06:35:36 +00:00
|
|
|
dockerCmd(c, "tag", name, "test:tag1")
|
2015-04-29 11:56:45 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
_, err = cli.ImageRemove(context.Background(), id, types.ImageRemoveOptions{})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, err, "unable to delete")
|
2015-04-29 11:56:45 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
_, err = cli.ImageRemove(context.Background(), "test:noexist", types.ImageRemoveOptions{})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, err, "No such image")
|
2015-05-04 07:39:40 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
_, err = cli.ImageRemove(context.Background(), "test:tag1", types.ImageRemoveOptions{})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-04-29 11:56:45 +00:00
|
|
|
}
|
2015-05-02 05:30:35 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPIImagesHistory(c *testing.T) {
|
2019-01-03 21:49:00 +00:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
|
2018-01-15 14:32:06 +00:00
|
|
|
if testEnv.OSType != "windows" {
|
2016-01-23 05:13:38 +00:00
|
|
|
testRequires(c, Network)
|
|
|
|
}
|
2015-05-02 05:30:35 +00:00
|
|
|
name := "test-api-images-history"
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
|
2017-01-16 10:30:14 +00:00
|
|
|
id := getIDByName(c, name)
|
2015-05-02 05:30:35 +00:00
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
historydata, err := cli.ImageHistory(context.Background(), id)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-05-02 05:30:35 +00:00
|
|
|
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, len(historydata) != 0)
|
2018-03-27 16:13:47 +00:00
|
|
|
var found bool
|
|
|
|
for _, tag := range historydata[0].Tags {
|
|
|
|
if tag == "test-api-images-history:latest" {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, found)
|
2015-05-02 05:30:35 +00:00
|
|
|
}
|
2015-07-22 09:07:41 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPIImagesImportBadSrc(c *testing.T) {
|
2018-12-24 12:25:53 +00:00
|
|
|
testRequires(c, Network, testEnv.IsLocalDaemon)
|
2017-02-05 11:39:37 +00:00
|
|
|
|
2017-04-14 23:01:56 +00:00
|
|
|
server := httptest.NewServer(http.NewServeMux())
|
|
|
|
defer server.Close()
|
|
|
|
|
2017-02-05 11:39:37 +00:00
|
|
|
tt := []struct {
|
|
|
|
statusExp int
|
|
|
|
fromSrc string
|
|
|
|
}{
|
2017-04-14 23:01:56 +00:00
|
|
|
{http.StatusNotFound, server.URL + "/nofile.tar"},
|
|
|
|
{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "/nofile.tar"},
|
|
|
|
{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "%2Fdata%2Ffile.tar"},
|
2017-02-05 11:39:37 +00:00
|
|
|
{http.StatusInternalServerError, "%2Fdata%2Ffile.tar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, te := range tt {
|
2017-05-24 03:56:26 +00:00
|
|
|
res, _, err := request.Post(strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), request.JSON)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, te.statusExp)
|
|
|
|
assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
|
2017-02-05 11:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-22 09:07:41 +00:00
|
|
|
// #14846
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPIImagesSearchJSONContentType(c *testing.T) {
|
2015-07-22 09:07:41 +00:00
|
|
|
testRequires(c, Network)
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
res, b, err := request.Get("/images/search?term=test", request.JSON)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-07-23 11:24:14 +00:00
|
|
|
b.Close()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
|
|
|
assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
|
2015-07-22 09:07:41 +00:00
|
|
|
}
|
2017-01-12 16:12:39 +00:00
|
|
|
|
|
|
|
// Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon.
|
|
|
|
// This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix.
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerAPISuite) TestAPIImagesSizeCompatibility(c *testing.T) {
|
2017-09-19 20:12:29 +00:00
|
|
|
apiclient := testEnv.APIClient()
|
|
|
|
defer apiclient.Close()
|
2017-05-24 03:56:26 +00:00
|
|
|
|
2017-09-19 20:12:29 +00:00
|
|
|
images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, len(images) != 0)
|
2017-01-12 16:12:39 +00:00
|
|
|
for _, image := range images {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, image.Size != int64(-1))
|
2017-01-12 16:12:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 20:12:29 +00:00
|
|
|
apiclient, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("v1.24"))
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2017-09-19 20:12:29 +00:00
|
|
|
defer apiclient.Close()
|
2017-05-24 03:56:26 +00:00
|
|
|
|
2017-09-19 20:12:29 +00:00
|
|
|
v124Images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, len(v124Images) != 0)
|
2017-01-12 16:12:39 +00:00
|
|
|
for _, image := range v124Images {
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, image.Size != int64(-1))
|
2017-01-12 16:12:39 +00:00
|
|
|
}
|
|
|
|
}
|