-
Notifications
You must be signed in to change notification settings - Fork 10
/
librbd_test.go
162 lines (124 loc) · 3.02 KB
/
librbd_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
package librbd
import (
"os"
"reflect"
"testing"
)
func TestVersion(t *testing.T) {
t.Log(Version())
}
func TestPool(t *testing.T) {
config, err := ReadConfig("/etc/rbdconfig.json")
if err != nil {
t.Fatal(err)
}
pool, err := GetPool(config, "rbd")
if err != nil {
t.Fatal(err)
}
if pool.ioctx == nil {
t.Fatal("ioctx was nil")
}
if pool.cluster == nil {
t.Fatal("rados was nil")
}
// it's ok if it fails, we just don't want the next call to unless stuff is
// broken.
pool.RemoveImage("test")
if err := pool.CreateImage("test", 10000000); err != nil {
t.Fatal(err)
}
if err := pool.CreateImage("test", 10000000); err == nil {
t.Fatal("No error was recieved trying to create image twice")
}
// FIXME finish
img, err := pool.GetImage("test")
if err != nil {
t.Fatal(err)
}
if img.imageName != "test" {
t.Fatal("Image name was not set properly")
}
if !reflect.DeepEqual(img.pool, pool) {
t.Fatal("Pool was not equal in image struct")
}
defer pool.RemoveImage("test")
items, err := pool.List()
if err != nil {
t.Fatal(err)
}
var found bool
for _, item := range items {
if item == "test" {
found = true
break
}
}
if !found {
t.Fatal("image list was invalid")
}
device, err := img.MapDevice()
if err != nil {
t.Fatal(err)
}
if _, err := os.Stat(device); err != nil {
t.Fatal(err)
}
list, err := img.ListSnapshots(100)
if err != nil {
t.Fatal(err)
}
if len(list) != 0 {
t.Fatalf("Snapshot list expected to be empty but is not: %v", list)
}
if err := img.CreateSnapshot("test-snap"); err != nil {
t.Fatal(err)
}
if err := img.CreateSnapshot("test-snap"); err == nil {
t.Fatal("Did not recieve error creating snapshot twice")
}
defer img.RemoveSnapshot("test-snap")
if list, err = img.ListSnapshots(100); err != nil {
t.Fatal(err)
}
if len(list) != 1 || !reflect.DeepEqual(list, []string{"test-snap"}) {
t.Fatal("Snapshot list after create did not match expectation")
}
if err := img.RemoveSnapshot("test-snap"); err != nil {
t.Fatal(err)
}
if err := img.RemoveSnapshot("test-snap"); err == nil {
t.Fatal("Did not receive error when trying to delete the same snapshot twice")
}
if list, err = img.ListSnapshots(100); err != nil {
t.Fatal(err)
}
if len(list) != 0 {
t.Fatal("Snapshot list after create did not match expectation")
}
device2, err := img.MapDevice()
if err != nil {
t.Fatal(err)
}
if device != device2 {
t.Fatal("mapdevice failed to find existing rbd device")
}
if err := img.UnmapDevice(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(device); err == nil {
t.Fatal("device still exists after unmap")
}
if err := img.UnmapDevice(); err == nil {
t.Fatal("Did not receive error trying to unmap device twice")
}
if err := pool.RemoveImage("test"); err != nil {
t.Fatal(err)
}
if err := pool.RemoveImage("test"); err == nil {
t.Fatal("Did not receive error trying to remove image a second time")
}
if _, err := pool.GetImage("test"); err == nil {
t.Fatal("Was able to get image after removal")
}
}