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