github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/integration-cli/docker_api_images_test.go (about)

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/api/types/image"
    10  	"github.com/docker/docker/client"
    11  	"github.com/docker/docker/integration-cli/cli"
    12  	"github.com/docker/docker/integration-cli/cli/build"
    13  	"github.com/docker/docker/testutil"
    14  	"github.com/docker/docker/testutil/request"
    15  	"gotest.tools/v3/assert"
    16  )
    17  
    18  func (s *DockerAPISuite) TestAPIImagesSaveAndLoad(c *testing.T) {
    19  	testRequires(c, Network)
    20  	buildImageSuccessfully(c, "saveandload", build.WithDockerfile("FROM busybox\nENV FOO bar"))
    21  	id := getIDByName(c, "saveandload")
    22  
    23  	ctx := testutil.GetContext(c)
    24  	res, body, err := request.Get(ctx, "/images/"+id+"/get")
    25  	assert.NilError(c, err)
    26  	defer body.Close()
    27  	assert.Equal(c, res.StatusCode, http.StatusOK)
    28  
    29  	cli.DockerCmd(c, "rmi", id)
    30  
    31  	res, loadBody, err := request.Post(ctx, "/images/load", request.RawContent(body), request.ContentType("application/x-tar"))
    32  	assert.NilError(c, err)
    33  	defer loadBody.Close()
    34  	assert.Equal(c, res.StatusCode, http.StatusOK)
    35  
    36  	inspectOut := cli.InspectCmd(c, id, cli.Format(".Id")).Combined()
    37  	assert.Equal(c, strings.TrimSpace(inspectOut), id, "load did not work properly")
    38  }
    39  
    40  func (s *DockerAPISuite) TestAPIImagesDelete(c *testing.T) {
    41  	apiClient, err := client.NewClientWithOpts(client.FromEnv)
    42  	assert.NilError(c, err)
    43  	defer apiClient.Close()
    44  
    45  	if testEnv.DaemonInfo.OSType != "windows" {
    46  		testRequires(c, Network)
    47  	}
    48  	name := "test-api-images-delete"
    49  	buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
    50  	id := getIDByName(c, name)
    51  
    52  	cli.DockerCmd(c, "tag", name, "test:tag1")
    53  
    54  	_, err = apiClient.ImageRemove(testutil.GetContext(c), id, image.RemoveOptions{})
    55  	assert.ErrorContains(c, err, "unable to delete")
    56  
    57  	_, err = apiClient.ImageRemove(testutil.GetContext(c), "test:noexist", image.RemoveOptions{})
    58  	assert.ErrorContains(c, err, "No such image")
    59  
    60  	_, err = apiClient.ImageRemove(testutil.GetContext(c), "test:tag1", image.RemoveOptions{})
    61  	assert.NilError(c, err)
    62  }
    63  
    64  func (s *DockerAPISuite) TestAPIImagesHistory(c *testing.T) {
    65  	apiClient, err := client.NewClientWithOpts(client.FromEnv)
    66  	assert.NilError(c, err)
    67  	defer apiClient.Close()
    68  
    69  	if testEnv.DaemonInfo.OSType != "windows" {
    70  		testRequires(c, Network)
    71  	}
    72  	name := "test-api-images-history"
    73  	buildImageSuccessfully(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
    74  	id := getIDByName(c, name)
    75  
    76  	historydata, err := apiClient.ImageHistory(testutil.GetContext(c), id)
    77  	assert.NilError(c, err)
    78  
    79  	assert.Assert(c, len(historydata) != 0)
    80  	var found bool
    81  	for _, tag := range historydata[0].Tags {
    82  		if tag == "test-api-images-history:latest" {
    83  			found = true
    84  			break
    85  		}
    86  	}
    87  	assert.Assert(c, found)
    88  }
    89  
    90  func (s *DockerAPISuite) TestAPIImagesImportBadSrc(c *testing.T) {
    91  	testRequires(c, Network, testEnv.IsLocalDaemon)
    92  
    93  	server := httptest.NewServer(http.NewServeMux())
    94  	defer server.Close()
    95  
    96  	tt := []struct {
    97  		statusExp int
    98  		fromSrc   string
    99  	}{
   100  		{http.StatusNotFound, server.URL + "/nofile.tar"},
   101  		{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "/nofile.tar"},
   102  		{http.StatusNotFound, strings.TrimPrefix(server.URL, "http://") + "%2Fdata%2Ffile.tar"},
   103  		{http.StatusInternalServerError, "%2Fdata%2Ffile.tar"},
   104  	}
   105  
   106  	ctx := testutil.GetContext(c)
   107  	for _, te := range tt {
   108  		res, _, err := request.Post(ctx, strings.Join([]string{"/images/create?fromSrc=", te.fromSrc}, ""), request.JSON)
   109  		assert.NilError(c, err)
   110  		assert.Equal(c, res.StatusCode, te.statusExp)
   111  		assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
   112  	}
   113  }
   114  
   115  // #14846
   116  func (s *DockerAPISuite) TestAPIImagesSearchJSONContentType(c *testing.T) {
   117  	testRequires(c, Network)
   118  
   119  	res, b, err := request.Get(testutil.GetContext(c), "/images/search?term=test", request.JSON)
   120  	assert.NilError(c, err)
   121  	b.Close()
   122  	assert.Equal(c, res.StatusCode, http.StatusOK)
   123  	assert.Equal(c, res.Header.Get("Content-Type"), "application/json")
   124  }
   125  
   126  // Test case for 30027: image size reported as -1 in v1.12 client against v1.13 daemon.
   127  // This test checks to make sure both v1.12 and v1.13 client against v1.13 daemon get correct `Size` after the fix.
   128  func (s *DockerAPISuite) TestAPIImagesSizeCompatibility(c *testing.T) {
   129  	apiclient := testEnv.APIClient()
   130  	defer apiclient.Close()
   131  
   132  	images, err := apiclient.ImageList(testutil.GetContext(c), image.ListOptions{})
   133  	assert.NilError(c, err)
   134  	assert.Assert(c, len(images) != 0)
   135  	for _, img := range images {
   136  		assert.Assert(c, img.Size != int64(-1))
   137  	}
   138  
   139  	apiclient, err = client.NewClientWithOpts(client.FromEnv, client.WithVersion("v1.24"))
   140  	assert.NilError(c, err)
   141  	defer apiclient.Close()
   142  
   143  	v124Images, err := apiclient.ImageList(testutil.GetContext(c), image.ListOptions{})
   144  	assert.NilError(c, err)
   145  	assert.Assert(c, len(v124Images) != 0)
   146  	for _, img := range v124Images {
   147  		assert.Assert(c, img.Size != int64(-1))
   148  	}
   149  }