]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: Introduce vmx2xmlmock
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 19 Nov 2025 11:50:49 +0000 (12:50 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 20 Nov 2025 14:09:10 +0000 (15:09 +0100)
If we want vmx2xmltest to use actual file name parser that's used
in production (esxParseVMXFileName()) we need a mock to stop it
from doing any HTTP requests and also to return predictable data.

So far, the function can call three functions that do HTTP
requests: esxVI_LookupDatastoreList(),
esxVI_LookupDatastoreHostMount() and
esxVI_LookupDatastoreByName().

Mock all three of them. And since their implementation uses some
other symbols (like allocators or _AppendToList() helpers) we
need to expose these symbols too.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/libvirt_esx.syms
tests/meson.build
tests/vmx2xmlmock.c [new file with mode: 0644]

index d05684fd9799df98f89ab6a6738f128667450be5..d228e2bef71e556e58c269414feaea0fb19ccc83 100644 (file)
@@ -18,8 +18,18 @@ esxVI_LookupDatastoreList;
 
 
 # esx/esx_vi_types.h
+esxVI_AnyType_Alloc;
 esxVI_DateTime_ConvertToCalendarTime;
 
+
+# esx/esx/esx_vi_types.generated.h
+esxVI_DatastoreHostMount_Alloc;
+esxVI_DynamicProperty_Alloc;
+esxVI_DynamicProperty_AppendToList;
+esxVI_HostMountInfo_Alloc;
+esxVI_ObjectContent_Alloc;
+esxVI_ObjectContent_AppendToList;
+
 # Let emacs know we want case-insensitive sorting
 # Local Variables:
 # sort-fold-case: t
index 0d76d379594203e395754581a7507790c19e65c5..9adf172b7f6cbb344fce5b60a19c6b5fbf39083b 100644 (file)
@@ -553,6 +553,9 @@ if conf.has('WITH_VMX')
     { 'name': 'vmx2xmltest' },
     { 'name': 'xml2vmxtest' },
   ]
+  mock_libs += [
+    { 'name': 'vmx2xmlmock', 'deps': [ esx_dep ] },
+  ]
 endif
 
 if conf.has('WITH_JSON')
diff --git a/tests/vmx2xmlmock.c b/tests/vmx2xmlmock.c
new file mode 100644 (file)
index 0000000..ea735dd
--- /dev/null
@@ -0,0 +1,77 @@
+#include <config.h>
+
+#include "internal.h"
+#include "esx_vi.h"
+
+int
+esxVI_LookupDatastoreList(esxVI_Context *ctx G_GNUC_UNUSED,
+                          esxVI_String *propertyNameList,
+                          esxVI_ObjectContent **datastoreList)
+{
+    esxVI_String *tmp;
+
+    for (tmp = propertyNameList; tmp; tmp = tmp->_next) {
+        esxVI_ObjectContent *obj = NULL;
+        esxVI_DynamicProperty *prop = NULL;
+
+        if (STRNEQ(tmp->value, "summary.name"))
+            continue;
+
+        esxVI_ObjectContent_Alloc(&obj);
+
+        esxVI_DynamicProperty_Alloc(&prop);
+        prop->name = g_strdup("summary.name");
+
+        esxVI_AnyType_Alloc(&prop->val);
+        prop->val->type = esxVI_Type_String;
+        prop->val->other = g_strdup("xsd:string");
+        prop->val->value = g_strdup("datastore");
+        prop->val->string = prop->val->value;
+        esxVI_DynamicProperty_AppendToList(&obj->propSet, prop);
+
+        esxVI_ObjectContent_AppendToList(datastoreList, obj);
+    }
+
+    return 0;
+}
+
+
+int
+esxVI_LookupDatastoreHostMount(esxVI_Context *ctx G_GNUC_UNUSED,
+                               esxVI_ManagedObjectReference *datastore G_GNUC_UNUSED,
+                               esxVI_DatastoreHostMount **hostMount,
+                               esxVI_Occurrence occurrence G_GNUC_UNUSED)
+{
+    esxVI_DatastoreHostMount *hm = NULL;
+
+    esxVI_DatastoreHostMount_Alloc(&hm);
+    esxVI_HostMountInfo_Alloc(&hm->mountInfo);
+    hm->mountInfo->path = g_strdup("/non/existent");
+    hm->mountInfo->accessMode = g_strdup("readWrite");
+    hm->mountInfo->accessible = esxVI_Boolean_True;
+
+    *hostMount = hm;
+    return 0;
+}
+
+
+int
+esxVI_LookupDatastoreByName(esxVI_Context *ctx G_GNUC_UNUSED,
+                            const char *name,
+                            esxVI_String *propertyNameList G_GNUC_UNUSED,
+                            esxVI_ObjectContent **datastore,
+                            esxVI_Occurrence occurrence G_GNUC_UNUSED)
+{
+    esxVI_ObjectContent *obj = NULL;
+
+    if (STREQ(name, "missing") || STREQ(name, "ds")) {
+        *datastore = NULL;
+        return 0;
+    }
+
+    /* No need to return anything useful, empty object is fine. */
+    esxVI_ObjectContent_Alloc(&obj);
+    esxVI_ObjectContent_AppendToList(datastore, obj);
+
+    return 0;
+}