github.com/portworx/docker@v1.12.1/integration-cli/docker_api_images_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/url" 7 "strings" 8 9 "github.com/docker/docker/pkg/integration/checker" 10 "github.com/docker/engine-api/types" 11 "github.com/go-check/check" 12 ) 13 14 func (s *DockerSuite) TestApiImagesFilter(c *check.C) { 15 name := "utest:tag1" 16 name2 := "utest/docker:tag2" 17 name3 := "utest:5000/docker:tag3" 18 for _, n := range []string{name, name2, name3} { 19 dockerCmd(c, "tag", "busybox", n) 20 } 21 type image types.Image 22 getImages := func(filter string) []image { 23 v := url.Values{} 24 v.Set("filter", filter) 25 status, b, err := sockRequest("GET", "/images/json?"+v.Encode(), nil) 26 c.Assert(err, checker.IsNil) 27 c.Assert(status, checker.Equals, http.StatusOK) 28 29 var images []image 30 err = json.Unmarshal(b, &images) 31 c.Assert(err, checker.IsNil) 32 33 return images 34 } 35 36 //incorrect number of matches returned 37 images := getImages("utest*/*") 38 c.Assert(images[0].RepoTags, checker.HasLen, 2) 39 40 images = getImages("utest") 41 c.Assert(images[0].RepoTags, checker.HasLen, 1) 42 43 images = getImages("utest*") 44 c.Assert(images[0].RepoTags, checker.HasLen, 1) 45 46 images = getImages("*5000*/*") 47 c.Assert(images[0].RepoTags, checker.HasLen, 1) 48 } 49 50 func (s *DockerSuite) TestApiImagesSaveAndLoad(c *check.C) { 51 // TODO Windows to Windows CI: Investigate further why this test fails. 52 testRequires(c, Network) 53 testRequires(c, DaemonIsLinux) 54 out, err := buildImage("saveandload", "FROM busybox\nENV FOO bar", false) 55 c.Assert(err, checker.IsNil) 56 id := strings.TrimSpace(out) 57 58 res, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "") 59 c.Assert(err, checker.IsNil) 60 defer body.Close() 61 c.Assert(res.StatusCode, checker.Equals, http.StatusOK) 62 63 dockerCmd(c, "rmi", id) 64 65 res, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar") 66 c.Assert(err, checker.IsNil) 67 defer loadBody.Close() 68 c.Assert(res.StatusCode, checker.Equals, http.StatusOK) 69 70 inspectOut := inspectField(c, id, "Id") 71 c.Assert(strings.TrimSpace(string(inspectOut)), checker.Equals, id, check.Commentf("load did not work properly")) 72 } 73 74 func (s *DockerSuite) TestApiImagesDelete(c *check.C) { 75 if daemonPlatform != "windows" { 76 testRequires(c, Network) 77 } 78 name := "test-api-images-delete" 79 out, err := buildImage(name, "FROM busybox\nENV FOO bar", false) 80 c.Assert(err, checker.IsNil) 81 id := strings.TrimSpace(out) 82 83 dockerCmd(c, "tag", name, "test:tag1") 84 85 status, _, err := sockRequest("DELETE", "/images/"+id, nil) 86 c.Assert(err, checker.IsNil) 87 c.Assert(status, checker.Equals, http.StatusConflict) 88 89 status, _, err = sockRequest("DELETE", "/images/test:noexist", nil) 90 c.Assert(err, checker.IsNil) 91 c.Assert(status, checker.Equals, http.StatusNotFound) //Status Codes:404 – no such image 92 93 status, _, err = sockRequest("DELETE", "/images/test:tag1", nil) 94 c.Assert(err, checker.IsNil) 95 c.Assert(status, checker.Equals, http.StatusOK) 96 } 97 98 func (s *DockerSuite) TestApiImagesHistory(c *check.C) { 99 if daemonPlatform != "windows" { 100 testRequires(c, Network) 101 } 102 name := "test-api-images-history" 103 out, err := buildImage(name, "FROM busybox\nENV FOO bar", false) 104 c.Assert(err, checker.IsNil) 105 106 id := strings.TrimSpace(out) 107 108 status, body, err := sockRequest("GET", "/images/"+id+"/history", nil) 109 c.Assert(err, checker.IsNil) 110 c.Assert(status, checker.Equals, http.StatusOK) 111 112 var historydata []types.ImageHistory 113 err = json.Unmarshal(body, &historydata) 114 c.Assert(err, checker.IsNil, check.Commentf("Error on unmarshal")) 115 116 c.Assert(historydata, checker.Not(checker.HasLen), 0) 117 c.Assert(historydata[0].Tags[0], checker.Equals, "test-api-images-history:latest") 118 } 119 120 // #14846 121 func (s *DockerSuite) TestApiImagesSearchJSONContentType(c *check.C) { 122 testRequires(c, Network) 123 124 res, b, err := sockRequestRaw("GET", "/images/search?term=test", nil, "application/json") 125 c.Assert(err, check.IsNil) 126 b.Close() 127 c.Assert(res.StatusCode, checker.Equals, http.StatusOK) 128 c.Assert(res.Header.Get("Content-Type"), checker.Equals, "application/json") 129 }