-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestlibrbd.c
303 lines (273 loc) · 8.56 KB
/
testlibrbd.c
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#define __STDC_FORMAT_MACROS
#include "include/rados/librados.h"
#include "include/rbd/librbd.h"
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define TEST_IMAGE "testimg"
#define TEST_POOL "librbdtest"
#define TEST_SNAP "testsnap"
#define TEST_IO_SIZE 513
#define MB_BYTES(mb) (mb << 20)
void test_create_and_stat(rados_ioctx_t io_ctx, const char *name, size_t size)
{
rbd_image_info_t info;
rbd_image_t image;
int order = 0;
assert(rbd_create(io_ctx, name, size, &order) == 0);
assert(rbd_open(io_ctx, name, &image, NULL) == 0);
assert(rbd_stat(image, &info, sizeof(info)) == 0);
printf("image has size %llu and order %d\n", (unsigned long long) info.size, info.order);
assert(info.size == size);
assert(info.order == order);
assert(rbd_close(image) == 0);
}
void test_resize_and_stat(rbd_image_t image, size_t size)
{
rbd_image_info_t info;
assert(rbd_resize(image, size) == 0);
assert(rbd_stat(image, &info, sizeof(info)) == 0);
printf("image has size %llu and order %d\n", (unsigned long long) info.size, info.order);
assert(info.size == size);
}
void test_ls(rados_ioctx_t io_ctx, size_t num_expected, ...)
{
int num_images, i, j;
char *expected, *names, *cur_name;
va_list ap;
size_t max_size = 1024;
names = (char *) malloc(sizeof(char *) * 1024);
printf("names is %p\n", names);
num_images = rbd_list(io_ctx, names, &max_size);
printf("names is %p\n", names);
printf("num images is: %d\nexpected: %d\n", num_images, (int)num_expected);
assert(num_images >= 0);
assert(num_images == (int)num_expected);
for (i = 0, cur_name = names; i < num_images; i++) {
printf("image: %s\n", cur_name);
cur_name += strlen(cur_name) + 1;
}
va_start(ap, num_expected);
for (i = num_expected; i > 0; i--) {
expected = va_arg(ap, char *);
printf("expected = %s\n", expected);
int found = 0;
for (j = 0, cur_name = names; j < num_images; j++) {
if (cur_name[0] == '_') {
cur_name += strlen(cur_name) + 1;
continue;
}
if (strcmp(cur_name, expected) == 0) {
printf("found %s\n", cur_name);
cur_name[0] = '_';
found = 1;
break;
}
}
assert(found);
}
for (i = 0, cur_name = names; i < num_images; i++) {
assert(cur_name[0] == '_');
cur_name += strlen(cur_name) + 1;
}
free(names);
}
void test_delete(rados_ioctx_t io_ctx, const char *name)
{
assert(rbd_remove(io_ctx, name) == 0);
}
void test_create_snap(rbd_image_t image, const char *name)
{
assert(rbd_snap_create(image, name) == 0);
}
void test_ls_snaps(rbd_image_t image, int num_expected, ...)
{
rbd_snap_info_t *snaps;
int num_snaps, i, j, expected_size, max_size = 10;
char *expected;
va_list ap;
snaps = (rbd_snap_info_t *) malloc(sizeof(rbd_snap_info_t *) * 10);
num_snaps = rbd_snap_list(image, snaps, &max_size);
printf("num snaps is: %d\nexpected: %d\n", num_snaps, num_expected);
assert(num_snaps == num_expected);
for (i = 0; i < num_snaps; i++) {
printf("snap: %s\n", snaps[i].name);
}
va_start(ap, num_expected);
for (i = num_expected; i > 0; i--) {
expected = va_arg(ap, char *);
expected_size = va_arg(ap, int);
int found = 0;
for (j = 0; j < num_snaps; j++) {
if (snaps[j].name == NULL)
continue;
if (strcmp(snaps[j].name, expected) == 0) {
printf("found %s with size %llu\n", snaps[j].name, (unsigned long long) snaps[j].size);
assert((int)snaps[j].size == expected_size);
free((void *) snaps[j].name);
snaps[j].name = NULL;
found = 1;
break;
}
}
assert(found);
}
for (i = 0; i < num_snaps; i++) {
assert(snaps[i].name == NULL);
}
free(snaps);
}
void test_delete_snap(rbd_image_t image, const char *name)
{
assert(rbd_snap_remove(image, name) == 0);
}
void simple_write_cb(rbd_completion_t cb, void *arg)
{
printf("write completion cb called!\n");
}
void simple_read_cb(rbd_completion_t cb, void *arg)
{
printf("read completion cb called!\n");
}
void aio_write_test_data(rbd_image_t image, const char *test_data, off_t off)
{
rbd_completion_t comp;
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
printf("created completion\n");
rbd_aio_write(image, off, strlen(test_data), test_data, comp);
printf("started write\n");
rbd_aio_wait_for_complete(comp);
int r = rbd_aio_get_return_value(comp);
printf("return value is: %d\n", r);
assert(r == 0);
printf("finished write\n");
rbd_aio_release(comp);
}
void write_test_data(rbd_image_t image, const char *test_data, off_t off)
{
int written;
size_t len = strlen(test_data);
while (len > 0) {
written = rbd_write(image, off, len, test_data);
assert(written >= 0);
len -= written;
off += written;
printf("wrote: %u\n", (unsigned int) written);
}
}
void aio_read_test_data(rbd_image_t image, const char *expected, off_t off)
{
rbd_completion_t comp;
char result[TEST_IO_SIZE];
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
printf("created completion\n");
rbd_aio_read(image, off, strlen(expected), result, comp);
printf("started read\n");
rbd_aio_wait_for_complete(comp);
int r = rbd_aio_get_return_value(comp);
printf("return value is: %d\n", r);
assert(r == TEST_IO_SIZE - 1);
assert(strncmp(expected, result, TEST_IO_SIZE) == 0);
printf("finished read\n");
rbd_aio_release(comp);
}
void read_test_data(rbd_image_t image, const char *expected, off_t off)
{
int read, total_read = 0;
size_t expected_len = strlen(expected);
size_t len = expected_len;
char result[TEST_IO_SIZE];
char *buf = result;
while (len > 0) {
read = rbd_read(image, off + total_read, len, buf);
assert(read >= 0);
total_read += read;
buf += read;
len -= read;
printf("read: %u\n", (unsigned int) read);
}
printf("read: %s\nexpected: %s\n", result, expected);
assert(memcmp(result, expected, expected_len) == 0);
}
void test_io(rados_ioctx_t io, rbd_image_t image)
{
char test_data[TEST_IO_SIZE];
int i;
srand(time(0));
for (i = 0; i < TEST_IO_SIZE - 1; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE - 1] = '\0';
for (i = 0; i < 5; ++i)
write_test_data(image, test_data, strlen(test_data) * i);
for (i = 5; i < 10; ++i)
aio_write_test_data(image, test_data, strlen(test_data) * i);
for (i = 0; i < 5; ++i)
read_test_data(image, test_data, strlen(test_data) * i);
for (i = 5; i < 10; ++i)
aio_read_test_data(image, test_data, strlen(test_data) * i);
}
int main(int argc, const char **argv)
{
rados_t cluster;
rados_ioctx_t io_ctx;
rbd_image_t image;
assert(rados_create(&cluster, NULL) == 0);
assert(rados_conf_read_file(cluster, "/etc/ceph/ceph.conf") == 0);
rados_reopen_log(cluster);
assert(rados_connect(cluster) == 0);
if (rados_ioctx_lookup(cluster, TEST_POOL) != -ENOENT) {
int r = rados_pool_delete(cluster, TEST_POOL);
printf("rados_pool_delete returned %d\n", r);
}
int r = rados_pool_create(cluster, TEST_POOL);
printf("rados_pool_create returned %d\n", r);
assert(rados_ioctx_create(cluster, TEST_POOL, &io_ctx) == 0);
struct rados_pool_stat_t stats;
rados_ioctx_pool_stat(io_ctx, &stats);
test_ls(io_ctx, 0);
test_ls(io_ctx, 0);
test_create_and_stat(io_ctx, TEST_IMAGE, MB_BYTES(1));
assert(rbd_open(io_ctx, TEST_IMAGE, &image, NULL) == 0);
test_ls(io_ctx, 1, TEST_IMAGE);
test_ls_snaps(image, 0);
test_create_snap(image, TEST_SNAP);
test_ls_snaps(image, 1, TEST_SNAP, MB_BYTES(1));
test_resize_and_stat(image, MB_BYTES(2));
test_io(io_ctx, image);
test_create_snap(image, TEST_SNAP "1");
test_ls_snaps(image, 2, TEST_SNAP, MB_BYTES(1), TEST_SNAP "1", MB_BYTES(2));
test_delete_snap(image, TEST_SNAP);
test_ls_snaps(image, 1, TEST_SNAP "1", MB_BYTES(2));
test_delete_snap(image, TEST_SNAP "1");
test_ls_snaps(image, 0);
assert(rbd_close(image) == 0);
test_create_and_stat(io_ctx, TEST_IMAGE "1", MB_BYTES(2));
test_ls(io_ctx, 2, TEST_IMAGE, TEST_IMAGE "1");
test_delete(io_ctx, TEST_IMAGE);
test_ls(io_ctx, 1, TEST_IMAGE "1");
test_delete(io_ctx, TEST_IMAGE "1");
test_ls(io_ctx, 0);
rados_ioctx_destroy(io_ctx);
rados_shutdown(cluster);
return 0;
}