[libvirt] [PATCH v2 1/3] Implement infrastracture for mocking up QEMU capabilities cache

Pavel Fedin p.fedin at samsung.com
Wed Sep 9 14:03:14 UTC 2015


The main purpose of this patch is to introduce test mode to
virQEMUCapsCacheLookup(). This is done by adding a global variable, which
effectively overrides binary name. This variable is supposed to be set by
test suite.

The second addition is qemuTestCapsCacheInsert() function which allows the
test suite to actually populate the cache.

Third, two utility functions are introduced for proper initialization and
cleanup of the driver.

Signed-off-by: Pavel Fedin <p.fedin at samsung.com>
---
 src/qemu/qemu_capabilities.c | 16 +++++-------
 src/qemu/qemu_capspriv.h     | 36 +++++++++++++++++++++++++
 tests/testutilsqemu.c        | 62 ++++++++++++++++++++++++++++++++++++++++++++
 tests/testutilsqemu.h        |  9 +++++++
 4 files changed, 114 insertions(+), 9 deletions(-)
 mode change 100644 => 100755 src/qemu/qemu_capabilities.c
 create mode 100644 src/qemu/qemu_capspriv.h
 mode change 100644 => 100755 tests/testutilsqemu.c
 mode change 100644 => 100755 tests/testutilsqemu.h

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
old mode 100644
new mode 100755
index 43d11af..fa344b1
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -42,6 +42,7 @@
 #include "virstring.h"
 #include "qemu_hostdev.h"
 #include "qemu_domain.h"
+#include "qemu_capspriv.h"
 
 #include <fcntl.h>
 #include <sys/stat.h>
@@ -327,15 +328,6 @@ struct _virQEMUCaps {
     unsigned int *machineMaxCpus;
 };
 
-struct _virQEMUCapsCache {
-    virMutex lock;
-    virHashTablePtr binaries;
-    char *libDir;
-    char *cacheDir;
-    uid_t runUid;
-    gid_t runGid;
-};
-
 struct virQEMUCapsSearchData {
     virArch arch;
 };
@@ -3708,11 +3700,17 @@ virQEMUCapsCacheNew(const char *libDir,
     return NULL;
 }
 
+const char *qemuTestCapsName;
 
 virQEMUCapsPtr
 virQEMUCapsCacheLookup(virQEMUCapsCachePtr cache, const char *binary)
 {
     virQEMUCapsPtr ret = NULL;
+
+    /* This is used only by test suite!!! */
+    if (qemuTestCapsName)
+        binary = qemuTestCapsName;
+
     virMutexLock(&cache->lock);
     ret = virHashLookup(cache->binaries, binary);
     if (ret &&
diff --git a/src/qemu/qemu_capspriv.h b/src/qemu/qemu_capspriv.h
new file mode 100644
index 0000000..f915ea9
--- /dev/null
+++ b/src/qemu/qemu_capspriv.h
@@ -0,0 +1,36 @@
+/*
+ * qemu_capspriv.h: private declarations for QEMU capabilities generation
+ *
+ * Copyright (C) 2015 Samsung Electronics Co. Ltd
+ * Copyright (C) 2015 Pavel Fedin
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Pavel Fedin <p.fedin at samsung.com>
+ */
+
+#ifndef __QEMU_CAPSPRIV_H__
+#define __QEMU_CAPSPRIV_H__
+
+struct _virQEMUCapsCache {
+    virMutex lock;
+    virHashTablePtr binaries;
+    char *libDir;
+    char *cacheDir;
+    uid_t runUid;
+    gid_t runGid;
+};
+
+#endif
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
old mode 100644
new mode 100755
index a2f4299..071a694
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -8,6 +8,7 @@
 # include "cpu_conf.h"
 # include "qemu/qemu_driver.h"
 # include "qemu/qemu_domain.h"
+# include "qemu/qemu_capspriv.h"
 # include "virstring.h"
 
 # define VIR_FROM_THIS VIR_FROM_QEMU
@@ -526,4 +527,65 @@ qemuTestParseCapabilities(const char *capsFile)
     xmlXPathFreeContext(ctxt);
     return NULL;
 }
+
+int qemuTestDriverInit(virQEMUDriver *driver)
+{
+    driver->config = virQEMUDriverConfigNew(false);
+    if (!driver->config)
+        return -ENOMEM;
+
+    driver->caps = testQemuCapsInit();
+    if (!driver->caps)
+	goto cleanup;
+
+    /* Using /dev/null for libDir and cacheDir automatically produces errors
+     * upon attempt to use any of them */
+    driver->qemuCapsCache = virQEMUCapsCacheNew("/dev/null", "/dev/null", 0, 0);
+    if (!driver->qemuCapsCache)
+	goto cleanup;
+
+    driver->xmlopt = virQEMUDriverCreateXMLConf(driver);
+    if (driver->xmlopt)
+	return 0;
+
+    virQEMUCapsCacheFree(driver->qemuCapsCache);
+cleanup:
+    virObjectUnref(driver->caps);
+    virObjectUnref(driver->config);
+    return -ENOMEM;
+}
+
+void qemuTestDriverFree(virQEMUDriver *driver)
+{
+    virObjectUnref(driver->xmlopt);
+    virQEMUCapsCacheFree(driver->qemuCapsCache);
+    virObjectUnref(driver->caps);
+    virObjectUnref(driver->config);
+}
+
+int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache, const char *binary,
+                            virQEMUCapsPtr caps)
+{
+    int ret;
+
+    if (caps) {
+        /* Our caps were created artificially, so we don't want
+         * virQEMUCapsCacheFree() to attempt to deallocate them */
+        virObjectRef(caps);
+    } else {
+        caps = virQEMUCapsNew();
+        if (!caps)
+            return -ENOMEM;
+    }
+
+    /* We can have repeating names for our test data sets,
+     * so make sure there's no old copy */
+    virHashRemoveEntry(cache->binaries, binary);
+
+    ret = virHashAddEntry(cache->binaries, binary, caps);
+    if (ret < 0)
+        virObjectUnref(caps);
+
+    return ret;
+}
 #endif
diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h
old mode 100644
new mode 100755
index 0ec5dad..53e882d
--- a/tests/testutilsqemu.h
+++ b/tests/testutilsqemu.h
@@ -16,4 +16,13 @@ extern virCPUDefPtr cpuHaswell;
 void testQemuCapsSetCPU(virCapsPtr caps,
                         virCPUDefPtr hostCPU);
 
+int qemuTestDriverInit(virQEMUDriver *driver);
+void qemuTestDriverFree(virQEMUDriver *driver);
+
+int qemuTestCapsCacheInsert(virQEMUCapsCachePtr cache, const char *binary,
+                            virQEMUCapsPtr caps);
+
+/* This variable is actually defined in src/qemu/qemu_capabilities.c */
+extern const char *qemuTestCapsName;
+
 #endif
-- 
2.1.4




More information about the libvir-list mailing list