github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration-cli/docker_cli_import_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bufio"
     5  	"compress/gzip"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"regexp"
    10  	"strings"
    11  
    12  	"github.com/docker/docker/integration-cli/checker"
    13  	"github.com/docker/docker/integration-cli/cli"
    14  	"github.com/go-check/check"
    15  	"gotest.tools/assert"
    16  	"gotest.tools/icmd"
    17  )
    18  
    19  func (s *DockerSuite) TestImportDisplay(c *check.C) {
    20  	testRequires(c, DaemonIsLinux)
    21  	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
    22  	cleanedContainerID := strings.TrimSpace(out)
    23  
    24  	out, err := RunCommandPipelineWithOutput(
    25  		exec.Command(dockerBinary, "export", cleanedContainerID),
    26  		exec.Command(dockerBinary, "import", "-"),
    27  	)
    28  	assert.NilError(c, err)
    29  
    30  	assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
    31  
    32  	image := strings.TrimSpace(out)
    33  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    34  	assert.Equal(c, out, "", "command output should've been nothing.")
    35  }
    36  
    37  func (s *DockerSuite) TestImportBadURL(c *check.C) {
    38  	out, _, err := dockerCmdWithError("import", "http://nourl/bad")
    39  	c.Assert(err, checker.NotNil, check.Commentf("import was supposed to fail but didn't"))
    40  	// Depending on your system you can get either of these errors
    41  	if !strings.Contains(out, "dial tcp") &&
    42  		!strings.Contains(out, "ApplyLayer exit status 1 stdout:  stderr: archive/tar: invalid tar header") &&
    43  		!strings.Contains(out, "Error processing tar file") {
    44  		c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out)
    45  	}
    46  }
    47  
    48  func (s *DockerSuite) TestImportFile(c *check.C) {
    49  	testRequires(c, DaemonIsLinux)
    50  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    51  
    52  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    53  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    54  	defer os.Remove(temporaryFile.Name())
    55  
    56  	icmd.RunCmd(icmd.Cmd{
    57  		Command: []string{dockerBinary, "export", "test-import"},
    58  		Stdout:  bufio.NewWriter(temporaryFile),
    59  	}).Assert(c, icmd.Success)
    60  
    61  	out, _ := dockerCmd(c, "import", temporaryFile.Name())
    62  	assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
    63  	image := strings.TrimSpace(out)
    64  
    65  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    66  	assert.Equal(c, out, "", "command output should've been nothing.")
    67  }
    68  
    69  func (s *DockerSuite) TestImportGzipped(c *check.C) {
    70  	testRequires(c, DaemonIsLinux)
    71  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    72  
    73  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    74  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    75  	defer os.Remove(temporaryFile.Name())
    76  
    77  	w := gzip.NewWriter(temporaryFile)
    78  	icmd.RunCmd(icmd.Cmd{
    79  		Command: []string{dockerBinary, "export", "test-import"},
    80  		Stdout:  w,
    81  	}).Assert(c, icmd.Success)
    82  	c.Assert(w.Close(), checker.IsNil, check.Commentf("failed to close gzip writer"))
    83  	temporaryFile.Close()
    84  	out, _ := dockerCmd(c, "import", temporaryFile.Name())
    85  	assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
    86  	image := strings.TrimSpace(out)
    87  
    88  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
    89  	assert.Equal(c, out, "", "command output should've been nothing.")
    90  }
    91  
    92  func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
    93  	testRequires(c, DaemonIsLinux)
    94  	dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
    95  
    96  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
    97  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
    98  	defer os.Remove(temporaryFile.Name())
    99  
   100  	icmd.RunCmd(icmd.Cmd{
   101  		Command: []string{dockerBinary, "export", "test-import"},
   102  		Stdout:  bufio.NewWriter(temporaryFile),
   103  	}).Assert(c, icmd.Success)
   104  
   105  	message := "Testing commit message"
   106  	out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
   107  	assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
   108  	image := strings.TrimSpace(out)
   109  
   110  	out, _ = dockerCmd(c, "history", image)
   111  	split := strings.Split(out, "\n")
   112  
   113  	c.Assert(split, checker.HasLen, 3, check.Commentf("expected 3 lines from image history"))
   114  	r := regexp.MustCompile("[\\s]{2,}")
   115  	split = r.Split(split[1], -1)
   116  
   117  	c.Assert(message, checker.Equals, split[3], check.Commentf("didn't get expected value in commit message"))
   118  
   119  	out, _ = dockerCmd(c, "run", "--rm", image, "true")
   120  	assert.Equal(c, out, "", "command output should've been nothing")
   121  }
   122  
   123  func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
   124  	_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
   125  	c.Assert(err, checker.NotNil, check.Commentf("import non-existing file must failed"))
   126  }
   127  
   128  func (s *DockerSuite) TestImportWithQuotedChanges(c *check.C) {
   129  	testRequires(c, DaemonIsLinux)
   130  	cli.DockerCmd(c, "run", "--name", "test-import", "busybox", "true")
   131  
   132  	temporaryFile, err := ioutil.TempFile("", "exportImportTest")
   133  	c.Assert(err, checker.IsNil, check.Commentf("failed to create temporary file"))
   134  	defer os.Remove(temporaryFile.Name())
   135  
   136  	cli.Docker(cli.Args("export", "test-import"), cli.WithStdout(bufio.NewWriter(temporaryFile))).Assert(c, icmd.Success)
   137  
   138  	result := cli.DockerCmd(c, "import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name())
   139  	image := strings.TrimSpace(result.Stdout())
   140  
   141  	result = cli.DockerCmd(c, "run", "--rm", image, "true")
   142  	result.Assert(c, icmd.Expected{Out: icmd.None})
   143  }