[PATCH 08/24] virCommandSetDryRun: Add flags to linebreak and strip prefix from the command buffer

Peter Krempa pkrempa at redhat.com
Wed Apr 7 15:09:31 UTC 2021


virCommandToStringFull used internally when virCommandSetDryRun is
requested allows to strip command path and wrap lines nicely. Expose
these via virCommandSetDryRun so that tests can use those features
instead of local hacks.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/vircommand.c            | 21 ++++++++++++++++-----
 src/util/vircommandpriv.h        |  2 ++
 tests/networkxml2firewalltest.c  |  2 +-
 tests/nodedevmdevctltest.c       |  4 ++--
 tests/nwfilterebiptablestest.c   | 14 +++++++-------
 tests/nwfilterxml2firewalltest.c |  2 +-
 tests/sysinfotest.c              |  2 +-
 tests/virfirewalltest.c          | 20 ++++++++++----------
 tests/viriscsitest.c             |  6 +++---
 tests/virkmodtest.c              |  4 ++--
 tests/virnetdevbandwidthtest.c   |  2 +-
 11 files changed, 46 insertions(+), 33 deletions(-)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 7b2588f5c8..3eb4f3a6d5 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -158,6 +158,8 @@ struct _virCommand {

 /* See virCommandSetDryRun for description for this variable */
 static virBufferPtr dryRunBuffer;
+static bool dryRunBufferArgLinebreaks;
+static bool dryRunBufferCommandStripPath;
 static virCommandDryRunCallback dryRunCallback;
 static void *dryRunOpaque;
 #ifndef WIN32
@@ -2585,18 +2587,18 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
         goto cleanup;
     }

