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