qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, aliguori@us.ibm.com, stefanha@gmail.com,
	blauwirbel@gmail.com, pbonzini@redhat.com, eblake@redhat.com,
	Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Subject: [Qemu-devel] [PATCH V2 6/6] libqblock test example
Date: Mon, 10 Sep 2012 16:26:26 +0800	[thread overview]
Message-ID: <1347265586-17698-7-git-send-email-xiawenc@linux.vnet.ibm.com> (raw)
In-Reply-To: <1347265586-17698-1-git-send-email-xiawenc@linux.vnet.ibm.com>

  Created a new directory in tests, and added a simple test case in it.
In this example, user first create two qcow2 images, and then get the
backing file relationship information of them. Then does write and read
sync IO on them.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 tests/Makefile                   |    3 +
 tests/libqblock/Makefile         |   28 +++++
 tests/libqblock/libqblock-test.c |  231 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 262 insertions(+), 0 deletions(-)
 create mode 100644 tests/libqblock/Makefile
 create mode 100644 tests/libqblock/libqblock-test.c

diff --git a/tests/Makefile b/tests/Makefile
index 26a67ce..69af1e2 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -148,4 +148,7 @@ check-unit: $(patsubst %,check-%, $(check-unit-y))
 check-block: $(patsubst %,check-%, $(check-block-y))
 check: check-unit check-qtest
 
