Browse code

Print which path failed when the mount source doesn't exist.

Changes Details:
--------------
Fixes: #36395

Refactoring the code to do the following:
1. Add the method `errBindSourceDoesNotExist` inside `validate.go` to be in-line with the rest of error message
2. Utilised the new method inside `linux_parser.go`, `windows_parser.go` and `validate_test.go`
3. Change the format from `bind mount source path: '%s' does not exist` to `bind mount source path does not exist: %s`
4. Reflected the format change into the 2 unit tests, namely: `volume_test.go` and `validate_test.go`
5. Reflected the format change into `docker_api_containers_test.go` integration test

Signed-off-by: Amr Gawish <amr.gawish@gmail.com>

Amr Gawish authored on 2018/02/26 11:10:46
Showing 6 changed files
... ...
@@ -1713,7 +1713,7 @@ func (s *DockerSuite) TestContainersAPICreateMountsValidation(c *check.C) {
1713 1713
 					Type:   "bind",
1714 1714
 					Source: notExistPath,
1715 1715
 					Target: destPath}}},
1716
-			msg: "bind source path does not exist",
1716
+			msg: "bind mount source path does not exist: " + notExistPath,
1717 1717
 		},
1718 1718
 		{
1719 1719
 			config: containertypes.Config{
... ...
@@ -83,7 +83,7 @@ func (p *linuxParser) validateMountConfigImpl(mnt *mount.Mount, validateBindSour
83 83
 		if validateBindSourceExists {
84 84
 			exists, _, _ := currentFileInfoProvider.fileInfo(mnt.Source)
85 85
 			if !exists {
86
-				return &errMountConfig{mnt, errBindNotExist}
86
+				return &errMountConfig{mnt, errBindSourceDoesNotExist(mnt.Source)}
87 87
 			}
88 88
 		}
89 89
 
... ...
@@ -7,8 +7,6 @@ import (
7 7
 	"github.com/pkg/errors"
8 8
 )
9 9
 
10
-var errBindNotExist = errors.New("bind source path does not exist")
11
-
12 10
 type errMountConfig struct {
13 11
 	mount *mount.Mount
14 12
 	err   error
... ...
@@ -18,6 +16,10 @@ func (e *errMountConfig) Error() string {
18 18
 	return fmt.Sprintf("invalid mount config for type %q: %v", e.mount.Type, e.err.Error())
19 19
 }
20 20
 
21
+func errBindSourceDoesNotExist(path string) error {
22
+	return errors.Errorf("bind mount source path does not exist: %s", path)
23
+}
24
+
21 25
 func errExtraField(name string) error {
22 26
 	return errors.Errorf("field %s must not be specified", name)
23 27
 }
... ...
@@ -31,7 +31,7 @@ func TestValidateMount(t *testing.T) {
31 31
 
32 32
 		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath}, nil},
33 33
 		{mount.Mount{Type: "invalid", Target: testDestinationPath}, errors.New("mount type unknown")},
34
-		{mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath}, errBindNotExist},
34
+		{mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath}, errBindSourceDoesNotExist(testSourcePath)},
35 35
 	}
36 36
 
37 37
 	lcowCases := []struct {
... ...
@@ -44,7 +44,7 @@ func TestValidateMount(t *testing.T) {
44 44
 		{mount.Mount{Type: mount.TypeBind}, errMissingField("Target")},
45 45
 		{mount.Mount{Type: mount.TypeBind, Target: "/foo"}, errMissingField("Source")},
46 46
 		{mount.Mount{Type: mount.TypeBind, Target: "/foo", Source: "c:\\foo", VolumeOptions: &mount.VolumeOptions{}}, errExtraField("VolumeOptions")},
47
-		{mount.Mount{Type: mount.TypeBind, Source: "c:\\foo", Target: "/foo"}, errBindNotExist},
47
+		{mount.Mount{Type: mount.TypeBind, Source: "c:\\foo", Target: "/foo"}, errBindSourceDoesNotExist("c:\\foo")},
48 48
 		{mount.Mount{Type: mount.TypeBind, Source: testDir, Target: "/foo"}, nil},
49 49
 		{mount.Mount{Type: "invalid", Target: "/foo"}, errors.New("mount type unknown")},
50 50
 	}
... ...
@@ -120,7 +120,7 @@ func TestParseMountRaw(t *testing.T) {
120 120
 			`c:\:d:\:xyzzy`:                    "invalid volume specification: ",
121 121
 			`c:`:                               "cannot be `c:`",
122 122
 			`c:\`:                              "cannot be `c:`",
123
-			`c:\notexist:d:`:                   `source path does not exist`,
123
+			`c:\notexist:d:`:                   `bind mount source path does not exist: c:\notexist`,
124 124
 			`c:\windows\system32\ntdll.dll:d:`: `source path must be a directory`,
125 125
 			`name<:d:`:                         `invalid volume specification`,
126 126
 			`name>:d:`:                         `invalid volume specification`,
... ...
@@ -189,7 +189,7 @@ func TestParseMountRaw(t *testing.T) {
189 189
 			`c:\:/foo:xyzzy`:                     "invalid volume specification: ",
190 190
 			`/`:                                  "destination can't be '/'",
191 191
 			`/..`:                                "destination can't be '/'",
192
-			`c:\notexist:/foo`:                   `source path does not exist`,
192
+			`c:\notexist:/foo`:                   `bind mount source path does not exist: c:\notexist`,
193 193
 			`c:\windows\system32\ntdll.dll:/foo`: `source path must be a directory`,
194 194
 			`name<:/foo`:                         `invalid volume specification`,
195 195
 			`name>:/foo`:                         `invalid volume specification`,
... ...
@@ -252,7 +252,7 @@ func (p *windowsParser) validateMountConfigReg(mnt *mount.Mount, destRegex strin
252 252
 			return &errMountConfig{mnt, err}
253 253
 		}
254 254
 		if !exists {
255
-			return &errMountConfig{mnt, errBindNotExist}
255
+			return &errMountConfig{mnt, errBindSourceDoesNotExist(mnt.Source)}
256 256
 		}
257 257
 		if !isdir {
258 258
 			return &errMountConfig{mnt, fmt.Errorf("source path must be a directory")}