validate_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package mounts // import "github.com/docker/docker/volume/mounts"
  2. import (
  3. "errors"
  4. "runtime"
  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 TestValidateMount(t *testing.T) {
  11. testDir := t.TempDir()
  12. parser := NewParser()
  13. tests := []struct {
  14. input mount.Mount
  15. expected error
  16. }{
  17. {
  18. input: mount.Mount{Type: mount.TypeVolume},
  19. expected: errMissingField("Target"),
  20. },
  21. {
  22. input: mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath, Source: "hello"},
  23. },
  24. {
  25. input: mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath},
  26. },
  27. {
  28. input: mount.Mount{Type: mount.TypeBind},
  29. expected: errMissingField("Target"),
  30. },
  31. {
  32. input: mount.Mount{Type: mount.TypeBind, Target: testDestinationPath},
  33. expected: errMissingField("Source"),
  34. },
  35. {
  36. input: mount.Mount{Type: mount.TypeBind, Target: testDestinationPath, Source: testSourcePath, VolumeOptions: &mount.VolumeOptions{}},
  37. expected: errExtraField("VolumeOptions"),
  38. },
  39. {
  40. input: mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath},
  41. },
  42. {
  43. input: mount.Mount{Type: "invalid", Target: testDestinationPath},
  44. expected: errors.New("mount type unknown"),
  45. },
  46. {
  47. input: mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath},
  48. expected: errBindSourceDoesNotExist(testSourcePath),
  49. },
  50. }
  51. for _, tc := range tests {
  52. tc := tc
  53. t.Run("", func(t *testing.T) {
  54. err := parser.ValidateMountConfig(&tc.input)
  55. if tc.expected != nil {
  56. assert.Check(t, is.ErrorContains(err, tc.expected.Error()))
  57. } else {
  58. assert.Check(t, err)
  59. }
  60. })
  61. }
  62. }
  63. func TestValidateLCOWMount(t *testing.T) {
  64. if runtime.GOOS != "windows" {
  65. t.Skip("only tested on Windows")
  66. }
  67. testDir := t.TempDir()
  68. parser := NewLCOWParser()
  69. tests := []struct {
  70. input mount.Mount
  71. expected error
  72. }{
  73. {
  74. input: mount.Mount{Type: mount.TypeVolume},
  75. expected: errMissingField("Target"),
  76. },
  77. {
  78. input: mount.Mount{Type: mount.TypeVolume, Target: "/foo", Source: "hello"},
  79. },
  80. {
  81. input: mount.Mount{Type: mount.TypeVolume, Target: "/foo"},
  82. },
  83. {
  84. input: mount.Mount{Type: mount.TypeBind},
  85. expected: errMissingField("Target"),
  86. },
  87. {
  88. input: mount.Mount{Type: mount.TypeBind, Target: "/foo"},
  89. expected: errMissingField("Source"),
  90. },
  91. {
  92. input: mount.Mount{Type: mount.TypeBind, Target: "/foo", Source: "c:\\foo", VolumeOptions: &mount.VolumeOptions{}},
  93. expected: errExtraField("VolumeOptions"),
  94. },
  95. {
  96. input: mount.Mount{Type: mount.TypeBind, Source: "c:\\foo", Target: "/foo"},
  97. expected: errBindSourceDoesNotExist("c:\\foo"),
  98. },
  99. {
  100. input: mount.Mount{Type: mount.TypeBind, Source: testDir, Target: "/foo"},
  101. },
  102. {
  103. input: mount.Mount{Type: "invalid", Target: "/foo"},
  104. expected: errors.New("mount type unknown"),
  105. },
  106. }
  107. for _, tc := range tests {
  108. tc := tc
  109. t.Run("", func(t *testing.T) {
  110. err := parser.ValidateMountConfig(&tc.input)
  111. if tc.expected != nil {
  112. assert.Check(t, is.ErrorContains(err, tc.expected.Error()))
  113. } else {
  114. assert.Check(t, err)
  115. }
  116. })
  117. }
  118. }