backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virshtest.c
blob673c13fe07f77da436731eb8235693e3a2b5e903
1 #include <config.h>
3 #include <unistd.h>
5 #include "internal.h"
6 #include "virxml.h"
7 #include "testutils.h"
8 #include "virstring.h"
10 #define VIR_FROM_THIS VIR_FROM_NONE
12 #ifdef WIN32
14 int
15 main(void)
17 return EXIT_AM_SKIP;
20 #else
22 # define DOM_UUID "ef861801-45b9-11cb-88e3-afbfe5370493"
24 static const char *dominfo_fc4 = "\
25 Id: 2\n\
26 Name: fc4\n\
27 UUID: " DOM_UUID "\n\
28 OS Type: linux\n\
29 State: running\n\
30 CPU(s): 1\n\
31 Max memory: 261072 KiB\n\
32 Used memory: 131072 KiB\n\
33 Persistent: yes\n\
34 Autostart: disable\n\
35 Managed save: no\n\
36 \n";
37 static const char *domuuid_fc4 = DOM_UUID "\n\n";
38 static const char *domid_fc4 = "2\n\n";
39 static const char *domname_fc4 = "fc4\n\n";
40 static const char *domstate_fc4 = "running\n\n";
42 static int testFilterLine(char *buffer,
43 const char *toRemove)
45 char *start;
46 char *end;
48 if (!(start = strstr(buffer, toRemove)))
49 return -1;
51 if (!(end = strstr(start+1, "\n"))) {
52 *start = '\0';
53 } else {
54 memmove(start, end, strlen(end)+1);
56 return 0;
59 static int
60 testCompareOutputLit(const char *expectData,
61 const char *filter, const char *const argv[])
63 int result = -1;
64 char *actualData = NULL;
66 if (virTestCaptureProgramOutput(argv, &actualData, 4096) < 0)
67 goto cleanup;
69 if (filter && testFilterLine(actualData, filter) < 0)
70 goto cleanup;
72 if (STRNEQ(expectData, actualData)) {
73 virTestDifference(stderr, expectData, actualData);
74 goto cleanup;
77 result = 0;
79 cleanup:
80 VIR_FREE(actualData);
82 return result;
85 # define VIRSH_DEFAULT abs_top_builddir "/tools/virsh", \
86 "--connect", \
87 "test:///default"
89 static char *custom_uri;
91 # define VIRSH_CUSTOM abs_top_builddir "/tools/virsh", \
92 "--connect", \
93 custom_uri
95 static int testCompareListDefault(const void *data ATTRIBUTE_UNUSED)
97 const char *const argv[] = { VIRSH_DEFAULT, "list", NULL };
98 const char *exp = "\
99 Id Name State\n\
100 ----------------------\n\
101 1 test running\n\
102 \n";
103 return testCompareOutputLit(exp, NULL, argv);
106 static int testCompareListCustom(const void *data ATTRIBUTE_UNUSED)
108 const char *const argv[] = { VIRSH_CUSTOM, "list", NULL };
109 const char *exp = "\
110 Id Name State\n\
111 ----------------------\n\
112 1 fv0 running\n\
113 2 fc4 running\n\
114 \n";
115 return testCompareOutputLit(exp, NULL, argv);
118 static int testCompareNodeinfoDefault(const void *data ATTRIBUTE_UNUSED)
120 const char *const argv[] = { VIRSH_DEFAULT, "nodeinfo", NULL };
121 const char *exp = "\
122 CPU model: i686\n\
123 CPU(s): 16\n\
124 CPU frequency: 1400 MHz\n\
125 CPU socket(s): 2\n\
126 Core(s) per socket: 2\n\
127 Thread(s) per core: 2\n\
128 NUMA cell(s): 2\n\
129 Memory size: 3145728 KiB\n\
130 \n";
131 return testCompareOutputLit(exp, NULL, argv);
134 static int testCompareNodeinfoCustom(const void *data ATTRIBUTE_UNUSED)
136 const char *const argv[] = {
137 VIRSH_CUSTOM,
138 "nodeinfo",
139 NULL
141 const char *exp = "\
142 CPU model: i986\n\
143 CPU(s): 50\n\
144 CPU frequency: 6000 MHz\n\
145 CPU socket(s): 4\n\
146 Core(s) per socket: 4\n\
147 Thread(s) per core: 2\n\
148 NUMA cell(s): 4\n\
149 Memory size: 8192000 KiB\n\
150 \n";
151 return testCompareOutputLit(exp, NULL, argv);
154 static int testCompareDominfoByID(const void *data ATTRIBUTE_UNUSED)
156 const char *const argv[] = { VIRSH_CUSTOM, "dominfo", "2", NULL };
157 const char *exp = dominfo_fc4;
158 return testCompareOutputLit(exp, "\nCPU time:", argv);
161 static int testCompareDominfoByUUID(const void *data ATTRIBUTE_UNUSED)
163 const char *const argv[] = { VIRSH_CUSTOM, "dominfo", DOM_UUID, NULL };
164 const char *exp = dominfo_fc4;
165 return testCompareOutputLit(exp, "\nCPU time:", argv);
168 static int testCompareDominfoByName(const void *data ATTRIBUTE_UNUSED)
170 const char *const argv[] = { VIRSH_CUSTOM, "dominfo", "fc4", NULL };
171 const char *exp = dominfo_fc4;
172 return testCompareOutputLit(exp, "\nCPU time:", argv);
175 static int testCompareDomuuidByID(const void *data ATTRIBUTE_UNUSED)
177 const char *const argv[] = { VIRSH_CUSTOM, "domuuid", "2", NULL };
178 const char *exp = domuuid_fc4;
179 return testCompareOutputLit(exp, NULL, argv);
182 static int testCompareDomuuidByName(const void *data ATTRIBUTE_UNUSED)
184 const char *const argv[] = { VIRSH_CUSTOM, "domuuid", "fc4", NULL };
185 const char *exp = domuuid_fc4;
186 return testCompareOutputLit(exp, NULL, argv);
189 static int testCompareDomidByName(const void *data ATTRIBUTE_UNUSED)
191 const char *const argv[] = { VIRSH_CUSTOM, "domid", "fc4", NULL };
192 const char *exp = domid_fc4;
193 return testCompareOutputLit(exp, NULL, argv);
196 static int testCompareDomidByUUID(const void *data ATTRIBUTE_UNUSED)
198 const char *const argv[] = { VIRSH_CUSTOM, "domid", DOM_UUID, NULL };
199 const char *exp = domid_fc4;
200 return testCompareOutputLit(exp, NULL, argv);
203 static int testCompareDomnameByID(const void *data ATTRIBUTE_UNUSED)
205 const char *const argv[] = { VIRSH_CUSTOM, "domname", "2", NULL };
206 const char *exp = domname_fc4;
207 return testCompareOutputLit(exp, NULL, argv);
210 static int testCompareDomnameByUUID(const void *data ATTRIBUTE_UNUSED)
212 const char *const argv[] = { VIRSH_CUSTOM, "domname", DOM_UUID, NULL };
213 const char *exp = domname_fc4;
214 return testCompareOutputLit(exp, NULL, argv);
217 static int testCompareDomstateByID(const void *data ATTRIBUTE_UNUSED)
219 const char *const argv[] = { VIRSH_CUSTOM, "domstate", "2", NULL };
220 const char *exp = domstate_fc4;
221 return testCompareOutputLit(exp, NULL, argv);
224 static int testCompareDomstateByUUID(const void *data ATTRIBUTE_UNUSED)
226 const char *const argv[] = { VIRSH_CUSTOM, "domstate", DOM_UUID, NULL };
227 const char *exp = domstate_fc4;
228 return testCompareOutputLit(exp, NULL, argv);
231 static int testCompareDomstateByName(const void *data ATTRIBUTE_UNUSED)
233 const char *const argv[] = { VIRSH_CUSTOM, "domstate", "fc4", NULL };
234 const char *exp = domstate_fc4;
235 return testCompareOutputLit(exp, NULL, argv);
238 struct testInfo {
239 const char *const *argv;
240 const char *result;
243 static int testCompareEcho(const void *data)
245 const struct testInfo *info = data;
246 return testCompareOutputLit(info->result, NULL, info->argv);
250 static int
251 mymain(void)
253 int ret = 0;
255 if (virAsprintf(&custom_uri, "test://%s/../examples/xml/test/testnode.xml",
256 abs_srcdir) < 0)
257 return EXIT_FAILURE;
259 if (virTestRun("virsh list (default)",
260 testCompareListDefault, NULL) != 0)
261 ret = -1;
263 if (virTestRun("virsh list (custom)",
264 testCompareListCustom, NULL) != 0)
265 ret = -1;
267 if (virTestRun("virsh nodeinfo (default)",
268 testCompareNodeinfoDefault, NULL) != 0)
269 ret = -1;
271 if (virTestRun("virsh nodeinfo (custom)",
272 testCompareNodeinfoCustom, NULL) != 0)
273 ret = -1;
275 if (virTestRun("virsh dominfo (by id)",
276 testCompareDominfoByID, NULL) != 0)
277 ret = -1;
279 if (virTestRun("virsh dominfo (by uuid)",
280 testCompareDominfoByUUID, NULL) != 0)
281 ret = -1;
283 if (virTestRun("virsh dominfo (by name)",
284 testCompareDominfoByName, NULL) != 0)
285 ret = -1;
287 if (virTestRun("virsh domid (by name)",
288 testCompareDomidByName, NULL) != 0)
289 ret = -1;
291 if (virTestRun("virsh domid (by uuid)",
292 testCompareDomidByUUID, NULL) != 0)
293 ret = -1;
295 if (virTestRun("virsh domuuid (by id)",
296 testCompareDomuuidByID, NULL) != 0)
297 ret = -1;
299 if (virTestRun("virsh domuuid (by name)",
300 testCompareDomuuidByName, NULL) != 0)
301 ret = -1;
303 if (virTestRun("virsh domname (by id)",
304 testCompareDomnameByID, NULL) != 0)
305 ret = -1;
307 if (virTestRun("virsh domname (by uuid)",
308 testCompareDomnameByUUID, NULL) != 0)
309 ret = -1;
311 if (virTestRun("virsh domstate (by id)",
312 testCompareDomstateByID, NULL) != 0)
313 ret = -1;
315 if (virTestRun("virsh domstate (by uuid)",
316 testCompareDomstateByUUID, NULL) != 0)
317 ret = -1;
319 if (virTestRun("virsh domstate (by name)",
320 testCompareDomstateByName, NULL) != 0)
321 ret = -1;
323 /* It's a bit awkward listing result before argument, but that's a
324 * limitation of C99 vararg macros. */
325 # define DO_TEST(i, result, ...) \
326 do { \
327 const char *myargv[] = { VIRSH_DEFAULT, __VA_ARGS__, NULL }; \
328 const struct testInfo info = { myargv, result }; \
329 if (virTestRun("virsh echo " #i, \
330 testCompareEcho, &info) < 0) \
331 ret = -1; \
332 } while (0)
334 /* Arg parsing quote removal tests. */
335 DO_TEST(0, "\n",
336 "echo");
337 DO_TEST(1, "a\n",
338 "echo", "a");
339 DO_TEST(2, "a b\n",
340 "echo", "a", "b");
341 DO_TEST(3, "a b\n",
342 "echo a \t b");
343 DO_TEST(4, "a \t b\n",
344 "echo \"a \t b\"");
345 DO_TEST(5, "a \t b\n",
346 "echo 'a \t b'");
347 DO_TEST(6, "a \t b\n",
348 "echo a\\ \\\t\\ b");
349 DO_TEST(7, "\n\n",
350 "echo ; echo");
351 DO_TEST(8, "a\nb\n",
352 ";echo a; ; echo b;");
353 DO_TEST(9, "' \" \\;echo\ta\n",
354 "echo", "'", "\"", "\\;echo\ta");
355 DO_TEST(10, "' \" ;echo a\n",
356 "echo \\' \\\" \\;echo\ta");
357 DO_TEST(11, "' \" \\\na\n",
358 "echo \\' \\\" \\\\;echo\ta");
359 DO_TEST(12, "' \" \\\\\n",
360 "echo \"'\" '\"' '\\'\"\\\\\"");
362 /* Tests of echo flags. */
363 DO_TEST(13, "a A 0 + * ; . ' \" / ? = \n < > &\n",
364 "echo", "a", "A", "0", "+", "*", ";", ".", "'", "\"", "/", "?",
365 "=", " ", "\n", "<", ">", "&");
366 DO_TEST(14, "a A 0 + '*' ';' . ''\\''' '\"' / '?' = ' ' '\n' '<' '>' '&'\n",
367 "echo", "--shell", "a", "A", "0", "+", "*", ";", ".", "'", "\"",
368 "/", "?", "=", " ", "\n", "<", ">", "&");
369 DO_TEST(15, "a A 0 + * ; . &apos; &quot; / ? = \n &lt; &gt; &amp;\n",
370 "echo", "--xml", "a", "A", "0", "+", "*", ";", ".", "'", "\"",
371 "/", "?", "=", " ", "\n", "<", ">", "&");
372 DO_TEST(16, "a A 0 + '*' ';' . '&apos;' '&quot;' / '?' = ' ' '\n' '&lt;'"
373 " '&gt;' '&amp;'\n",
374 "echo", "--shell", "--xml", "a", "A", "0", "+", "*", ";", ".", "'",
375 "\"", "/", "?", "=", " ", "\n", "<", ">", "&");
376 DO_TEST(17, "\n",
377 "echo", "");
378 DO_TEST(18, "''\n",
379 "echo", "--shell", "");
380 DO_TEST(19, "\n",
381 "echo", "--xml", "");
382 DO_TEST(20, "''\n",
383 "echo", "--xml", "--shell", "");
384 DO_TEST(21, "\n",
385 "echo ''");
386 DO_TEST(22, "''\n",
387 "echo --shell \"\"");
388 DO_TEST(23, "\n",
389 "echo --xml ''");
390 DO_TEST(24, "''\n",
391 "echo --xml --shell \"\"''");
393 /* Tests of -- handling. */
394 DO_TEST(25, "a\n",
395 "--", "echo", "--shell", "a");
396 DO_TEST(26, "a\n",
397 "--", "echo", "a", "--shell");
398 DO_TEST(27, "a --shell\n",
399 "--", "echo", "--", "a", "--shell");
400 DO_TEST(28, "-- --shell a\n",
401 "echo", "--", "--", "--shell", "a");
402 DO_TEST(29, "a\n",
403 "echo --s\\h'e'\"l\"l -- a");
404 DO_TEST(30, "--shell a\n",
405 "echo \t '-'\"-\" \t --shell \t a");
407 /* Tests of alias handling. */
408 DO_TEST(31, "hello\n", "echo", "--string", "hello");
409 DO_TEST(32, "hello\n", "echo --string hello");
410 DO_TEST(33, "hello\n", "echo", "--str", "hello");
411 DO_TEST(34, "hello\n", "echo --str hello");
412 DO_TEST(35, "hello\n", "echo --hi");
414 /* Tests of multiple commands. */
415 DO_TEST(36, "a\nb\n", " echo a; echo b;");
416 DO_TEST(37, "a\nb\n", "\necho a\n echo b\n");
417 DO_TEST(38, "a\nb\n", "ec\\\nho a\n echo \\\n b;");
418 DO_TEST(39, "a\n b\n", "\"ec\\\nho\" a\n echo \"\\\n b\";");
419 DO_TEST(40, "a\n\\\n b\n", "ec\\\nho a\n echo '\\\n b';");
420 DO_TEST(41, "a\n", "echo a # b");
421 DO_TEST(42, "a\nc\n", "echo a #b\necho c");
422 DO_TEST(43, "a\nc\n", "echo a # b\\\necho c");
423 DO_TEST(44, "a # b\n", "echo a '#' b");
424 DO_TEST(45, "a # b\n", "echo a \\# b");
425 DO_TEST(46, "a\n", "#unbalanced; 'quotes\"\necho a # b");
426 DO_TEST(47, "a\n", "\\# ignored;echo a\n'#also' ignored");
428 # undef DO_TEST
430 VIR_FREE(custom_uri);
431 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
434 VIR_TEST_MAIN(mymain)
436 #endif /* WIN32 */