Skip to content

Commit

Permalink
fix validation of non-existing bind-mount source
Browse files Browse the repository at this point in the history
Unlike `docker run -v..`, `docker service create --mount`
does not allow bind-mounting non-existing host paths.

This adds validation for the specified `source`, and
produces an error if the path is not found on the
host.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Aug 8, 2016
1 parent 2684459 commit 84d5ab9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions daemon/cluster/executor/container/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package container

import (
"fmt"
"os"
"path/filepath"

"github.com/docker/swarmkit/api"
Expand All @@ -23,6 +24,9 @@ func validateMounts(mounts []api.Mount) error {
if !filepath.IsAbs(mount.Source) {
return fmt.Errorf("invalid bind mount source, must be an absolute path: %s", mount.Source)
}
if _, err := os.Stat(mount.Source); os.IsNotExist(err) {
return fmt.Errorf("invalid bind mount source, source path not found: %s", mount.Source)
}
case api.MountTypeVolume:
if filepath.IsAbs(mount.Source) {
return fmt.Errorf("invalid volume mount source, must not be an absolute path: %s", mount.Source)
Expand Down
27 changes: 25 additions & 2 deletions daemon/cluster/executor/container/validate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package container

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -37,10 +39,25 @@ func TestControllerValidateMountBind(t *testing.T) {
t.Fatalf("expected error, got: %v", err)
}

// with non-existing source
if _, err := newTestControllerWithMount(api.Mount{
Type: api.MountTypeBind,
Source: "/some-non-existing-host-path/",
Target: testAbsPath,
}); err == nil || !strings.Contains(err.Error(), "invalid bind mount source") {
t.Fatalf("expected error, got: %v", err)
}

// with proper source
tmpdir, err := ioutil.TempDir("", "TestControllerValidateMountBind")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.Remove(tmpdir)

if _, err := newTestControllerWithMount(api.Mount{
Type: api.MountTypeBind,
Source: testAbsPath,
Source: tmpdir,
Target: testAbsPath,
}); err != nil {
t.Fatalf("expected error, got: %v", err)
Expand Down Expand Up @@ -68,6 +85,12 @@ func TestControllerValidateMountVolume(t *testing.T) {
}

func TestControllerValidateMountTarget(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "TestControllerValidateMountTarget")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.Remove(tmpdir)

// with improper target
if _, err := newTestControllerWithMount(api.Mount{
Type: api.MountTypeBind,
Expand All @@ -80,7 +103,7 @@ func TestControllerValidateMountTarget(t *testing.T) {
// with proper target
if _, err := newTestControllerWithMount(api.Mount{
Type: api.MountTypeBind,
Source: testAbsPath,
Source: tmpdir,
Target: testAbsPath,
}); err != nil {
t.Fatalf("expected no error, got: %v", err)
Expand Down

0 comments on commit 84d5ab9

Please sign in to comment.