From 4426189b35aee4a64bedfb48604ad342a3a5b946 Mon Sep 17 00:00:00 2001 From: Boaz Shuster Date: Mon, 31 Oct 2016 14:12:10 +0200 Subject: [PATCH] Validate docker-load receives a tar file To load an image from a tar file, you can specify the tar file in the -i/--input option: docker load -i image_1.tar or using stdin: docker load < image_1.tar cat image_1.tat | docker load If the image file isn't given the `docker load` command gets stuck. To avoid that, the load makes sure the CLI input is not a terminal or the `--input` option was set. If not then an error message is shown. Signed-off-by: Boaz Shuster --- cli/command/image/load.go | 8 +++++++ .../docker_cli_save_load_unix_test.go | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cli/command/image/load.go b/cli/command/image/load.go index 56145a8a34..4f88faf094 100644 --- a/cli/command/image/load.go +++ b/cli/command/image/load.go @@ -1,6 +1,7 @@ package image import ( + "fmt" "io" "os" @@ -49,6 +50,13 @@ func runLoad(dockerCli *command.DockerCli, opts loadOptions) error { defer file.Close() input = file } + + // To avoid getting stuck, verify that a tar file is given either in + // the input flag or through stdin and if not display an error message and exit. + if opts.input == "" && dockerCli.In().IsTerminal() { + return fmt.Errorf("requested load from stdin, but stdin is empty") + } + if !dockerCli.Out().IsTerminal() { opts.quiet = true } diff --git a/integration-cli/docker_cli_save_load_unix_test.go b/integration-cli/docker_cli_save_load_unix_test.go index 7cc1013432..22445e5bbe 100644 --- a/integration-cli/docker_cli_save_load_unix_test.go +++ b/integration-cli/docker_cli_save_load_unix_test.go @@ -3,11 +3,13 @@ package main import ( + "context" "fmt" "io/ioutil" "os" "os/exec" "strings" + "time" "github.com/docker/docker/pkg/integration/checker" "github.com/go-check/check" @@ -86,3 +88,22 @@ func (s *DockerSuite) TestSaveAndLoadWithProgressBar(c *check.C) { expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name) c.Assert(out, checker.Contains, expected) } + +// fail because load didn't receive data from stdin +func (s *DockerSuite) TestLoadNoStdinFail(c *check.C) { + pty, tty, err := pty.Open() + c.Assert(err, check.IsNil) + 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 + c.Assert(cmd.Run(), check.NotNil) // docker-load should fail + + buf := make([]byte, 1024) + + n, err := pty.Read(buf) + c.Assert(err, check.IsNil) //could not read tty output + c.Assert(string(buf[:n]), checker.Contains, "requested load from stdin, but stdin is empty") +}