diff mbox series

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

Message ID 20210617100031.1380288-1-imammedo@redhat.com
State New
Headers show
Series [RFC,v2,1/3] tests: qtest: add qtest_has_kvm() to check if tested binary supports KVM | expand

Commit Message

Igor Mammedov June 17, 2021, 10 a.m. UTC
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(+)

Comments

Igor Mammedov June 17, 2021, 4:28 p.m. UTC | #1
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 */
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..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 */