github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/volume/store/store_test.go (about)

     1  package store
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/volume"
    13  	"github.com/docker/docker/volume/drivers"
    14  	volumetestutils "github.com/docker/docker/volume/testutils"
    15  )
    16  
    17  func TestCreate(t *testing.T) {
    18  	volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
    19  	defer volumedrivers.Unregister("fake")
    20  	dir, err := ioutil.TempDir("", "test-create")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer os.RemoveAll(dir)
    25  
    26  	s, err := New(dir)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	v, err := s.Create("fake1", "fake", nil, nil)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	if v.Name() != "fake1" {
    35  		t.Fatalf("Expected fake1 volume, got %v", v)
    36  	}
    37  	if l, _, _ := s.List(); len(l) != 1 {
    38  		t.Fatalf("Expected 1 volume in the store, got %v: %v", len(l), l)
    39  	}
    40  
    41  	if _, err := s.Create("none", "none", nil, nil); err == nil {
    42  		t.Fatalf("Expected unknown driver error, got nil")
    43  	}
    44  
    45  	_, err = s.Create("fakeerror", "fake", map[string]string{"error": "create error"}, nil)
    46  	expected := &OpErr{Op: "create", Name: "fakeerror", Err: errors.New("create error")}
    47  	if err != nil && err.Error() != expected.Error() {
    48  		t.Fatalf("Expected create fakeError: create error, got %v", err)
    49  	}
    50  }
    51  
    52  func TestRemove(t *testing.T) {
    53  	volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
    54  	volumedrivers.Register(volumetestutils.NewFakeDriver("noop"), "noop")
    55  	defer volumedrivers.Unregister("fake")
    56  	defer volumedrivers.Unregister("noop")
    57  	dir, err := ioutil.TempDir("", "test-remove")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	defer os.RemoveAll(dir)
    62  	s, err := New(dir)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	// doing string compare here since this error comes directly from the driver
    68  	expected := "no such volume"
    69  	if err := s.Remove(volumetestutils.NoopVolume{}); err == nil || !strings.Contains(err.Error(), expected) {
    70  		t.Fatalf("Expected error %q, got %v", expected, err)
    71  	}
    72  
    73  	v, err := s.CreateWithRef("fake1", "fake", "fake", nil, nil)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if err := s.Remove(v); !IsInUse(err) {
    79  		t.Fatalf("Expected ErrVolumeInUse error, got %v", err)
    80  	}
    81  	s.Dereference(v, "fake")
    82  	if err := s.Remove(v); err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	if l, _, _ := s.List(); len(l) != 0 {
    86  		t.Fatalf("Expected 0 volumes in the store, got %v, %v", len(l), l)
    87  	}
    88  }
    89  
    90  func TestList(t *testing.T) {
    91  	volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
    92  	volumedrivers.Register(volumetestutils.NewFakeDriver("fake2"), "fake2")
    93  	defer volumedrivers.Unregister("fake")
    94  	defer volumedrivers.Unregister("fake2")
    95  	dir, err := ioutil.TempDir("", "test-list")
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	defer os.RemoveAll(dir)
   100  
   101  	s, err := New(dir)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	if _, err := s.Create("test", "fake", nil, nil); err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	if _, err := s.Create("test2", "fake2", nil, nil); err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	ls, _, err := s.List()
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	if len(ls) != 2 {
   117  		t.Fatalf("expected 2 volumes, got: %d", len(ls))
   118  	}
   119  	if err := s.Shutdown(); err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	// and again with a new store
   124  	s, err = New(dir)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	ls, _, err = s.List()
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	if len(ls) != 2 {
   133  		t.Fatalf("expected 2 volumes, got: %d", len(ls))
   134  	}
   135  }
   136  
   137  func TestFilterByDriver(t *testing.T) {
   138  	volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
   139  	volumedrivers.Register(volumetestutils.NewFakeDriver("noop"), "noop")
   140  	defer volumedrivers.Unregister("fake")
   141  	defer volumedrivers.Unregister("noop")
   142  	dir, err := ioutil.TempDir("", "test-filter-driver")
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  	s, err := New(dir)
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	if _, err := s.Create("fake1", "fake", 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  	if _, err := s.Create("fake3", "noop", nil, nil); err != nil {
   158  		t.Fatal(err)
   159  	}
   160  
   161  	if l, _ := s.FilterByDriver("fake"); len(l) != 2 {
   162  		t.Fatalf("Expected 2 volumes, got %v, %v", len(l), l)
   163  	}
   164  
   165  	if l, _ := s.FilterByDriver("noop"); len(l) != 1 {
   166  		t.Fatalf("Expected 1 volume, got %v, %v", len(l), l)
   167  	}
   168  }
   169  
   170  func TestFilterByUsed(t *testing.T) {
   171  	volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
   172  	volumedrivers.Register(volumetestutils.NewFakeDriver("noop"), "noop")
   173  	dir, err := ioutil.TempDir("", "test-filter-used")
   174  	if err != nil {
   175  		t.Fatal(err)
   176  	}
   177  
   178  	s, err := New(dir)
   179  	if err != nil {
   180  		t.Fatal(err)
   181  	}
   182  
   183  	if _, err := s.CreateWithRef("fake1", "fake", "volReference", nil, nil); err != nil {
   184  		t.Fatal(err)
   185  	}
   186  	if _, err := s.Create("fake2", "fake", nil, nil); err != nil {
   187  		t.Fatal(err)
   188  	}
   189  
   190  	vols, _, err := s.List()
   191  	if err != nil {
   192  		t.Fatal(err)
   193  	}
   194  
   195  	dangling := s.FilterByUsed(vols, false)
   196  	if len(dangling) != 1 {
   197  		t.Fatalf("expected 1 dangling volume, got %v", len(dangling))
   198  	}
   199  	if dangling[0].Name() != "fake2" {
   200  		t.Fatalf("expected dangling volume fake2, got %s", dangling[0].Name())
   201  	}
   202  
   203  	used := s.FilterByUsed(vols, true)
   204  	if len(used) != 1 {
   205  		t.Fatalf("expected 1 used volume, got %v", len(used))
   206  	}
   207  	if used[0].Name() != "fake1" {
   208  		t.Fatalf("expected used volume fake1, got %s", used[0].Name())
   209  	}
   210  }
   211  
   212  func TestDerefMultipleOfSameRef(t *testing.T) {
   213  	volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
   214  	dir, err := ioutil.TempDir("", "test-same-deref")
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  	defer os.RemoveAll(dir)
   219  	s, err := New(dir)
   220  	if err != nil {
   221  		t.Fatal(err)
   222  	}
   223  
   224  	v, err := s.CreateWithRef("fake1", "fake", "volReference", nil, nil)
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  
   229  	if _, err := s.GetWithRef("fake1", "fake", "volReference"); err != nil {
   230  		t.Fatal(err)
   231  	}
   232  
   233  	s.Dereference(v, "volReference")
   234  	if err := s.Remove(v); err != nil {
   235  		t.Fatal(err)
   236  	}
   237  }
   238  
   239  func TestCreateKeepOptsLabelsWhenExistsRemotely(t *testing.T) {
   240  	vd := volumetestutils.NewFakeDriver("fake")
   241  	volumedrivers.Register(vd, "fake")
   242  	dir, err := ioutil.TempDir("", "test-same-deref")
   243  	if err != nil {
   244  		t.Fatal(err)
   245  	}
   246  	defer os.RemoveAll(dir)
   247  	s, err := New(dir)
   248  	if err != nil {
   249  		t.Fatal(err)
   250  	}
   251  
   252  	// Create a volume in the driver directly
   253  	if _, err := vd.Create("foo", nil); err != nil {
   254  		t.Fatal(err)
   255  	}
   256  
   257  	v, err := s.Create("foo", "fake", nil, map[string]string{"hello": "world"})
   258  	if err != nil {
   259  		t.Fatal(err)
   260  	}
   261  
   262  	switch dv := v.(type) {
   263  	case volume.DetailedVolume:
   264  		if dv.Labels()["hello"] != "world" {
   265  			t.Fatalf("labels don't match")
   266  		}
   267  	default:
   268  		t.Fatalf("got unexpected type: %T", v)
   269  	}
   270  }
   271  
   272  func TestDefererencePluginOnCreateError(t *testing.T) {
   273  	var (
   274  		l   net.Listener
   275  		err error
   276  	)
   277  
   278  	for i := 32768; l == nil && i < 40000; i++ {
   279  		l, err = net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", i))
   280  	}
   281  	if l == nil {
   282  		t.Fatalf("could not create listener: %v", err)
   283  	}
   284  	defer l.Close()
   285  
   286  	d := volumetestutils.NewFakeDriver("TestDefererencePluginOnCreateError")
   287  	p, err := volumetestutils.MakeFakePlugin(d, l)
   288  	if err != nil {
   289  		t.Fatal(err)
   290  	}
   291  
   292  	pg := volumetestutils.NewFakePluginGetter(p)
   293  	volumedrivers.RegisterPluginGetter(pg)
   294  
   295  	dir, err := ioutil.TempDir("", "test-plugin-deref-err")
   296  	if err != nil {
   297  		t.Fatal(err)
   298  	}
   299  	defer os.RemoveAll(dir)
   300  
   301  	s, err := New(dir)
   302  	if err != nil {
   303  		t.Fatal(err)
   304  	}
   305  
   306  	// create a good volume so we have a plugin reference
   307  	_, err = s.Create("fake1", d.Name(), nil, nil)
   308  	if err != nil {
   309  		t.Fatal(err)
   310  	}
   311  
   312  	// Now create another one expecting an error
   313  	_, err = s.Create("fake2", d.Name(), map[string]string{"error": "some error"}, nil)
   314  	if err == nil || !strings.Contains(err.Error(), "some error") {
   315  		t.Fatalf("expected an error on create: %v", err)
   316  	}
   317  
   318  	// There should be only 1 plugin reference
   319  	if refs := volumetestutils.FakeRefs(p); refs != 1 {
   320  		t.Fatalf("expected 1 plugin reference, got: %d", refs)
   321  	}
   322  }