diff mbox series

[RFC,1/3] tests: qtest: add qtest_has_kvm() to check if tested binary supports KVM

Message ID 20210616152455.1270264-2-imammedo@redhat.com (mailing list archive)
State New, archived
Headers show
Series qtest: pick tests that require KVM at runtime | expand

Commit Message

Igor Mammedov June 16, 2021, 3:24 p.m. UTC
From: root <root@apm-mustang-ev3-29.khw2.lab.eng.bos.redhat.com>

Currently it not possible to create tests that have KVM as a hard
requirement on a host that doesn't support KVM for tested target
binary (modulo going through the trouble of compiling out
the offending test case).

Following scenario makes test fail when it's run on non x86 host:
  qemu-system-x86_64 -enable-kvm -M q35,kernel-irqchip=on -smp 1,maxcpus=288

This patch introduces qtest_has_kvm() to let users check if KVM is
available in advance and skip registering non run-able test-cases.

Signed-off-by: root <root@apm-mustang-ev3-29.khw2.lab.eng.bos.redhat.com>
---
PS:
It's simplistic and not as versatile/precise as earlier proposed
'query-accels' series, but it get job done for simple cases.

on upside it's much cheaper to execute than the 'query-accels' as
it doesn't need to run QEMU for probing.


 tests/qtest/libqos/libqtest.h |  7 +++++++
 meson.build                   |  1 +
 tests/qtest/libqtest.c        | 20 ++++++++++++++++++++
 3 files changed, 28 insertions(+)
diff mbox series

Patch

diff --git a/tests/qtest/libqos/libqtest.h b/tests/qtest/libqos/libqtest.h
index a68dcd79d4..bab0047117 100644
--- a/tests/qtest/libqos/libqtest.h
+++ b/tests/qtest/libqos/libqtest.h
@@ -588,6 +588,13 @@  bool qtest_big_endian(QTestState *s);
  */
 const char *qtest_get_arch(void);
 
+/**
+ * qtest_has_kvm:
+ *
+ * Returns: True if the QEMU executable under test supports KVM
+ */
+bool qtest_has_kvm(void);
+
 /**
  * qtest_add_func:
  * @str: Test case path.
diff --git a/meson.build b/meson.build
index d2a9ce91f5..dbd31f9f6c 100644
--- a/meson.build
+++ b/meson.build
@@ -75,6 +75,7 @@  elif cpu in ['mips', 'mips64']
 else
   kvm_targets = []
 endif
+config_host_data.set_quoted('CONFIG_KVM_TARGETS', ' ,'.join(kvm_targets))
 
 accelerator_targets = { 'CONFIG_KVM': kvm_targets }
 if cpu in ['x86', 'x86_64', 'arm', 'aarch64']
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 825b13a44c..d019ac88c9 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -920,6 +920,26 @@  const char *qtest_get_arch(void)
     return end + 1;
 }
 
+bool qtest_has_kvm(void)
+{
+    int i;
+    bool ret = false;
+    const char *arch = qtest_get_arch();
+    const char *kvm_targets_str = CONFIG_KVM_TARGETS;
+    gchar **targets = g_strsplit(kvm_targets_str, " ", -1);
+
+    for (i = 0; targets[i]; i++) {
+        if (!strncmp(targets[i], arch, strlen(arch))) {
+            if (access("/dev/kvm", R_OK | W_OK)) {
+                ret = true;
+                break;
+            }
+        }
+    }
+    g_strfreev(targets);
+    return ret;
+}
+
 bool qtest_get_irq(QTestState *s, int num)
 {
     /* dummy operation in order to make sure irq is up to date */