]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virshtest: use virCommand instead of custom impl
authorJán Tomko <jtomko@redhat.com>
Sun, 9 Feb 2020 01:00:06 +0000 (02:00 +0100)
committerJán Tomko <jtomko@redhat.com>
Tue, 11 Feb 2020 15:30:18 +0000 (16:30 +0100)
Our virCommand helper API already has the ability to capture
program output, there's no need to open-code it.

Apart from simplifying the code, the test is marginally faster
due to recent improvements in virCommandMassClose.

Until now, both stderr and stdout were stored in the same buffer.
This change stores stderr separately and expects it to be empty
for all the tests we currently run.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
tests/virshtest.c

index 83675710ea17f3dbcda8c6cd838f4052f6d1b774..add33215b78567cbf63cb17498ac9b67e585c6b8 100644 (file)
@@ -5,6 +5,7 @@
 #include "internal.h"
 #include "virxml.h"
 #include "testutils.h"
+#include "vircommand.h"
 #include "virstring.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
@@ -61,10 +62,26 @@ testCompareOutputLit(const char *expectData,
                      const char *filter, const char *const argv[])
 {
     g_autofree char *actualData = NULL;
+    const char *empty = "";
+    g_autoptr(virCommand) cmd = NULL;
+    g_autofree char *errbuf = NULL;
 
-    if (virTestCaptureProgramOutput(argv, &actualData, 4096) < 0)
+    if (!(cmd = virCommandNewArgs(argv)))
         return -1;
 
+    virCommandAddEnvString(cmd, "LANG=C");
+    virCommandSetInputBuffer(cmd, empty);
+    virCommandSetOutputBuffer(cmd, &actualData);
+    virCommandSetErrorBuffer(cmd, &errbuf);
+
+    if (virCommandRun(cmd, NULL) < 0)
+        return -1;
+
+    if (STRNEQ(errbuf, "")) {
+        fprintf(stderr, "Command reported error: %s", errbuf);
+        return -1;
+    }
+
     if (filter && testFilterLine(actualData, filter) < 0)
         return -1;