]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virshtest: Allow to test failure of commands
authorPeter Krempa <pkrempa@redhat.com>
Tue, 12 Mar 2024 16:15:39 +0000 (17:15 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 Apr 2024 12:24:30 +0000 (14:24 +0200)
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>
tests/virshtest.c

index 5a5068dc783c9140589bdc0463a2183a151c56a2..4097c1d427bc6ff9aba406fe229427c6488fc042 100644 (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;
     }