validate_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package volume
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "os"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/api/types/mount"
  10. )
  11. func TestValidateMount(t *testing.T) {
  12. testDir, err := ioutil.TempDir("", "test-validate-mount")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. defer os.RemoveAll(testDir)
  17. cases := []struct {
  18. input mount.Mount
  19. expected error
  20. }{
  21. {mount.Mount{Type: mount.TypeVolume}, errMissingField("Target")},
  22. {mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath, Source: "hello"}, nil},
  23. {mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath}, nil},
  24. {mount.Mount{Type: mount.TypeBind}, errMissingField("Target")},
  25. {mount.Mount{Type: mount.TypeBind, Target: testDestinationPath}, errMissingField("Source")},
  26. {mount.Mount{Type: mount.TypeBind, Target: testDestinationPath, Source: testSourcePath, VolumeOptions: &mount.VolumeOptions{}}, errExtraField("VolumeOptions")},
  27. {mount.Mount{Type: mount.TypeBind, Source: testDir, Target: testDestinationPath}, nil},
  28. {mount.Mount{Type: "invalid", Target: testDestinationPath}, errors.New("mount type unknown")},
  29. }
  30. if runtime.GOOS == "windows" {
  31. cases = append(cases, struct {
  32. input mount.Mount
  33. expected error
  34. }{mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath}, errBindNotExist}) // bind source existance is not checked on linux
  35. }
  36. lcowCases := []struct {
  37. input mount.Mount
  38. expected error
  39. }{
  40. {mount.Mount{Type: mount.TypeVolume}, errMissingField("Target")},
  41. {mount.Mount{Type: mount.TypeVolume, Target: "/foo", Source: "hello"}, nil},
  42. {mount.Mount{Type: mount.TypeVolume, Target: "/foo"}, nil},
  43. {mount.Mount{Type: mount.TypeBind}, errMissingField("Target")},
  44. {mount.Mount{Type: mount.TypeBind, Target: "/foo"}, errMissingField("Source")},
  45. {mount.Mount{Type: mount.TypeBind, Target: "/foo", Source: "c:\\foo", VolumeOptions: &mount.VolumeOptions{}}, errExtraField("VolumeOptions")},
  46. {mount.Mount{Type: mount.TypeBind, Source: "c:\\foo", Target: "/foo"}, errBindNotExist},
  47. {mount.Mount{Type: mount.TypeBind, Source: testDir, Target: "/foo"}, nil},
  48. {mount.Mount{Type: "invalid", Target: "/foo"}, errors.New("mount type unknown")},
  49. }
  50. parser := NewParser(runtime.GOOS)
  51. for i, x := range cases {
  52. err := parser.validateMountConfig(&x.input)
  53. if err == nil && x.expected == nil {
  54. continue
  55. }
  56. if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
  57. t.Errorf("expected %q, got %q, case: %d", x.expected, err, i)
  58. }
  59. }
  60. if runtime.GOOS == "windows" {
  61. parser = &lcowParser{}
  62. for i, x := range lcowCases {
  63. err := parser.validateMountConfig(&x.input)
  64. if err == nil && x.expected == nil {
  65. continue
  66. }
  67. if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
  68. t.Errorf("expected %q, got %q, case: %d", x.expected, err, i)
  69. }
  70. }
  71. }
  72. }