backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / qemucommandutiltest.c
blobf0921e3b935425fa42c09e393d3b2bcc07f4053b
1 /*
2 * Copyright (C) 2015-2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
20 #include <config.h>
22 #include "util/virjson.h"
23 #include "util/virqemu.h"
24 #include "testutils.h"
25 #include "testutilsqemu.h"
27 #define VIR_FROM_THIS VIR_FROM_NONE
29 typedef struct
31 const char *props;
32 const char *expectprops;
33 virQEMUBuildCommandLineJSONArrayFormatFunc arrayfunc;
34 } testQemuCommandBuildObjectFromJSONData;
36 static int
37 testQemuCommandBuildFromJSON(const void *opaque)
39 const testQemuCommandBuildObjectFromJSONData *data = opaque;
40 virJSONValuePtr val = NULL;
41 virBuffer buf = VIR_BUFFER_INITIALIZER;
42 char *result = NULL;
43 int ret = -1;
45 if (!(val = virJSONValueFromString(data->props))) {
46 fprintf(stderr, "Failed to parse JSON string '%s'", data->props);
47 return -1;
50 if (virQEMUBuildCommandLineJSON(val, &buf, data->arrayfunc) < 0) {
51 fprintf(stderr,
52 "\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n",
53 data->props);
54 goto cleanup;
57 result = virBufferContentAndReset(&buf);
59 if (STRNEQ_NULLABLE(data->expectprops, result)) {
60 fprintf(stderr, "\nFailed to create object string. "
61 "\nExpected:\n'%s'\nGot:\n'%s'",
62 NULLSTR(data->expectprops), NULLSTR(result));
63 goto cleanup;
66 ret = 0;
67 cleanup:
68 virJSONValueFree(val);
69 VIR_FREE(result);
70 return ret;
73 static int
74 mymain(void)
76 int ret = 0;
77 testQemuCommandBuildObjectFromJSONData data1;
79 #if !WITH_YAJL
80 fputs("libvirt not compiled with JSON support, skipping this test\n", stderr);
81 return EXIT_AM_SKIP;
82 #endif
84 virTestCounterReset("testQemuCommandBuildFromJSON");
86 #define DO_TEST_COMMAND_FROM_JSON(PROPS, ARRAYFUNC, EXPECT) \
87 do { \
88 data1.props = PROPS; \
89 data1.expectprops = EXPECT; \
90 data1.arrayfunc = ARRAYFUNC; \
91 if (virTestRun(virTestCounterNext(), \
92 testQemuCommandBuildFromJSON, \
93 &data1) < 0) \
94 ret = -1; \
95 } while (0)
97 #define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT) \
98 DO_TEST_COMMAND_FROM_JSON(PROPS, virQEMUBuildCommandLineJSONArrayBitmap, EXPECT)
100 #define DO_TEST_COMMAND_DRIVE_FROM_JSON(PROPS, EXPECT) \
101 DO_TEST_COMMAND_FROM_JSON(PROPS, virQEMUBuildCommandLineJSONArrayNumbered, EXPECT)
103 DO_TEST_COMMAND_OBJECT_FROM_JSON("{}", NULL);
104 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"string\":\"qwer\"}", "string=qwer");
105 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"string\":\"qw,e,r\"}", "string=qw,,e,,r");
106 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"number\":1234}", "number=1234");
107 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true}", "boolean=yes");
108 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":false}", "boolean=no");
109 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[]}", NULL);
110 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0]}", "bitmap=0");
111 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,3,5]}",
112 "bitmap=1,bitmap=3,bitmap=5");
113 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0,1,2,3]}", "bitmap=0-3");
114 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5]}",
115 "bitmap=1-3,bitmap=5");
116 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5,7,8,9]}",
117 "bitmap=1-3,bitmap=5,bitmap=7-9");
118 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"array\":[\"bleah\",\"qwerty\",1]}",
119 "array=bleah,array=qwerty,array=1");
120 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true,\"hyphen-name\":1234,\"some_string\":\"bleah\"}",
121 "boolean=yes,hyphen-name=1234,some_string=bleah");
122 DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"nest\": {\"boolean\":true,"
123 "\"hyphen-name\":1234,"
124 "\"some_string\":\"bleah\","
125 "\"bleah\":\"bl,eah\""
127 "}",
128 "nest.boolean=yes,nest.hyphen-name=1234,"
129 "nest.some_string=bleah,nest.bleah=bl,,eah");
130 DO_TEST_COMMAND_DRIVE_FROM_JSON("{\"driver\":\"gluster\","
131 "\"volume\":\"test\","
132 "\"path\":\"img\","
133 "\"server\":[ { \"type\":\"tcp\","
134 "\"host\":\"example.com\","
135 "\"port\":\"1234\""
136 "},"
137 "{ \"type\":\"unix\","
138 "\"socket\":\"/path/socket\""
139 "},"
140 "{ \"type\":\"tcp\","
141 "\"host\":\"example.com\""
144 "}",
145 "driver=gluster,volume=test,path=img,"
146 "server.0.type=tcp,"
147 "server.0.host=example.com,"
148 "server.0.port=1234,"
149 "server.1.type=unix,"
150 "server.1.socket=/path/socket,"
151 "server.2.type=tcp,"
152 "server.2.host=example.com");
154 return ret;
158 VIR_TEST_MAIN(mymain)