2014-11-20 08:19:16 +03:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-04-06 16:21:18 +03:00
|
|
|
"strings"
|
2014-11-20 08:19:16 +03:00
|
|
|
|
|
|
|
"github.com/docker/docker/vendor/src/github.com/kr/pty"
|
2015-04-18 19:46:47 +03:00
|
|
|
"github.com/go-check/check"
|
2014-11-20 08:19:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// save a repo and try to load it using stdout
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
|
2014-11-20 08:19:16 +03:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("failed to create a container: %s, %v", out, err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:21:18 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-11-20 08:19:16 +03:00
|
|
|
|
|
|
|
repoName := "foobar-save-load-test"
|
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
|
|
|
if out, _, err = runCommandWithOutput(inspectCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("output should've been a container id: %s, %v", out, err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
|
|
|
|
if out, _, err = runCommandWithOutput(commitCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("failed to commit container: %s, %v", out, err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
|
|
|
|
before, _, err := runCommandWithOutput(inspectCmd)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("the repo should exist before saving it: %s, %v", before, err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
saveCmdTemplate := `%v save %v > /tmp/foobar-save-load-test.tar`
|
|
|
|
saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
|
|
|
|
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
|
|
|
|
if out, _, err = runCommandWithOutput(saveCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("failed to save repo: %s, %v", out, err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteImages(repoName)
|
|
|
|
|
|
|
|
loadCmdFinal := `cat /tmp/foobar-save-load-test.tar | docker load`
|
|
|
|
loadCmd := exec.Command("bash", "-c", loadCmdFinal)
|
|
|
|
if out, _, err = runCommandWithOutput(loadCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("failed to load repo: %s, %v", out, err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
|
|
|
|
after, _, err := runCommandWithOutput(inspectCmd)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("the repo should exist after loading it: %s %v", after, err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if before != after {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("inspect is not the same after a save / load")
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteContainer(cleanedContainerID)
|
|
|
|
deleteImages(repoName)
|
|
|
|
|
|
|
|
os.Remove("/tmp/foobar-save-load-test.tar")
|
|
|
|
|
|
|
|
pty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Could not open pty: %v", err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
cmd := exec.Command(dockerBinary, "save", repoName)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("start err: %v", err)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
if err := cmd.Wait(); err == nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal("did not break writing to a TTY")
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
|
|
n, err := pty.Read(buf)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal("could not read tty output")
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Contains(buf[:n], []byte("Cowardly refusing")) {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal("help output is not being yielded", out)
|
2014-11-20 08:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|