2015-09-18 19:58:05 -04:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2015-10-19 20:41:22 -04:00
|
|
|
"errors"
|
2015-09-23 16:29:14 -04:00
|
|
|
"strings"
|
2015-09-18 19:58:05 -04:00
|
|
|
"testing"
|
|
|
|
|
2016-09-07 20:01:10 -04:00
|
|
|
pluginstore "github.com/docker/docker/plugin/store"
|
2015-09-18 19:58:05 -04:00
|
|
|
"github.com/docker/docker/volume/drivers"
|
2016-09-12 03:34:19 -04:00
|
|
|
volumetestutils "github.com/docker/docker/volume/testutils"
|
2015-09-18 19:58:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
2016-09-07 20:01:10 -04:00
|
|
|
pluginStore := pluginstore.NewStore("/var/lib/docker")
|
|
|
|
volumedrivers.RegisterPluginGetter(pluginStore)
|
|
|
|
|
2016-09-12 03:34:19 -04:00
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
|
2015-09-23 16:29:14 -04:00
|
|
|
defer volumedrivers.Unregister("fake")
|
2016-03-16 17:52:34 -04:00
|
|
|
s, err := New("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
v, err := s.Create("fake1", "fake", nil, nil)
|
2015-09-18 19:58:05 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if v.Name() != "fake1" {
|
|
|
|
t.Fatalf("Expected fake1 volume, got %v", v)
|
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
if l, _, _ := s.List(); len(l) != 1 {
|
2015-09-18 19:58:05 -04:00
|
|
|
t.Fatalf("Expected 1 volume in the store, got %v: %v", len(l), l)
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
if _, err := s.Create("none", "none", nil, nil); err == nil {
|
2015-09-18 19:58:05 -04:00
|
|
|
t.Fatalf("Expected unknown driver error, got nil")
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
_, err = s.Create("fakeerror", "fake", map[string]string{"error": "create error"}, nil)
|
2015-11-13 15:30:13 -05:00
|
|
|
expected := &OpErr{Op: "create", Name: "fakeerror", Err: errors.New("create error")}
|
2015-10-19 20:41:22 -04:00
|
|
|
if err != nil && err.Error() != expected.Error() {
|
|
|
|
t.Fatalf("Expected create fakeError: create error, got %v", err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
2016-09-12 03:34:19 -04:00
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
|
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("noop"), "noop")
|
2015-09-23 16:29:14 -04:00
|
|
|
defer volumedrivers.Unregister("fake")
|
|
|
|
defer volumedrivers.Unregister("noop")
|
2016-03-16 17:52:34 -04:00
|
|
|
s, err := New("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
|
|
|
|
// doing string compare here since this error comes directly from the driver
|
|
|
|
expected := "no such volume"
|
2016-09-12 03:34:19 -04:00
|
|
|
if err := s.Remove(volumetestutils.NoopVolume{}); err == nil || !strings.Contains(err.Error(), expected) {
|
2015-09-23 16:29:14 -04:00
|
|
|
t.Fatalf("Expected error %q, got %v", expected, err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
v, err := s.CreateWithRef("fake1", "fake", "fake", nil, nil)
|
2015-09-18 19:58:05 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
|
2015-10-19 20:41:22 -04:00
|
|
|
if err := s.Remove(v); !IsInUse(err) {
|
2015-09-23 16:29:14 -04:00
|
|
|
t.Fatalf("Expected ErrVolumeInUse error, got %v", err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
s.Dereference(v, "fake")
|
2015-09-18 19:58:05 -04:00
|
|
|
if err := s.Remove(v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
if l, _, _ := s.List(); len(l) != 0 {
|
2015-09-18 19:58:05 -04:00
|
|
|
t.Fatalf("Expected 0 volumes in the store, got %v, %v", len(l), l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
func TestList(t *testing.T) {
|
2016-09-12 03:34:19 -04:00
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
|
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("fake2"), "fake2")
|
2015-09-23 16:29:14 -04:00
|
|
|
defer volumedrivers.Unregister("fake")
|
|
|
|
defer volumedrivers.Unregister("fake2")
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
s, err := New("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := s.Create("test", "fake", nil, nil); err != nil {
|
2015-09-23 16:29:14 -04:00
|
|
|
t.Fatal(err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
if _, err := s.Create("test2", "fake2", nil, nil); err != nil {
|
2015-09-23 16:29:14 -04:00
|
|
|
t.Fatal(err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
ls, _, err := s.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
if len(ls) != 2 {
|
|
|
|
t.Fatalf("expected 2 volumes, got: %d", len(ls))
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
// and again with a new store
|
2016-03-16 17:52:34 -04:00
|
|
|
s, err = New("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
ls, _, err = s.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(ls) != 2 {
|
|
|
|
t.Fatalf("expected 2 volumes, got: %d", len(ls))
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
func TestFilterByDriver(t *testing.T) {
|
2016-09-12 03:34:19 -04:00
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
|
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("noop"), "noop")
|
2015-09-23 16:29:14 -04:00
|
|
|
defer volumedrivers.Unregister("fake")
|
|
|
|
defer volumedrivers.Unregister("noop")
|
2016-03-16 17:52:34 -04:00
|
|
|
s, err := New("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-18 19:58:05 -04:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
if _, err := s.Create("fake1", "fake", nil, nil); err != nil {
|
2015-09-23 16:29:14 -04:00
|
|
|
t.Fatal(err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
if _, err := s.Create("fake2", "fake", nil, nil); err != nil {
|
2015-09-23 16:29:14 -04:00
|
|
|
t.Fatal(err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
if _, err := s.Create("fake3", "noop", nil, nil); err != nil {
|
2015-09-23 16:29:14 -04:00
|
|
|
t.Fatal(err)
|
2015-09-18 19:58:05 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
if l, _ := s.FilterByDriver("fake"); len(l) != 2 {
|
2015-09-18 19:58:05 -04:00
|
|
|
t.Fatalf("Expected 2 volumes, got %v, %v", len(l), l)
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
if l, _ := s.FilterByDriver("noop"); len(l) != 1 {
|
2015-09-18 19:58:05 -04:00
|
|
|
t.Fatalf("Expected 1 volume, got %v, %v", len(l), l)
|
|
|
|
}
|
|
|
|
}
|
2016-01-25 14:39:41 -05:00
|
|
|
|
|
|
|
func TestFilterByUsed(t *testing.T) {
|
2016-09-12 03:34:19 -04:00
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
|
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("noop"), "noop")
|
2016-01-25 14:39:41 -05:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
s, err := New("")
|
|
|
|
if err != nil {
|
2016-01-25 14:39:41 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
|
|
|
|
if _, err := s.CreateWithRef("fake1", "fake", "volReference", nil, nil); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := s.Create("fake2", "fake", nil, nil); err != nil {
|
2016-01-25 14:39:41 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vols, _, err := s.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dangling := s.FilterByUsed(vols, false)
|
|
|
|
if len(dangling) != 1 {
|
2016-12-01 04:07:38 -05:00
|
|
|
t.Fatalf("expected 1 dangling volume, got %v", len(dangling))
|
2016-01-25 14:39:41 -05:00
|
|
|
}
|
|
|
|
if dangling[0].Name() != "fake2" {
|
2016-12-01 04:07:38 -05:00
|
|
|
t.Fatalf("expected dangling volume fake2, got %s", dangling[0].Name())
|
2016-01-25 14:39:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
used := s.FilterByUsed(vols, true)
|
|
|
|
if len(used) != 1 {
|
|
|
|
t.Fatalf("expected 1 used volume, got %v", len(used))
|
|
|
|
}
|
|
|
|
if used[0].Name() != "fake1" {
|
|
|
|
t.Fatalf("expected used volume fake1, got %s", used[0].Name())
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 21:10:45 -05:00
|
|
|
|
|
|
|
func TestDerefMultipleOfSameRef(t *testing.T) {
|
2016-09-12 03:34:19 -04:00
|
|
|
volumedrivers.Register(volumetestutils.NewFakeDriver("fake"), "fake")
|
2016-02-16 21:10:45 -05:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
s, err := New("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := s.CreateWithRef("fake1", "fake", "volReference", nil, nil)
|
2016-02-16 21:10:45 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s.GetWithRef("fake1", "fake", "volReference"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Dereference(v, "volReference")
|
|
|
|
if err := s.Remove(v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|