Browse code

Handle error from GetDevice early

Also more verbose error.

Fixes panic from #7701

Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com>

Alexandr Morozov authored on 2014/08/24 19:09:30
Showing 2 changed files
... ...
@@ -230,10 +230,10 @@ func populateCommand(c *Container, env []string) error {
230 230
 	userSpecifiedDevices := make([]*devices.Device, len(c.hostConfig.Devices))
231 231
 	for i, deviceMapping := range c.hostConfig.Devices {
232 232
 		device, err := devices.GetDevice(deviceMapping.PathOnHost, deviceMapping.CgroupPermissions)
233
-		device.Path = deviceMapping.PathInContainer
234 233
 		if err != nil {
235
-			return fmt.Errorf("error gathering device information while adding custom device %s", err)
234
+			return fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err)
236 235
 		}
236
+		device.Path = deviceMapping.PathInContainer
237 237
 		userSpecifiedDevices[i] = device
238 238
 	}
239 239
 	allowedDevices := append(devices.DefaultAllowedDevices, userSpecifiedDevices...)
... ...
@@ -1630,3 +1630,17 @@ func TestWriteResolvFileAndNotCommit(t *testing.T) {
1630 1630
 
1631 1631
 	logDone("run - write to /etc/resolv.conf and not commited")
1632 1632
 }
1633
+
1634
+func TestRunWithBadDevice(t *testing.T) {
1635
+	name := "baddevice"
1636
+	cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true")
1637
+	out, _, err := runCommandWithOutput(cmd)
1638
+	if err == nil {
1639
+		t.Fatal("Run should fail with bad device")
1640
+	}
1641
+	expected := `"/etc": not a device node`
1642
+	if !strings.Contains(out, expected) {
1643
+		t.Fatalf("Output should contain %q, actual out: %q", expected, out)
1644
+	}
1645
+	logDone("run - error with bad device")
1646
+}