github.com/damirazo/docker@v1.9.0/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/api/types"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func (s *DockerSuite) TestApiImagesFilter(c *check.C) {
    14  	testRequires(c, DaemonIsLinux)
    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, check.IsNil)
    27  		c.Assert(status, check.Equals, http.StatusOK)
    28  
    29  		var images []image
    30  		if err := json.Unmarshal(b, &images); err != nil {
    31  			c.Fatal(err)
    32  		}
    33  
    34  		return images
    35  	}
    36  
    37  	errMsg := "incorrect number of matches returned"
    38  	if images := getImages("utest*/*"); len(images[0].RepoTags) != 2 {
    39  		c.Fatal(errMsg)
    40  	}
    41  	if images := getImages("utest"); len(images[0].RepoTags) != 1 {
    42  		c.Fatal(errMsg)
    43  	}
    44  	if images := getImages("utest*"); len(images[0].RepoTags) != 1 {
    45  		c.Fatal(errMsg)
    46  	}
    47  	if images := getImages("*5000*/*"); len(images[0].RepoTags) != 1 {
    48  		c.Fatal(errMsg)
    49  	}
    50  }
    51  
    52  func (s *DockerSuite) TestApiImagesSaveAndLoad(c *check.C) {
    53  	testRequires(c, Network)
    54  	testRequires(c, DaemonIsLinux)
    55  	out, err := buildImage("saveandload", "FROM hello-world\nENV FOO bar", false)
    56  	if err != nil {
    57  		c.Fatal(err)
    58  	}
    59  	id := strings.TrimSpace(out)
    60  
    61  	res, body, err := sockRequestRaw("GET", "/images/"+id+"/get", nil, "")
    62  	c.Assert(err, check.IsNil)
    63  	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
    64  
    65  	defer body.Close()
    66  
    67  	dockerCmd(c, "rmi", id)
    68  
    69  	res, loadBody, err := sockRequestRaw("POST", "/images/load", body, "application/x-tar")
    70  	c.Assert(err, check.IsNil)
    71  	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
    72  
    73  	defer loadBody.Close()
    74  
    75  	inspectOut, _ := dockerCmd(c, "inspect", "--format='{{ .Id }}'", id)
    76  	if strings.TrimSpace(string(inspectOut)) != id {
    77  		c.Fatal("load did not work properly")
    78  	}
    79  }
    80  
    81  func (s *DockerSuite) TestApiImagesDelete(c *check.C) {
    82  	testRequires(c, Network)
    83  	testRequires(c, DaemonIsLinux)
    84  	name := "test-api-images-delete"
    85  	out, err := buildImage(name, "FROM hello-world\nENV FOO bar", false)
    86  	if err != nil {
    87  		c.Fatal(err)
    88  	}
    89  	id := strings.TrimSpace(out)
    90  
    91  	dockerCmd(c, "tag", name, "test:tag1")
    92  
    93  	status, _, err := sockRequest("DELETE", "/images/"+id, nil)
    94  	c.Assert(err, check.IsNil)
    95  	c.Assert(status, check.Equals, http.StatusConflict)
    96  
    97  	status, _, err = sockRequest("DELETE", "/images/test:noexist", nil)
    98  	c.Assert(err, check.IsNil)
    99  	c.Assert(status, check.Equals, http.StatusNotFound) //Status Codes:404 – no such image
   100  
   101  	status, _, err = sockRequest("DELETE", "/images/test:tag1", nil)
   102  	c.Assert(err, check.IsNil)
   103  	c.Assert(status, check.Equals, http.StatusOK)
   104  }
   105  
   106  func (s *DockerSuite) TestApiImagesHistory(c *check.C) {
   107  	testRequires(c, Network)
   108  	testRequires(c, DaemonIsLinux)
   109  	name := "test-api-images-history"
   110  	out, err := buildImage(name, "FROM hello-world\nENV FOO bar", false)
   111  	c.Assert(err, check.IsNil)
   112  
   113  	id := strings.TrimSpace(out)
   114  
   115  	status, body, err := sockRequest("GET", "/images/"+id+"/history", nil)
   116  	c.Assert(err, check.IsNil)
   117  	c.Assert(status, check.Equals, http.StatusOK)
   118  
   119  	var historydata []types.ImageHistory
   120  	if err = json.Unmarshal(body, &historydata); err != nil {
   121  		c.Fatalf("Error on unmarshal: %s", err)
   122  	}
   123  
   124  	c.Assert(len(historydata), check.Not(check.Equals), 0)
   125  	c.Assert(historydata[0].Tags[0], check.Equals, "test-api-images-history:latest")
   126  }
   127  
   128  // #14846
   129  func (s *DockerSuite) TestApiImagesSearchJSONContentType(c *check.C) {
   130  	testRequires(c, Network)
   131  
   132  	res, b, err := sockRequestRaw("GET", "/images/search?term=test", nil, "application/json")
   133  	c.Assert(err, check.IsNil)
   134  	b.Close()
   135  	c.Assert(res.StatusCode, check.Equals, http.StatusOK)
   136  	c.Assert(res.Header.Get("Content-Type"), check.Equals, "application/json")
   137  }