validate_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package volume
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. "testing"
  8. "github.com/docker/docker/api/types/mount"
  9. )
  10. func TestValidateMount(t *testing.T) {
  11. testDir, err := ioutil.TempDir("", "test-validate-mount")
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. defer os.RemoveAll(testDir)
  16. cases := []struct {
  17. input mount.Mount
  18. expected error
  19. }{
  20. {mount.Mount{Type: mount.TypeVolume}, errMissingField("Target")},
  21. {mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath, Source: "hello"}, nil},
  22. {mount.Mount{Type: mount.TypeVolume, Target: testDestinationPath}, nil},
  23. {mount.Mount{Type: mount.TypeBind}, errMissingField("Target")},
  24. {mount.Mount{Type: mount.TypeBind, Target: testDestinationPath}, errMissingField("Source")},
  25. {mount.Mount{Type: mount.TypeBind, Target: testDestinationPath, Source: testSourcePath, VolumeOptions: &mount.VolumeOptions{}}, errExtraField("VolumeOptions")},
  26. {mount.Mount{Type: mount.TypeBind, Source: testSourcePath, Target: testDestinationPath}, errBindNotExist},
  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. for i, x := range cases {
  31. err := validateMountConfig(&x.input)
  32. if err == nil && x.expected == nil {
  33. continue
  34. }
  35. if (err == nil && x.expected != nil) || (x.expected == nil && err != nil) || !strings.Contains(err.Error(), x.expected.Error()) {
  36. t.Fatalf("expected %q, got %q, case: %d", x.expected, err, i)
  37. }
  38. }
  39. }