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