From 54e2105d39aa377c3f168d8766085d442a3b19ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Fri, 2 Jun 2023 18:03:35 +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 | 35 +++++++++++++++++++++++++++++++++++ opts/mount.go | 2 ++ 2 files changed, 37 insertions(+) diff --git a/e2e/container/run_test.go b/e2e/container/run_test.go index 3b0414ffab1c..dbd76f63cb25 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,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"}) + }) +} 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 {