1 From 017d593f2c83b3e8b415789c10c9d423cf1e811e Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Wed, 14 Nov 2012 09:57:04 +0100
4 Subject: [PATCH v3 5/6] add regression tests for backup
6 Simple regression tests using vma-reader and vma-writer.
8 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
10 tests/Makefile | 11 +-
11 tests/backup-test.c | 511 +++++++++++++++++++++++++++++++++++++++++++++++++++
12 2 files changed, 520 insertions(+), 2 deletions(-)
13 create mode 100644 tests/backup-test.c
15 diff --git a/tests/Makefile b/tests/Makefile
16 index b60f0fb..cffbd22 100644
19 @@ -20,6 +20,8 @@ check-unit-y += tests/test-thread-pool$(EXESUF)
21 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
23 +check-backup-y = tests/backup-test$(EXESUF)
25 # All QTests for now are POSIX-only, but the dependencies are
26 # really in libqtest, not in the testcases themselves.
27 check-qtest-i386-y = tests/fdc-test$(EXESUF)
28 @@ -54,6 +56,7 @@ tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(coroutine-obj-y) $(tools
29 tests/test-aio$(EXESUF): tests/test-aio.o $(coroutine-obj-y) $(tools-obj-y) $(block-obj-y) libqemustub.a
30 tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(coroutine-obj-y) $(tools-obj-y) $(block-obj-y) libqemustub.a
31 tests/test-iov$(EXESUF): tests/test-iov.o iov.o
32 +tests/backup-test$(EXESUF): tests/backup-test.o vma-reader.o $(tools-obj-y) $(block-obj-y) libqemustub.a
34 tests/test-qapi-types.c tests/test-qapi-types.h :\
35 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
36 @@ -146,10 +149,14 @@ check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF)
38 # Consolidated targets
40 -.PHONY: check-qtest check-unit check
41 +.PHONY: check-backup check-qtest check-unit check
42 check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS))
43 check-unit: $(patsubst %,check-%, $(check-unit-y))
44 check-block: $(patsubst %,check-%, $(check-block-y))
45 -check: check-unit check-qtest
47 +check-backup: tests/backup-test$(EXESUF)
50 +check: check-unit check-qtest check-backup
52 -include $(wildcard tests/*.d)
53 diff --git a/tests/backup-test.c b/tests/backup-test.c
55 index 0000000..c434c5f
57 +++ b/tests/backup-test.c
60 + * QEMU backup test suit
62 + * Copyright (C) Proxmox Server Solutions
65 + * Dietmar Maurer (dietmar@proxmox.com)
67 + * This work is licensed under the terms of the GNU GPL, version 2. See
68 + * the COPYING file in the top-level directory.
70 + * Fixme: running 'backup-test -l' trigger a bug in g_slice_alloc()
71 + * Note: 'G_SLICE=always-malloc ./tests/backup-test -l' works
75 +#include <sys/time.h>
76 +#include <sys/types.h>
82 +#include "qemu-common.h"
83 +#include "main-loop.h"
84 +#include "block_int.h"
88 +static int opt_debug;
91 +#define DPRINTF(fmt, ...) \
92 + do { if (opt_debug) { printf(fmt, ## __VA_ARGS__); } } while (0)
94 +#define CLUSTER(x) (x*BACKUP_CLUSTER_SIZE)
96 +#define RUN_TEST(testfunc, speed) \
97 + backup_test(#testfunc " speed " #speed, speed, testfunc);
100 +static unsigned char buf_sec_pattern_cd[BDRV_SECTOR_SIZE];
101 +static unsigned char buf_sec_pattern_32[BDRV_SECTOR_SIZE];
103 +#define TEST_IMG_SIZE (6*1024*1024+BDRV_SECTOR_SIZE)
104 +#define TEST_IMG_NAME "backuptest.raw"
105 +#define TEST_IMG_RESTORE_NAME "backuptest.raw.restore"
106 +#define TEST_VMA_NAME "backuptest.vma"
108 +typedef struct BackupCB {
113 +static int backup_dump_cb(void *opaque, BlockDriverState *bs,
114 + int64_t cluster_num, unsigned char *buf)
116 + BackupCB *bcb = opaque;
118 + DPRINTF("backup_dump_cb C%zd %d\n", cluster_num, bcb->dev_id);
121 + if (vma_writer_write(bcb->vmaw, bcb->dev_id, cluster_num, buf, &zb) < 0) {
122 + printf("backup_dump_cb vma_writer_write failed\n");
129 +static void backup_complete_cb(void *opaque, int ret)
131 + BackupCB *bcb = opaque;
133 + DPRINTF("backup_complete_cb %d %d\n", bcb->dev_id, ret);
136 + vma_writer_set_error(bcb->vmaw, "backup_complete_cb %d", ret);
139 + if (vma_writer_close_stream(bcb->vmaw, bcb->dev_id) <= 0) {
141 + if (vma_writer_close(bcb->vmaw, &err) != 0) {
142 + g_error("vma_writer_close failed %s", error_get_pretty(err));
145 + DPRINTF("backup_complete_cb finish\n");
148 +static void write_sec_pattern_cd(BlockDriverState *bs, int64_t offset)
152 + DPRINTF("write_sec_pattern_cd %zd\n", offset);
154 + if (offset & 0x1ff) {
155 + g_error("write_sec_pattern_cd offset %zd is not sector aligned\n",
159 + ret = bdrv_write(bs, offset >> 9, buf_sec_pattern_cd, 1);
161 + g_error("write_sec_pattern_cd %zd failed", offset);
166 +static void read_sec(BlockDriverState *bs, int64_t offset, unsigned char *buf)
168 + DPRINTF("read_sec C%zd start %zd\n", offset>>VMA_CLUSTER_BITS, offset);
170 + if (offset & 0x1ff) {
171 + g_error("read_sec offset %zd is not sector aligned\n", offset);
174 + if (bdrv_read(bs, offset >> 9, buf, 1) < 0) {
175 + g_error("bdrv_read failed");
179 +static bool request_term;
181 +typedef struct TestCB {
183 + BlockDriverState *bs;
187 +static TestCB *enter_test_co(BlockDriverState *bs, CoroutineEntry *entry)
189 + TestCB *cb = g_new0(TestCB, 1);
191 + cb->co = qemu_coroutine_create(entry);
192 + qemu_coroutine_enter(cb->co, cb);
196 +static void test_co_sleep(double sec)
198 + co_sleep_ns(rt_clock, (int64_t)(sec*1000000000));
201 +static void test_co_yield(void)
203 + co_sleep_ns(rt_clock, (int64_t)(1000));
206 +static void coroutine_fn run_co_test1(void *opaque)
209 + TestCB *cb = (TestCB *)opaque;
211 + test_co_sleep(0.2);
212 + write_sec_pattern_cd(cb->bs, 5*BACKUP_CLUSTER_SIZE);
213 + test_co_sleep(0.2);
214 + write_sec_pattern_cd(cb->bs, 10*BACKUP_CLUSTER_SIZE);
215 + test_co_sleep(0.2);
216 + write_sec_pattern_cd(cb->bs, 10*BACKUP_CLUSTER_SIZE);
218 + cb->finished = true;
221 +static void coroutine_fn run_co_test2(void *opaque)
224 + TestCB *cb = (TestCB *)opaque;
225 + unsigned char buf[512];
227 + test_co_sleep(0.2);
228 + read_sec(cb->bs, 5*BACKUP_CLUSTER_SIZE, buf);
229 + write_sec_pattern_cd(cb->bs, 6*BACKUP_CLUSTER_SIZE);
231 + cb->finished = true;
234 +static void coroutine_fn run_co_random_read(void *opaque)
237 + TestCB *cb = (TestCB *)opaque;
238 + int64_t sectors = bdrv_getlength(cb->bs)/BDRV_SECTOR_SIZE - 1;
239 + unsigned char buf[512];
243 + if (request_term) {
244 + DPRINTF("finish run_co_random_read\n");
247 + int64_t s = (rand()*sectors)/RAND_MAX;
248 + read_sec(cb->bs, s*BDRV_SECTOR_SIZE, buf);
251 + cb->finished = true;
254 +static void coroutine_fn run_co_random_write(void *opaque)
257 + TestCB *cb = (TestCB *)opaque;
258 + int64_t sectors = bdrv_getlength(cb->bs)/BDRV_SECTOR_SIZE;
262 + if (request_term) {
263 + DPRINTF("finish run_co_random_write\n");
266 + int64_t s = (rand()*sectors)/RAND_MAX;
267 + write_sec_pattern_cd(cb->bs, s*BDRV_SECTOR_SIZE);
270 + cb->finished = true;
273 +static void fill_test_sector(void *buf, size_t sector_num)
275 + int64_t *i64buf = (int64_t *)buf;
278 + int data = sector_num;
279 + if (sector_num >= 8 && sector_num < 8*(2*16+2)) {
280 + data = 0; /* add zero region for testing */
283 + for (i = 0; i < (512/sizeof(int64_t)); i++) {
288 +static void verify_archive(const char *archive, size_t size)
290 + Error *errp = NULL;
292 + VmaReader *vmar = vma_reader_create(archive, &errp);
295 + g_error("%s", error_get_pretty(errp));
298 + VmaDeviceInfo *di = vma_reader_get_device_info(vmar, 1);
299 + if (!di || strcmp((char *)di->devname, "hda") || di->size != size) {
300 + g_error("got wrong device info");
303 + unlink(TEST_IMG_RESTORE_NAME);
305 + int flags = BDRV_O_NATIVE_AIO|BDRV_O_RDWR|BDRV_O_CACHE_WB;
307 + if (bdrv_img_create(TEST_IMG_RESTORE_NAME, "raw", NULL, NULL, NULL,
309 + g_error("can't create file %s", TEST_IMG_RESTORE_NAME);
312 + BlockDriverState *bs = NULL;
313 + if (bdrv_file_open(&bs, TEST_IMG_RESTORE_NAME, flags)) {
314 + g_error("can't open file %s", TEST_IMG_RESTORE_NAME);
316 + if (vma_reader_register_bs(vmar, 1, bs, false, &errp) < 0) {
317 + g_error("%s", error_get_pretty(errp));
320 + if (vma_reader_restore(vmar, -1, &errp) < 0) {
321 + g_error("restore failed - %s", error_get_pretty(errp));
325 + size_t sectors = size/BDRV_SECTOR_SIZE;
326 + int64_t buf[512/sizeof(int64_t)];
327 + int64_t buf2[512/sizeof(int64_t)];
329 + for (i = 0; i < sectors; i++) {
330 + if (bdrv_read(bs, i, (uint8_t *)buf, 1) != 0) {
331 + g_error("bdrv_read failed");
333 + fill_test_sector(buf2, i);
334 + if (bcmp(buf, buf2, sizeof(buf))) {
335 + g_error("data is different at sector %zd", i);
339 + vma_reader_destroy(vmar);
341 + unlink(TEST_IMG_RESTORE_NAME);
344 +static void prepare_vm_image(const char *filename, size_t sectors)
346 + int fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
348 + g_error("can't open file %s\n", filename);
352 + int64_t buf[512/sizeof(int64_t)];
354 + for (i = 0; i < sectors; i++) {
355 + fill_test_sector(buf, i);
359 + res = write(fd, buf, sizeof(buf));
360 + if (!(res < 0 && errno == EINTR)) {
364 + if (res != sizeof(buf)) {
365 + g_error("can't initialize file %s - %s %d\n",
366 + filename, strerror(errno), res);
370 + if (close(fd) != 0) {
371 + g_error("close failed");
375 +static GList *simple_test(BlockDriverState *bs)
377 + GList *cb_list = NULL;
379 + cb_list = g_list_append(cb_list, enter_test_co(bs, run_co_test1));
380 + cb_list = g_list_append(cb_list, enter_test_co(bs, run_co_test2));
385 +static GList *random_read_write_test(BlockDriverState *bs)
387 + GList *cb_list = NULL;
389 + cb_list = g_list_append(cb_list, enter_test_co(bs, run_co_random_read));
390 + cb_list = g_list_append(cb_list, enter_test_co(bs, run_co_random_read));
391 + cb_list = g_list_append(cb_list, enter_test_co(bs, run_co_random_write));
392 + cb_list = g_list_append(cb_list, enter_test_co(bs, run_co_random_write));
397 +static void backup_test(const char *testname, int64_t speed,
398 + GList *(*start_test_cb)(BlockDriverState *bs))
400 + BlockDriverState *bs = bdrv_new("hda");
402 + static int test_counter;
406 + printf("starting test #%d '%s'\n", test_counter, testname);
408 + const char *filename = TEST_IMG_NAME;
410 + prepare_vm_image(TEST_IMG_NAME, TEST_IMG_SIZE/BDRV_SECTOR_SIZE);
412 + int flags = BDRV_O_NATIVE_AIO|BDRV_O_RDWR|BDRV_O_CACHE_WB;
414 + if (bdrv_open(bs, filename, flags, NULL) < 0) {
415 + g_error("can't open device %s\n", filename);
420 + uuid_generate(uuid);
422 + unlink(TEST_VMA_NAME);
424 + VmaWriter *vmaw = vma_writer_create(TEST_VMA_NAME, uuid, speed, &err);
426 + g_error("%s", error_get_pretty(err));
431 + bcb.dev_id = vma_writer_register_stream(vmaw, bdrv_get_device_name(bs),
432 + bdrv_getlength(bs));
433 + if (backup_job_start(bs, backup_dump_cb, backup_complete_cb, &bcb) < 0) {
434 + g_error("backup_job_start failed");
437 + request_term = false;
439 + GList *cb_list = start_test_cb(bs);
442 + main_loop_wait(false);
445 + vma_writer_get_status(vmaw, &vmastat);
446 + if (vmastat.closed) {
451 + request_term = true;
454 + GList *l = cb_list;
456 + while (l && l->data) {
457 + TestCB *cb = (TestCB *)l->data;
458 + l = g_list_next(l);
459 + if (!cb->finished) {
465 + DPRINTF("All test coroutines finished\n");
468 + main_loop_wait(false);
471 + /* Make sure all outstanding requests complete */
475 + vma_writer_get_status(vmaw, &vmastat);
476 + DPRINTF("statistic %zd %zd\n", vmastat.stream_info[1].size,
477 + vmastat.stream_info[1].transferred);
478 + assert(vmastat.stream_info[1].size == vmastat.stream_info[1].transferred);
480 + vma_writer_destroy(vmaw);
484 + /* start verification */
485 + verify_archive(TEST_VMA_NAME, TEST_IMG_SIZE);
489 + unlink(TEST_IMG_NAME);
490 + unlink(TEST_VMA_NAME);
492 + printf("finish test #%d '%s' OK\n", test_counter, testname);
495 +static void help(void)
497 + const char *help_msg =
498 + "usage: backup-test [options]\n"
500 + "backup-test run default regression test (fast)\n"
501 + "backup-test -l run long running test loop (endless)\n"
503 + "use option -d to turn on verbose debug output\n"
506 + printf("%s", help_msg);
510 +int main(int argc, char **argv)
515 + c = getopt(argc, argv, "hdl");
531 + g_assert_not_reached();
535 + memset(buf_sec_pattern_cd, 0xcd, sizeof(buf_sec_pattern_cd));
536 + memset(buf_sec_pattern_32, 0x32, sizeof(buf_sec_pattern_32));
540 + /* ignore SIGPIPE */
541 + struct sigaction act;
542 + sigfillset(&act.sa_mask);
544 + act.sa_handler = SIG_IGN;
545 + sigaction(SIGPIPE, &act, NULL);
547 + qemu_init_main_loop();
551 + if (opt_loop) { /* endless test loop */
553 + RUN_TEST(random_read_write_test, 0);
558 + if (opt_debug) { /* run simple test (rate limited) */
559 + RUN_TEST(simple_test, 1024*1024);
563 + /* run default regression tests at full speed */
565 + RUN_TEST(simple_test, 0);
566 + RUN_TEST(random_read_write_test, 0);