Skip to content

Commit

Permalink
mount: Add volume-subpath option
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
  • Loading branch information
vvoland committed Jun 2, 2023
1 parent 74be852 commit 54e2105
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions e2e/container/run_test.go
Expand Up @@ -2,6 +2,7 @@ package container

import (
"fmt"
"strings"
"testing"

"github.com/docker/cli/e2e/internal/fixtures"
Expand Down Expand Up @@ -148,3 +149,37 @@ func TestRunWithCgroupNamespace(t *testing.T) {
"/bin/grep", "-q", "':memory:/$'", "/proc/1/cgroup")
result.Assert(t, icmd.Success)
}

func TestMountSubvolume(t *testing.T) {
volName := "test-volume-" + t.Name()
icmd.RunCommand("docker", "volume", "create", volName).Assert(t, icmd.Success)

defer icmd.RunCommand("docker", "volume", "remove", "-f", volName).Assert(t, icmd.Success)

defaultMountOpts := []string{
"type=volume",
"src=" + volName,
"dst=/volume",
}

// Populate the volume with test data.
icmd.RunCommand("docker", "run", "--mount", strings.Join(defaultMountOpts, ","), fixtures.AlpineImage, "sh", "-c",
"echo foo > /volume/bar.txt && "+
"mkdir /volume/subdir && echo world > /volume/subdir/hello.txt;",
).Assert(t, icmd.Success)

runMount := func(cmd string, mountOpts ...string) *icmd.Result {
mountArg := strings.Join(append(defaultMountOpts, mountOpts...), ",")
return icmd.RunCommand("docker", "run", "--mount", mountArg, fixtures.AlpineImage, cmd, "/volume")
}

t.Run("subpath not exists", func(t *testing.T) {
runMount("ls", "volume-subpath=some-path/that/doesnt-exist").Assert(t, icmd.Expected{ExitCode: 127})
})
t.Run("subdirectory mount", func(t *testing.T) {
runMount("ls", "volume-subpath=subdir").Assert(t, icmd.Expected{Out: "hello.txt"})
})
t.Run("file mount", func(t *testing.T) {
runMount("cat", "volume-subpath=bar.txt").Assert(t, icmd.Expected{Out: "foo"})
})
}
2 changes: 2 additions & 0 deletions opts/mount.go
Expand Up @@ -112,6 +112,8 @@ func (m *MountOpt) Set(value string) error {
if err != nil {
return fmt.Errorf("invalid value for %s: %s", key, val)
}
case "volume-subpath":
volumeOptions().SubPath = val
case "volume-nocopy":
volumeOptions().NoCopy, err = strconv.ParseBool(val)
if err != nil {
Expand Down

0 comments on commit 54e2105

Please sign in to comment.