[PATCH v1 6/7] tests/qtest/migration: Add a wrapper to print test names

Fabiano Rosas posted 7 patches 1 year ago
Maintainers: Juan Quintela <quintela@redhat.com>, Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, Leonardo Bras <leobras@redhat.com>, Thomas Huth <thuth@redhat.com>, Laurent Vivier <lvivier@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
[PATCH v1 6/7] tests/qtest/migration: Add a wrapper to print test names
Posted by Fabiano Rosas 1 year ago
Our usage of gtest results in us losing the very basic functionality
of "knowing which test failed". The issue is that gtest only prints
test names ("paths" in gtest parlance) once the test has finished, but
we use asserts in the tests and crash gtest itself before it can print
anything. We also use a final abort when the result of g_test_run is
not 0.

Depending on how the test failed/broke we can see the function that
trigged the abort, which may be representative of the test, but it
could also just be some generic function.

We have been relying on the primitive method of looking at the name of
the previous successful test and then looking at the code to figure
out which test should have come next.

Add a wrapper to the test registration that does the job of printing
the test name before running.

Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
 tests/qtest/migration-helpers.c | 32 ++++++++++++++++++++++++++++++++
 tests/qtest/migration-helpers.h |  1 +
 2 files changed, 33 insertions(+)

diff --git a/tests/qtest/migration-helpers.c b/tests/qtest/migration-helpers.c
index f1106128a9..164e09c299 100644
--- a/tests/qtest/migration-helpers.c
+++ b/tests/qtest/migration-helpers.c
@@ -298,3 +298,35 @@ char *resolve_machine_version(const char *alias, const char *var1,
 
     return find_common_machine_version(machine_name, var1, var2);
 }
+
+typedef struct {
+    char *name;
+    void (*func)(void);
+} MigrationTest;
+
+static void migration_test_destroy(gpointer data)
+{
+    MigrationTest *test = (MigrationTest *)data;
+
+    g_free(test->name);
+    g_free(test);
+}
+
+static void migration_test_wrapper(const void *data)
+{
+    MigrationTest *test = (MigrationTest *)data;
+
+    g_test_message("Running /%s%s", qtest_get_arch(), test->name);
+    test->func();
+}
+
+void migration_test_add(const char *path, void (*fn)(void))
+{
+    MigrationTest *test = g_new0(MigrationTest, 1);
+
+    test->func = fn;
+    test->name = g_strdup(path);
+
+    qtest_add_data_func_full(path, test, migration_test_wrapper,
+                             migration_test_destroy);
+}
diff --git a/tests/qtest/migration-helpers.h b/tests/qtest/migration-helpers.h
index e31dc85cc7..0d9a02edc7 100644
--- a/tests/qtest/migration-helpers.h
+++ b/tests/qtest/migration-helpers.h
@@ -47,4 +47,5 @@ char *find_common_machine_version(const char *mtype, const char *var1,
                                   const char *var2);
 char *resolve_machine_version(const char *alias, const char *var1,
                               const char *var2);
+void migration_test_add(const char *path, void (*fn)(void));
 #endif /* MIGRATION_HELPERS_H */