+check-libqblock:
+	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C tests/libqblock V="$(V)" TARGET_DIR="$*/" check-libqblock,)
+
 -include $(wildcard tests/*.d)
diff --git a/tests/libqblock/Makefile b/tests/libqblock/Makefile
new file mode 100644
index 0000000..dc5acda
--- /dev/null
+++ b/tests/libqblock/Makefile
@@ -0,0 +1,28 @@
+-include ../../config-host.mak
+-include $(SRC_PATH)/Makefile.objs
+-include $(SRC_PATH)/rules.mak
+
+$(call set-vpath, $(SRC_PATH))
+
+#library test case objects
+libqblock-test-objs=libqblock-test.lo
+
+QEMU_CFLAGS+=-I $(SRC_PATH)/$(libqblock-lib-path)
+libqblock-la-path = $(SRC_PATH)/$(libqblock-lib-path)/$(libqblock-lib-la)
+
+##########################################################################
+#runtime rules:
+ifeq ($(LIBTOOL),)
+libqblock-test.bin:
+	@echo "libtool is missing, please install and rerun configure"; exit 1
+else
+libqblock-test.bin: $(libqblock-test-objs) $(libqblock-la-path)
+	$(call quiet-command,$(LIBTOOL) --mode=link --quiet --tag=CC $(CC) -shared -rpath $(libdir) -o $@ $^ ,"  lt LINK $@")
+endif
+
+check-libqblock: libqblock-test.bin
+	./libqblock-test.bin
+
+clean:
+	rm -f *.lo *.o *.d *.la *.bin
+	rm -rf .libs
diff --git a/tests/libqblock/libqblock-test.c b/tests/libqblock/libqblock-test.c
new file mode 100644
index 0000000..2d1a119
--- /dev/null
+++ b/tests/libqblock/libqblock-test.c
@@ -0,0 +1,231 @@
+/*
+ * QEMU block layer library test
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Wenchao Xia   <xiawenc@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include "libqblock.h"
+
+#define TEST_BUF_SIZE 1024
+static unsigned char buf_r[TEST_BUF_SIZE];
+static unsigned char buf_w[TEST_BUF_SIZE] = {0, 0, 0, 0};
+
+struct VerifyData {
+    unsigned char *buf_r;
+    unsigned char *buf_w;
+    int len;
+};
+
+static void print_loc(struct QBlockProtInfo *loc)
+{
+    switch (loc->prot_type) {
+    case QB_PROT_NONE:
+        printf("protocol type [none].");
+        break;
+    case QB_PROT_FILE:
+        printf("protocol type [file], filename [%s].",
+               loc->prot_op.o_file.filename);
+        break;
+    default:
+        printf("protocol type not supported.");
+        break;
+    }
+}
+
+static void print_info_image_static(struct QBlockStaticInfo *info)
+{
+    printf("=======image location:\n");
+    print_loc(&info->loc);
+    printf("\nvirtual_size %" PRId64 ", format type %d [%s]",
+           info->virt_size, info->fmt_type, qb_fmttype2str(info->fmt_type));
+    printf("\nbacking image location:\n");
+    print_loc(&info->backing_loc);
+    printf("\n");
+}
+
+static void test_check(struct VerifyData *vdata)
+{
+    int cmp;
+    cmp = memcmp(vdata->buf_r, vdata->buf_w, vdata->len);
+    if (cmp == 0) {
+        printf("compare succeed, %d.\n", vdata->buf_r[24]);
+    } else {
+        printf("!!! compare fail, %d.\n", vdata->buf_r[24]);
+        exit(1);
+    }
+}
+
+int main(int argc, char **argv)
+{
+    const char *filename1, *filename2;
+    struct QBroker *broker = NULL;
+    struct QBlockState *qbs = NULL;
+    struct QBlockProtInfo *ol = NULL;
+    struct QBlockFmtInfo *of = NULL;
+    struct QBlockStaticInfo *info_st = NULL;
+    int ret, flag;
+    int test_offset = 510;
+    int test_len = 520;
+    struct VerifyData vdata;
+    char err_str[1024];
+
+    vdata.buf_r = buf_r;
+    vdata.buf_w = buf_w;
+    vdata.len = test_len;
+
+    filename1 = "./qemu_image1";
+    filename2 = "./qemu_image2";
+    printf("qemu test, filename1 is %s, filename2 is %s.\n",
+                                       filename1, filename2);
+
+    ret = qb_broker_new(&broker);
+    if (ret < 0) {
+        goto free;
+    }
+
+    ret = qb_state_new(broker, &qbs);
+    if (ret < 0) {
+        goto free;
+    }
+
+    ret = qb_prot_info_new(broker, &ol);
+    if (ret < 0) {
+        goto free;
+    }
+
+    ret = qb_fmt_info_new(broker, &of);
+    if (ret < 0) {
+        goto free;
+    }
+
+    /* create a new image */
+
+    ol->prot_type = QB_PROT_FILE;
+    ol->prot_op.o_file.filename = filename2;
+    of->fmt_type = QB_FMT_QCOW2;
+    of->fmt_op.o_qcow2.virt_size = 100 * 1024;
+    flag = 0;
+
+    ret = qb_create(broker, qbs, ol, of, flag);
+    if (ret < 0) {
+        qb_error_get_human_str(broker, err_str, sizeof(err_str));
+        printf("create fail 1. %s.\n", err_str);
+        goto unlink;
+    }
+
+    ol->prot_type = QB_PROT_FILE;
+    ol->prot_op.o_file.filename = filename1;
+    of->fmt_type = QB_FMT_QCOW2;
+    of->fmt_op.o_qcow2.backing_loc.prot_type = QB_PROT_FILE;
+    of->fmt_op.o_qcow2.backing_loc.prot_op.o_file.filename = filename2;
+    flag = 0;
+    ret = qb_create(broker, qbs, ol, of, flag);
+    if (ret < 0) {
+        qb_error_get_human_str(broker, err_str, sizeof(err_str));
+        printf("create fail 2. %s.\n", err_str);
+        goto unlink;
+    }
+
+    /* get informations */
+    ol->prot_type = QB_PROT_FILE;
+    ol->prot_op.o_file.filename = filename1;
+    of->fmt_type = QB_FMT_NONE;
+    flag = LIBQBLOCK_O_NO_BACKING;
+    ret = qb_open(broker, qbs, ol, of, flag);
+    if (ret < 0) {
+        qb_error_get_human_str(broker, err_str, sizeof(err_str));
+        printf("info getting, open failed. %s.\n", err_str);
+        goto free;
+    }
+
+    while (1) {
+        ret = qb_info_image_static_get(broker, qbs, &info_st);
+        if (ret < 0) {
+            qb_error_get_human_str(broker, err_str, sizeof(err_str));
+            printf("info get error. %s.\n", err_str);
+            goto close;
+        }
+        print_info_image_static(info_st);
+        qb_close(broker, qbs);
+        if (info_st->backing_loc.prot_type == QB_FMT_NONE) {
+            break;
+        }
+        *ol = info_st->backing_loc;
+        ret = qb_open(broker, qbs, ol, of, flag);
+        if (ret < 0) {
+            qb_error_get_human_str(broker, err_str, sizeof(err_str));
+            printf("info getting, open failed in backing file. %s.\n",
+                                                       err_str);
+            goto free;
+        }
+        qb_info_image_static_delete(broker, &info_st);
+    }
+    /* read and write the image */
+    ol->prot_type = QB_PROT_FILE;
+    ol->prot_op.o_file.filename = filename1;
+    of->fmt_type = QB_FMT_NONE;
+    flag = LIBQBLOCK_O_RDWR;
+    ret = qb_open(broker, qbs, ol, of, flag);
+    if (ret < 0) {
+        qb_error_get_human_str(broker, err_str, sizeof(err_str));
+        printf("open failed. %s.\n", err_str);
+        goto free;
+    }
+
+    buf_w[1] = 1;
+    buf_w[2] = 2;
+    buf_w[514] = 4;
+    memset(buf_r, 0, sizeof(buf_r));
+
+    ret = qb_write(broker, qbs, buf_w, test_len, test_offset);
+    if (ret < 0) {
+        qb_error_get_human_str(broker, err_str, sizeof(err_str));
+        printf("%s.\n", err_str);
+        goto close;
+    }
+
+    ret = qb_read(broker, qbs, buf_r, test_len, test_offset);
+    if (ret < 0) {
+        qb_error_get_human_str(broker, err_str, sizeof(err_str));
+        printf("%s.\n", err_str);
+        goto close;
+    }
+
+    test_check(&vdata);
+
+ close:
+    qb_close(broker, qbs);
+ unlink:
+    unlink(filename1);
+    unlink(filename2);
+ free:
+    if (info_st != NULL) {
+        qb_info_image_static_delete(broker, &info_st);
+    }
+    if (qbs != NULL) {
+        qb_state_delete(broker, &qbs);
+    }
+    if (ol != NULL) {
+        qb_prot_info_delete(broker, &ol);
+    }
+    if (of != NULL) {
+        qb_fmt_info_delete(broker, &of);
+    }
+    if (broker != NULL) {
+        qb_broker_delete(&broker);
+    }
+    return 0;
+}
-- 
1.7.1

      parent reply	other threads:[~2012-09-10  8:28 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-10  8:26 [Qemu-devel] [PATCH V2 0/6] libqblock, qemu block layer library Wenchao Xia
2012-09-10  8:26 ` [Qemu-devel] [PATCH V2 1/6] libqblock API design Wenchao Xia
2012-09-10 21:07   ` Eric Blake
2012-09-11  3:16     ` Wenchao Xia
2012-09-14  2:03       ` Wenchao Xia
2012-09-11 20:28   ` Blue Swirl
2012-09-12  2:54     ` Wenchao Xia
2012-09-12  8:19     ` Kevin Wolf
2012-09-12  9:21       ` Wenchao Xia
2012-09-14 19:08       ` Blue Swirl
2012-09-10  8:26 ` [Qemu-devel] [PATCH V2 2/6] libqblock type and structure defines Wenchao Xia
2012-09-10 21:27   ` Eric Blake
2012-09-11  3:26     ` Wenchao Xia
2012-09-11  4:12       ` Eric Blake
2012-09-11 20:31   ` Blue Swirl
2012-09-11 22:52     ` Eric Blake
2012-09-12  3:05       ` Wenchao Xia
2012-09-12 12:59         ` Eric Blake
2012-09-13  3:24           ` Wenchao Xia
2012-09-13  3:33             ` Eric Blake
2012-09-13  3:49               ` Eric Blake
2012-09-14 18:11                 ` Blue Swirl
2012-09-17  2:23                   ` Wenchao Xia
2012-09-17 19:08                     ` Blue Swirl
2012-09-14 18:02       ` Blue Swirl
2012-09-10  8:26 ` [Qemu-devel] [PATCH V2 3/6] libqblock error handling Wenchao Xia
2012-09-10 21:33   ` Eric Blake
2012-09-11  4:36     ` Wenchao Xia
2012-09-11 20:32   ` Blue Swirl
2012-09-12  2:58     ` Wenchao Xia
2012-09-14 17:09       ` Blue Swirl
2012-09-10  8:26 ` [Qemu-devel] [PATCH V2 4/6] libqblock export some qemu block function Wenchao Xia
2012-09-10  8:26 ` [Qemu-devel] [PATCH V2 5/6] libqblock building system Wenchao Xia
2012-09-10  8:26 ` Wenchao Xia [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1347265586-17698-7-git-send-email-xiawenc@linux.vnet.ibm.com \
    --to=xiawenc@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=blauwirbel@gmail.com \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).