github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/volume/testutils/testutils.go (about)

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