-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
275 lines (232 loc) · 7.53 KB
/
main_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strconv"
"testing"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/docker/go-plugins-helpers/volume"
"github.com/mellanox-senior-design/docker-volume-rdma/db"
"github.com/mellanox-senior-design/docker-volume-rdma/drivers"
)
func configureTest(t *testing.T) (*drivers.RDMAVolumeDriver, *volume.Handler, string, string) {
// Create temporary directory to "mount" volumes to.
tempDir, err := ioutil.TempDir("", "docker-volume-rdma")
if err != nil {
t.Fatal("Unable to create temp dir! ", err)
}
t.Log("Temp Dir: ", tempDir)
httpPort := strconv.Itoa(10000 + rand.Intn(10000))
t.Logf("Configuring server for port: %s", httpPort)
// Configure flags.
flag.Set("port", httpPort)
flag.Set("db", "in-memory")
flag.Set("sc", "on-disk")
flag.Set("scpath", tempDir)
flag.Parse()
// Configure driver and handler.
configuredDriver, configuredHandler, err := configure()
if err != nil {
t.Fatal(err)
}
// Ensure that we are using an in memory database, if this fails, check for flag parsing.
if _, ok := configuredDriver.VolumeDatabase.(db.InMemoryVolumeDatabase); !ok {
t.Fatal("Configured Driver's VolumeDatabase was not an db.InMemoryVolumeDatabase")
}
// Ensure that we are using an on disk storage controller, if this failes, check for flag parsing.
if _, ok := configuredDriver.StorageController.(drivers.OnDiskStorageController); !ok {
t.Fatal("Configured Driver's StorageController was not an drivers.OnDiskStorageController")
}
if configuredHandler == nil {
t.Fatal("Configured Handler is nil")
}
return configuredDriver, configuredHandler, tempDir, httpPort
}
func TestMain(t *testing.T) {
_, err := os.Open("/etc/docker")
if err != nil {
t.Skip("/etc/docker is required to exist.")
}
// Get Configured Driver.
_, _, tempDir, httpPort := configureTest(t)
defer os.Remove(tempDir)
// Start main!
go main()
// Create json for request
json, err := json.Marshal(volume.Request{})
if err != nil {
t.Fatal(err)
}
// Create request to server.
body := bytes.NewBuffer(json)
client := &http.Client{}
req, err := http.NewRequest("POST", "http://localhost:"+httpPort+"/VolumeDriver.Capabilities", body)
if err != nil {
t.Fatal(err)
}
// Fetch Request
req.Header.Add("Content-Type", "application/json")
_, err = client.Do(req)
if err != nil {
t.Fatal("Failed to connect to server! ", err)
}
}
func Test_createListDelete(t *testing.T) {
// Get Configured Driver.
driver, _, tempDir, _ := configureTest(t)
defer os.Remove(tempDir)
// Get a Docker approved random name
volumeName := namesgenerator.GetRandomName(0)
// List all volumes
t.Logf("driver.List(request.Volume{})")
response := driver.List(volume.Request{})
if response.Err != "" {
t.Fatal("Failed to list volumes! {} ", response.Err)
}
if len(response.Volumes) != 0 {
t.Fatal("driver.List returned ", len(response.Volumes), " volumes. Expected 0.")
}
// Create a volume
t.Logf("driver.Create(volume.Request{Name: %s, Options: map[string]string{}})", volumeName)
response = driver.Create(volume.Request{
Name: volumeName,
Options: map[string]string{},
})
if response.Err != "" { // It is sufficent that there is not an error.
t.Fatal("Failed to create volume! {Name: ", volumeName, "} ", response.Err)
}
// List all volumes
t.Logf("driver.List(request.Volume{})")
response = driver.List(volume.Request{})
if response.Err != "" {
t.Fatal("Failed to list volumes! {} ", response.Err)
}
if len(response.Volumes) != 1 {
t.Fatal("driver.List returned ", len(response.Volumes), " volumes. Expected 1.")
}
// Remove volume
t.Logf("driver.Remove(volume.Request{Name: %s})", volumeName)
response = driver.Remove(volume.Request{
Name: volumeName,
})
if response.Err != "" { // It is sufficent that there is not an error.
t.Fatal("Failed to remove volume! {Name: ", volumeName, "} ", response.Err)
}
// List all volumes
t.Logf("driver.List(request.Volume{})")
response = driver.List(volume.Request{})
if response.Err != "" {
t.Fatal("Failed to list volumes! {} ", response.Err)
}
if len(response.Volumes) == 1 {
t.Fatal("driver.Remove failed to remove volume.")
} else if len(response.Volumes) != 0 {
t.Fatal("driver.List returned ", len(response.Volumes), " volumes. Expected 0.")
}
}
func Test_badConfigurations(t *testing.T) {
// Test Volume Database
// Configure flags.
flag.Set("db", "invalid")
flag.Set("sc", "on-disk")
flag.Parse()
// Configure driver and handler.
_, _, err := configure()
if err == nil {
t.Error("Invalid -db did not cause an error.")
}
// Test Storage Controller
// Configure flags.
flag.Set("db", "in-memory")
flag.Set("sc", "invalid")
flag.Parse()
// Configure driver and handler.
_, _, err = configure()
if err == nil {
t.Error("Invalid -sc did not cause an error.")
}
}
func TestGetDatabaseConnection_inmemory(t *testing.T) {
// Test in-memory Volume Database
// Configure flags.
flag.Set("db", "in-memory")
flag.Set("sc", "on-disk")
flag.Parse()
// Configure driver and handler.
configuredDriver, _, err := configure()
if err != nil {
t.Error(err)
}
// Ensure that we are using an in memory database, if this fails, check for flag parsing.
if _, ok := configuredDriver.VolumeDatabase.(db.InMemoryVolumeDatabase); !ok {
t.Fatal("Configured Driver's VolumeDatabase was not an db.InMemoryVolumeDatabase")
}
}
func TestGetDatabaseConnection_sqlite(t *testing.T) {
// Test sqlite Volume Database
// Configure flags.
flag.Set("db", "sqlite")
flag.Set("sc", "on-disk")
flag.Parse()
// Configure driver and handler.
configuredDriver, _, err := configure()
if err != nil {
t.Error(err)
}
// Ensure that we are using an in memory database, if this fails, check for flag parsing.
if _, ok := configuredDriver.VolumeDatabase.(db.SQLVolumeDatabase); !ok {
t.Fatal("Configured Driver's VolumeDatabase was not an db.SQLVolumeDatabase")
}
}
func TestGetDatabaseConnection_mysql(t *testing.T) {
// Test mysql Volume Database
// Configure flags.
flag.Set("db", "mysql")
flag.Set("dbschema", "rdma")
flag.Set("sc", "on-disk")
flag.Parse()
// Configure driver and handler.
configuredDriver, _, err := configure()
if err != nil {
t.Error(err)
}
// Ensure that we are using an in memory database, if this fails, check for flag parsing.
if _, ok := configuredDriver.VolumeDatabase.(db.SQLVolumeDatabase); !ok {
t.Fatal("Configured Driver's VolumeDatabase was not an db.SQLVolumeDatabase")
}
}
func TestGetStorageConnection_ondisk(t *testing.T) {
// Test OnDiskStorageController Volume Database
// Configure flags.
flag.Set("sc", "on-disk")
flag.Parse()
// Configure driver and handler.
configuredDriver, _, err := configure()
if err != nil {
t.Error(err)
}
// Ensure that we are using an in memory database, if this fails, check for flag parsing.
if _, ok := configuredDriver.StorageController.(drivers.OnDiskStorageController); !ok {
t.Fatal("Configured Driver's StorageController was not an drivers.OnDiskStorageController")
}
}
func TestGetStorageConnection_glusterfs(t *testing.T) {
// Test GlusterStorageController Volume Database
// Configure flags.
flag.Set("sc", "glusterfs")
flag.Parse()
// Configure driver and handler.
configuredDriver, _, err := configure()
if err != nil {
t.Error(err)
}
// Ensure that we are using an in memory database, if this fails, check for flag parsing.
if _, ok := configuredDriver.StorageController.(drivers.GlusterStorageController); !ok {
t.Fatal("Configured Driver's StorageController was not an drivers.GlusterStorageController")
}
}