Przeglądaj źródła

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>
Sebastiaan van Stijn 4 lat temu
rodzic
commit
28b0f47599

+ 5 - 0
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):

+ 2 - 2
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)

+ 5 - 0
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) {

+ 4 - 4
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 {

+ 2 - 2
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()
 }

+ 1 - 1
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 {

+ 5 - 0
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 (

+ 3 - 3
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,