github.com/wulonghui/docker@v1.8.0-rc2/daemon/volumes_linux_unit_test.go (about)

     1  // +build experimental
     2  
     3  package daemon
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/docker/docker/runconfig"
     9  	"github.com/docker/docker/volume"
    10  	"github.com/docker/docker/volume/drivers"
    11  )
    12  
    13  type fakeDriver struct{}
    14  
    15  func (fakeDriver) Name() string                              { return "fake" }
    16  func (fakeDriver) Create(name string) (volume.Volume, error) { return nil, nil }
    17  func (fakeDriver) Remove(v volume.Volume) error              { return nil }
    18  
    19  func TestGetVolumeDriver(t *testing.T) {
    20  	_, err := getVolumeDriver("missing")
    21  	if err == nil {
    22  		t.Fatal("Expected error, was nil")
    23  	}
    24  
    25  	volumedrivers.Register(fakeDriver{}, "fake")
    26  	d, err := getVolumeDriver("fake")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	if d.Name() != "fake" {
    31  		t.Fatalf("Expected fake driver, got %s\n", d.Name())
    32  	}
    33  }
    34  
    35  func TestParseBindMount(t *testing.T) {
    36  	cases := []struct {
    37  		bind       string
    38  		driver     string
    39  		expDest    string
    40  		expSource  string
    41  		expName    string
    42  		expDriver  string
    43  		mountLabel string
    44  		expRW      bool
    45  		fail       bool
    46  	}{
    47  		{"/tmp:/tmp", "", "/tmp", "/tmp", "", "", "", true, false},
    48  		{"/tmp:/tmp:ro", "", "/tmp", "/tmp", "", "", "", false, false},
    49  		{"/tmp:/tmp:rw", "", "/tmp", "/tmp", "", "", "", true, false},
    50  		{"/tmp:/tmp:foo", "", "/tmp", "/tmp", "", "", "", false, true},
    51  		{"name:/tmp", "", "/tmp", "", "name", "local", "", true, false},
    52  		{"name:/tmp", "external", "/tmp", "", "name", "external", "", true, false},
    53  		{"name:/tmp:ro", "local", "/tmp", "", "name", "local", "", false, false},
    54  		{"local/name:/tmp:rw", "", "/tmp", "", "local/name", "local", "", true, false},
    55  	}
    56  
    57  	for _, c := range cases {
    58  		conf := &runconfig.Config{VolumeDriver: c.driver}
    59  		m, err := parseBindMount(c.bind, c.mountLabel, conf)
    60  		if c.fail {
    61  			if err == nil {
    62  				t.Fatalf("Expected error, was nil, for spec %s\n", c.bind)
    63  			}
    64  			continue
    65  		}
    66  
    67  		if m.Destination != c.expDest {
    68  			t.Fatalf("Expected destination %s, was %s, for spec %s\n", c.expDest, m.Destination, c.bind)
    69  		}
    70  
    71  		if m.Source != c.expSource {
    72  			t.Fatalf("Expected source %s, was %s, for spec %s\n", c.expSource, m.Source, c.bind)
    73  		}
    74  
    75  		if m.Name != c.expName {
    76  			t.Fatalf("Expected name %s, was %s for spec %s\n", c.expName, m.Name, c.bind)
    77  		}
    78  
    79  		if m.Driver != c.expDriver {
    80  			t.Fatalf("Expected driver %s, was %s, for spec %s\n", c.expDriver, m.Driver, c.bind)
    81  		}
    82  
    83  		if m.RW != c.expRW {
    84  			t.Fatalf("Expected RW %v, was %v for spec %s\n", c.expRW, m.RW, c.bind)
    85  		}
    86  	}
    87  }