github.com/jingleWang/moby@v1.13.1/volume/store/store_test.go (about)

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