summaryrefslogtreecommitdiff
path: root/tests/qemucommandutiltest.c
diff options
context:
space:
mode:
authorPeter Krempa <pkrempa@redhat.com>2015-01-13 18:19:34 +0100
committerPeter Krempa <pkrempa@redhat.com>2015-01-31 08:53:22 +0100
commit331b2583ec2928bf9ad5d9ca6f42d1e0c12caf34 (patch)
tree4e2e3ad1c095d55afe62433aa348aebf78439d22 /tests/qemucommandutiltest.c
parenta6f277a67bc6912a2cbb79a00ffd856599d989ef (diff)
downloadlibvirt-331b2583ec2928bf9ad5d9ca6f42d1e0c12caf34.tar.gz
qemu: command: Add helper to format -object strings from JSON representation
Unlike -device, qemu uses a JSON object to add backend "objects" via the monitor rather than the string that would be passed on the commandline. To be able to reuse code parts that configure backends for various devices, this patch adds a helper that will allow generating the command line representations from the JSON property object.
Diffstat (limited to 'tests/qemucommandutiltest.c')
-rw-r--r--tests/qemucommandutiltest.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/qemucommandutiltest.c b/tests/qemucommandutiltest.c
new file mode 100644
index 0000000000..8c52f02d18
--- /dev/null
+++ b/tests/qemucommandutiltest.c
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <config.h>
+
+#include "qemu/qemu_command.h"
+#include "util/virjson.h"
+#include "testutils.h"
+#include "testutilsqemu.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+typedef struct
+{
+ const char *props;
+ const char *expectprops;
+} testQemuCommandBuildObjectFromJSONData;
+
+static int
+testQemuCommandBuildObjectFromJSON(const void *opaque)
+{
+ const testQemuCommandBuildObjectFromJSONData *data = opaque;
+ virJSONValuePtr val = NULL;
+ char *expect = NULL;
+ char *result = NULL;
+ int ret = -1;
+
+ if (!(val = virJSONValueFromString(data->props))) {
+ fprintf(stderr, "Failed to parse JSON string '%s'", data->props);
+ return -1;
+ }
+
+ if (virAsprintf(&expect, "testobject,id=testalias%s%s",
+ data->expectprops ? "," : "",
+ data->expectprops ? data->expectprops : "") < 0)
+ return -1;
+
+ result = qemuBuildObjectCommandlineFromJSON("testobject",
+ "testalias",
+ val);
+
+ if (STRNEQ_NULLABLE(expect, result)) {
+ fprintf(stderr, "\nFailed to create object string. "
+ "\nExpected:\n'%s'\nGot:\n'%s'",
+ NULLSTR(expect), NULLSTR(result));
+ goto cleanup;
+ }
+
+ ret = 0;
+ cleanup:
+ virJSONValueFree(val);
+ VIR_FREE(result);
+ return ret;
+}
+
+static int
+mymain(void)
+{
+ int ret = 0;
+ testQemuCommandBuildObjectFromJSONData data1;
+
+#if !WITH_YAJL
+ fputs("libvirt not compiled with yajl, skipping this test\n", stderr);
+ return EXIT_AM_SKIP;
+#endif
+
+ virtTestCounterReset("testQemuCommandBuildObjectFromJSON");
+
+#define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT) \
+ do { \
+ data1.props = PROPS; \
+ data1.expectprops = EXPECT; \
+ if (virtTestRun(virtTestCounterNext(), \
+ testQemuCommandBuildObjectFromJSON, \
+ &data1) < 0) \
+ ret = -1; \
+ } while (0)
+
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{}", NULL);
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"string\":\"qwer\"}", "string=qwer");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"number\":1234}", "number=1234");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true}", "boolean=yes");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":false}", "boolean=no");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[]}", NULL);
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0]}", "bitmap=0");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,3,5]}",
+ "bitmap=1,bitmap=3,bitmap=5");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0,1,2,3]}", "bitmap=0-3");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5]}",
+ "bitmap=1-3,bitmap=5");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5,7,8,9]}",
+ "bitmap=1-3,bitmap=5,bitmap=7-9");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"array\":[\"bleah\",\"qwerty\",1]}",
+ "array=bleah,array=qwerty,array=1");
+ DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true,\"hyphen-name\":1234,\"some_string\":\"bleah\"}",
+ "boolean=yes,hyphen-name=1234,some_string=bleah");
+
+ return ret;
+
+}
+
+VIRT_TEST_MAIN(mymain)