volume/mounts: add constructors for each parser

This adds constructors for the Linux, Windows, and LCOW,
to allow using these parsers externally.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-07-02 13:41:47 +02:00
parent df179a1d6a
commit 28b0f47599
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
8 changed files with 27 additions and 12 deletions

View File

@ -7,6 +7,11 @@ import (
"github.com/docker/docker/api/types/mount"
)
// NewLCOWParser creates a parser with Linux Containers on Windows semantics.
func NewLCOWParser() Parser {
return &lcowParser{}
}
// rxLCOWDestination is the regex expression for the mount destination for LCOW
//
// Destination (aka container path):

View File

@ -80,7 +80,7 @@ func TestLCOWParseMountRaw(t *testing.T) {
`\\.\pipe\foo:/foo`: `Linux containers on Windows do not support named pipe mounts`,
}
parser := &lcowParser{}
parser := NewLCOWParser()
for _, path := range valid {
if _, err := parser.ParseMountRaw(path, "local"); err != nil {
@ -129,7 +129,7 @@ func TestLCOWParseMountRawSplit(t *testing.T) {
{`c:\foo\bar:\\.\pipe\foo`, "local", mount.TypeNamedPipe, ``, ``, "", "", true, true},
}
parser := &lcowParser{}
parser := NewLCOWParser()
for i, c := range cases {
t.Logf("case %d", i)
m, err := parser.ParseMountRaw(c.bind, c.driver)

View File

@ -12,6 +12,11 @@ import (
"github.com/docker/docker/volume"
)
// NewLinuxParser creates a parser with Linux semantics.
func NewLinuxParser() Parser {
return &linuxParser{}
}
type linuxParser struct{}
func linuxSplitRawSpec(raw string) ([]string, error) {

View File

@ -79,7 +79,7 @@ func TestLinuxParseMountRaw(t *testing.T) {
"name:/absolute-path:rprivate": "invalid volume specification",
}
parser := &linuxParser{}
parser := NewLinuxParser()
for _, path := range valid {
if _, err := parser.ParseMountRaw(path, "local"); err != nil {
@ -125,7 +125,7 @@ func TestLinuxParseMountRawSplit(t *testing.T) {
{"/tmp:tmp", "", mount.TypeBind, "", "", "", "", true, true},
}
parser := &linuxParser{}
parser := NewLinuxParser()
for i, c := range cases {
t.Logf("case %d", i)
m, err := parser.ParseMountRaw(c.bind, c.driver)
@ -191,7 +191,7 @@ func TestLinuxParseMountSpecBindWithFileinfoError(t *testing.T) {
testErr := fmt.Errorf("some crazy error")
currentFileInfoProvider = &mockFiProviderWithError{err: testErr}
parser := &linuxParser{}
parser := NewLinuxParser()
_, err := parser.ParseMountSpec(mount.Mount{
Type: mount.TypeBind,
@ -222,7 +222,7 @@ func TestConvertTmpfsOptions(t *testing.T) {
unexpectedSubstrings: []string{},
},
}
p := &linuxParser{}
p := NewLinuxParser()
for _, c := range cases {
data, err := p.ConvertTmpfsOptions(&c.opt, c.readOnly)
if err != nil {

View File

@ -36,7 +36,7 @@ type Parser interface {
// NewParser creates a parser for the current host OS
func NewParser() Parser {
if runtime.GOOS == "windows" {
return &windowsParser{}
return NewWindowsParser()
}
return &linuxParser{}
return NewLinuxParser()
}

View File

@ -59,7 +59,7 @@ func TestValidateMount(t *testing.T) {
}
}
if runtime.GOOS == "windows" {
parser = &lcowParser{}
parser = NewLCOWParser()
for i, x := range lcowCases {
err := parser.ValidateMountConfig(&x.input)
if err == nil && x.expected == nil {

View File

@ -12,6 +12,11 @@ import (
"github.com/docker/docker/pkg/stringid"
)
// NewWindowsParser creates a parser with Windows semantics.
func NewWindowsParser() Parser {
return &windowsParser{}
}
type windowsParser struct{}
const (

View File

@ -87,7 +87,7 @@ func TestWindowsParseMountRaw(t *testing.T) {
`\\.\pipe\foo:c:\pipe`: `'c:\pipe' is not a valid pipe path`,
}
parser := &windowsParser{}
parser := NewWindowsParser()
for _, path := range valid {
if _, err := parser.ParseMountRaw(path, "local"); err != nil {
@ -137,7 +137,7 @@ func TestWindowsParseMountRawSplit(t *testing.T) {
{`c:\foo\bar:\\.\pipe\foo`, "local", mount.TypeNamedPipe, ``, ``, "", "", true, true},
}
parser := &windowsParser{}
parser := NewWindowsParser()
for i, c := range cases {
t.Logf("case %d", i)
m, err := parser.ParseMountRaw(c.bind, c.driver)
@ -203,7 +203,7 @@ func TestWindowsParseMountSpecBindWithFileinfoError(t *testing.T) {
testErr := fmt.Errorf("some crazy error")
currentFileInfoProvider = &mockFiProviderWithError{err: testErr}
parser := &windowsParser{}
parser := NewWindowsParser()
_, err := parser.ParseMountSpec(mount.Mount{
Type: mount.TypeBind,