parser_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package mounts // import "github.com/docker/docker/volume/mounts"
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/docker/docker/api/types/mount"
  6. "gotest.tools/v3/assert"
  7. is "gotest.tools/v3/assert/cmp"
  8. )
  9. type mockFiProvider struct{}
  10. func (mockFiProvider) fileInfo(path string) (exists, isDir bool, err error) {
  11. dirs := map[string]struct{}{
  12. `c:\`: {},
  13. `c:\windows\`: {},
  14. `c:\windows`: {},
  15. `c:\program files`: {},
  16. `c:\Windows`: {},
  17. `c:\Program Files (x86)`: {},
  18. `\\?\c:\windows\`: {},
  19. }
  20. files := map[string]struct{}{
  21. `c:\windows\system32\ntdll.dll`: {},
  22. }
  23. if _, ok := dirs[path]; ok {
  24. return true, true, nil
  25. }
  26. if _, ok := files[path]; ok {
  27. return true, false, nil
  28. }
  29. return false, false, nil
  30. }
  31. // always returns the configured error
  32. // this is used to test error handling
  33. type mockFiProviderWithError struct{ err error }
  34. func (m mockFiProviderWithError) fileInfo(path string) (bool, bool, error) {
  35. return false, false, m.err
  36. }
  37. func TestParseMountSpec(t *testing.T) {
  38. testDir, err := os.MkdirTemp("", "test-mount-config")
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. defer os.RemoveAll(testDir)
  43. parser := NewParser()
  44. cases := []struct {
  45. input mount.Mount
  46. expected MountPoint
  47. }{
  48. {
  49. input: mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath, ReadOnly: true},
  50. expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()},
  51. },
  52. {
  53. input: mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath},
  54. expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, RW: true, Propagation: parser.DefaultPropagationMode()},
  55. },
  56. {
  57. input: mount.Mount{Type: mount.TypeBind, Source: testDir + string(os.PathSeparator), Target: testDestinationPath, ReadOnly: true},
  58. expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()},
  59. },
  60. {
  61. input: mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath + string(os.PathSeparator), ReadOnly: true},
  62. expected: MountPoint{Type: mount.TypeBind, Source: testDir, Destination: testDestinationPath, Propagation: parser.DefaultPropagationMode()},
  63. },
  64. {
  65. input: mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath},
  66. expected: MountPoint{Type: mount.TypeVolume, Destination: testDestinationPath, RW: true, CopyData: parser.DefaultCopyMode()},
  67. },
  68. {
  69. input: mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath + string(os.PathSeparator)},
  70. expected: MountPoint{Type: mount.TypeVolume, Destination: testDestinationPath, RW: true, CopyData: parser.DefaultCopyMode()},
  71. },
  72. }
  73. for _, tc := range cases {
  74. tc := tc
  75. t.Run("", func(t *testing.T) {
  76. mp, err := parser.ParseMountSpec(tc.input)
  77. assert.NilError(t, err)
  78. assert.Check(t, is.Equal(mp.Type, tc.expected.Type))
  79. assert.Check(t, is.Equal(mp.Destination, tc.expected.Destination))
  80. assert.Check(t, is.Equal(mp.Source, tc.expected.Source))
  81. assert.Check(t, is.Equal(mp.RW, tc.expected.RW))
  82. assert.Check(t, is.Equal(mp.Propagation, tc.expected.Propagation))
  83. assert.Check(t, is.Equal(mp.Driver, tc.expected.Driver))
  84. assert.Check(t, is.Equal(mp.CopyData, tc.expected.CopyData))
  85. })
  86. }
  87. }