mount: Add `volume-subpath` option

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2024-01-24 14:15:49 +01:00
parent a253318869
commit edc09e6038
No known key found for this signature in database
GPG Key ID: B85EFCFE26DEF92A
2 changed files with 57 additions and 0 deletions

View File

@ -2,10 +2,12 @@ package container
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
"github.com/docker/cli/e2e/internal/fixtures" "github.com/docker/cli/e2e/internal/fixtures"
"github.com/docker/cli/internal/test/environment" "github.com/docker/cli/internal/test/environment"
"github.com/docker/docker/api/types/versions"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/golden" "gotest.tools/v3/golden"
@ -149,3 +151,56 @@ func TestRunWithCgroupNamespace(t *testing.T) {
"cat", "/sys/fs/cgroup/cgroup.controllers") "cat", "/sys/fs/cgroup/cgroup.controllers")
result.Assert(t, icmd.Success) result.Assert(t, icmd.Success)
} }
func TestMountSubvolume(t *testing.T) {
skip.If(t, versions.LessThan(environment.DaemonAPIVersion(t), "1.45"))
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", "--rm", "--mount", strings.Join(defaultMountOpts, ","), fixtures.AlpineImage, "sh", "-c",
"echo foo > /volume/bar.txt && "+
"mkdir /volume/etc && echo root > /volume/etc/passwd && "+
"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", "--rm", "--mount", mountArg, fixtures.AlpineImage, cmd, "/volume")
}
for _, tc := range []struct {
name string
cmd string
subpath string
expectedOut string
expectedErr string
expectedCode int
}{
{name: "absolute", cmd: "cat", subpath: "/etc/passwd", expectedErr: "subpath must be a relative path within the volume", expectedCode: 125},
{name: "subpath not exists", cmd: "ls", subpath: "some-path/that/doesnt-exist", expectedErr: "cannot access path ", expectedCode: 127},
{name: "subdirectory mount", cmd: "ls", subpath: "subdir", expectedOut: "hello.txt"},
{name: "file mount", cmd: "cat", subpath: "bar.txt", expectedOut: "foo"},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
runMount(tc.cmd, "volume-subpath="+tc.subpath).Assert(t, icmd.Expected{
Err: tc.expectedErr,
ExitCode: tc.expectedCode,
Out: tc.expectedOut,
})
})
}
}

View File

@ -131,6 +131,8 @@ func (m *MountOpt) Set(value string) error {
return fmt.Errorf("invalid value for %s: %s (must be \"enabled\", \"disabled\", \"writable\", or \"readonly\")", return fmt.Errorf("invalid value for %s: %s (must be \"enabled\", \"disabled\", \"writable\", or \"readonly\")",
key, val) key, val)
} }
case "volume-subpath":
volumeOptions().Subpath = val
case "volume-nocopy": case "volume-nocopy":
volumeOptions().NoCopy, err = strconv.ParseBool(val) volumeOptions().NoCopy, err = strconv.ParseBool(val)
if err != nil { if err != nil {