forked from yp-engineering/rbd-docker-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
124 lines (98 loc) · 3.67 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
// 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 (
"fmt"
"os"
"testing"
dkvolume "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?
var (
testDriver cephRBDVolumeDriver
)
func TestMain(m *testing.M) {
cephConf := os.Getenv("CEPH_CONF")
testDriver = newCephRBDVolumeDriver(
"test",
"",
"admin",
"rbd",
dkvolume.DefaultDockerRootDirectory,
cephConf,
)
defer testDriver.shutdown()
os.Exit(m.Run())
}
func TestDriverReload(t *testing.T) {
t.Skip("This causes an error at driver.go:755 rbdImage.Open()")
testDriver.reload()
}
func TestLocalLockerCookie(t *testing.T) {
assert.NotEqual(t, "HOST_UNKNOWN", testDriver.localLockerCookie())
}
func TestRbdImageExists_noName(t *testing.T) {
f_bool, err := testDriver.rbdImageExists(testDriver.defaultPool, "")
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.defaultPool, "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.defaultPool, 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.defaultPool, 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.defaultPool, pool, "Pool should be same")
assert.Equal(t, "foo", name, "Name should be same")
assert.Equal(t, 1024, size, "Size should be same")
}
func TestSh_success(t *testing.T) {
out, err := sh("ls")
assert.Nil(t, err, formatError("sh", err))
assert.Contains(t, out, "driver_test.go")
}
func TestSh_fail(t *testing.T) {
_, err := sh("false")
assert.NotNil(t, err, formatError("false", err))
}
// Helpers
func formatError(name string, err error) string {
return fmt.Sprintf("ERROR calling %s: %s", 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
}