linux_parser_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package mounts // import "github.com/docker/docker/volume/mounts"
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/docker/docker/api/types/mount"
  7. "gotest.tools/v3/assert"
  8. is "gotest.tools/v3/assert/cmp"
  9. )
  10. func TestLinuxParseMountRaw(t *testing.T) {
  11. valid := []string{
  12. "/home",
  13. "/home:/home",
  14. "/home:/something/else",
  15. "/with space",
  16. "/home:/with space",
  17. "relative:/absolute-path",
  18. "hostPath:/containerPath:ro",
  19. "/hostPath:/containerPath:rw",
  20. "/rw:/ro",
  21. "/hostPath:/containerPath:shared",
  22. "/hostPath:/containerPath:rshared",
  23. "/hostPath:/containerPath:slave",
  24. "/hostPath:/containerPath:rslave",
  25. "/hostPath:/containerPath:private",
  26. "/hostPath:/containerPath:rprivate",
  27. "/hostPath:/containerPath:ro,shared",
  28. "/hostPath:/containerPath:ro,slave",
  29. "/hostPath:/containerPath:ro,private",
  30. "/hostPath:/containerPath:ro,z,shared",
  31. "/hostPath:/containerPath:ro,Z,slave",
  32. "/hostPath:/containerPath:Z,ro,slave",
  33. "/hostPath:/containerPath:slave,Z,ro",
  34. "/hostPath:/containerPath:Z,slave,ro",
  35. "/hostPath:/containerPath:slave,ro,Z",
  36. "/hostPath:/containerPath:rslave,ro,Z",
  37. "/hostPath:/containerPath:ro,rshared,Z",
  38. "/hostPath:/containerPath:ro,Z,rprivate",
  39. }
  40. invalid := map[string]string{
  41. "": "invalid volume specification",
  42. "./": "mount path must be absolute",
  43. "../": "mount path must be absolute",
  44. "/:../": "mount path must be absolute",
  45. "/:path": "mount path must be absolute",
  46. ":": "invalid volume specification",
  47. "/tmp:": "invalid volume specification",
  48. ":test": "invalid volume specification",
  49. ":/test": "invalid volume specification",
  50. "tmp:": "invalid volume specification",
  51. ":test:": "invalid volume specification",
  52. "::": "invalid volume specification",
  53. ":::": "invalid volume specification",
  54. "/tmp:::": "invalid volume specification",
  55. ":/tmp::": "invalid volume specification",
  56. "/path:rw": "invalid volume specification",
  57. "/path:ro": "invalid volume specification",
  58. "/rw:rw": "invalid volume specification",
  59. "path:ro": "invalid volume specification",
  60. "/path:/path:sw": `invalid mode`,
  61. "/path:/path:rwz": `invalid mode`,
  62. "/path:/path:ro,rshared,rslave": `invalid mode`,
  63. "/path:/path:ro,z,rshared,rslave": `invalid mode`,
  64. "/path:shared": "invalid volume specification",
  65. "/path:slave": "invalid volume specification",
  66. "/path:private": "invalid volume specification",
  67. "name:/absolute-path:shared": "invalid volume specification",
  68. "name:/absolute-path:rshared": "invalid volume specification",
  69. "name:/absolute-path:slave": "invalid volume specification",
  70. "name:/absolute-path:rslave": "invalid volume specification",
  71. "name:/absolute-path:private": "invalid volume specification",
  72. "name:/absolute-path:rprivate": "invalid volume specification",
  73. }
  74. parser := NewLinuxParser()
  75. if p, ok := parser.(*linuxParser); ok {
  76. p.fi = mockFiProvider{}
  77. }
  78. for _, path := range valid {
  79. if _, err := parser.ParseMountRaw(path, "local"); err != nil {
  80. t.Errorf("ParseMountRaw(`%q`) should succeed: error %q", path, err)
  81. }
  82. }
  83. for path, expectedError := range invalid {
  84. if mp, err := parser.ParseMountRaw(path, "local"); err == nil {
  85. t.Errorf("ParseMountRaw(`%q`) should have failed validation. Err '%v' - MP: %v", path, err, mp)
  86. } else {
  87. if !strings.Contains(err.Error(), expectedError) {
  88. t.Errorf("ParseMountRaw(`%q`) error should contain %q, got %v", path, expectedError, err.Error())
  89. }
  90. }
  91. }
  92. }
  93. func TestLinuxParseMountRawSplit(t *testing.T) {
  94. cases := []struct {
  95. bind string
  96. driver string
  97. expType mount.Type
  98. expDest string
  99. expSource string
  100. expName string
  101. expDriver string
  102. expRW bool
  103. fail bool
  104. }{
  105. {
  106. bind: "/tmp:/tmp1",
  107. expType: mount.TypeBind,
  108. expDest: "/tmp1",
  109. expSource: "/tmp",
  110. expRW: true,
  111. },
  112. {
  113. bind: "/tmp:/tmp2:ro",
  114. expType: mount.TypeBind,
  115. expDest: "/tmp2",
  116. expSource: "/tmp",
  117. },
  118. {
  119. bind: "/tmp:/tmp3:rw",
  120. expType: mount.TypeBind,
  121. expDest: "/tmp3",
  122. expSource: "/tmp",
  123. expRW: true,
  124. },
  125. {
  126. bind: "/tmp:/tmp4:foo",
  127. expType: mount.TypeBind,
  128. fail: true,
  129. },
  130. {
  131. bind: "name:/named1",
  132. expType: mount.TypeVolume,
  133. expDest: "/named1",
  134. expName: "name",
  135. expRW: true,
  136. },
  137. {
  138. bind: "name:/named2",
  139. driver: "external",
  140. expType: mount.TypeVolume,
  141. expDest: "/named2",
  142. expName: "name",
  143. expDriver: "external",
  144. expRW: true,
  145. },
  146. {
  147. bind: "name:/named3:ro",
  148. driver: "local",
  149. expType: mount.TypeVolume,
  150. expDest: "/named3",
  151. expName: "name",
  152. expDriver: "local",
  153. },
  154. {
  155. bind: "local/name:/tmp:rw",
  156. expType: mount.TypeVolume,
  157. expDest: "/tmp",
  158. expName: "local/name",
  159. expRW: true,
  160. },
  161. {
  162. bind: "/tmp:tmp",
  163. expType: mount.TypeBind,
  164. expRW: true,
  165. fail: true,
  166. },
  167. }
  168. parser := NewLinuxParser()
  169. if p, ok := parser.(*linuxParser); ok {
  170. p.fi = mockFiProvider{}
  171. }
  172. for _, tc := range cases {
  173. tc := tc
  174. t.Run(tc.bind, func(t *testing.T) {
  175. m, err := parser.ParseMountRaw(tc.bind, tc.driver)
  176. if tc.fail {
  177. assert.Check(t, is.ErrorContains(err, ""), "expected an error")
  178. return
  179. }
  180. assert.NilError(t, err)
  181. assert.Check(t, is.Equal(m.Destination, tc.expDest))
  182. assert.Check(t, is.Equal(m.Source, tc.expSource))
  183. assert.Check(t, is.Equal(m.Name, tc.expName))
  184. assert.Check(t, is.Equal(m.Driver, tc.expDriver))
  185. assert.Check(t, is.Equal(m.RW, tc.expRW))
  186. assert.Check(t, is.Equal(m.Type, tc.expType))
  187. })
  188. }
  189. }
  190. // TestLinuxParseMountSpecBindWithFileinfoError makes sure that the parser returns
  191. // the error produced by the fileinfo provider.
  192. //
  193. // Some extra context for the future in case of changes and possible wtf are we
  194. // testing this for:
  195. //
  196. // Currently this "fileInfoProvider" returns (bool, bool, error)
  197. // The 1st bool is "does this path exist"
  198. // The 2nd bool is "is this path a dir"
  199. // Then of course the error is an error.
  200. //
  201. // The issue is the parser was ignoring the error and only looking at the
  202. // "does this path exist" boolean, which is always false if there is an error.
  203. // Then the error returned to the caller was a (slightly, maybe) friendlier
  204. // error string than what comes from `os.Stat`
  205. // So ...the caller was always getting an error saying the path doesn't exist
  206. // even if it does exist but got some other error (like a permission error).
  207. // This is confusing to users.
  208. func TestLinuxParseMountSpecBindWithFileinfoError(t *testing.T) {
  209. parser := NewLinuxParser()
  210. testErr := fmt.Errorf("some crazy error")
  211. if pr, ok := parser.(*linuxParser); ok {
  212. pr.fi = &mockFiProviderWithError{err: testErr}
  213. }
  214. _, err := parser.ParseMountSpec(mount.Mount{
  215. Type: mount.TypeBind,
  216. Source: `/bananas`,
  217. Target: `/bananas`,
  218. })
  219. assert.ErrorContains(t, err, testErr.Error())
  220. }
  221. func TestConvertTmpfsOptions(t *testing.T) {
  222. type testCase struct {
  223. opt mount.TmpfsOptions
  224. readOnly bool
  225. expectedSubstrings []string
  226. unexpectedSubstrings []string
  227. }
  228. cases := []testCase{
  229. {
  230. opt: mount.TmpfsOptions{SizeBytes: 1024 * 1024, Mode: 0o700},
  231. readOnly: false,
  232. expectedSubstrings: []string{"size=1m", "mode=700"},
  233. unexpectedSubstrings: []string{"ro"},
  234. },
  235. {
  236. opt: mount.TmpfsOptions{},
  237. readOnly: true,
  238. expectedSubstrings: []string{"ro"},
  239. unexpectedSubstrings: []string{},
  240. },
  241. }
  242. p := NewLinuxParser()
  243. for _, tc := range cases {
  244. data, err := p.ConvertTmpfsOptions(&tc.opt, tc.readOnly)
  245. if err != nil {
  246. t.Fatalf("could not convert %+v (readOnly: %v) to string: %v",
  247. tc.opt, tc.readOnly, err)
  248. }
  249. t.Logf("data=%q", data)
  250. for _, s := range tc.expectedSubstrings {
  251. if !strings.Contains(data, s) {
  252. t.Fatalf("expected substring: %s, got %v (case=%+v)", s, data, tc)
  253. }
  254. }
  255. for _, s := range tc.unexpectedSubstrings {
  256. if strings.Contains(data, s) {
  257. t.Fatalf("unexpected substring: %s, got %v (case=%+v)", s, data, tc)
  258. }
  259. }
  260. }
  261. }