From f82d30307da8bea396a32dcab2ba9be5c3236b7c Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Fri, 14 Nov 2025 10:35:14 +0100 Subject: [PATCH] vmx2xmltest: Drop custom file name parse function Having a custom file name parsing function in vmx2xml that's different to the one used in production (esxParseVMXFileName()) might have served us well, but it also defeats the point of having a unit test. More specifically, if there's a bug in esxParseVMXFileName() then our unit test would not catch it. But now that we have vmx2xmlmock the custom parsing function can be dropped and the test can use the real one. Signed-off-by: Michal Privoznik Reviewed-by: Martin Kletzander --- tests/meson.build | 2 +- tests/vmx2xmltest.c | 61 +++++++-------------------------------------- 2 files changed, 10 insertions(+), 53 deletions(-) diff --git a/tests/meson.build b/tests/meson.build index 9adf172b7f..cd53e48aa4 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -550,7 +550,7 @@ endif if conf.has('WITH_VMX') tests += [ - { 'name': 'vmx2xmltest' }, + { 'name': 'vmx2xmltest', 'include': [ esx_inc_dir ], 'link_with': [ esx_lib ] }, { 'name': 'xml2vmxtest' }, ] mock_libs += [ diff --git a/tests/vmx2xmltest.c b/tests/vmx2xmltest.c index bcd95ed87d..cb8e04af0d 100644 --- a/tests/vmx2xmltest.c +++ b/tests/vmx2xmltest.c @@ -8,6 +8,8 @@ # include "internal.h" # include "vmx/vmx.h" +# define LIBVIRT_ESX_DRIVERPRIV_H_ALLOW +# include "esx_driverpriv.h" # define VIR_FROM_THIS VIR_FROM_VMWARE @@ -104,60 +106,12 @@ testCompareHelper(const void *data) return ret; } -static int -testParseVMXFileName(const char *fileName, - void *opaque G_GNUC_UNUSED, - char **src, - bool allow_missing) -{ - g_autofree char *copyOfFileName = NULL; - char *tmp = NULL; - char *saveptr = NULL; - char *datastoreName = NULL; - char *directoryAndFileName = NULL; - - *src = NULL; - - if (STRPREFIX(fileName, "/vmfs/volumes/")) { - /* Found absolute path referencing a file inside a datastore */ - copyOfFileName = g_strdup(fileName); - - /* Expected format: '/vmfs/volumes//' */ - if ((tmp = STRSKIP(copyOfFileName, "/vmfs/volumes/")) == NULL || - (datastoreName = strtok_r(tmp, "/", &saveptr)) == NULL || - (directoryAndFileName = strtok_r(NULL, "", &saveptr)) == NULL) { - return -1; - } - - if (STREQ(datastoreName, "missing") || - STRPREFIX(directoryAndFileName, "missing")) { - if (allow_missing) - return 0; - - virReportError(VIR_ERR_INTERNAL_ERROR, - "Referenced missing file '%s'", fileName); - return -1; - } - - *src = g_strdup_printf("[%s] %s", datastoreName, directoryAndFileName); - } else if (STRPREFIX(fileName, "/")) { - /* Found absolute path referencing a file outside a datastore */ - *src = g_strdup(fileName); - } else if (strchr(fileName, '/') != NULL) { - /* Found relative path, this is not supported */ - return -1; - } else { - /* Found single file name referencing a file inside a datastore */ - *src = g_strdup_printf("[datastore] directory/%s", fileName); - } - - return 0; -} static int mymain(void) { int ret = 0; + esxVMX_Data data = { 0 }; # define DO_TEST_FULL(file, should_fail) \ do { \ @@ -180,8 +134,10 @@ mymain(void) if (!(xmlopt = virVMXDomainXMLConfInit(caps))) return EXIT_FAILURE; - ctx.opaque = NULL; - ctx.parseFileName = testParseVMXFileName; + data.datastorePathWithoutFileName = (char*) "[datastore] directory"; + + ctx.opaque = &data; + ctx.parseFileName = esxParseVMXFileName; ctx.formatFileName = NULL; ctx.autodetectSCSIControllerModel = NULL; ctx.datacenterPath = NULL; @@ -296,7 +252,8 @@ mymain(void) return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -VIR_TEST_MAIN(mymain) +VIR_TEST_MAIN_PRELOAD(mymain, + VIR_TEST_MOCK("vmx2xml")) #else -- 2.47.3