github.com/sevki/docker@v1.7.1/integration-cli/docker_cli_save_load_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"bytes"
     7  	"io/ioutil"
     8  	"os"
     9  	"os/exec"
    10  
    11  	"github.com/docker/docker/vendor/src/github.com/kr/pty"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  // save a repo and try to load it using stdout
    16  func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
    17  	name := "test-save-and-load-repo-stdout"
    18  	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
    19  	out, _, err := runCommandWithOutput(runCmd)
    20  	if err != nil {
    21  		c.Fatalf("failed to create a container: %s, %v", out, err)
    22  	}
    23  
    24  	repoName := "foobar-save-load-test"
    25  
    26  	commitCmd := exec.Command(dockerBinary, "commit", name, repoName)
    27  	if out, _, err = runCommandWithOutput(commitCmd); err != nil {
    28  		c.Fatalf("failed to commit container: %s, %v", out, err)
    29  	}
    30  
    31  	inspectCmd := exec.Command(dockerBinary, "inspect", repoName)
    32  	before, _, err := runCommandWithOutput(inspectCmd)
    33  	if err != nil {
    34  		c.Fatalf("the repo should exist before saving it: %s, %v", before, err)
    35  	}
    36  
    37  	tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar")
    38  	c.Assert(err, check.IsNil)
    39  	defer os.Remove(tmpFile.Name())
    40  
    41  	saveCmd := exec.Command(dockerBinary, "save", repoName)
    42  	saveCmd.Stdout = tmpFile
    43  
    44  	if _, err = runCommand(saveCmd); err != nil {
    45  		c.Fatalf("failed to save repo: %v", err)
    46  	}
    47  
    48  	tmpFile, err = os.Open(tmpFile.Name())
    49  	c.Assert(err, check.IsNil)
    50  
    51  	deleteImages(repoName)
    52  
    53  	loadCmd := exec.Command(dockerBinary, "load")
    54  	loadCmd.Stdin = tmpFile
    55  
    56  	if out, _, err = runCommandWithOutput(loadCmd); err != nil {
    57  		c.Fatalf("failed to load repo: %s, %v", out, err)
    58  	}
    59  
    60  	inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
    61  	after, _, err := runCommandWithOutput(inspectCmd)
    62  	if err != nil {
    63  		c.Fatalf("the repo should exist after loading it: %s %v", after, err)
    64  	}
    65  
    66  	if before != after {
    67  		c.Fatalf("inspect is not the same after a save / load")
    68  	}
    69  
    70  	deleteImages(repoName)
    71  
    72  	pty, tty, err := pty.Open()
    73  	if err != nil {
    74  		c.Fatalf("Could not open pty: %v", err)
    75  	}
    76  	cmd := exec.Command(dockerBinary, "save", repoName)
    77  	cmd.Stdin = tty
    78  	cmd.Stdout = tty
    79  	cmd.Stderr = tty
    80  	if err := cmd.Start(); err != nil {
    81  		c.Fatalf("start err: %v", err)
    82  	}
    83  	if err := cmd.Wait(); err == nil {
    84  		c.Fatal("did not break writing to a TTY")
    85  	}
    86  
    87  	buf := make([]byte, 1024)
    88  
    89  	n, err := pty.Read(buf)
    90  	if err != nil {
    91  		c.Fatal("could not read tty output")
    92  	}
    93  
    94  	if !bytes.Contains(buf[:n], []byte("Cowardly refusing")) {
    95  		c.Fatal("help output is not being yielded", out)
    96  	}
    97  
    98  }