forked from MatchbookLab/local-persist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
132 lines (99 loc) · 3.42 KB
/
driver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import(
"testing"
"os"
"github.com/docker/go-plugins-helpers/volume"
)
var (
defaultTestName = "test-volume"
defaultTestMountpoint = "/tmp/data/local-persist-test"
)
func TestCreate(t *testing.T) {
driver := newLocalPersistDriver()
defaultCreateHelper(driver, t)
// test that a directory is created
_, err := os.Stat(defaultTestMountpoint)
if os.IsNotExist(err) {
t.Error("Mountpoint directory was not created:", err.Error())
}
// test that volumes has one
if len(driver.volumes) != 1 {
t.Error("Driver should have exactly 1 volume")
}
defaultCleanupHelper(driver, t)
// test that options are required
res := driver.Create(volume.Request{
Name: defaultTestName,
});
if res.Err != "The `mountpoint` option is required" {
t.Error("Should error out without mountpoint option")
}
}
func TestGet(t *testing.T) {
driver := newLocalPersistDriver()
defaultCreateHelper(driver, t)
res := driver.Get(volume.Request{ Name: defaultTestName })
if res.Err != "" {
t.Error("Should have found a volume!")
}
defaultCleanupHelper(driver, t)
}
func TestList(t *testing.T) {
driver := newLocalPersistDriver()
name := defaultTestName + "2"
mountpoint := defaultTestMountpoint + "2"
defaultCreateHelper(driver, t)
res := driver.List(volume.Request{})
if len(res.Volumes) != 1 {
t.Error("Should have found 1 volume!")
}
createHelper(driver, t, name, mountpoint)
res2 := driver.List(volume.Request{})
if len(res2.Volumes) != 2 {
t.Error("Should have found 1 volume!")
}
defaultCleanupHelper(driver, t)
cleanupHelper(driver, t, name, mountpoint)
}
func TestMountUnmountPath(t *testing.T) {
driver := newLocalPersistDriver()
defaultCreateHelper(driver, t)
// mount, mount and path should have same output (they all use Path under the hood)
pathRes := driver.Path(volume.Request{ Name: defaultTestName })
mountRes := driver.Mount(volume.MountRequest{ Name: defaultTestName })
unmountRes := driver.Unmount(volume.UnmountRequest{ Name: defaultTestName })
if !(pathRes.Mountpoint == mountRes.Mountpoint &&
mountRes.Mountpoint == unmountRes.Mountpoint &&
unmountRes.Mountpoint == defaultTestMountpoint) {
t.Error("Mount, Unmount and Path should all return the same Mountpoint")
}
}
func createHelper(driver localPersistDriver, t *testing.T, name string, mountpoint string) {
res := driver.Create(volume.Request{
Name: name,
Options: map[string]string{
"mountpoint": mountpoint,
},
});
if res.Err != "" {
t.Error(res.Err)
}
}
func defaultCreateHelper(driver localPersistDriver, t *testing.T) {
createHelper(driver, t, defaultTestName, defaultTestMountpoint)
}
func cleanupHelper(driver localPersistDriver, t *testing.T, name string, mountpoint string) {
os.RemoveAll(mountpoint)
_, err := os.Stat(mountpoint)
if !os.IsNotExist(err) {
t.Error("[Cleanup] Mountpoint still exists:", err.Error())
}
driver.Remove(volume.Request{ Name: name })
res := driver.Get(volume.Request{ Name: name })
if res.Err == "" {
t.Error("[Cleanup] Volume still exists:", res.Err)
}
}
func defaultCleanupHelper(driver localPersistDriver, t *testing.T) {
cleanupHelper(driver, t, defaultTestName, defaultTestMountpoint)
}