From 28b0f47599e9ff32d2abebb54ee4b2a60977f722 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 2 Jul 2021 13:41:47 +0200 Subject: [PATCH] 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 --- volume/mounts/lcow_parser.go | 5 +++++ volume/mounts/lcow_parser_test.go | 4 ++-- volume/mounts/linux_parser.go | 5 +++++ volume/mounts/linux_parser_test.go | 8 ++++---- volume/mounts/parser.go | 4 ++-- volume/mounts/validate_test.go | 2 +- volume/mounts/windows_parser.go | 5 +++++ volume/mounts/windows_parser_test.go | 6 +++--- 8 files changed, 27 insertions(+), 12 deletions(-) diff --git a/volume/mounts/lcow_parser.go b/volume/mounts/lcow_parser.go index 645a731d93..150285f98a 100644 --- a/volume/mounts/lcow_parser.go +++ b/volume/mounts/lcow_parser.go @@ -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): diff --git a/volume/mounts/lcow_parser_test.go b/volume/mounts/lcow_parser_test.go index 8e5e2261df..800e1f1d75 100644 --- a/volume/mounts/lcow_parser_test.go +++ b/volume/mounts/lcow_parser_test.go @@ -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) diff --git a/volume/mounts/linux_parser.go b/volume/mounts/linux_parser.go index 8dc9afd34c..7dc6a1dd0a 100644 --- a/volume/mounts/linux_parser.go +++ b/volume/mounts/linux_parser.go @@ -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) { diff --git a/volume/mounts/linux_parser_test.go b/volume/mounts/linux_parser_test.go index 00c8cff47a..9b09af3a92 100644 --- a/volume/mounts/linux_parser_test.go +++ b/volume/mounts/linux_parser_test.go @@ -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 { diff --git a/volume/mounts/parser.go b/volume/mounts/parser.go index 07ac7fd3df..58107f490c 100644 --- a/volume/mounts/parser.go +++ b/volume/mounts/parser.go @@ -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() } diff --git a/volume/mounts/validate_test.go b/volume/mounts/validate_test.go index e1c1796cd2..ff9ff57595 100644 --- a/volume/mounts/validate_test.go +++ b/volume/mounts/validate_test.go @@ -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 { diff --git a/volume/mounts/windows_parser.go b/volume/mounts/windows_parser.go index e0f8f5a7d1..41254ad1f9 100644 --- a/volume/mounts/windows_parser.go +++ b/volume/mounts/windows_parser.go @@ -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 ( diff --git a/volume/mounts/windows_parser_test.go b/volume/mounts/windows_parser_test.go index 82fce8d0f9..bf62e3b59d 100644 --- a/volume/mounts/windows_parser_test.go +++ b/volume/mounts/windows_parser_test.go @@ -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,