diff mbox series

[for-7.0,4/4] tests/qtest: Add a function to check whether a machine is available

Message ID 20211201104347.51922-5-thuth@redhat.com
State New
Headers show
Series qtest patches for binaries with reduced machines | expand

Commit Message

Thomas Huth Dec. 1, 2021, 10:43 a.m. UTC
It is nowadays possible to build QEMU with a reduced set of machines
in each binary. However, the qtests still hard-code the expected
machines and fail if the binary does not feature the required machine.
Let's get a little bit more flexible here: Add a function that can be
used to query whether a certain machine is available or not, and use
it in some tests as an example (more work has to be done in other
tests which will follow later).

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tests/qtest/boot-serial-test.c |  3 ++-
 tests/qtest/cdrom-test.c       |  8 +++++---
 tests/qtest/libqos/libqtest.h  |  8 ++++++++
 tests/qtest/libqtest.c         | 17 +++++++++++++++++
 tests/qtest/prom-env-test.c    |  8 +++++---
 5 files changed, 37 insertions(+), 7 deletions(-)

Comments

John Snow Dec. 6, 2021, 8:02 p.m. UTC | #1
On Wed, Dec 1, 2021 at 5:44 AM Thomas Huth <thuth@redhat.com> wrote:

> It is nowadays possible to build QEMU with a reduced set of machines
> in each binary. However, the qtests still hard-code the expected
> machines and fail if the binary does not feature the required machine.
> Let's get a little bit more flexible here: Add a function that can be
> used to query whether a certain machine is available or not, and use
> it in some tests as an example (more work has to be done in other
> tests which will follow later).
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  tests/qtest/boot-serial-test.c |  3 ++-
>  tests/qtest/cdrom-test.c       |  8 +++++---
>  tests/qtest/libqos/libqtest.h  |  8 ++++++++
>  tests/qtest/libqtest.c         | 17 +++++++++++++++++
>  tests/qtest/prom-env-test.c    |  8 +++++---
>  5 files changed, 37 insertions(+), 7 deletions(-)
>
> diff --git a/tests/qtest/boot-serial-test.c
> b/tests/qtest/boot-serial-test.c
> index 83828ba270..4d8e1343bd 100644
> --- a/tests/qtest/boot-serial-test.c
> +++ b/tests/qtest/boot-serial-test.c
> @@ -285,7 +285,8 @@ int main(int argc, char *argv[])
>      g_test_init(&argc, &argv, NULL);
>
>      for (i = 0; tests[i].arch != NULL; i++) {
> -        if (strcmp(arch, tests[i].arch) == 0) {
> +        if (g_str_equal(arch, tests[i].arch) &&
> +            qtest_has_machine(tests[i].machine)) {
>              char *name = g_strdup_printf("boot-serial/%s",
> tests[i].machine);
>              qtest_add_data_func(name, &tests[i], test_machine);
>              g_free(name);
> diff --git a/tests/qtest/cdrom-test.c b/tests/qtest/cdrom-test.c
> index 5af944a5fb..c1fcac5c45 100644
> --- a/tests/qtest/cdrom-test.c
> +++ b/tests/qtest/cdrom-test.c
> @@ -109,9 +109,11 @@ static void test_cdrom_param(gconstpointer data)
>  static void add_cdrom_param_tests(const char **machines)
>  {
>      while (*machines) {
> -        char *testname = g_strdup_printf("cdrom/param/%s", *machines);
> -        qtest_add_data_func(testname, *machines, test_cdrom_param);
> -        g_free(testname);
> +        if (qtest_has_machine(*machines)) {
> +            char *testname = g_strdup_printf("cdrom/param/%s", *machines);
> +            qtest_add_data_func(testname, *machines, test_cdrom_param);
> +            g_free(testname);
> +        }
>          machines++;
>      }
>  }
> diff --git a/tests/qtest/libqos/libqtest.h b/tests/qtest/libqos/libqtest.h
> index 59e9271195..dff6b31cf0 100644
> --- a/tests/qtest/libqos/libqtest.h
> +++ b/tests/qtest/libqos/libqtest.h
> @@ -710,6 +710,14 @@ QDict *qmp_fd(int fd, const char *fmt, ...)
> GCC_FMT_ATTR(2, 3);
>  void qtest_cb_for_every_machine(void (*cb)(const char *machine),
>                                  bool skip_old_versioned);
>
> +/**
> + * qtest_has_machine:
> + * @machine: The machine to look for
> + *
> + * Returns: true if the machine is available in the target binary.
> + */
> +bool qtest_has_machine(const char *machine);
> +
>  /**
>   * qtest_qmp_device_add_qdict:
>   * @qts: QTestState instance to operate on
> diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
> index 7ae2dc4e1d..65ed949685 100644
> --- a/tests/qtest/libqtest.c
> +++ b/tests/qtest/libqtest.c
> @@ -1401,6 +1401,23 @@ void qtest_cb_for_every_machine(void (*cb)(const
> char *machine),
>      }
>  }
>
> +bool qtest_has_machine(const char *machine)
> +{
> +    struct MachInfo *machines;
> +    int i;
> +
> +    machines = qtest_get_machines();
> +
> +    for (i = 0; machines[i].name != NULL; i++) {
> +        if (g_str_equal(machine, machines[i].name) ||
> +            (machines[i].alias && g_str_equal(machine,
> machines[i].alias))) {
> +            return true;
> +        }
> +    }
> +
> +    return false;
> +}
> +
>  /*
>   * Generic hot-plugging test via the device_add QMP commands.
>   */
> diff --git a/tests/qtest/prom-env-test.c b/tests/qtest/prom-env-test.c
> index f41d80154a..bdbb01d8e5 100644
> --- a/tests/qtest/prom-env-test.c
> +++ b/tests/qtest/prom-env-test.c
> @@ -71,9 +71,11 @@ static void add_tests(const char *machines[])
>      char *name;
>
>      for (i = 0; machines[i] != NULL; i++) {
> -        name = g_strdup_printf("prom-env/%s", machines[i]);
> -        qtest_add_data_func(name, machines[i], test_machine);
> -        g_free(name);
> +        if (qtest_has_machine(machines[i])) {
> +            name = g_strdup_printf("prom-env/%s", machines[i]);
> +            qtest_add_data_func(name, machines[i], test_machine);
> +            g_free(name);
> +        }
>      }
>  }
>
>
I wonder if the arch-specific CDROM tests should be split out into their
own file, because IIRC it's not really about testing the *cdrom* itself,
it's just a test that uses the CDROM.

Eh.

cdrom test bits: Acked-by: John Snow <jsnow@redhat.com>

--js
diff mbox series

Patch

diff --git a/tests/qtest/boot-serial-test.c b/tests/qtest/boot-serial-test.c
index 83828ba270..4d8e1343bd 100644
--- a/tests/qtest/boot-serial-test.c
+++ b/tests/qtest/boot-serial-test.c
@@ -285,7 +285,8 @@  int main(int argc, char *argv[])
     g_test_init(&argc, &argv, NULL);
 
     for (i = 0; tests[i].arch != NULL; i++) {
-        if (strcmp(arch, tests[i].arch) == 0) {
+        if (g_str_equal(arch, tests[i].arch) &&
+            qtest_has_machine(tests[i].machine)) {
             char *name = g_strdup_printf("boot-serial/%s", tests[i].machine);
             qtest_add_data_func(name, &tests[i], test_machine);
             g_free(name);
diff --git a/tests/qtest/cdrom-test.c b/tests/qtest/cdrom-test.c
index 5af944a5fb..c1fcac5c45 100644
--- a/tests/qtest/cdrom-test.c
+++ b/tests/qtest/cdrom-test.c
@@ -109,9 +109,11 @@  static void test_cdrom_param(gconstpointer data)
 static void add_cdrom_param_tests(const char **machines)
 {
     while (*machines) {
-        char *testname = g_strdup_printf("cdrom/param/%s", *machines);
-        qtest_add_data_func(testname, *machines, test_cdrom_param);
-        g_free(testname);
+        if (qtest_has_machine(*machines)) {
+            char *testname = g_strdup_printf("cdrom/param/%s", *machines);
+            qtest_add_data_func(testname, *machines, test_cdrom_param);
+            g_free(testname);
+        }
         machines++;
     }
 }
diff --git a/tests/qtest/libqos/libqtest.h b/tests/qtest/libqos/libqtest.h
index 59e9271195..dff6b31cf0 100644
--- a/tests/qtest/libqos/libqtest.h
+++ b/tests/qtest/libqos/libqtest.h
@@ -710,6 +710,14 @@  QDict *qmp_fd(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 void qtest_cb_for_every_machine(void (*cb)(const char *machine),
                                 bool skip_old_versioned);
 
+/**
+ * qtest_has_machine:
+ * @machine: The machine to look for
+ *
+ * Returns: true if the machine is available in the target binary.
+ */
+bool qtest_has_machine(const char *machine);
+
 /**
  * qtest_qmp_device_add_qdict:
  * @qts: QTestState instance to operate on
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 7ae2dc4e1d..65ed949685 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -1401,6 +1401,23 @@  void qtest_cb_for_every_machine(void (*cb)(const char *machine),
     }
 }
 
+bool qtest_has_machine(const char *machine)
+{
+    struct MachInfo *machines;
+    int i;
+
+    machines = qtest_get_machines();
+
+    for (i = 0; machines[i].name != NULL; i++) {
+        if (g_str_equal(machine, machines[i].name) ||
+            (machines[i].alias && g_str_equal(machine, machines[i].alias))) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 /*
  * Generic hot-plugging test via the device_add QMP commands.
  */
diff --git a/tests/qtest/prom-env-test.c b/tests/qtest/prom-env-test.c
index f41d80154a..bdbb01d8e5 100644
--- a/tests/qtest/prom-env-test.c
+++ b/tests/qtest/prom-env-test.c
@@ -71,9 +71,11 @@  static void add_tests(const char *machines[])
     char *name;
 
     for (i = 0; machines[i] != NULL; i++) {
-        name = g_strdup_printf("prom-env/%s", machines[i]);
-        qtest_add_data_func(name, machines[i], test_machine);
-        g_free(name);
+        if (qtest_has_machine(machines[i])) {
+            name = g_strdup_printf("prom-env/%s", machines[i]);
+            qtest_add_data_func(name, machines[i], test_machine);
+            g_free(name);
+        }
     }
 }