* [Qemu-devel] [PATCH] QemuOpt: add unit tests
@ 2014-03-16 21:18 Leandro Dorileo
2014-03-17 16:16 ` Eric Blake
0 siblings, 1 reply; 3+ messages in thread
From: Leandro Dorileo @ 2014-03-16 21:18 UTC (permalink / raw)
To: qemu-devel
Cc: Wenchao Xia, Stefan Hajnoczi, Chunyan Liu, Markus Armbruster,
Anthony Liguori, Andreas Färber, Leandro Dorileo
Cover basic aspects and API usage for QemuOpt. The current implementation
covers the API's planed to be changed by Chunyan Liu in his QEMUOptionParameter
replacement/cleanup job.
Other APIs should be covered in future improvements.
Signed-off-by: Leandro Dorileo <l@dorileo.org>
---
tests/Makefile | 3 +
tests/test-qemu-opts.c | 309 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 312 insertions(+)
create mode 100644 tests/test-qemu-opts.c
diff --git a/tests/Makefile b/tests/Makefile
index 471b4c8..4814283 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -60,6 +60,8 @@ check-unit-y += tests/test-qdev-global-props$(EXESUF)
check-unit-y += tests/check-qom-interface$(EXESUF)
gcov-files-check-qom-interface-y = qom/object.c
check-unit-y += tests/test-vmstate$(EXESUF)
+check-unit-y += tests/test-qemu-opts$(EXESUF)
+gcov-files-test-qemu-opts-y = qom/test-qemu-opts.c
check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
@@ -272,6 +274,7 @@ tests/qom-test$(EXESUF): tests/qom-test.o
tests/blockdev-test$(EXESUF): tests/blockdev-test.o $(libqos-pc-obj-y)
tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y)
tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o
+tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o libqemuutil.a libqemustub.a
# QTest rules
diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c
new file mode 100644
index 0000000..61fc6076
--- /dev/null
+++ b/tests/test-qemu-opts.c
@@ -0,0 +1,309 @@
+/*
+ * QemuOpts unit-tests.
+ *
+ * Copyright (C) 2014 Leandro Dorileo <l@dorileo.org>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qapi/error.h"
+#include "qapi/qmp/qstring.h"
+#include "qemu/config-file.h"
+
+#include <glib.h>
+#include <string.h>
+
+static QemuOptsList opts_list_01 = {
+ .name = "opts_list_01",
+ .head = QTAILQ_HEAD_INITIALIZER(opts_list_01.head),
+ .desc = {
+ {
+ .name = "str1",
+ .type = QEMU_OPT_STRING,
+ },{
+ .name = "str2",
+ .type = QEMU_OPT_STRING,
+ },{
+ .name = "str3",
+ .type = QEMU_OPT_STRING,
+ },{
+ .name = "number1",
+ .type = QEMU_OPT_NUMBER,
+ },
+ { /* end of list */ }
+ },
+};
+
+static QemuOptsList opts_list_02 = {
+ .name = "opts_list_02",
+ .head = QTAILQ_HEAD_INITIALIZER(opts_list_02.head),
+ .desc = {
+ {
+ .name = "str1",
+ .type = QEMU_OPT_STRING,
+ },{
+ .name = "bool1",
+ .type = QEMU_OPT_BOOL,
+ },{
+ .name = "str2",
+ .type = QEMU_OPT_STRING,
+ },{
+ .name = "size1",
+ .type = QEMU_OPT_SIZE,
+ },
+ { /* end of list */ }
+ },
+};
+
+static void register_opts(void)
+{
+ qemu_add_opts(&opts_list_01);
+ qemu_add_opts(&opts_list_02);
+}
+
+static void test_find_unknown_opts(void)
+{
+ QemuOptsList *list;
+
+ register_opts();
+
+ /** should not return anything, we don't known "unknown" option */
+ list = qemu_find_opts("unknown");
+ g_assert(list == NULL);
+}
+
+static void test_qemu_find_opts(void)
+{
+ QemuOptsList *list;
+
+ register_opts();
+
+ /** we have an rtc option, should return it */
+ list = qemu_find_opts("opts_list_01");
+ g_assert(list != NULL);
+}
+
+static void test_qemu_opts_create(void)
+{
+ QemuOptsList *list;
+ QemuOpts *opts;
+
+ register_opts();
+
+ list = qemu_find_opts("opts_list_01");
+ g_assert(list != NULL);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+
+ /** create the opts */
+ opts = qemu_opts_create(list, NULL, 0, &error_abort);
+ g_assert(opts != NULL);
+
+ /** now we've create the opts, must find it */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts != NULL);
+
+ qemu_opts_del(opts);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+}
+
+static void test_qemu_opt_get(void)
+{
+ QemuOptsList *list;
+ QemuOpts *opts;
+ const char *opt = NULL;
+
+ register_opts();
+
+ list = qemu_find_opts("opts_list_01");
+ g_assert(list != NULL);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+
+ /** create the opts */
+ opts = qemu_opts_create(list, NULL, 0, &error_abort);
+ g_assert(opts != NULL);
+
+ /** haven't set anything to str2 yet */
+ opt = qemu_opt_get(opts, "str2");
+ g_assert(opt == NULL);
+
+ qemu_opt_set(opts, "str2", "value");
+
+ /** now we have set str2, should know about it */
+ opt = qemu_opt_get(opts, "str2");
+ g_assert(opt != NULL && !strcmp(opt, "value"));
+
+ qemu_opt_set(opts, "str2", "value2");
+
+ /** having reset the value, the returned should be reset one */
+ opt = qemu_opt_get(opts, "str2");
+ g_assert(opt != NULL && !strcmp(opt, "value2"));
+
+ qemu_opts_del(opts);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+}
+
+static void test_qemu_opt_get_bool(void)
+{
+ QemuOptsList *list;
+ QemuOpts *opts;
+ bool opt;
+
+ register_opts();
+
+ list = qemu_find_opts("opts_list_02");
+ g_assert(list != NULL);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+
+ /** create the opts */
+ opts = qemu_opts_create(list, NULL, 0, &error_abort);
+ g_assert(opts != NULL);
+
+ /** haven't set anything to bool1 yet, so defval should be returned */
+ opt = qemu_opt_get_bool(opts, "bool1", false);
+ g_assert(opt == false);
+
+ qemu_opt_set_bool(opts, "bool1", true);
+
+ /** now we have set bool1, should know about it */
+ opt = qemu_opt_get_bool(opts, "bool1", false);
+ g_assert(opt == true);
+
+ /** having reset the value, the returned should be reset one not defval */
+ qemu_opt_set_bool(opts, "bool1", false);
+ opt = qemu_opt_get_bool(opts, "bool1", true);
+ g_assert(opt == false);
+
+ qemu_opts_del(opts);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+}
+
+static void test_qemu_opt_get_number(void)
+{
+ QemuOptsList *list;
+ QemuOpts *opts;
+ uint64_t opt;
+
+ register_opts();
+
+ list = qemu_find_opts("opts_list_01");
+ g_assert(list != NULL);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+
+ /** create the opts */
+ opts = qemu_opts_create(list, NULL, 0, &error_abort);
+ g_assert(opts != NULL);
+
+ /** haven't set anything to number1 yet, so defval should be returned */
+ opt = qemu_opt_get_number(opts, "number1", 5);
+ g_assert(opt == 5);
+
+ qemu_opt_set_number(opts, "number1", 10);
+
+ /** now we have set number1, should know about it */
+ opt = qemu_opt_get_number(opts, "number1", 5);
+ g_assert(opt == 10);
+
+ /** having reset it, the returned should be the reset one not defval */
+ qemu_opt_set_number(opts, "number1", 15);
+ opt = qemu_opt_get_number(opts, "number1", 5);
+ g_assert(opt == 15);
+
+ qemu_opts_del(opts);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+}
+
+static void test_qemu_opt_get_size(void)
+{
+ QemuOptsList *list;
+ QemuOpts *opts;
+ uint64_t opt;
+ QDict *dict;
+ Error *local_err = NULL;
+
+ register_opts();
+
+ list = qemu_find_opts("opts_list_02");
+ g_assert(list != NULL);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+
+ /** create the opts */
+ opts = qemu_opts_create(list, NULL, 0, &error_abort);
+ g_assert(opts != NULL);
+
+ /** haven't set anything to number1 yet, so defval should be returned */
+ opt = qemu_opt_get_size(opts, "size1", 5);
+ g_assert(opt == 5);
+
+ dict = qdict_new();
+ g_assert(dict != NULL);
+
+ qdict_put(dict, "size1", qstring_from_str("10"));
+
+ qemu_opts_absorb_qdict(opts, dict, &local_err);
+ g_assert(local_err == NULL);
+
+ /** now we have set number1, should know about it */
+ opt = qemu_opt_get_size(opts, "size1", 5);
+ g_assert(opt == 10);
+
+ /** reset value */
+ qdict_put(dict, "size1", qstring_from_str("15"));
+
+ qemu_opts_absorb_qdict(opts, dict, &local_err);
+ g_assert(local_err == NULL);
+
+ /** test the reset value */
+ opt = qemu_opt_get_size(opts, "size1", 5);
+ g_assert(opt == 15);
+
+ qdict_del(dict, "size1");
+ g_free(dict);
+
+ qemu_opts_del(opts);
+
+ /** should not find anything at this point */
+ opts = qemu_opts_find(list, NULL);
+ g_assert(opts == NULL);
+}
+
+int main(int argc, char *argv[])
+{
+ g_test_init(&argc, &argv, NULL);
+ g_test_add_func("/qemu-opts/find_unknown_opts", test_find_unknown_opts);
+ g_test_add_func("/qemu-opts/find_opts", test_qemu_find_opts);
+ g_test_add_func("/qemu-opts/opts_create", test_qemu_opts_create);
+ g_test_add_func("/qemu-opts/opt_get", test_qemu_opt_get);
+ g_test_add_func("/qemu-opts/opt_get_bool", test_qemu_opt_get_bool);
+ g_test_add_func("/qemu-opts/opt_get_number", test_qemu_opt_get_number);
+ g_test_add_func("/qemu-opts/opt_get_size", test_qemu_opt_get_size);
+ g_test_run();
+ return 0;
+}
--
1.9.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] QemuOpt: add unit tests
2014-03-16 21:18 [Qemu-devel] [PATCH] QemuOpt: add unit tests Leandro Dorileo
@ 2014-03-17 16:16 ` Eric Blake
2014-03-17 16:24 ` Leandro Dorileo
0 siblings, 1 reply; 3+ messages in thread
From: Eric Blake @ 2014-03-17 16:16 UTC (permalink / raw)
To: Leandro Dorileo, qemu-devel
Cc: Wenchao Xia, Stefan Hajnoczi, Chunyan Liu, Markus Armbruster,
Anthony Liguori, Andreas Färber
[-- Attachment #1: Type: text/plain, Size: 2033 bytes --]
On 03/16/2014 03:18 PM, Leandro Dorileo wrote:
> Cover basic aspects and API usage for QemuOpt. The current implementation
> covers the API's planed to be changed by Chunyan Liu in his QEMUOptionParameter
s/planed/planned/
> replacement/cleanup job.
>
> Other APIs should be covered in future improvements.
>
> Signed-off-by: Leandro Dorileo <l@dorileo.org>
> ---
> tests/Makefile | 3 +
> tests/test-qemu-opts.c | 309 +++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 312 insertions(+)
> create mode 100644 tests/test-qemu-opts.c
>
> +
> +static void test_find_unknown_opts(void)
> +{
> + QemuOptsList *list;
> +
> + register_opts();
> +
> + /** should not return anything, we don't known "unknown" option */
s/known/have an/
Here, and throughout the file: /** */ comments are special to doc
markup, and should only be needed prior to declaring a function. Within
a function body, use the simpler /* */ comment.
> +static void test_qemu_find_opts(void)
> +{
> + QemuOptsList *list;
> +
> + register_opts();
> +
> + /** we have an rtc option, should return it */
> + list = qemu_find_opts("opts_list_01");
> + g_assert(list != NULL);
Should you do any further testing on the contents of the returned list?
> +static void test_qemu_opt_get(void)
> +{
> + /** now we have set str2, should know about it */
> + opt = qemu_opt_get(opts, "str2");
> + g_assert(opt != NULL && !strcmp(opt, "value"));
Isn't g_assert_cmpstr nicer for this, as in:
g_assert_cmpstr(opt, ==, "value");
> +static void test_qemu_opt_get_size(void)
> +{
> + QemuOptsList *list;
> +
> + qemu_opts_absorb_qdict(opts, dict, &local_err);
> + g_assert(local_err == NULL);
shorter as:
qemu_opts_absorb_qdict(opts, dict, &error_abort);
Overall, I like where this is headed; looking forward to v2.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] QemuOpt: add unit tests
2014-03-17 16:16 ` Eric Blake
@ 2014-03-17 16:24 ` Leandro Dorileo
0 siblings, 0 replies; 3+ messages in thread
From: Leandro Dorileo @ 2014-03-17 16:24 UTC (permalink / raw)
To: Eric Blake
Cc: Wenchao Xia, Stefan Hajnoczi, Markus Armbruster, qemu-devel,
Anthony Liguori, Chunyan Liu, Andreas Färber
On Mon, Mar 17, 2014 at 10:16:12AM -0600, Eric Blake wrote:
> On 03/16/2014 03:18 PM, Leandro Dorileo wrote:
> > Cover basic aspects and API usage for QemuOpt. The current implementation
> > covers the API's planed to be changed by Chunyan Liu in his QEMUOptionParameter
>
> s/planed/planned/
>
> > replacement/cleanup job.
> >
> > Other APIs should be covered in future improvements.
> >
> > Signed-off-by: Leandro Dorileo <l@dorileo.org>
> > ---
> > tests/Makefile | 3 +
> > tests/test-qemu-opts.c | 309 +++++++++++++++++++++++++++++++++++++++++++++++++
> > 2 files changed, 312 insertions(+)
> > create mode 100644 tests/test-qemu-opts.c
> >
>
> > +
> > +static void test_find_unknown_opts(void)
> > +{
> > + QemuOptsList *list;
> > +
> > + register_opts();
> > +
> > + /** should not return anything, we don't known "unknown" option */
>
> s/known/have an/
ok.
>
> Here, and throughout the file: /** */ comments are special to doc
> markup, and should only be needed prior to declaring a function. Within
> a function body, use the simpler /* */ comment.
>
> > +static void test_qemu_find_opts(void)
> > +{
> > + QemuOptsList *list;
> > +
> > + register_opts();
> > +
> > + /** we have an rtc option, should return it */
> > + list = qemu_find_opts("opts_list_01");
> > + g_assert(list != NULL);
>
> Should you do any further testing on the contents of the returned list?
ok. I'll see the API.
>
> > +static void test_qemu_opt_get(void)
> > +{
>
> > + /** now we have set str2, should know about it */
> > + opt = qemu_opt_get(opts, "str2");
> > + g_assert(opt != NULL && !strcmp(opt, "value"));
>
> Isn't g_assert_cmpstr nicer for this, as in:
>
> g_assert_cmpstr(opt, ==, "value");
Yep, sure, I wasn't awere of g_assert_cmpstr, thanks.
>
> > +static void test_qemu_opt_get_size(void)
> > +{
> > + QemuOptsList *list;
>
> > +
> > + qemu_opts_absorb_qdict(opts, dict, &local_err);
> > + g_assert(local_err == NULL);
>
> shorter as:
>
> qemu_opts_absorb_qdict(opts, dict, &error_abort);
I see.
>
> Overall, I like where this is headed; looking forward to v2.
>
Thanks for reviewing...
--
Leandro Dorileo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-03-17 16:25 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-16 21:18 [Qemu-devel] [PATCH] QemuOpt: add unit tests Leandro Dorileo
2014-03-17 16:16 ` Eric Blake
2014-03-17 16:24 ` Leandro Dorileo
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).