Skip to content

Commit a17d739

Browse files
committed
mount: Add volume-subpath option
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 74be852 commit a17d739

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

e2e/container/run_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package container
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/docker/cli/e2e/internal/fixtures"
@@ -148,3 +149,37 @@ func TestRunWithCgroupNamespace(t *testing.T) {
148149
"/bin/grep", "-q", "':memory:/$'", "/proc/1/cgroup")
149150
result.Assert(t, icmd.Success)
150151
}
152+
153+
func TestMountSubvolume(t *testing.T) {
154+
volName := "test-volume-" + t.Name()
155+
icmd.RunCommand("docker", "volume", "create", volName).Assert(t, icmd.Success)
156+
157+
defer icmd.RunCommand("docker", "volume", "remove", "-f", volName).Assert(t, icmd.Success)
158+
159+
defaultMountOpts := []string{
160+
"type=volume",
161+
"src=" + volName,
162+
"dst=/volume",
163+
}
164+
165+
// Populate the volume with test data.
166+
icmd.RunCommand("docker", "run", "--mount", strings.Join(defaultMountOpts, ","), fixtures.AlpineImage, "sh", "-c",
167+
"echo foo > /volume/bar.txt && "+
168+
"mkdir /volume/subdir && echo world > /volume/subdir/hello.txt;",
169+
).Assert(t, icmd.Success)
170+
171+
runMount := func(cmd string, mountOpts ...string) *icmd.Result {
172+
mountArg := strings.Join(append(defaultMountOpts, mountOpts...), ",")
173+
return icmd.RunCommand("docker", "run", "--mount", mountArg, fixtures.AlpineImage, cmd, "/volume")
174+
}
175+
176+
t.Run("subpath not exists", func(t *testing.T) {
177+
runMount("ls", "volume-subpath=some-path/that/doesnt-exist").Assert(t, icmd.Expected{Err: "volume's path is not accessible", ExitCode: 125})
178+
})
179+
t.Run("subdirectory mount", func(t *testing.T) {
180+
runMount("ls", "volume-subpath=subdir").Assert(t, icmd.Expected{Out: "hello.txt"})
181+
})
182+
t.Run("file mount", func(t *testing.T) {
183+
runMount("cat", "volume-subpath=bar.txt").Assert(t, icmd.Expected{Out: "foo"})
184+
})
185+
}

opts/mount.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func (m *MountOpt) Set(value string) error {
112112
if err != nil {
113113
return fmt.Errorf("invalid value for %s: %s", key, val)
114114
}
115+
case "volume-subpath":
116+
volumeOptions().SubPath = val
115117
case "volume-nocopy":
116118
volumeOptions().NoCopy, err = strconv.ParseBool(val)
117119
if err != nil {

0 commit comments

Comments
 (0)