From: Peter Krempa Date: Tue, 12 Mar 2024 16:15:39 +0000 (+0100) Subject: virshtest: Allow to test failure of commands X-Git-Tag: v10.3.0-rc1~110 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=99392689cbff20b6cb5983b9bc66f35b2faebf01;p=thirdparty%2Flibvirt.git 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 Reviewed-by: Ján Tomko --- diff --git a/tests/virshtest.c b/tests/virshtest.c index 5a5068dc78..4097c1d427 100644 --- a/tests/virshtest.c +++ b/tests/virshtest.c @@ -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; }