2015-04-07 21:57:54 -04:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2015-04-20 17:03:56 -04:00
|
|
|
|
"net/http"
|
2015-04-10 17:51:40 -04:00
|
|
|
|
"net/url"
|
2015-04-13 23:02:29 -04:00
|
|
|
|
"strings"
|
2015-04-07 21:57:54 -04:00
|
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
"github.com/go-check/check"
|
2015-04-07 21:57:54 -04:00
|
|
|
|
)
|
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
|
func (s *DockerSuite) TestApiImagesFilter(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
|
testRequires(c, DaemonIsLinux)
|
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
|
|
|
|
}
|
2015-04-29 07:56:45 -04:00
|
|
|
|
type image types.Image
|
2015-04-10 17:51:40 -04:00
|
|
|
|
getImages := func(filter string) []image {
|
|
|
|
|
v := url.Values{}
|
|
|
|
|
v.Set("filter", filter)
|
2015-04-20 17:03:56 -04:00
|
|
|
|
status, b, err := sockRequest("GET", "/images/json?"+v.Encode(), nil)
|
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 07:24:14 -04:00
|
|
|
|
c.Assert(status, check.Equals, http.StatusOK)
|
2015-04-20 17:03:56 -04:00
|
|
|
|
|
2015-04-10 17:51:40 -04:00
|
|
|
|
var images []image
|
|
|
|
|
if err := json.Unmarshal(b, &images); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
|
c.Fatal(err)
|
2015-04-10 17:51:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return images
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errMsg := "incorrect number of matches returned"
|
|
|
|
|
if images := getImages("utest*/*"); len(images[0].RepoTags) != 2 {
|
2015-04-18 12:46:47 -04:00
|
|
|
|
c.Fatal(errMsg)
|
2015-04-10 17:51:40 -04:00
|
|
|
|
}
|
|
|
|
|
if images := getImages("utest"); len(images[0].RepoTags) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
|
c.Fatal(errMsg)
|
2015-04-10 17:51:40 -04:00
|
|
|
|
}
|
|
|
|
|
if images := getImages("utest*"); len(images[0].RepoTags) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
|
c.Fatal(errMsg)
|
2015-04-10 17:51:40 -04:00
|
|
|
|
}
|
|
|
|
|
if images := getImages("*5000*/*"); len(images[0].RepoTags) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
|
c.Fatal(errMsg)
|
2015-04-10 17:51:40 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-13 23:02:29 -04:00
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
|
func (s *DockerSuite) TestApiImagesSaveAndLoad(c *check.C) {
|
|
|
|
|
testRequires(c, Network)
|
2015-08-28 13:36:42 -04:00
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-13 23:02:29 -04:00
|
|
|
|
out, err := buildImage("saveandload", "FROM hello-world\nENV FOO bar", false)
|
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
|
c.Fatal(err)
|
2015-04-13 23:02:29 -04:00
|
|
|
|
}
|
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
2015-04-27 12:33:08 -04:00
|
|
|
|
res, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "")
|
2015-04-20 17:03:56 -04:00
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-04-27 12:33:08 -04:00
|
|
|
|
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
2015-04-20 17:03:56 -04:00
|
|
|
|
|
2015-04-13 23:02:29 -04:00
|
|
|
|
defer body.Close()
|
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
|
dockerCmd(c, "rmi", id)
|
2015-04-13 23:02:29 -04:00
|
|
|
|
|
2015-04-27 12:33:08 -04:00
|
|
|
|
res, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar")
|
2015-04-20 17:03:56 -04:00
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-04-27 12:33:08 -04:00
|
|
|
|
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
2015-04-20 17:03:56 -04:00
|
|
|
|
|
2015-04-13 23:02:29 -04:00
|
|
|
|
defer loadBody.Close()
|
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
|
inspectOut, _ := dockerCmd(c, "inspect", "--format='{{ .Id }}'", id)
|
2015-04-18 12:46:47 -04:00
|
|
|
|
if strings.TrimSpace(string(inspectOut)) != id {
|
|
|
|
|
c.Fatal("load did not work properly")
|
2015-04-13 23:02:29 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-29 07:56:45 -04:00
|
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestApiImagesDelete(c *check.C) {
|
2015-04-30 14:49:28 -04:00
|
|
|
|
testRequires(c, Network)
|
2015-08-28 13:36:42 -04:00
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-29 07:56:45 -04:00
|
|
|
|
name := "test-api-images-delete"
|
|
|
|
|
out, err := buildImage(name, "FROM hello-world\nENV FOO bar", false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
|
dockerCmd(c, "tag", name, "test:tag1")
|
2015-04-29 07:56:45 -04:00
|
|
|
|
|
|
|
|
|
status, _, err := sockRequest("DELETE", "/images/"+id, nil)
|
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 07:24:14 -04:00
|
|
|
|
c.Assert(status, check.Equals, http.StatusConflict)
|
2015-04-29 07:56:45 -04:00
|
|
|
|
|
2015-05-04 03:39:40 -04:00
|
|
|
|
status, _, err = sockRequest("DELETE", "/images/test:noexist", nil)
|
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 07:24:14 -04:00
|
|
|
|
c.Assert(status, check.Equals, http.StatusNotFound) //Status Codes:404 – no such image
|
2015-05-04 03:39:40 -04:00
|
|
|
|
|
2015-04-29 07:56:45 -04:00
|
|
|
|
status, _, err = sockRequest("DELETE", "/images/test:tag1", nil)
|
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 07:24:14 -04:00
|
|
|
|
c.Assert(status, check.Equals, http.StatusOK)
|
2015-04-29 07:56:45 -04:00
|
|
|
|
}
|
2015-05-02 01:30:35 -04:00
|
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestApiImagesHistory(c *check.C) {
|
|
|
|
|
testRequires(c, Network)
|
2015-08-28 13:36:42 -04:00
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-02 01:30:35 -04:00
|
|
|
|
name := "test-api-images-history"
|
|
|
|
|
out, err := buildImage(name, "FROM hello-world\nENV FOO bar", false)
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
|
|
status, body, err := sockRequest("GET", "/images/"+id+"/history", nil)
|
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
c.Assert(status, check.Equals, http.StatusOK)
|
|
|
|
|
|
|
|
|
|
var historydata []types.ImageHistory
|
|
|
|
|
if err = json.Unmarshal(body, &historydata); err != nil {
|
|
|
|
|
c.Fatalf("Error on unmarshal: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Assert(len(historydata), check.Not(check.Equals), 0)
|
|
|
|
|
c.Assert(historydata[0].Tags[0], check.Equals, "test-api-images-history:latest")
|
|
|
|
|
}
|
2015-07-22 05:07:41 -04:00
|
|
|
|
|
|
|
|
|
// #14846
|
|
|
|
|
func (s *DockerSuite) TestApiImagesSearchJSONContentType(c *check.C) {
|
|
|
|
|
testRequires(c, Network)
|
|
|
|
|
|
|
|
|
|
res, b, err := sockRequestRaw("GET", "/images/search?term=test", nil, "application/json")
|
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 07:24:14 -04:00
|
|
|
|
b.Close()
|
2015-07-22 05:07:41 -04:00
|
|
|
|
c.Assert(res.StatusCode, check.Equals, http.StatusOK)
|
|
|
|
|
c.Assert(res.Header.Get("Content-Type"), check.Equals, "application/json")
|
|
|
|
|
}
|