-    str = virCommandToString(cmd, false);
     if (dryRunBuffer || dryRunCallback) {
+        g_autofree char *cmdstr = NULL;
         dryRunStatus = 0;
-        if (!str) {
-            /* error already reported by virCommandToString */
+
+        if (!(cmdstr = virCommandToStringFull(cmd, dryRunBufferArgLinebreaks,
+                                              dryRunBufferCommandStripPath)))
             goto cleanup;
-        }

         if (dryRunBuffer) {
             VIR_DEBUG("Dry run requested, appending stringified "
                       "command to dryRunBuffer=%p", dryRunBuffer);
-            virBufferAdd(dryRunBuffer, str, -1);
+            virBufferAdd(dryRunBuffer, cmdstr, -1);
             virBufferAddChar(dryRunBuffer, '\n');
         }
         if (dryRunCallback) {
@@ -2609,6 +2611,7 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
         goto cleanup;
     }

+    str = virCommandToString(cmd, false);
     VIR_DEBUG("About to run %s", str ? str : cmd->args[0]);
     ret = virExec(cmd);
     VIR_DEBUG("Command result %d, with PID %d",
@@ -3102,6 +3105,8 @@ virCommandDoAsyncIO(virCommandPtr cmd)
 /**
  * virCommandSetDryRun:
  * @buf: buffer to store stringified commands
+ * @bufArgLinebreaks: add linebreaks after command and every argument or argument pair
+ * @bufCommandStripPath: strip leading paths of command
  * @callback: callback to process input/output/args
  *
  * Sometimes it's desired to not actually run given command, but
@@ -3134,10 +3139,14 @@ virCommandDoAsyncIO(virCommandPtr cmd)
 void
 virCommandSetDryRun(virCommandDryRunToken tok G_GNUC_UNUSED,
                     virBufferPtr buf,
+                    bool bufArgLinebreaks,
+                    bool bufCommandStripPath,
                     virCommandDryRunCallback cb,
                     void *opaque)
 {
     dryRunBuffer = buf;
+    dryRunBufferArgLinebreaks = bufArgLinebreaks;
+    dryRunBufferCommandStripPath = bufCommandStripPath;
     dryRunCallback = cb;
     dryRunOpaque = opaque;
 }
@@ -3147,6 +3156,8 @@ void
 virCommandDryRunResetHelper(virCommandDryRunToken *tok G_GNUC_UNUSED)
 {
     dryRunBuffer = NULL;
+    dryRunBufferArgLinebreaks = false;
+    dryRunBufferCommandStripPath = false;
     dryRunCallback = NULL;
     dryRunOpaque = NULL;
 }
diff --git a/src/util/vircommandpriv.h b/src/util/vircommandpriv.h
index 8a47a6d5e3..23a604ccd9 100644
--- a/src/util/vircommandpriv.h
+++ b/src/util/vircommandpriv.h
@@ -39,6 +39,8 @@ typedef int virCommandDryRunToken;

 void virCommandSetDryRun(virCommandDryRunToken tok,
                          virBufferPtr buf,
+                         bool bufArgLinebreaks,
+                         bool bufCommandStripPath,
                          virCommandDryRunCallback cb,
                          void *opaque);

diff --git a/tests/networkxml2firewalltest.c b/tests/networkxml2firewalltest.c
index 574b553fa6..2e4ff59fa6 100644
--- a/tests/networkxml2firewalltest.c
+++ b/tests/networkxml2firewalltest.c
@@ -98,7 +98,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
     char *actual;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, testCommandDryRun, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, testCommandDryRun, NULL);

     if (!(def = virNetworkDefParseFile(xml, NULL)))
         goto cleanup;
diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c
index ce8e5e85f6..bd32519498 100644
--- a/tests/nodedevmdevctltest.c
+++ b/tests/nodedevmdevctltest.c
@@ -72,7 +72,7 @@ testMdevctlStart(const char *virt_type,
     if (!cmd)
         goto cleanup;

-    virCommandSetDryRun(dryRunToken, &buf, testCommandDryRunCallback, &stdinbuf);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, testCommandDryRunCallback, &stdinbuf);
     if (virCommandRun(cmd, NULL) < 0)
         goto cleanup;

@@ -127,7 +127,7 @@ testMdevctlStop(const void *data)
     if (!cmd)
         goto cleanup;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);
     if (virCommandRun(cmd, NULL) < 0)
         goto cleanup;

diff --git a/tests/nwfilterebiptablestest.c b/tests/nwfilterebiptablestest.c
index 0893548c0a..14601031a7 100644
--- a/tests/nwfilterebiptablestest.c
+++ b/tests/nwfilterebiptablestest.c
@@ -105,7 +105,7 @@ testNWFilterEBIPTablesAllTeardown(const void *opaque G_GNUC_UNUSED)
     int ret = -1;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (ebiptables_driver.allTeardown("vnet0") < 0)
         goto cleanup;
@@ -172,7 +172,7 @@ testNWFilterEBIPTablesTearOldRules(const void *opaque G_GNUC_UNUSED)
     int ret = -1;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (ebiptables_driver.tearOldRules("vnet0") < 0)
         goto cleanup;
@@ -217,7 +217,7 @@ testNWFilterEBIPTablesRemoveBasicRules(const void *opaque G_GNUC_UNUSED)
     int ret = -1;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (ebiptables_driver.removeBasicRules("vnet0") < 0)
         goto cleanup;
@@ -247,7 +247,7 @@ testNWFilterEBIPTablesTearNewRules(const void *opaque G_GNUC_UNUSED)
     int ret = -1;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (ebiptables_driver.tearNewRules("vnet0") < 0)
         goto cleanup;
@@ -315,7 +315,7 @@ testNWFilterEBIPTablesApplyBasicRules(const void *opaque G_GNUC_UNUSED)
     virMacAddr mac = { .addr = { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60 } };
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (ebiptables_driver.applyBasicRules("vnet0", &mac) < 0)
         goto cleanup;
@@ -401,7 +401,7 @@ testNWFilterEBIPTablesApplyDHCPOnlyRules(const void *opaque G_GNUC_UNUSED)
     };
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (ebiptables_driver.applyDHCPOnlyRules("vnet0", &mac, &val, false) < 0)
         goto cleanup;
@@ -470,7 +470,7 @@ testNWFilterEBIPTablesApplyDropAllRules(const void *opaque G_GNUC_UNUSED)
     int ret = -1;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (ebiptables_driver.applyDropAllRules("vnet0") < 0)
         goto cleanup;
diff --git a/tests/nwfilterxml2firewalltest.c b/tests/nwfilterxml2firewalltest.c
index a3b8ae70c1..446461c122 100644
--- a/tests/nwfilterxml2firewalltest.c
+++ b/tests/nwfilterxml2firewalltest.c
@@ -375,7 +375,7 @@ static int testCompareXMLToArgvFiles(const char *xml,

     memset(&inst, 0, sizeof(inst));

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (!vars)
         goto cleanup;
diff --git a/tests/sysinfotest.c b/tests/sysinfotest.c
index 85687e995a..b3bab0a627 100644
--- a/tests/sysinfotest.c
+++ b/tests/sysinfotest.c
@@ -102,7 +102,7 @@ testSysinfo(const void *data)
     cpuinfo = g_strdup_printf("%s/sysinfodata/%scpuinfo.data", abs_srcdir, testdata->name);
     expected = g_strdup_printf("%s/sysinfodata/%ssysinfo.expect", abs_srcdir, testdata->name);

-    virCommandSetDryRun(dryRunToken, NULL, testDMIDecodeDryRun, sysinfo);
+    virCommandSetDryRun(dryRunToken, NULL, false, false, testDMIDecodeDryRun, sysinfo);

     virSysinfoSetup(sysinfo, cpuinfo);

diff --git a/tests/virfirewalltest.c b/tests/virfirewalltest.c
index c442aca9da..80b287acdb 100644
--- a/tests/virfirewalltest.c
+++ b/tests/virfirewalltest.c
@@ -197,7 +197,7 @@ testFirewallSingleGroup(const void *opaque)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD)
-        virCommandSetDryRun(dryRunToken, &cmdbuf, NULL, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, NULL, NULL);
     else
         fwBuf = &cmdbuf;

@@ -251,7 +251,7 @@ testFirewallRemoveRule(const void *opaque)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD)
-        virCommandSetDryRun(dryRunToken, &cmdbuf, NULL, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, NULL, NULL);
     else
         fwBuf = &cmdbuf;

@@ -312,7 +312,7 @@ testFirewallManyGroups(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD)
-        virCommandSetDryRun(dryRunToken, &cmdbuf, NULL, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, NULL, NULL);
     else
         fwBuf = &cmdbuf;

@@ -400,7 +400,7 @@ testFirewallIgnoreFailGroup(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
-        virCommandSetDryRun(dryRunToken, &cmdbuf, testFirewallRollbackHook, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, testFirewallRollbackHook, NULL);
     } else {
         fwBuf = &cmdbuf;
         fwError = true;
@@ -469,7 +469,7 @@ testFirewallIgnoreFailRule(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
-        virCommandSetDryRun(dryRunToken, &cmdbuf, testFirewallRollbackHook, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, testFirewallRollbackHook, NULL);
     } else {
         fwBuf = &cmdbuf;
         fwError = true;
@@ -535,7 +535,7 @@ testFirewallNoRollback(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
-        virCommandSetDryRun(dryRunToken, &cmdbuf, testFirewallRollbackHook, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, testFirewallRollbackHook, NULL);
     } else {
         fwBuf = &cmdbuf;
         fwError = true;
@@ -599,7 +599,7 @@ testFirewallSingleRollback(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
-        virCommandSetDryRun(dryRunToken, &cmdbuf, testFirewallRollbackHook, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, testFirewallRollbackHook, NULL);
     } else {
         fwError = true;
         fwBuf = &cmdbuf;
@@ -679,7 +679,7 @@ testFirewallManyRollback(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
-        virCommandSetDryRun(dryRunToken, &cmdbuf, testFirewallRollbackHook, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, testFirewallRollbackHook, NULL);
     } else {
         fwBuf = &cmdbuf;
         fwError = true;
@@ -767,7 +767,7 @@ testFirewallChainedRollback(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
-        virCommandSetDryRun(dryRunToken, &cmdbuf, testFirewallRollbackHook, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, testFirewallRollbackHook, NULL);
     } else {
         fwBuf = &cmdbuf;
         fwError = true;
@@ -963,7 +963,7 @@ testFirewallQuery(const void *opaque G_GNUC_UNUSED)

     if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT ||
         data->expectBackend == VIR_FIREWALL_BACKEND_FIREWALLD) {
-        virCommandSetDryRun(dryRunToken, &cmdbuf, testFirewallQueryHook, NULL);
+        virCommandSetDryRun(dryRunToken, &cmdbuf, false, false, testFirewallQueryHook, NULL);
     } else {
         fwBuf = &cmdbuf;
         fwError = true;
diff --git a/tests/viriscsitest.c b/tests/viriscsitest.c
index 2927e235cd..de50ec3770 100644
--- a/tests/viriscsitest.c
+++ b/tests/viriscsitest.c
@@ -216,7 +216,7 @@ testISCSIGetSession(const void *data)

     cbData.output_version = info->output_version;

-    virCommandSetDryRun(dryRunToken, NULL, testIscsiadmCb, &cbData);
+    virCommandSetDryRun(dryRunToken, NULL, false, false, testIscsiadmCb, &cbData);

     actual_session = virISCSIGetSession(info->device_path, true);

@@ -252,7 +252,7 @@ testISCSIScanTargets(const void *data)
     size_t i;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, NULL, testIscsiadmCb, NULL);
+    virCommandSetDryRun(dryRunToken, NULL, false, false, testIscsiadmCb, NULL);

     if (virISCSIScanTargets(info->portal, NULL,
                             false, &ntargets, &targets) < 0)
@@ -299,7 +299,7 @@ testISCSIConnectionLogin(const void *data)
     int ret = -1;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, NULL, testIscsiadmCb, &cbData);
+    virCommandSetDryRun(dryRunToken, NULL, false, false, testIscsiadmCb, &cbData);

     if (virISCSIConnectionLogin(info->portal, info->initiatoriqn, info->target) < 0)
         goto cleanup;
diff --git a/tests/virkmodtest.c b/tests/virkmodtest.c
index 725c204667..c4bd098740 100644
--- a/tests/virkmodtest.c
+++ b/tests/virkmodtest.c
@@ -63,7 +63,7 @@ testKModLoad(const void *args G_GNUC_UNUSED)
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     errbuf = virKModLoad(MODNAME);
     if (errbuf) {
@@ -90,7 +90,7 @@ testKModUnload(const void *args G_GNUC_UNUSED)
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     VIR_COMMAND_DRY_RUN_TOKEN;

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     errbuf = virKModUnload(MODNAME);
     if (errbuf) {
diff --git a/tests/virnetdevbandwidthtest.c b/tests/virnetdevbandwidthtest.c
index 5233fc47d3..640d10d300 100644
--- a/tests/virnetdevbandwidthtest.c
+++ b/tests/virnetdevbandwidthtest.c
@@ -79,7 +79,7 @@ testVirNetDevBandwidthSet(const void *data)
     if (!iface)
         iface = "eth0";

-    virCommandSetDryRun(dryRunToken, &buf, NULL, NULL);
+    virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL);

     if (virNetDevBandwidthSet(iface, band, info->hierarchical_class, true) < 0)
         goto cleanup;
-- 
2.30.2




More information about the libvir-list mailing list