forked from yp-engineering/rbd-docker-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver_test.go
131 lines (104 loc) · 4.24 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
// Copyright 2015 YP LLC.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
// unit tests that don't rely on ceph
import (
"flag"
"fmt"
"os"
"testing"
"github.com/docker/go-plugins-helpers/volume"
"github.com/stretchr/testify/assert"
)
// TODO: tests that need ceph
// make fake cluster?
// use dockerized container with ceph for tests?
const (
TEST_SOCKET_PATH = "/tmp/rbd-test.sock"
EXPECTED_ACTIVATION_RESPONSE = "{\"Implements\": [\"VolumeDriver\"]}"
)
var (
testDriver cephRBDVolumeDriver
)
func TestMain(m *testing.M) {
flag.Parse()
cephConf := os.Getenv("CEPH_CONF")
testDriver = newCephRBDVolumeDriver(
"test",
"",
"admin",
"rbd",
volume.DefaultDockerRootDirectory,
cephConf,
)
handler := volume.NewHandler(testDriver)
// Serve won't return so spin off routine
go handler.ServeUnix(TEST_SOCKET_PATH, 0)
os.Exit(m.Run())
}
func TestLocalLockerCookie(t *testing.T) {
assert.NotEqual(t, "HOST_UNKNOWN", testDriver.localLockerCookie())
}
func TestRbdImageExists_noName(t *testing.T) {
f_bool, err := testDriver.rbdImageExists(testDriver.pool, "")
assert.Equal(t, false, f_bool, fmt.Sprintf("%s", err))
}
func TestRbdImageExists_withName(t *testing.T) {
t.Skip("This fails for many reasons. Need to figure out how to do this in a container.")
err := testDriver.createRBDImage("rbd", "foo", 1, "xfs")
assert.Nil(t, err, formatError("createRBDImage", err))
t_bool, err := testDriver.rbdImageExists(testDriver.pool, "foo")
assert.Equal(t, true, t_bool, formatError("rbdImageExists", err))
}
// cephRBDDriver.parseImagePoolNameSize(string) (string, string, int, error)
func TestParseImagePoolNameSize_name(t *testing.T) {
pool, name, size := parseImageAndHandleError(t, "foo")
assert.Equal(t, testDriver.pool, pool, "Pool should be same")
assert.Equal(t, "foo", name, "Name should be same")
assert.Equal(t, *defaultImageSizeMB, size, "Size should be same")
}
func TestParseImagePoolNameSize_complexName(t *testing.T) {
pool, name, size := parseImageAndHandleError(t, "es-data1_v2.3")
assert.Equal(t, testDriver.pool, pool, "Pool should be same")
assert.Equal(t, "es-data1_v2.3", name, "Name should be same")
assert.Equal(t, *defaultImageSizeMB, size, "Size should be same")
}
func TestParseImagePoolNameSize_withPool(t *testing.T) {
pool, name, size := parseImageAndHandleError(t, "liverpool/foo")
assert.Equal(t, "liverpool", pool, "Pool should be same")
assert.Equal(t, "foo", name, "Name should be same")
assert.Equal(t, *defaultImageSizeMB, size, "Size should be same")
}
func TestParseImagePoolNameSize_withSize(t *testing.T) {
pool, name, size := parseImageAndHandleError(t, "liverpool/foo@1024")
assert.Equal(t, "liverpool", pool, "Pool should be same")
assert.Equal(t, "foo", name, "Name should be same")
assert.Equal(t, 1024, size, "Size should be same")
}
func TestParseImagePoolNameSize_withPoolAndSize(t *testing.T) {
pool, name, size := parseImageAndHandleError(t, "foo@1024")
assert.Equal(t, testDriver.pool, pool, "Pool should be same")
assert.Equal(t, "foo", name, "Name should be same")
assert.Equal(t, 1024, size, "Size should be same")
}
// need a way to test the socket access using basic format - since this broke
// in golang 1.6 with strict Host header checking even if using Unix sockets.
// Requires socat and sudo
//
// Error response when built with golang 1.6: 400 Bad Request: missing required Host header
func TestSocketActivate(t *testing.T) {
t.Skip("This test requires socket, which seems to need root privs to build. So this test fails if run as normal user. TODO: Find a proper workaround.")
out, err := sh("bash", "-c", "echo \"POST /Plugin.Activate HTTP/1.1\r\n\" | sudo socat unix-connect:/tmp/rbd-test.sock STDIO")
assert.Nil(t, err, formatError("socat plugin activate", err))
assert.Contains(t, out, EXPECTED_ACTIVATION_RESPONSE, "Expecting Implements VolumeDriver message")
}
// Helpers
func formatError(name string, err error) string {
return fmt.Sprintf("ERROR calling %s: %q", name, err)
}
func parseImageAndHandleError(t *testing.T, name string) (string, string, int) {
pool, name, size, err := testDriver.parseImagePoolNameSize(name)
assert.Nil(t, err, formatError("parseImagePoolNameSize", err))
return pool, name, size
}