[PATCH 5/7] Add virCommandSetMockOutputFile

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



After this file is set, all commands executed will be replaced by
cat <file>.

This is useful for testing functions that parse output of asynchronous
commands.
---
 src/libvirt_private.syms  |  1 +
 src/util/vircommand.c     | 33 +++++++++++++++++++++++++++++++++
 src/util/vircommandpriv.h |  1 +
 3 files changed, 35 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ea16cf5..8c15519 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1123,6 +1123,7 @@ virCommandSetInputFD;
 virCommandSetMaxFiles;
 virCommandSetMaxMemLock;
 virCommandSetMaxProcesses;
+virCommandSetMockOutputFile;
 virCommandSetOutputBuffer;
 virCommandSetOutputFD;
 virCommandSetPidFile;
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 7a799f2..f954141 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -133,6 +133,9 @@ struct _virCommand {
 /* See virCommandSetDryRun for description for this variable */
 static virBufferPtr dryRunBuffer;
 
+/* See virCommandSetMockOutputFile */
+static const char *mockOutputFile;
+
 /*
  * virCommandFDIsSet:
  * @fd: FD to test
@@ -2278,6 +2281,19 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
         goto cleanup;
     }
 
+    if (mockOutputFile) {
+        VIR_DEBUG("Replacing %s with cat '%s'", str, mockOutputFile);
+        for (i = 0; i < cmd->nargs; i++)
+            VIR_FREE(cmd->args[i]);
+        cmd->nargs = 0;
+
+        virCommandAddArgList(cmd, "cat", mockOutputFile, NULL);
+        if (cmd->has_error) {
+            virReportOOMError();
+            goto cleanup;
+        }
+    }
+
     VIR_DEBUG("About to run %s", str ? str : cmd->args[0]);
     ret = virExec(cmd);
     VIR_DEBUG("Command result %d, with PID %d",
@@ -2729,3 +2745,20 @@ virCommandSetDryRun(virBufferPtr buf)
 {
     dryRunBuffer = buf;
 }
+
+/**
+ * virCommandSetMockOutputFile:
+ * @path path to the file that should be concatenated
+ *       instead of running a command
+ *
+ * After calling this function, every command run will be replaced
+ * by 'cat @path'. This is useful for testing functions that
+ * parse output of asynchronous commands.
+ *
+ * NULL argument restores normal behavior.
+ */
+void
+virCommandSetMockOutputFile(const char *path)
+{
+    mockOutputFile = path;
+}
diff --git a/src/util/vircommandpriv.h b/src/util/vircommandpriv.h
index 2fbf3de..4d83f00 100644
--- a/src/util/vircommandpriv.h
+++ b/src/util/vircommandpriv.h
@@ -25,4 +25,5 @@
 # include "vircommand.h"
 
 void virCommandSetDryRun(virBufferPtr buf);
+void virCommandSetMockOutputFile(const char *path);
 #endif /* __VIR_COMMAND_PRIV_H__ */
-- 
1.8.3.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]