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

Igor Mammedov posted 3 patches 3 years, 4 months ago
Maintainers: Laurent Vivier <lvivier@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Thomas Huth <thuth@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>
[RFC 1/3] tests: qtest: add qtest_has_kvm() to check if tested binary supports KVM
Posted by Igor Mammedov 3 years, 4 months ago
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 --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 */
-- 
2.27.0


[RFC v2 1/3] tests: qtest: add qtest_has_kvm() to check if tested binary supports KVM
Posted by Igor Mammedov 3 years, 4 months ago
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.

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.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
v2:
  - fix access() check.
     s/access()/!access()/
  - format C array items at meson.build time, and drop
    splitting targets string at runtime

 tests/qtest/libqos/libqtest.h |  7 +++++++
 meson.build                   |  2 ++
 tests/qtest/libqtest.c        | 18 ++++++++++++++++++
 3 files changed, 27 insertions(+)

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..7fec4e8289 100644
--- a/meson.build
+++ b/meson.build
@@ -75,6 +75,8 @@ elif cpu in ['mips', 'mips64']
 else
   kvm_targets = []
 endif
+kvm_targets_c = '"' + '" ,"'.join(kvm_targets) + '"'
+config_host_data.set('CONFIG_KVM_TARGETS', kvm_targets_c)
 
 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..daa6d54059 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -920,6 +920,24 @@ 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 *targets[] = { CONFIG_KVM_TARGETS };
+
+    for (i = 0; i < ARRAY_SIZE(targets); i++) {
+        if (!strncmp(targets[i], arch, strlen(arch))) {
+            if (!access("/dev/kvm", R_OK | W_OK)) {
+                ret = true;
+                break;
+            }
+        }
+    }
+    return ret;
+}
+
 bool qtest_get_irq(QTestState *s, int num)
 {
     /* dummy operation in order to make sure irq is up to date */
-- 
2.27.0


Re: [RFC v2 1/3] tests: qtest: add qtest_has_kvm() to check if tested binary supports KVM
Posted by Igor Mammedov 3 years, 4 months ago
On Thu, 17 Jun 2021 06:00:31 -0400
Igor Mammedov <imammedo@redhat.com> wrote:

Paolo,
Is it acceptable to (ab)use meson like in this patch?

> 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.
> 
> 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.
> 
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
> v2:
>   - fix access() check.
>      s/access()/!access()/
>   - format C array items at meson.build time, and drop
>     splitting targets string at runtime
> 
>  tests/qtest/libqos/libqtest.h |  7 +++++++
>  meson.build                   |  2 ++
>  tests/qtest/libqtest.c        | 18 ++++++++++++++++++
>  3 files changed, 27 insertions(+)
> 
> 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..7fec4e8289 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -75,6 +75,8 @@ elif cpu in ['mips', 'mips64']
>  else
>    kvm_targets = []
>  endif
> +kvm_targets_c = '"' + '" ,"'.join(kvm_targets) + '"'
> +config_host_data.set('CONFIG_KVM_TARGETS', kvm_targets_c)
>  
>  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..daa6d54059 100644
> --- a/tests/qtest/libqtest.c
> +++ b/tests/qtest/libqtest.c
> @@ -920,6 +920,24 @@ 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 *targets[] = { CONFIG_KVM_TARGETS };
> +
> +    for (i = 0; i < ARRAY_SIZE(targets); i++) {
> +        if (!strncmp(targets[i], arch, strlen(arch))) {
> +            if (!access("/dev/kvm", R_OK | W_OK)) {
> +                ret = true;
> +                break;
> +            }
> +        }
> +    }
> +    return ret;
> +}
> +
>  bool qtest_get_irq(QTestState *s, int num)
>  {
>      /* dummy operation in order to make sure irq is up to date */