[libvirt] [PATCH 0/2] Fix VIR_TEST_REGENERATE_OUTPUT in tests

Instead of the proper solution of moving all the host-changing stuff out of qemuBuildCommandLine, separate one helper for easier mocking. Ján Tomko (2): qemuExecuteEthernetScript: move to util qemuxml2argvtest: do not mock virCommand src/libvirt_private.syms | 1 + src/qemu/qemu_interface.c | 29 +---------------------------- src/util/virnetdev.c | 29 +++++++++++++++++++++++++++++ src/util/virnetdev.h | 2 ++ tests/qemuxml2argvmock.c | 7 ++----- 5 files changed, 35 insertions(+), 33 deletions(-) -- 2.7.3

This is just a wrapper for virCommand that takes two strings and runs them. Move it to virnetdev.c for easier mocking. --- src/qemu/qemu_interface.c | 29 +---------------------------- src/util/virnetdev.c | 29 +++++++++++++++++++++++++++++ src/util/virnetdev.h | 2 ++ 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index 74c9ca8..a4e9d86 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -387,33 +387,6 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfigPtr cfg, return *tapfd < 0 ? -1 : 0; } -/** - * qemuExecuteEthernetScript: - * @ifname: the interface name - * @script: the script name - * - * This function executes script for new tap device created by libvirt. - * Returns 0 in case of success or -1 on failure - */ -static int -qemuExecuteEthernetScript(const char *ifname, const char *script) -{ - virCommandPtr cmd; - int ret; - - cmd = virCommandNew(script); - virCommandAddArgFormat(cmd, "%s", ifname); - virCommandClearCaps(cmd); -#ifdef CAP_NET_ADMIN - virCommandAllowCap(cmd, CAP_NET_ADMIN); -#endif - virCommandAddEnvPassCommon(cmd); - - ret = virCommandRun(cmd, NULL); - - virCommandFree(cmd); - return ret; -} /* qemuInterfaceEthernetConnect: * @def: the definition of the VM @@ -515,7 +488,7 @@ qemuInterfaceEthernetConnect(virDomainDefPtr def, if (net->script && - qemuExecuteEthernetScript(net->ifname, net->script) < 0) + virNetDevRunEthernetScript(net->ifname, net->script) < 0) goto cleanup; if (cfg->macFilter && diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index a505b6c..bb17b84 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -3370,3 +3370,32 @@ virNetDevGetFeatures(const char *ifname ATTRIBUTE_UNUSED, return 0; } #endif + + +/** + * virNetDevRunEthernetScript: + * @ifname: the interface name + * @script: the script name + * + * This function executes script for new tap device created by libvirt. + * Returns 0 in case of success or -1 on failure + */ +int +virNetDevRunEthernetScript(const char *ifname, const char *script) +{ + virCommandPtr cmd; + int ret; + + cmd = virCommandNew(script); + virCommandAddArgFormat(cmd, "%s", ifname); + virCommandClearCaps(cmd); +#ifdef CAP_NET_ADMIN + virCommandAllowCap(cmd, CAP_NET_ADMIN); +#endif + virCommandAddEnvPassCommon(cmd); + + ret = virCommandRun(cmd, NULL); + + virCommandFree(cmd); + return ret; +} diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 240fff7..dcc81a6 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -230,4 +230,6 @@ int virNetDevSysfsFile(char **pf_sysfs_device_link, const char *file) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK; + +int virNetDevRunEthernetScript(const char *ifname, const char *script); #endif /* __VIR_NETDEV_H__ */ -- 2.7.3

Mock virNetDevRunEthernetScript instead. This restores the VIR_TEST_REGENERATE_OUTPUT functionality. --- src/libvirt_private.syms | 1 + tests/qemuxml2argvmock.c | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a79d85e..45ad961 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1810,6 +1810,7 @@ virNetDevReplaceMacAddress; virNetDevReplaceNetConfig; virNetDevRestoreMacAddress; virNetDevRestoreNetConfig; +virNetDevRunEthernetScript; virNetDevRxFilterFree; virNetDevRxFilterModeTypeFromString; virNetDevRxFilterModeTypeToString; diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c index eb2a5b4..1616eed 100644 --- a/tests/qemuxml2argvmock.c +++ b/tests/qemuxml2argvmock.c @@ -132,12 +132,9 @@ virNetDevSetOnline(const char *ifname ATTRIBUTE_UNUSED, } int -virCommandRun(virCommandPtr cmd ATTRIBUTE_UNUSED, - int *exitstatus) +virNetDevRunEthernetScript(const char *ifname ATTRIBUTE_UNUSED, + const char *script ATTRIBUTE_UNUSED) { - if (exitstatus) - *exitstatus = 0; - return 0; } -- 2.7.3

On Wed, Apr 13, 2016 at 10:43:05 +0200, Ján Tomko wrote:
Instead of the proper solution of moving all the host-changing stuff out of qemuBuildCommandLine, separate one helper for easier mocking.
While the new placement of the helper might seem weird I quite like that we don't longer mock virCommandRun in this case. So ACK series from my side unless anybody else objects. Peter

On 04/13/2016 07:00 AM, Peter Krempa wrote:
On Wed, Apr 13, 2016 at 10:43:05 +0200, Ján Tomko wrote:
Instead of the proper solution of moving all the host-changing stuff out of qemuBuildCommandLine, separate one helper for easier mocking.
While the new placement of the helper might seem weird I quite like that we don't longer mock virCommandRun in this case.
So ACK series from my side unless anybody else objects.
ACK as well (though maybe i'll resend my patch that uses system() anyways, it greatly simplifies the Regenerate function) - Cole
participants (3)
-
Cole Robinson
-
Ján Tomko
-
Peter Krempa