github.com/getong/docker@v1.13.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/api/types"
    10  	"github.com/docker/docker/pkg/integration/checker"
    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.ImageSummary
    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  }
   130  
   131  // Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon.
   132  // This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix.
   133  func (s *DockerSuite) TestAPIImagesSizeCompatibility(c *check.C) {
   134  	status, b, err := sockRequest("GET", "/images/json", nil)
   135  	c.Assert(err, checker.IsNil)
   136  	c.Assert(status, checker.Equals, http.StatusOK)
   137  	var images []types.ImageSummary
   138  	err = json.Unmarshal(b, &images)
   139  	c.Assert(err, checker.IsNil)
   140  	c.Assert(len(images), checker.Not(checker.Equals), 0)
   141  	for _, image := range images {
   142  		c.Assert(image.Size, checker.Not(checker.Equals), int64(-1))
   143  	}
   144  
   145  	type v124Image struct {
   146  		ID          string `json:"Id"`
   147  		ParentID    string `json:"ParentId"`
   148  		RepoTags    []string
   149  		RepoDigests []string
   150  		Created     int64
   151  		Size        int64
   152  		VirtualSize int64
   153  		Labels      map[string]string
   154  	}
   155  	status, b, err = sockRequest("GET", "/v1.24/images/json", nil)
   156  	c.Assert(err, checker.IsNil)
   157  	c.Assert(status, checker.Equals, http.StatusOK)
   158  	var v124Images []v124Image
   159  	err = json.Unmarshal(b, &v124Images)
   160  	c.Assert(err, checker.IsNil)
   161  	c.Assert(len(v124Images), checker.Not(checker.Equals), 0)
   162  	for _, image := range v124Images {
   163  		c.Assert(image.Size, checker.Not(checker.Equals), int64(-1))
   164  	}
   165  }