1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/volume/testutils/testutils.go
Brian Goff d3eca4451d Move responsibility of ls/inspect to volume driver
Makes `docker volume ls` and `docker volume inspect` ask the volume
drivers rather than only using what is cached locally.

Previously in order to use a volume from an external driver, one would
either have to use `docker volume create` or have a container that is
already using that volume for it to be visible to the other volume
API's.

For keeping uniqueness of volume names in the daemon, names are bound to
a driver on a first come first serve basis. If two drivers have a volume
with the same name, the first one is chosen, and a warning is logged
about the second one.

Adds 2 new methods to the plugin API, `List` and `Get`.
If a plugin does not implement these endpoints, a user will not be able
to find the specified volumes as well requests go through the drivers.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2016-01-05 16:28:38 -05:00

104 lines
2.6 KiB
Go

package volumetestutils
import (
"fmt"
"github.com/docker/docker/volume"
)
// NoopVolume is a volume that doesn't perform any operation
type NoopVolume struct{}
// Name is the name of the volume
func (NoopVolume) Name() string { return "noop" }
// DriverName is the name of the driver
func (NoopVolume) DriverName() string { return "noop" }
// Path is the filesystem path to the volume
func (NoopVolume) Path() string { return "noop" }
// Mount mounts the volume in the container
func (NoopVolume) Mount() (string, error) { return "noop", nil }
// Unmount unmounts the volume from the container
func (NoopVolume) Unmount() error { return nil }
// FakeVolume is a fake volume with a random name
type FakeVolume struct {
name string
}
// NewFakeVolume creates a new fake volume for testing
func NewFakeVolume(name string) volume.Volume {
return FakeVolume{name: name}
}
// Name is the name of the volume
func (f FakeVolume) Name() string { return f.name }
// DriverName is the name of the driver
func (FakeVolume) DriverName() string { return "fake" }
// Path is the filesystem path to the volume
func (FakeVolume) Path() string { return "fake" }
// Mount mounts the volume in the container
func (FakeVolume) Mount() (string, error) { return "fake", nil }
// Unmount unmounts the volume from the container
func (FakeVolume) Unmount() error { return nil }
// FakeDriver is a driver that generates fake volumes
type FakeDriver struct {
name string
vols map[string]volume.Volume
}
// NewFakeDriver creates a new FakeDriver with the specified name
func NewFakeDriver(name string) volume.Driver {
return &FakeDriver{
name: name,
vols: make(map[string]volume.Volume),
}
}
// Name is the name of the driver
func (d *FakeDriver) Name() string { return d.name }
// Create initializes a fake volume.
// It returns an error if the options include an "error" key with a message
func (d *FakeDriver) Create(name string, opts map[string]string) (volume.Volume, error) {
if opts != nil && opts["error"] != "" {
return nil, fmt.Errorf(opts["error"])
}
v := NewFakeVolume(name)
d.vols[name] = v
return v, nil
}
// Remove deletes a volume.
func (d *FakeDriver) Remove(v volume.Volume) error {
if _, exists := d.vols[v.Name()]; !exists {
return fmt.Errorf("no such volume")
}
delete(d.vols, v.Name())
return nil
}
// List lists the volumes
func (d *FakeDriver) List() ([]volume.Volume, error) {
var vols []volume.Volume
for _, v := range d.vols {
vols = append(vols, v)
}
return vols, nil
}
// Get gets the volume
func (d *FakeDriver) Get(name string) (volume.Volume, error) {
if v, exists := d.vols[name]; exists {
return v, nil
}
return nil, fmt.Errorf("no such volume")
}