From 01c357a4c9e27d5123e8996cad14eb0e66786bd3 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 6 Apr 2021 10:56:23 +0200 Subject: [PATCH] virCommandSetDryRun: Add flags to linebreak and strip prefix from the command buffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko Reviewed-by: Pavel Hrdina --- src/util/vircommand.c | 21 ++++++++++++++++----- src/util/vircommandpriv.h | 2 ++ tests/networkxml2firewalltest.c | 2 +- tests/nodedevmdevctltest.c | 6 +++--- 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, 47 insertions(+), 34 deletions(-) diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 192613ed0a..e4f6eba148 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 @@ -2573,18 +2575,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) { @@ -2597,6 +2599,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", @@ -3117,6 +3120,8 @@ void virCommandDryRunTokenFree(virCommandDryRunToken *tok) { dryRunBuffer = NULL; + dryRunBufferArgLinebreaks = false; + dryRunBufferCommandStripPath = false; dryRunCallback = NULL; dryRunOpaque = NULL; g_free(tok); @@ -3127,6 +3132,8 @@ virCommandDryRunTokenFree(virCommandDryRunToken *tok) * virCommandSetDryRun: * @tok: a virCommandDryRunToken obtained from virCommandDryRunTokenNew * @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 @@ -3159,6 +3166,8 @@ virCommandDryRunTokenFree(virCommandDryRunToken *tok) void virCommandSetDryRun(virCommandDryRunToken *tok, virBufferPtr buf, + bool bufArgLinebreaks, + bool bufCommandStripPath, virCommandDryRunCallback cb, void *opaque) { @@ -3166,6 +3175,8 @@ virCommandSetDryRun(virCommandDryRunToken *tok, abort(); dryRunBuffer = buf; + dryRunBufferArgLinebreaks = bufArgLinebreaks; + dryRunBufferCommandStripPath = bufCommandStripPath; dryRunCallback = cb; dryRunOpaque = opaque; } diff --git a/src/util/vircommandpriv.h b/src/util/vircommandpriv.h index d06a8f5e30..6e01585766 100644 --- a/src/util/vircommandpriv.h +++ b/src/util/vircommandpriv.h @@ -43,5 +43,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCommandDryRunToken, virCommandDryRunTokenFree); 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 952c076a19..2974d3f2af 100644 --- a/tests/networkxml2firewalltest.c +++ b/tests/networkxml2firewalltest.c @@ -98,7 +98,7 @@ static int testCompareXMLToArgvFiles(const char *xml, char *actual; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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 cb9dbe5431..48d4f9e818 100644 --- a/tests/nodedevmdevctltest.c +++ b/tests/nodedevmdevctltest.c @@ -87,7 +87,7 @@ testMdevctlStartOrDefine(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; @@ -159,7 +159,7 @@ testMdevctlUuidCommand(const char *uuid, GetStopUndefineCmdFunc func, const char if (!cmd) goto cleanup; - virCommandSetDryRun(dryRunToken, &buf, NULL, NULL); + virCommandSetDryRun(dryRunToken, &buf, false, false, NULL, NULL); if (virCommandRun(cmd, NULL) < 0) goto cleanup; @@ -221,7 +221,7 @@ testMdevctlListDefined(const void *data G_GNUC_UNUSED) 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 36df17d1c1..c20b2bed42 100644 --- a/tests/nwfilterebiptablestest.c +++ b/tests/nwfilterebiptablestest.c @@ -105,7 +105,7 @@ testNWFilterEBIPTablesAllTeardown(const void *opaque G_GNUC_UNUSED) int ret = -1; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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 } }; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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) }; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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 38f2ab7040..f72cd06e58 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 5d028d2fd3..9129b6db7a 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 5515da4b4d..ba44201fa2 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 e673b69ed6..37922ba552 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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 d278032aec..b2dfecb42c 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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; g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); - 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 52742dff05..f399c95da0 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;