github.com/dpiddy/docker@v1.12.2-rc1/volume/store/store_test.go (about)

     1  package store
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/volume/drivers"
     9  	vt "github.com/docker/docker/volume/testutils"
    10  )
    11  
    12  func TestCreate(t *testing.T) {
    13  	volumedrivers.Register(vt.NewFakeDriver("fake"), "fake")
    14  	defer volumedrivers.Unregister("fake")
    15  	s, err := New("")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	v, err := s.Create("fake1", "fake", nil, nil)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	if v.Name() != "fake1" {
    24  		t.Fatalf("Expected fake1 volume, got %v", v)
    25  	}
    26  	if l, _, _ := s.List(); len(l) != 1 {
    27  		t.Fatalf("Expected 1 volume in the store, got %v: %v", len(l), l)
    28  	}
    29  
    30  	if _, err := s.Create("none", "none", nil, nil); err == nil {
    31  		t.Fatalf("Expected unknown driver error, got nil")
    32  	}
    33  
    34  	_, err = s.Create("fakeerror", "fake", map[string]string{"error": "create error"}, nil)
    35  	expected := &OpErr{Op: "create", Name: "fakeerror", Err: errors.New("create error")}
    36  	if err != nil && err.Error() != expected.Error() {
    37  		t.Fatalf("Expected create fakeError: create error, got %v", err)
    38  	}
    39  }
    40  
    41  func TestRemove(t *testing.T) {
    42  	volumedrivers.Register(vt.NewFakeDriver("fake"), "fake")
    43  	volumedrivers.Register(vt.NewFakeDriver("noop"), "noop")
    44  	defer volumedrivers.Unregister("fake")
    45  	defer volumedrivers.Unregister("noop")
    46  	s, err := New("")
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	// doing string compare here since this error comes directly from the driver
    52  	expected := "no such volume"
    53  	if err := s.Remove(vt.NoopVolume{}); err == nil || !strings.Contains(err.Error(), expected) {
    54  		t.Fatalf("Expected error %q, got %v", expected, err)
    55  	}
    56  
    57  	v, err := s.CreateWithRef("fake1", "fake", "fake", nil, nil)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	if err := s.Remove(v); !IsInUse(err) {
    63  		t.Fatalf("Expected ErrVolumeInUse error, got %v", err)
    64  	}
    65  	s.Dereference(v, "fake")
    66  	if err := s.Remove(v); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	if l, _, _ := s.List(); len(l) != 0 {
    70  		t.Fatalf("Expected 0 volumes in the store, got %v, %v", len(l), l)
    71  	}
    72  }
    73  
    74  func TestList(t *testing.T) {
    75  	volumedrivers.Register(vt.NewFakeDriver("fake"), "fake")
    76  	volumedrivers.Register(vt.NewFakeDriver("fake2"), "fake2")
    77  	defer volumedrivers.Unregister("fake")
    78  	defer volumedrivers.Unregister("fake2")
    79  
    80  	s, err := New("")
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if _, err := s.Create("test", "fake", nil, nil); err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	if _, err := s.Create("test2", "fake2", nil, nil); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	ls, _, err := s.List()
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	if len(ls) != 2 {
    96  		t.Fatalf("expected 2 volumes, got: %d", len(ls))
    97  	}
    98  
    99  	// and again with a new store
   100  	s, err = New("")
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	ls, _, err = s.List()
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	if len(ls) != 2 {
   109  		t.Fatalf("expected 2 volumes, got: %d", len(ls))
   110  	}
   111  }
   112  
   113  func TestFilterByDriver(t *testing.T) {
   114  	volumedrivers.Register(vt.NewFakeDriver("fake"), "fake")
   115  	volumedrivers.Register(vt.NewFakeDriver("noop"), "noop")
   116  	defer volumedrivers.Unregister("fake")
   117  	defer volumedrivers.Unregister("noop")
   118  	s, err := New("")
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	if _, err := s.Create("fake1", "fake", nil, nil); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	if _, err := s.Create("fake2", "fake", nil, nil); err != nil {
   127  		t.Fatal(err)
   128  	}
   129  	if _, err := s.Create("fake3", "noop", nil, nil); err != nil {
   130  		t.Fatal(err)
   131  	}
   132  
   133  	if l, _ := s.FilterByDriver("fake"); len(l) != 2 {
   134  		t.Fatalf("Expected 2 volumes, got %v, %v", len(l), l)
   135  	}
   136  
   137  	if l, _ := s.FilterByDriver("noop"); len(l) != 1 {
   138  		t.Fatalf("Expected 1 volume, got %v, %v", len(l), l)
   139  	}
   140  }
   141  
   142  func TestFilterByUsed(t *testing.T) {
   143  	volumedrivers.Register(vt.NewFakeDriver("fake"), "fake")
   144  	volumedrivers.Register(vt.NewFakeDriver("noop"), "noop")
   145  
   146  	s, err := New("")
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	if _, err := s.CreateWithRef("fake1", "fake", "volReference", nil, nil); err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	if _, err := s.Create("fake2", "fake", nil, nil); err != nil {
   155  		t.Fatal(err)
   156  	}
   157  
   158  	vols, _, err := s.List()
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  
   163  	dangling := s.FilterByUsed(vols, false)
   164  	if len(dangling) != 1 {
   165  		t.Fatalf("expected 1 danging volume, got %v", len(dangling))
   166  	}
   167  	if dangling[0].Name() != "fake2" {
   168  		t.Fatalf("expected danging volume fake2, got %s", dangling[0].Name())
   169  	}
   170  
   171  	used := s.FilterByUsed(vols, true)
   172  	if len(used) != 1 {
   173  		t.Fatalf("expected 1 used volume, got %v", len(used))
   174  	}
   175  	if used[0].Name() != "fake1" {
   176  		t.Fatalf("expected used volume fake1, got %s", used[0].Name())
   177  	}
   178  }
   179  
   180  func TestDerefMultipleOfSameRef(t *testing.T) {
   181  	volumedrivers.Register(vt.NewFakeDriver("fake"), "fake")
   182  
   183  	s, err := New("")
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  
   188  	v, err := s.CreateWithRef("fake1", "fake", "volReference", nil, nil)
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	if _, err := s.GetWithRef("fake1", "fake", "volReference"); err != nil {
   194  		t.Fatal(err)
   195  	}
   196  
   197  	s.Dereference(v, "volReference")
   198  	if err := s.Remove(v); err != nil {
   199  		t.Fatal(err)
   200  	}
   201  }