From 17b95ecb08f1705bd74d6c94c8bcfd4c87ccfca6 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Sun, 24 Aug 2014 14:09:30 +0400 Subject: [PATCH] Handle error from GetDevice early Also more verbose error. Fixes panic from #7701 Signed-off-by: Alexandr Morozov --- daemon/container.go | 4 ++-- integration-cli/docker_cli_run_test.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/daemon/container.go b/daemon/container.go index df6bd66190..0d7023c688 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -230,10 +230,10 @@ func populateCommand(c *Container, env []string) error { userSpecifiedDevices := make([]*devices.Device, len(c.hostConfig.Devices)) for i, deviceMapping := range c.hostConfig.Devices { device, err := devices.GetDevice(deviceMapping.PathOnHost, deviceMapping.CgroupPermissions) - device.Path = deviceMapping.PathInContainer if err != nil { - return fmt.Errorf("error gathering device information while adding custom device %s", err) + return fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err) } + device.Path = deviceMapping.PathInContainer userSpecifiedDevices[i] = device } allowedDevices := append(devices.DefaultAllowedDevices, userSpecifiedDevices...) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index b95fd86d2b..1021e152ab 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -1630,3 +1630,17 @@ func TestWriteResolvFileAndNotCommit(t *testing.T) { logDone("run - write to /etc/resolv.conf and not commited") } + +func TestRunWithBadDevice(t *testing.T) { + name := "baddevice" + cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true") + out, _, err := runCommandWithOutput(cmd) + if err == nil { + t.Fatal("Run should fail with bad device") + } + expected := `"/etc": not a device node` + if !strings.Contains(out, expected) { + t.Fatalf("Output should contain %q, actual out: %q", expected, out) + } + logDone("run - error with bad device") +}