github.com/skanehira/moby@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_save_load_unix_test.go (about) 1 // +build !windows 2 3 package main 4 5 import ( 6 "context" 7 "fmt" 8 "io/ioutil" 9 "os" 10 "os/exec" 11 "strings" 12 "time" 13 14 "github.com/docker/docker/integration-cli/checker" 15 "github.com/docker/docker/integration-cli/cli/build" 16 "github.com/go-check/check" 17 "github.com/gotestyourself/gotestyourself/icmd" 18 "github.com/kr/pty" 19 ) 20 21 // save a repo and try to load it using stdout 22 func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) { 23 name := "test-save-and-load-repo-stdout" 24 dockerCmd(c, "run", "--name", name, "busybox", "true") 25 26 repoName := "foobar-save-load-test" 27 before, _ := dockerCmd(c, "commit", name, repoName) 28 before = strings.TrimRight(before, "\n") 29 30 tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar") 31 c.Assert(err, check.IsNil) 32 defer os.Remove(tmpFile.Name()) 33 34 icmd.RunCmd(icmd.Cmd{ 35 Command: []string{dockerBinary, "save", repoName}, 36 Stdout: tmpFile, 37 }).Assert(c, icmd.Success) 38 39 tmpFile, err = os.Open(tmpFile.Name()) 40 c.Assert(err, check.IsNil) 41 defer tmpFile.Close() 42 43 deleteImages(repoName) 44 45 icmd.RunCmd(icmd.Cmd{ 46 Command: []string{dockerBinary, "load"}, 47 Stdin: tmpFile, 48 }).Assert(c, icmd.Success) 49 50 after := inspectField(c, repoName, "Id") 51 after = strings.TrimRight(after, "\n") 52 53 c.Assert(after, check.Equals, before) //inspect is not the same after a save / load 54 55 deleteImages(repoName) 56 57 pty, tty, err := pty.Open() 58 c.Assert(err, check.IsNil) 59 cmd := exec.Command(dockerBinary, "save", repoName) 60 cmd.Stdin = tty 61 cmd.Stdout = tty 62 cmd.Stderr = tty 63 c.Assert(cmd.Start(), check.IsNil) 64 c.Assert(cmd.Wait(), check.NotNil) //did not break writing to a TTY 65 66 buf := make([]byte, 1024) 67 68 n, err := pty.Read(buf) 69 c.Assert(err, check.IsNil) //could not read tty output 70 c.Assert(string(buf[:n]), checker.Contains, "cowardly refusing", check.Commentf("help output is not being yielded")) 71 } 72 73 func (s *DockerSuite) TestSaveAndLoadWithProgressBar(c *check.C) { 74 name := "test-load" 75 buildImageSuccessfully(c, name, build.WithDockerfile(`FROM busybox 76 RUN touch aa 77 `)) 78 79 tmptar := name + ".tar" 80 dockerCmd(c, "save", "-o", tmptar, name) 81 defer os.Remove(tmptar) 82 83 dockerCmd(c, "rmi", name) 84 dockerCmd(c, "tag", "busybox", name) 85 out, _ := dockerCmd(c, "load", "-i", tmptar) 86 expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name) 87 c.Assert(out, checker.Contains, expected) 88 } 89 90 // fail because load didn't receive data from stdin 91 func (s *DockerSuite) TestLoadNoStdinFail(c *check.C) { 92 pty, tty, err := pty.Open() 93 c.Assert(err, check.IsNil) 94 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 95 defer cancel() 96 cmd := exec.CommandContext(ctx, dockerBinary, "load") 97 cmd.Stdin = tty 98 cmd.Stdout = tty 99 cmd.Stderr = tty 100 c.Assert(cmd.Run(), check.NotNil) // docker-load should fail 101 102 buf := make([]byte, 1024) 103 104 n, err := pty.Read(buf) 105 c.Assert(err, check.IsNil) //could not read tty output 106 c.Assert(string(buf[:n]), checker.Contains, "requested load from stdin, but stdin is empty") 107 }