-- 
2.35.3
Re: [PATCH v1 6/7] tests/qtest/migration: Add a wrapper to print test names
Posted by Peter Xu 1 year ago
On Fri, Nov 24, 2023 at 01:14:31PM -0300, Fabiano Rosas wrote:
> Our usage of gtest results in us losing the very basic functionality
> of "knowing which test failed". The issue is that gtest only prints
> test names ("paths" in gtest parlance) once the test has finished, but
> we use asserts in the tests and crash gtest itself before it can print
> anything. We also use a final abort when the result of g_test_run is
> not 0.
> 
> Depending on how the test failed/broke we can see the function that
> trigged the abort, which may be representative of the test, but it
> could also just be some generic function.
> 
> We have been relying on the primitive method of looking at the name of
> the previous successful test and then looking at the code to figure
> out which test should have come next.
> 
> Add a wrapper to the test registration that does the job of printing
> the test name before running.
> 
> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> ---
>  tests/qtest/migration-helpers.c | 32 ++++++++++++++++++++++++++++++++
>  tests/qtest/migration-helpers.h |  1 +
>  2 files changed, 33 insertions(+)
> 
> diff --git a/tests/qtest/migration-helpers.c b/tests/qtest/migration-helpers.c
> index f1106128a9..164e09c299 100644
> --- a/tests/qtest/migration-helpers.c
> +++ b/tests/qtest/migration-helpers.c
> @@ -298,3 +298,35 @@ char *resolve_machine_version(const char *alias, const char *var1,
>  
>      return find_common_machine_version(machine_name, var1, var2);
>  }
> +
> +typedef struct {
> +    char *name;
> +    void (*func)(void);
> +} MigrationTest;
> +
> +static void migration_test_destroy(gpointer data)
> +{
> +    MigrationTest *test = (MigrationTest *)data;
> +
> +    g_free(test->name);
> +    g_free(test);
> +}
> +
> +static void migration_test_wrapper(const void *data)
> +{
> +    MigrationTest *test = (MigrationTest *)data;
> +
> +    g_test_message("Running /%s%s", qtest_get_arch(), test->name);

/%s/%s?

> +    test->func();
> +}

Reviewed-by: Peter Xu <peterx@redhat.com>

-- 
Peter Xu
Re: [PATCH v1 6/7] tests/qtest/migration: Add a wrapper to print test names
Posted by Fabiano Rosas 1 year ago
Peter Xu <peterx@redhat.com> writes:

> On Fri, Nov 24, 2023 at 01:14:31PM -0300, Fabiano Rosas wrote:
>> Our usage of gtest results in us losing the very basic functionality
>> of "knowing which test failed". The issue is that gtest only prints
>> test names ("paths" in gtest parlance) once the test has finished, but
>> we use asserts in the tests and crash gtest itself before it can print
>> anything. We also use a final abort when the result of g_test_run is
>> not 0.
>> 
>> Depending on how the test failed/broke we can see the function that
>> trigged the abort, which may be representative of the test, but it
>> could also just be some generic function.
>> 
>> We have been relying on the primitive method of looking at the name of
>> the previous successful test and then looking at the code to figure
>> out which test should have come next.
>> 
>> Add a wrapper to the test registration that does the job of printing
>> the test name before running.
>> 
>> Signed-off-by: Fabiano Rosas <farosas@suse.de>
>> ---
>>  tests/qtest/migration-helpers.c | 32 ++++++++++++++++++++++++++++++++
>>  tests/qtest/migration-helpers.h |  1 +
>>  2 files changed, 33 insertions(+)
>> 
>> diff --git a/tests/qtest/migration-helpers.c b/tests/qtest/migration-helpers.c
>> index f1106128a9..164e09c299 100644
>> --- a/tests/qtest/migration-helpers.c
>> +++ b/tests/qtest/migration-helpers.c
>> @@ -298,3 +298,35 @@ char *resolve_machine_version(const char *alias, const char *var1,
>>  
>>      return find_common_machine_version(machine_name, var1, var2);
>>  }
>> +
>> +typedef struct {
>> +    char *name;
>> +    void (*func)(void);
>> +} MigrationTest;
>> +
>> +static void migration_test_destroy(gpointer data)
>> +{
>> +    MigrationTest *test = (MigrationTest *)data;
>> +
>> +    g_free(test->name);
>> +    g_free(test);
>> +}
>> +
>> +static void migration_test_wrapper(const void *data)
>> +{
>> +    MigrationTest *test = (MigrationTest *)data;
>> +
>> +    g_test_message("Running /%s%s", qtest_get_arch(), test->name);
>
> /%s/%s?

The test name contains a leading slash.