2015-04-07 21:57:54 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-04-20 17:03:56 -04:00
|
|
|
"net/http"
|
2017-04-14 19:01:56 -04:00
|
|
|
"net/http/httptest"
|
2018-05-09 17:03:48 -04:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2015-04-13 23:02:29 -04:00
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-04-07 21:57:54 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-05-23 23:56:26 -04:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/client"
|
2017-03-23 13:35:22 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2018-05-09 17:03:48 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2019-04-04 09:23:19 -04:00
|
|
|
"gotest.tools/assert"
|
2015-04-07 21:57:54 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestAPIImagesFilter(c *testing.T) {
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
|
2015-04-10 17:51:40 -04:00
|
|
|
name := "utest:tag1"
|
|
|
|
name2 := "utest/docker:tag2"
|
|
|
|
name3 := "utest:5000/docker:tag3"
|
2015-04-18 12:46:47 -04:00
|
|
|
for _, n := range []string{name, name2, name3} {
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "tag", "busybox", n)
|
2015-04-18 12:46:47 -04:00
|
|
|
}
|
2017-05-23 23:56:26 -04: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 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-10 17:51:40 -04:00
|
|
|
|
|
|
|
return images
|
|
|
|
}
|
|
|
|
|
2015-11-18 08:06:29 -05:00
|
|
|
//incorrect number of matches returned
|
|
|
|
images := getImages("utest*/*")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 2)
|
2015-11-18 08:06:29 -05:00
|
|
|
|
|
|
|
images = getImages("utest")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 1)
|
2015-11-18 08:06:29 -05:00
|
|
|
|
|
|
|
images = getImages("utest*")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 1)
|
2015-11-18 08:06:29 -05:00
|
|
|
|
|
|
|
images = getImages("*5000*/*")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(images[0].RepoTags), 1)
|
2015-04-10 17:51:40 -04:00
|
|
|
}
|
2015-04-13 23:02:29 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestAPIImagesSaveAndLoad(c *testing.T) {
|
2018-05-09 17:03:48 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
v, err := kernel.GetKernelVersion()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2018-05-09 17:03:48 -04:00
|
|
|
build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0])
|
2019-09-11 14:59:48 -04:00
|
|
|
if build <= 16299 {
|
|
|
|
c.Skip("Temporarily disabled on RS3 and older because they are too slow. See #39909")
|
2018-05-09 17:03:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
testRequires(c, Network)
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, "saveandload", build.WithDockerfile("FROM busybox\nENV FOO bar"))
|
2017-01-16 05:30:14 -05:00
|
|
|
id := getIDByName(c, "saveandload")
|
2015-04-13 23:02:29 -04:00
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
res, body, err := request.Get("/images/" + id + "/get")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-13 23:02:29 -04:00
|
|
|
defer body.Close()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-04-13 23:02:29 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "rmi", id)
|
2015-04-13 23:02:29 -04:00
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
res, loadBody, err := request.Post("/images/load", request.RawContent(body), request.ContentType("application/x-tar"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-13 23:02:29 -04:00
|
|
|
defer loadBody.Close()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-04-13 23:02:29 -04:00
|
|
|
|
2017-03-23 13:35:22 -04:00
|
|
|
inspectOut := cli.InspectCmd(c, id, cli.Format(".Id")).Combined()
|
2019-08-05 11:54:15 -04:00
|
|
|
assert.Equal(c, strings.TrimSpace(inspectOut), id, "load did not work properly")
|
2015-04-13 23:02:29 -04:00
|
|
|
}
|
2015-04-29 07:56:45 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestAPIImagesDelete(c *testing.T) {
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
|
2018-01-15 09:32:06 -05:00
|
|
|
if testEnv.OSType != "windows" {
|
2016-01-23 00:13:38 -05:00
|
|
|
testRequires(c, Network)
|
|
|
|
}
|
2015-04-29 07:56:45 -04:00
|
|
|
name := "test-api-images-delete"
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
|
2017-01-16 05:30:14 -05:00
|
|
|
id := getIDByName(c, name)
|
2015-04-29 07:56:45 -04:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "tag", name, "test:tag1")
|
2015-04-29 07:56:45 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
_, err = cli.ImageRemove(context.Background(), id, types.ImageRemoveOptions{})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "unable to delete")
|
2015-04-29 07:56:45 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
_, err = cli.ImageRemove(context.Background(), "test:noexist", types.ImageRemoveOptions{})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "No such image")
|
2015-05-04 03:39:40 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
_, err = cli.ImageRemove(context.Background(), "test:tag1", types.ImageRemoveOptions{})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-04-29 07:56:45 -04:00
|
|
|
}
|
2015-05-02 01:30:35 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestAPIImagesHistory(c *testing.T) {
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
|
2018-01-15 09:32:06 -05:00
|
|
|
if testEnv.OSType != "windows" {
|
2016-01-23 00:13:38 -05:00
|
|
|
testRequires(c, Network)
|
|
|
|
}
|
2015-05-02 01:30:35 -04:00
|
|
|
name := "test-api-images-history"
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
|
2017-01-16 05:30:14 -05:00
|
|
|
id := getIDByName(c, name)
|
2015-05-02 01:30:35 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
historydata, err := cli.ImageHistory(context.Background(), id)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-05-02 01:30:35 -04:00
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(historydata) != 0)
|
2018-03-27 12:13:47 -04:00
|
|
|
var found bool
|
|
|
|
for _, tag := range historydata[0].Tags {
|
|
|
|
if tag == "test-api-images-history:latest" {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, found)
|
2015-05-02 01:30:35 -04:00
|
|
|
}
|
2015-07-22 05:07:41 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestAPIImagesImportBadSrc(c *testing.T) {
|
2018-05-09 17:03:48 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
v, err := kernel.GetKernelVersion()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2018-05-09 17:03:48 -04:00
|
|
|
build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0])
|
|
|
|
if build == 16299 {
|
|
|
|
c.Skip("Temporarily disabled on RS3 builds")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, Network, testEnv.IsLocalDaemon)
|
2017-02-05 06:39:37 -05:00
|
|
|
|
2017-04-14 19:01:56 -04:00
|
|
|
server := httptest.NewServer(http.NewServeMux())
|
|
|
|
defer server.Close()
|
|
|
|
|
2017-02-05 06:39:37 -05:00
|
|
|
tt := []struct {
|
|
|
|
statusExp int
|
|
|
|
fromSrc string
|
|
|
|
}{
|
2017-04-14 19:01:56 -04: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 06:39:37 -05:00
|
|
|
{http.StatusInternalServerError, "%2Fdata%2Ffile.tar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, te := range tt {
|
2017-05-23 23:56:26 -04:00
|
|
|
res, _, err := request.Post(strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), request.JSON)
|
2019-04-04 09:23:19 -04: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 06:39:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:07:41 -04:00
|
|
|
// #14846
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *testing.T) {
|
2015-07-22 05:07:41 -04:00
|
|
|
testRequires(c, Network)
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
res, b, err := request.Get("/images/search?term=test", request.JSON)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-07-23 07:24:14 -04:00
|
|
|
b.Close()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
|
|
|
assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
|
2015-07-22 05:07:41 -04:00
|
|
|
}
|
2017-01-12 11:12:39 -05: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.
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *testing.T) {
|
2017-09-19 16:12:29 -04:00
|
|
|
apiclient := testEnv.APIClient()
|
|
|
|
defer apiclient.Close()
|
2017-05-23 23:56:26 -04:00
|
|
|
|
2017-09-19 16:12:29 -04:00
|
|
|
images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, len(images) != 0)
|
2017-01-12 11:12:39 -05:00
|
|
|
for _, image := range images {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, image.Size != int64(-1))
|
2017-01-12 11:12:39 -05:00
|
|
|
}
|
|
|
|
|
2017-09-19 16:12:29 -04:00
|
|
|
apiclient, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("v1.24"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-09-19 16:12:29 -04:00
|
|
|
defer apiclient.Close()
|
2017-05-23 23:56:26 -04:00
|
|
|
|
2017-09-19 16:12:29 -04:00
|
|
|
v124Images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, len(v124Images) != 0)
|
2017-01-12 11:12:39 -05:00
|
|
|
for _, image := range v124Images {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, image.Size != int64(-1))
|
2017-01-12 11:12:39 -05:00
|
|
|
}
|
|
|
|
}
|