From 81e961cc149fbccd60b968c2089f856e41105954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Mon, 5 Jun 2023 17:45:22 +0200 Subject: [PATCH] mount: Add `volume-subpath` option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- e2e/container/run_test.go | 37 +++++++++++++++++++++++++++++++++++++ opts/mount.go | 2 ++ 2 files changed, 39 insertions(+) diff --git a/e2e/container/run_test.go b/e2e/container/run_test.go index 3b0414ffab1c..11fd1bef67d3 100644 --- a/e2e/container/run_test.go +++ b/e2e/container/run_test.go @@ -2,6 +2,7 @@ package container import ( "fmt" + "strings" "testing" "github.com/docker/cli/e2e/internal/fixtures" @@ -148,3 +149,39 @@ 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) + + t.Cleanup(func() { + 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{Err: "volume's path is not accessible", ExitCode: 125}) + }) + 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"}) + }) +} diff --git a/opts/mount.go b/opts/mount.go index 2b531127ebdb..241eb8791277 100644 --- a/opts/mount.go +++ b/opts/mount.go @@ -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 {