10 #define VIR_FROM_THIS VIR_FROM_NONE
22 # define DOM_UUID "ef861801-45b9-11cb-88e3-afbfe5370493"
24 static const char *dominfo_fc4
= "\
31 Max memory: 261072 KiB\n\
32 Used memory: 131072 KiB\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
,
48 if (!(start
= strstr(buffer
, toRemove
)))
51 if (!(end
= strstr(start
+1, "\n"))) {
54 memmove(start
, end
, strlen(end
)+1);
60 testCompareOutputLit(const char *expectData
,
61 const char *filter
, const char *const argv
[])
64 char *actualData
= NULL
;
66 if (virTestCaptureProgramOutput(argv
, &actualData
, 4096) < 0)
69 if (filter
&& testFilterLine(actualData
, filter
) < 0)
72 if (STRNEQ(expectData
, actualData
)) {
73 virTestDifference(stderr
, expectData
, actualData
);
85 # define VIRSH_DEFAULT abs_top_builddir "/tools/virsh", \
89 static char *custom_uri
;
91 # define VIRSH_CUSTOM abs_top_builddir "/tools/virsh", \
95 static int testCompareListDefault(const void *data ATTRIBUTE_UNUSED
)
97 const char *const argv
[] = { VIRSH_DEFAULT
, "list", NULL
};
100 ----------------------\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
};
111 ----------------------\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
};
124 CPU frequency: 1400 MHz\n\
126 Core(s) per socket: 2\n\
127 Thread(s) per core: 2\n\
129 Memory size: 3145728 KiB\n\
131 return testCompareOutputLit(exp
, NULL
, argv
);
134 static int testCompareNodeinfoCustom(const void *data ATTRIBUTE_UNUSED
)
136 const char *const argv
[] = {
144 CPU frequency: 6000 MHz\n\
146 Core(s) per socket: 4\n\
147 Thread(s) per core: 2\n\
149 Memory size: 8192000 KiB\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
);
239 const char *const *argv
;
243 static int testCompareEcho(const void *data
)
245 const struct testInfo
*info
= data
;
246 return testCompareOutputLit(info
->result
, NULL
, info
->argv
);
255 if (virAsprintf(&custom_uri
, "test://%s/../examples/xml/test/testnode.xml",
259 if (virTestRun("virsh list (default)",
260 testCompareListDefault
, NULL
) != 0)
263 if (virTestRun("virsh list (custom)",
264 testCompareListCustom
, NULL
) != 0)
267 if (virTestRun("virsh nodeinfo (default)",
268 testCompareNodeinfoDefault
, NULL
) != 0)
271 if (virTestRun("virsh nodeinfo (custom)",
272 testCompareNodeinfoCustom
, NULL
) != 0)
275 if (virTestRun("virsh dominfo (by id)",
276 testCompareDominfoByID
, NULL
) != 0)
279 if (virTestRun("virsh dominfo (by uuid)",
280 testCompareDominfoByUUID
, NULL
) != 0)
283 if (virTestRun("virsh dominfo (by name)",
284 testCompareDominfoByName
, NULL
) != 0)
287 if (virTestRun("virsh domid (by name)",
288 testCompareDomidByName
, NULL
) != 0)
291 if (virTestRun("virsh domid (by uuid)",
292 testCompareDomidByUUID
, NULL
) != 0)
295 if (virTestRun("virsh domuuid (by id)",
296 testCompareDomuuidByID
, NULL
) != 0)
299 if (virTestRun("virsh domuuid (by name)",
300 testCompareDomuuidByName
, NULL
) != 0)
303 if (virTestRun("virsh domname (by id)",
304 testCompareDomnameByID
, NULL
) != 0)
307 if (virTestRun("virsh domname (by uuid)",
308 testCompareDomnameByUUID
, NULL
) != 0)
311 if (virTestRun("virsh domstate (by id)",
312 testCompareDomstateByID
, NULL
) != 0)
315 if (virTestRun("virsh domstate (by uuid)",
316 testCompareDomstateByUUID
, NULL
) != 0)
319 if (virTestRun("virsh domstate (by name)",
320 testCompareDomstateByName
, NULL
) != 0)
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, ...) \
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) \
334 /* Arg parsing quote removal tests. */
343 DO_TEST(4, "a \t b\n",
345 DO_TEST(5, "a \t b\n",
347 DO_TEST(6, "a \t b\n",
348 "echo a\\ \\\t\\ b");
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 + * ; . ' " / ? = \n < > &\n",
370 "echo", "--xml", "a", "A", "0", "+", "*", ";", ".", "'", "\"",
371 "/", "?", "=", " ", "\n", "<", ">", "&");
372 DO_TEST(16, "a A 0 + '*' ';' . ''' '"' / '?' = ' ' '\n' '<'"
374 "echo", "--shell", "--xml", "a", "A", "0", "+", "*", ";", ".", "'",
375 "\"", "/", "?", "=", " ", "\n", "<", ">", "&");
379 "echo", "--shell", "");
381 "echo", "--xml", "");
383 "echo", "--xml", "--shell", "");
387 "echo --shell \"\"");
391 "echo --xml --shell \"\"''");
393 /* Tests of -- handling. */
395 "--", "echo", "--shell", "a");
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");
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");
430 VIR_FREE(custom_uri
);
431 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
434 VIR_TEST_MAIN(mymain
)