github.com/dpiddy/docker@v1.12.2-rc1/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) (string, error) { return "noop", nil }
    23  
    24  // Unmount unmounts the volume from the container
    25  func (NoopVolume) Unmount(_ string) error { return nil }
    26  
    27  // Status proivdes low-level details about the volume
    28  func (NoopVolume) Status() map[string]interface{} { return nil }
    29  
    30  // FakeVolume is a fake volume with a random name
    31  type FakeVolume struct {
    32  	name       string
    33  	driverName string
    34  }
    35  
    36  // NewFakeVolume creates a new fake volume for testing
    37  func NewFakeVolume(name string, driverName string) volume.Volume {
    38  	return FakeVolume{name: name, driverName: driverName}
    39  }
    40  
    41  // Name is the name of the volume
    42  func (f FakeVolume) Name() string { return f.name }
    43  
    44  // DriverName is the name of the driver
    45  func (f FakeVolume) DriverName() string { return f.driverName }
    46  
    47  // Path is the filesystem path to the volume
    48  func (FakeVolume) Path() string { return "fake" }
    49  
    50  // Mount mounts the volume in the container
    51  func (FakeVolume) Mount(_ string) (string, error) { return "fake", nil }
    52  
    53  // Unmount unmounts the volume from the container
    54  func (FakeVolume) Unmount(_ string) error { return nil }
    55  
    56  // Status proivdes low-level details about the volume
    57  func (FakeVolume) Status() map[string]interface{} { return nil }
    58  
    59  // FakeDriver is a driver that generates fake volumes
    60  type FakeDriver struct {
    61  	name string
    62  	vols map[string]volume.Volume
    63  }
    64  
    65  // NewFakeDriver creates a new FakeDriver with the specified name
    66  func NewFakeDriver(name string) volume.Driver {
    67  	return &FakeDriver{
    68  		name: name,
    69  		vols: make(map[string]volume.Volume),
    70  	}
    71  }
    72  
    73  // Name is the name of the driver
    74  func (d *FakeDriver) Name() string { return d.name }
    75  
    76  // Create initializes a fake volume.
    77  // It returns an error if the options include an "error" key with a message
    78  func (d *FakeDriver) Create(name string, opts map[string]string) (volume.Volume, error) {
    79  	if opts != nil && opts["error"] != "" {
    80  		return nil, fmt.Errorf(opts["error"])
    81  	}
    82  	v := NewFakeVolume(name, d.name)
    83  	d.vols[name] = v
    84  	return v, nil
    85  }
    86  
    87  // Remove deletes a volume.
    88  func (d *FakeDriver) Remove(v volume.Volume) error {
    89  	if _, exists := d.vols[v.Name()]; !exists {
    90  		return fmt.Errorf("no such volume")
    91  	}
    92  	delete(d.vols, v.Name())
    93  	return nil
    94  }
    95  
    96  // List lists the volumes
    97  func (d *FakeDriver) List() ([]volume.Volume, error) {
    98  	var vols []volume.Volume
    99  	for _, v := range d.vols {
   100  		vols = append(vols, v)
   101  	}
   102  	return vols, nil
   103  }
   104  
   105  // Get gets the volume
   106  func (d *FakeDriver) Get(name string) (volume.Volume, error) {
   107  	if v, exists := d.vols[name]; exists {
   108  		return v, nil
   109  	}
   110  	return nil, fmt.Errorf("no such volume")
   111  }
   112  
   113  // Scope returns the local scope
   114  func (*FakeDriver) Scope() string {
   115  	return "local"
   116  }