validate_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. {mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath}, errBindNotExist},
  30. }
  31. lcowCases := []struct {
  32. input mount.Mount
  33. expected error
  34. }{
  35. {mount.Mount{Type: mount.TypeVolume}, errMissingField("Target")},
  36. {mount.Mount{Type: mount.TypeVolume, Target: "/foo", Source: "hello"}, nil},
  37. {mount.Mount{Type: mount.TypeVolume, Target: "/foo"}, nil},
  38. {mount.Mount{Type: mount.TypeBind}, errMissingField("Target")},
  39. {mount.Mount{Type: mount.TypeBind, Target: "/foo"}, errMissingField("Source")},
  40. {mount.Mount{Type: mount.TypeBind, Target: "/foo", Source: "c:\\foo", VolumeOptions: &mount.VolumeOptions{}}, errExtraField("VolumeOptions")},
  41. {mount.Mount{Type: mount.TypeBind, Source: "c:\\foo", Target: "/foo"}, errBindNotExist},
  42. {mount.Mount{Type: mount.TypeBind, Source: testDir, Target: "/foo"}, nil},
  43. {mount.Mount{Type: "invalid", Target: "/foo"}, errors.New("mount type unknown")},
  44. }
  45. parser := NewParser(runtime.GOOS)
  46. for i, x := range cases {
  47. err := parser.ValidateMountConfig(&x.input)
  48. if err == nil && x.expected == nil {
  49. continue
  50. }
  51. if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
  52. t.Errorf("expected %q, got %q, case: %d", x.expected, err, i)
  53. }
  54. }
  55. if runtime.GOOS == "windows" {
  56. parser = &lcowParser{}
  57. for i, x := range lcowCases {
  58. err := parser.ValidateMountConfig(&x.input)
  59. if err == nil && x.expected == nil {
  60. continue
  61. }
  62. if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
  63. t.Errorf("expected %q, got %q, case: %d", x.expected, err, i)
  64. }
  65. }
  66. }
  67. }