github.com/a4a881d4/docker@v1.9.0-rc2/volume/store/store_test.go (about)

     1  package store
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/volume"
     7  	"github.com/docker/docker/volume/drivers"
     8  	vt "github.com/docker/docker/volume/testutils"
     9  )
    10  
    11  func TestList(t *testing.T) {
    12  	volumedrivers.Register(vt.FakeDriver{}, "fake")
    13  	s := New()
    14  	s.AddAll([]volume.Volume{vt.NewFakeVolume("fake1"), vt.NewFakeVolume("fake2")})
    15  	l := s.List()
    16  	if len(l) != 2 {
    17  		t.Fatalf("Expected 2 volumes in the store, got %v: %v", len(l), l)
    18  	}
    19  }
    20  
    21  func TestGet(t *testing.T) {
    22  	volumedrivers.Register(vt.FakeDriver{}, "fake")
    23  	s := New()
    24  	s.AddAll([]volume.Volume{vt.NewFakeVolume("fake1"), vt.NewFakeVolume("fake2")})
    25  	v, err := s.Get("fake1")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if v.Name() != "fake1" {
    30  		t.Fatalf("Expected fake1 volume, got %v", v)
    31  	}
    32  
    33  	if _, err := s.Get("fake4"); err != ErrNoSuchVolume {
    34  		t.Fatalf("Expected ErrNoSuchVolume error, got %v", err)
    35  	}
    36  }
    37  
    38  func TestCreate(t *testing.T) {
    39  	volumedrivers.Register(vt.FakeDriver{}, "fake")
    40  	s := New()
    41  	v, err := s.Create("fake1", "fake", nil)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	if v.Name() != "fake1" {
    46  		t.Fatalf("Expected fake1 volume, got %v", v)
    47  	}
    48  	if l := s.List(); len(l) != 1 {
    49  		t.Fatalf("Expected 1 volume in the store, got %v: %v", len(l), l)
    50  	}
    51  
    52  	if _, err := s.Create("none", "none", nil); err == nil {
    53  		t.Fatalf("Expected unknown driver error, got nil")
    54  	}
    55  
    56  	_, err = s.Create("fakeError", "fake", map[string]string{"error": "create error"})
    57  	if err == nil || err.Error() != "create error" {
    58  		t.Fatalf("Expected create error, got %v", err)
    59  	}
    60  }
    61  
    62  func TestRemove(t *testing.T) {
    63  	volumedrivers.Register(vt.FakeDriver{}, "fake")
    64  	s := New()
    65  	if err := s.Remove(vt.NoopVolume{}); err != ErrNoSuchVolume {
    66  		t.Fatalf("Expected ErrNoSuchVolume error, got %v", err)
    67  	}
    68  	v, err := s.Create("fake1", "fake", nil)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	s.Increment(v)
    73  	if err := s.Remove(v); err != ErrVolumeInUse {
    74  		t.Fatalf("Expected ErrVolumeInUse error, got %v", err)
    75  	}
    76  	s.Decrement(v)
    77  	if err := s.Remove(v); err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if l := s.List(); len(l) != 0 {
    81  		t.Fatalf("Expected 0 volumes in the store, got %v, %v", len(l), l)
    82  	}
    83  }
    84  
    85  func TestIncrement(t *testing.T) {
    86  	s := New()
    87  	v := vt.NewFakeVolume("fake1")
    88  	s.Increment(v)
    89  	if l := s.List(); len(l) != 1 {
    90  		t.Fatalf("Expected 1 volume, got %v, %v", len(l), l)
    91  	}
    92  	if c := s.Count(v); c != 1 {
    93  		t.Fatalf("Expected 1 counter, got %v", c)
    94  	}
    95  
    96  	s.Increment(v)
    97  	if l := s.List(); len(l) != 1 {
    98  		t.Fatalf("Expected 1 volume, got %v, %v", len(l), l)
    99  	}
   100  	if c := s.Count(v); c != 2 {
   101  		t.Fatalf("Expected 2 counter, got %v", c)
   102  	}
   103  
   104  	v2 := vt.NewFakeVolume("fake2")
   105  	s.Increment(v2)
   106  	if l := s.List(); len(l) != 2 {
   107  		t.Fatalf("Expected 2 volume, got %v, %v", len(l), l)
   108  	}
   109  }
   110  
   111  func TestDecrement(t *testing.T) {
   112  	s := New()
   113  	v := vt.NoopVolume{}
   114  	s.Decrement(v)
   115  	if c := s.Count(v); c != 0 {
   116  		t.Fatalf("Expected 0 volumes, got %v", c)
   117  	}
   118  
   119  	s.Increment(v)
   120  	s.Increment(v)
   121  	s.Decrement(v)
   122  	if c := s.Count(v); c != 1 {
   123  		t.Fatalf("Expected 1 volume, got %v", c)
   124  	}
   125  
   126  	s.Decrement(v)
   127  	if c := s.Count(v); c != 0 {
   128  		t.Fatalf("Expected 0 volumes, got %v", c)
   129  	}
   130  
   131  	// Test counter cannot be negative.
   132  	s.Decrement(v)
   133  	if c := s.Count(v); c != 0 {
   134  		t.Fatalf("Expected 0 volumes, got %v", c)
   135  	}
   136  }
   137  
   138  func TestFilterByDriver(t *testing.T) {
   139  	s := New()
   140  
   141  	s.Increment(vt.NewFakeVolume("fake1"))
   142  	s.Increment(vt.NewFakeVolume("fake2"))
   143  	s.Increment(vt.NoopVolume{})
   144  
   145  	if l := s.FilterByDriver("fake"); len(l) != 2 {
   146  		t.Fatalf("Expected 2 volumes, got %v, %v", len(l), l)
   147  	}
   148  
   149  	if l := s.FilterByDriver("noop"); len(l) != 1 {
   150  		t.Fatalf("Expected 1 volume, got %v, %v", len(l), l)
   151  	}
   152  }