github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/integration-cli/docker_api_images_test.go (about) 1 package main 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "runtime" 8 "strconv" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/api/types/filters" 14 "github.com/docker/docker/client" 15 "github.com/docker/docker/integration-cli/cli" 16 "github.com/docker/docker/integration-cli/cli/build" 17 "github.com/docker/docker/internal/test/request" 18 "github.com/docker/docker/pkg/parsers/kernel" 19 "gotest.tools/assert" 20 ) 21 22 func (s *DockerSuite) TestAPIImagesFilter(c *testing.T) { 23 cli, err := client.NewClientWithOpts(client.FromEnv) 24 assert.NilError(c, err) 25 defer cli.Close() 26 27 name := "utest:tag1" 28 name2 := "utest/docker:tag2" 29 name3 := "utest:5000/docker:tag3" 30 for _, n := range []string{name, name2, name3} { 31 dockerCmd(c, "tag", "busybox", n) 32 } 33 getImages := func(filter string) []types.ImageSummary { 34 filters := filters.NewArgs() 35 filters.Add("reference", filter) 36 options := types.ImageListOptions{ 37 All: false, 38 Filters: filters, 39 } 40 images, err := cli.ImageList(context.Background(), options) 41 assert.NilError(c, err) 42 43 return images 44 } 45 46 //incorrect number of matches returned 47 images := getImages("utest*/*") 48 assert.Equal(c, len(images[0].RepoTags), 2) 49 50 images = getImages("utest") 51 assert.Equal(c, len(images[0].RepoTags), 1) 52 53 images = getImages("utest*") 54 assert.Equal(c, len(images[0].RepoTags), 1) 55 56 images = getImages("*5000*/*") 57 assert.Equal(c, len(images[0].RepoTags), 1) 58 } 59 60 func (s *DockerSuite) TestAPIImagesSaveAndLoad(c *testing.T) { 61 if runtime.GOOS == "windows" { 62 v, err := kernel.GetKernelVersion() 63 assert.NilError(c, err) 64 build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0]) 65 if build <= 16299 { 66 c.Skip("Temporarily disabled on RS3 and older because they are too slow. See #39909") 67 } 68 } 69 70 testRequires(c, Network) 71 buildImageSuccessfully(c, "saveandload", build.WithDockerfile("FROM busybox\nENV FOO bar")) 72 id := getIDByName(c, "saveandload") 73 74 res, body, err := request.Get("/images/" + id + "/get") 75 assert.NilError(c, err) 76 defer body.Close() 77 assert.Equal(c, res.StatusCode, http.StatusOK) 78 79 dockerCmd(c, "rmi", id) 80 81 res, loadBody, err := request.Post("/images/load", request.RawContent(body), request.ContentType("application/x-tar")) 82 assert.NilError(c, err) 83 defer loadBody.Close() 84 assert.Equal(c, res.StatusCode, http.StatusOK) 85 86 inspectOut := cli.InspectCmd(c, id, cli.Format(".Id")).Combined() 87 assert.Equal(c, strings.TrimSpace(inspectOut), id, "load did not work properly") 88 } 89 90 func (s *DockerSuite) TestAPIImagesDelete(c *testing.T) { 91 cli, err := client.NewClientWithOpts(client.FromEnv) 92 assert.NilError(c, err) 93 defer cli.Close() 94 95 if testEnv.OSType != "windows" { 96 testRequires(c, Network) 97 } 98 name := "test-api-images-delete" 99 buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar")) 100 id := getIDByName(c, name) 101 102 dockerCmd(c, "tag", name, "test:tag1") 103 104 _, err = cli.ImageRemove(context.Background(), id, types.ImageRemoveOptions{}) 105 assert.ErrorContains(c, err, "unable to delete") 106 107 _, err = cli.ImageRemove(context.Background(), "test:noexist", types.ImageRemoveOptions{}) 108 assert.ErrorContains(c, err, "No such image") 109 110 _, err = cli.ImageRemove(context.Background(), "test:tag1", types.ImageRemoveOptions{}) 111 assert.NilError(c, err) 112 } 113 114 func (s *DockerSuite) TestAPIImagesHistory(c *testing.T) { 115 cli, err := client.NewClientWithOpts(client.FromEnv) 116 assert.NilError(c, err) 117 defer cli.Close() 118 119 if testEnv.OSType != "windows" { 120 testRequires(c, Network) 121 } 122 name := "test-api-images-history" 123 buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar")) 124 id := getIDByName(c, name) 125 126 historydata, err := cli.ImageHistory(context.Background(), id) 127 assert.NilError(c, err) 128 129 assert.Assert(c, len(historydata) != 0) 130 var found bool 131 for _, tag := range historydata[0].Tags { 132 if tag == "test-api-images-history:latest" { 133 found = true 134 break 135 } 136 } 137 assert.Assert(c, found) 138 } 139 140 func (s *DockerSuite) TestAPIImagesImportBadSrc(c *testing.T) { 141 if runtime.GOOS == "windows" { 142 v, err := kernel.GetKernelVersion() 143 assert.NilError(c, err) 144 build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0]) 145 if build == 16299 { 146 c.Skip("Temporarily disabled on RS3 builds") 147 } 148 } 149 150 testRequires(c, Network, testEnv.IsLocalDaemon) 151 152 server := httptest.NewServer(http.NewServeMux()) 153 defer server.Close() 154 155 tt := []struct { 156 statusExp int 157 fromSrc string 158 }{ 159 {http.StatusNotFound, server.URL + "/nofile.tar"}, 160 {http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "/nofile.tar"}, 161 {http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "%2Fdata%2Ffile.tar"}, 162 {http.StatusInternalServerError, "%2Fdata%2Ffile.tar"}, 163 } 164 165 for _, te := range tt { 166 res, _, err := request.Post(strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), request.JSON) 167 assert.NilError(c, err) 168 assert.Equal(c, res.StatusCode, te.statusExp) 169 assert.Equal(c, res.Header.Get("Content-Type"), "application/json") 170 } 171 172 } 173 174 // #14846 175 func (s *DockerSuite) TestAPIImagesSearchJSONContentType(c *testing.T) { 176 testRequires(c, Network) 177 178 res, b, err := request.Get("/images/search?term=test", request.JSON) 179 assert.NilError(c, err) 180 b.Close() 181 assert.Equal(c, res.StatusCode, http.StatusOK) 182 assert.Equal(c, res.Header.Get("Content-Type"), "application/json") 183 } 184 185 // Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon. 186 // This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix. 187 func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *testing.T) { 188 apiclient := testEnv.APIClient() 189 defer apiclient.Close() 190 191 images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{}) 192 assert.NilError(c, err) 193 assert.Assert(c, len(images) != 0) 194 for _, image := range images { 195 assert.Assert(c, image.Size != int64(-1)) 196 } 197 198 apiclient, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("v1.24")) 199 assert.NilError(c, err) 200 defer apiclient.Close() 201 202 v124Images, err := apiclient.ImageList(context.Background(), types.ImageListOptions{}) 203 assert.NilError(c, err) 204 assert.Assert(c, len(v124Images) != 0) 205 for _, image := range v124Images { 206 assert.Assert(c, image.Size != int64(-1)) 207 } 208 }