github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/integration-cli/docker_cli_save_load_unix_test.go (about)

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