github.com/portworx/docker@v1.12.1/integration-cli/docker_cli_save_load_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"os/exec"
    10  	"strings"
    11  
    12  	"github.com/docker/docker/pkg/integration/checker"
    13  	"github.com/go-check/check"
    14  	"github.com/kr/pty"
    15  )
    16  
    17  // save a repo and try to load it using stdout
    18  func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
    19  	name := "test-save-and-load-repo-stdout"
    20  	dockerCmd(c, "run", "--name", name, "busybox", "true")
    21  
    22  	repoName := "foobar-save-load-test"
    23  	before, _ := dockerCmd(c, "commit", name, repoName)
    24  	before = strings.TrimRight(before, "\n")
    25  
    26  	tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar")
    27  	c.Assert(err, check.IsNil)
    28  	defer os.Remove(tmpFile.Name())
    29  
    30  	saveCmd := exec.Command(dockerBinary, "save", repoName)
    31  	saveCmd.Stdout = tmpFile
    32  
    33  	_, err = runCommand(saveCmd)
    34  	c.Assert(err, check.IsNil)
    35  
    36  	tmpFile, err = os.Open(tmpFile.Name())
    37  	c.Assert(err, check.IsNil)
    38  
    39  	deleteImages(repoName)
    40  
    41  	loadCmd := exec.Command(dockerBinary, "load")
    42  	loadCmd.Stdin = tmpFile
    43  
    44  	out, _, err := runCommandWithOutput(loadCmd)
    45  	c.Assert(err, check.IsNil, check.Commentf(out))
    46  
    47  	after := inspectField(c, repoName, "Id")
    48  	after = strings.TrimRight(after, "\n")
    49  
    50  	c.Assert(after, check.Equals, before) //inspect is not the same after a save / load
    51  
    52  	deleteImages(repoName)
    53  
    54  	pty, tty, err := pty.Open()
    55  	c.Assert(err, check.IsNil)
    56  	cmd := exec.Command(dockerBinary, "save", repoName)
    57  	cmd.Stdin = tty
    58  	cmd.Stdout = tty
    59  	cmd.Stderr = tty
    60  	c.Assert(cmd.Start(), check.IsNil)
    61  	c.Assert(cmd.Wait(), check.NotNil) //did not break writing to a TTY
    62  
    63  	buf := make([]byte, 1024)
    64  
    65  	n, err := pty.Read(buf)
    66  	c.Assert(err, check.IsNil) //could not read tty output
    67  	c.Assert(string(buf[:n]), checker.Contains, "Cowardly refusing", check.Commentf("help output is not being yielded", out))
    68  }
    69  
    70  func (s *DockerSuite) TestSaveAndLoadWithProgressBar(c *check.C) {
    71  	name := "test-load"
    72  	_, err := buildImage(name, `
    73  	FROM busybox
    74  	RUN touch aa
    75  	`, true)
    76  	c.Assert(err, check.IsNil)
    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  }