2015-04-08 01:57:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
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/client"
|
2017-03-23 17:35:22 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
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) 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
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
|
|
|
res, body, err := request.Get(ctx, "/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
|
|
|
|
2023-07-27 11:13:00 +00:00
|
|
|
cli.DockerCmd(c, "rmi", id)
|
2015-04-14 03:02:29 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
res, loadBody, err := request.Post(ctx, "/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) {
|
2023-04-03 11:00:29 +00:00
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-04-03 11:00:29 +00:00
|
|
|
defer apiClient.Close()
|
2017-05-24 03:56:26 +00:00
|
|
|
|
2023-06-14 09:46:00 +00:00
|
|
|
if testEnv.DaemonInfo.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
|
|
|
|
2023-07-27 11:13:00 +00:00
|
|
|
cli.DockerCmd(c, "tag", name, "test:tag1")
|
2015-04-29 11:56:45 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
_, err = apiClient.ImageRemove(testutil.GetContext(c), 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
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
_, err = apiClient.ImageRemove(testutil.GetContext(c), "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
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
_, err = apiClient.ImageRemove(testutil.GetContext(c), "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) {
|
2023-04-03 11:00:29 +00:00
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2023-04-03 11:00:29 +00:00
|
|
|
defer apiClient.Close()
|
2017-05-24 03:56:26 +00:00
|
|
|
|
2023-06-14 09:46:00 +00:00
|
|
|
if testEnv.DaemonInfo.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
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
historydata, err := apiClient.ImageHistory(testutil.GetContext(c), 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"},
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.GetContext(c)
|
2017-02-05 11:39:37 +00:00
|
|
|
for _, te := range tt {
|
2023-07-14 18:02:38 +00:00
|
|
|
res, _, err := request.Post(ctx, 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)
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
res, b, err := request.Get(testutil.GetContext(c), "/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
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
images, err := apiclient.ImageList(testutil.GetContext(c), types.ImageListOptions{})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, len(images) != 0)
|
2024-01-19 11:31:25 +00:00
|
|
|
for _, img := range images {
|
|
|
|
assert.Assert(c, img.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
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
v124Images, err := apiclient.ImageList(testutil.GetContext(c), types.ImageListOptions{})
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, len(v124Images) != 0)
|
2024-01-19 11:31:25 +00:00
|
|
|
for _, img := range v124Images {
|
|
|
|
assert.Assert(c, img.Size != int64(-1))
|
2017-01-12 16:12:39 +00:00
|
|
|
}
|
|
|
|
}
|