testutils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package volumetestutils
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/volume"
  5. )
  6. // NoopVolume is a volume that doesn't perform any operation
  7. type NoopVolume struct{}
  8. // Name is the name of the volume
  9. func (NoopVolume) Name() string { return "noop" }
  10. // DriverName is the name of the driver
  11. func (NoopVolume) DriverName() string { return "noop" }
  12. // Path is the filesystem path to the volume
  13. func (NoopVolume) Path() string { return "noop" }
  14. // Mount mounts the volume in the container
  15. func (NoopVolume) Mount(_ string) (string, error) { return "noop", nil }
  16. // Unmount unmounts the volume from the container
  17. func (NoopVolume) Unmount(_ string) error { return nil }
  18. // Status proivdes low-level details about the volume
  19. func (NoopVolume) Status() map[string]interface{} { return nil }
  20. // FakeVolume is a fake volume with a random name
  21. type FakeVolume struct {
  22. name string
  23. driverName string
  24. }
  25. // NewFakeVolume creates a new fake volume for testing
  26. func NewFakeVolume(name string, driverName string) volume.Volume {
  27. return FakeVolume{name: name, driverName: driverName}
  28. }
  29. // Name is the name of the volume
  30. func (f FakeVolume) Name() string { return f.name }
  31. // DriverName is the name of the driver
  32. func (f FakeVolume) DriverName() string { return f.driverName }
  33. // Path is the filesystem path to the volume
  34. func (FakeVolume) Path() string { return "fake" }
  35. // Mount mounts the volume in the container
  36. func (FakeVolume) Mount(_ string) (string, error) { return "fake", nil }
  37. // Unmount unmounts the volume from the container
  38. func (FakeVolume) Unmount(_ string) error { return nil }
  39. // Status proivdes low-level details about the volume
  40. func (FakeVolume) Status() map[string]interface{} { return nil }
  41. // FakeDriver is a driver that generates fake volumes
  42. type FakeDriver struct {
  43. name string
  44. vols map[string]volume.Volume
  45. }
  46. // NewFakeDriver creates a new FakeDriver with the specified name
  47. func NewFakeDriver(name string) volume.Driver {
  48. return &FakeDriver{
  49. name: name,
  50. vols: make(map[string]volume.Volume),
  51. }
  52. }
  53. // Name is the name of the driver
  54. func (d *FakeDriver) Name() string { return d.name }
  55. // Create initializes a fake volume.
  56. // It returns an error if the options include an "error" key with a message
  57. func (d *FakeDriver) Create(name string, opts map[string]string) (volume.Volume, error) {
  58. if opts != nil && opts["error"] != "" {
  59. return nil, fmt.Errorf(opts["error"])
  60. }
  61. v := NewFakeVolume(name, d.name)
  62. d.vols[name] = v
  63. return v, nil
  64. }
  65. // Remove deletes a volume.
  66. func (d *FakeDriver) Remove(v volume.Volume) error {
  67. if _, exists := d.vols[v.Name()]; !exists {
  68. return fmt.Errorf("no such volume")
  69. }
  70. delete(d.vols, v.Name())
  71. return nil
  72. }
  73. // List lists the volumes
  74. func (d *FakeDriver) List() ([]volume.Volume, error) {
  75. var vols []volume.Volume
  76. for _, v := range d.vols {
  77. vols = append(vols, v)
  78. }
  79. return vols, nil
  80. }
  81. // Get gets the volume
  82. func (d *FakeDriver) Get(name string) (volume.Volume, error) {
  83. if v, exists := d.vols[name]; exists {
  84. return v, nil
  85. }
  86. return nil, fmt.Errorf("no such volume")
  87. }
  88. // Scope returns the local scope
  89. func (*FakeDriver) Scope() string {
  90. return "local"
  91. }