2014-11-19 21:19:16 -08:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-31 14:12:10 +02:00
|
|
|
"context"
|
2016-04-12 22:45:42 -04:00
|
|
|
"fmt"
|
2015-05-16 11:04:25 -07:00
|
|
|
"io/ioutil"
|
2014-11-19 21:19:16 -08:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-01-28 22:19:25 +08:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2016-10-31 14:12:10 +02:00
|
|
|
"time"
|
2014-11-19 21:19:16 -08:00
|
|
|
|
2019-07-29 16:59:08 -07:00
|
|
|
"github.com/creack/pty"
|
2017-03-23 18:35:22 +01:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2020-02-07 14:39:24 +01:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-11-19 21:19:16 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// save a repo and try to load it using stdout
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *testing.T) {
|
2015-05-16 16:59:53 +02:00
|
|
|
name := "test-save-and-load-repo-stdout"
|
2015-07-22 02:01:24 +08:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "true")
|
2014-11-19 21:19:16 -08:00
|
|
|
|
|
|
|
repoName := "foobar-save-load-test"
|
2015-11-18 14:20:54 -08:00
|
|
|
before, _ := dockerCmd(c, "commit", name, repoName)
|
2016-01-28 22:19:25 +08:00
|
|
|
before = strings.TrimRight(before, "\n")
|
2014-11-19 21:19:16 -08:00
|
|
|
|
2015-05-16 11:04:25 -07:00
|
|
|
tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar")
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.NilError(c, err)
|
2015-05-16 11:04:25 -07:00
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
2016-12-13 21:21:51 +01:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "save", repoName},
|
|
|
|
Stdout: tmpFile,
|
|
|
|
}).Assert(c, icmd.Success)
|
2014-11-19 21:19:16 -08:00
|
|
|
|
2015-05-16 11:04:25 -07:00
|
|
|
tmpFile, err = os.Open(tmpFile.Name())
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.NilError(c, err)
|
2016-06-25 11:57:21 +08:00
|
|
|
defer tmpFile.Close()
|
2015-05-16 11:04:25 -07:00
|
|
|
|
2014-11-19 21:19:16 -08:00
|
|
|
deleteImages(repoName)
|
|
|
|
|
2017-01-16 16:39:12 +01:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "load"},
|
|
|
|
Stdin: tmpFile,
|
|
|
|
}).Assert(c, icmd.Success)
|
2014-11-19 21:19:16 -08:00
|
|
|
|
2016-01-28 22:19:25 +08:00
|
|
|
after := inspectField(c, repoName, "Id")
|
|
|
|
after = strings.TrimRight(after, "\n")
|
2014-11-19 21:19:16 -08:00
|
|
|
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.Equal(c, after, before, "inspect is not the same after a save / load")
|
2014-11-19 21:19:16 -08:00
|
|
|
|
|
|
|
deleteImages(repoName)
|
|
|
|
|
|
|
|
pty, tty, err := pty.Open()
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.NilError(c, err)
|
2014-11-19 21:19:16 -08:00
|
|
|
cmd := exec.Command(dockerBinary, "save", repoName)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.NilError(c, cmd.Start())
|
|
|
|
assert.ErrorContains(c, cmd.Wait(), "", "did not break writing to a TTY")
|
2014-11-19 21:19:16 -08:00
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
|
|
n, err := pty.Read(buf)
|
2019-11-27 15:36:45 +01:00
|
|
|
assert.NilError(c, err, "could not read tty output")
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.Assert(c, strings.Contains(string(buf[:n]), "cowardly refusing"), "help output is not being yielded")
|
2014-11-19 21:19:16 -08:00
|
|
|
}
|
2016-04-12 22:45:42 -04:00
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestSaveAndLoadWithProgressBar(c *testing.T) {
|
2016-04-12 22:45:42 -04:00
|
|
|
name := "test-load"
|
2017-03-23 18:35:22 +01:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile(`FROM busybox
|
2016-04-12 22:45:42 -04:00
|
|
|
RUN touch aa
|
2017-01-16 11:30:14 +01:00
|
|
|
`))
|
2016-04-12 22:45:42 -04:00
|
|
|
|
|
|
|
tmptar := name + ".tar"
|
|
|
|
dockerCmd(c, "save", "-o", tmptar, name)
|
|
|
|
defer os.Remove(tmptar)
|
|
|
|
|
|
|
|
dockerCmd(c, "rmi", name)
|
|
|
|
dockerCmd(c, "tag", "busybox", name)
|
|
|
|
out, _ := dockerCmd(c, "load", "-i", tmptar)
|
|
|
|
expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name)
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.Assert(c, strings.Contains(out, expected))
|
2016-04-12 22:45:42 -04:00
|
|
|
}
|
2016-10-31 14:12:10 +02:00
|
|
|
|
|
|
|
// fail because load didn't receive data from stdin
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestLoadNoStdinFail(c *testing.T) {
|
2016-10-31 14:12:10 +02:00
|
|
|
pty, tty, err := pty.Open()
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.NilError(c, err)
|
2016-10-31 14:12:10 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, dockerBinary, "load")
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.ErrorContains(c, cmd.Run(), "", "docker-load should fail")
|
2016-10-31 14:12:10 +02:00
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
|
|
|
n, err := pty.Read(buf)
|
2019-04-04 15:23:19 +02:00
|
|
|
assert.NilError(c, err) //could not read tty output
|
|
|
|
assert.Assert(c, strings.Contains(string(buf[:n]), "requested load from stdin, but stdin is empty"))
|
2016-10-31 14:12:10 +02:00
|
|
|
}
|