virCommandSetDryRun: Add flags to linebreak and strip prefix from the command buffer

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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Peter Krempa 2021-04-06 10:56:23 +02:00
parent 0dffca8f95
commit 01c357a4c9
11 changed files with 47 additions and 34 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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;