virshtest: Allow to test failure of commands

Modify the test code so that if virsh fails both 'stdout' and 'stderr'
are captured and compared against the output and also the return value
is checked by appending it to the output.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2024-03-12 17:15:39 +01:00
parent 86bf707838
commit 99392689cb

View File

@ -113,36 +113,37 @@ testCompareOutputLit(const char *expectFile,
const char *filter,
const char *const argv[])
{
g_autofree char *actualData = NULL;
g_autofree char *actual = NULL;
const char *empty = "";
g_autoptr(virCommand) cmd = NULL;
g_autofree char *errbuf = NULL;
int exitstatus = 0;
cmd = virCommandNewArgs(argv);
virCommandAddEnvString(cmd, "LANG=C");
virCommandSetInputBuffer(cmd, empty);
virCommandSetOutputBuffer(cmd, &actualData);
virCommandSetErrorBuffer(cmd, &errbuf);
virCommandSetOutputBuffer(cmd, &actual);
virCommandSetErrorBuffer(cmd, &actual);
if (virCommandRun(cmd, NULL) < 0)
if (virCommandRun(cmd, &exitstatus) < 0)
return -1;
if (STRNEQ(errbuf, "")) {
fprintf(stderr, "Command reported error: %s", errbuf);
return -1;
if (exitstatus != 0) {
g_autofree char *tmp = g_steal_pointer(&actual);
actual = g_strdup_printf("%s\n## Exit code: %d\n", tmp, exitstatus);
}
if (filter && testFilterLine(actualData, filter) < 0)
if (filter && testFilterLine(actual, filter) < 0)
return -1;
if (expectData) {
if (virTestCompareToString(expectData, actualData) < 0)
if (virTestCompareToString(expectData, actual) < 0)
return -1;
}
if (expectFile) {
if (virTestCompareToFileFull(actualData, expectFile, false) < 0)
if (virTestCompareToFileFull(actual, expectFile, false) < 0)
return -1;
}