testutils.go 3.4 KB

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