Merge pull request #27901 from ripcurld00d/load_stdin_valid

Validate docker-load receives a tar file
This commit is contained in:
Sebastiaan van Stijn 2016-10-31 14:23:18 -07:00 коммит произвёл GitHub
Родитель a13fc3fd36 4426189b35
Коммит c600cfa93c
2 изменённых файлов: 29 добавлений и 0 удалений

Просмотреть файл

@ -1,6 +1,7 @@
package image package image
import ( import (
"fmt"
"io" "io"
"os" "os"
@ -49,6 +50,13 @@ func runLoad(dockerCli *command.DockerCli, opts loadOptions) error {
defer file.Close() defer file.Close()
input = file 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() { if !dockerCli.Out().IsTerminal() {
opts.quiet = true opts.quiet = true
} }

Просмотреть файл

@ -3,11 +3,13 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"time"
"github.com/docker/docker/pkg/integration/checker" "github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check" "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) expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name)
c.Assert(out, checker.Contains, expected) 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")
}