[PATCH 03/12] tests: Fix test-qdev-global-props on anonymous qdev realize()

Peter Xu posted 12 patches 2 days, 17 hours ago
There is a newer version of this series
[PATCH 03/12] tests: Fix test-qdev-global-props on anonymous qdev realize()
Posted by Peter Xu 2 days, 17 hours ago
test-qdev-global-props creates a few subprocesses and test things based on
qdev realize().  One thing was overlooked since the start, that anonymous
creations of qdev (then realize() the device) requires the machine object's
presence, as all these devices need to be attached to QOM tree, by default
to path "/machine/unattached".

The test didn't crash simply because container_get() has an implicit
semantic to silently create any missing container, hence what happened here
is container_get() (when running these tests) will try to create containers
at QOM path "/machine" on the fly.  That's probably unexpected by the test,
but worked like charm before.

We're going to fix device_set_realized() soon, but before that make the
test case prepared, by creating the machine object on its own.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tests/unit/test-qdev-global-props.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/tests/unit/test-qdev-global-props.c b/tests/unit/test-qdev-global-props.c
index c8862cac5f..b2a1598f8e 100644
--- a/tests/unit/test-qdev-global-props.c
+++ b/tests/unit/test-qdev-global-props.c
@@ -72,6 +72,25 @@ static const TypeInfo subclass_type = {
     .parent = TYPE_STATIC_PROPS,
 };
 
+/*
+ * Initialize a fake machine, being prepared for future tests.
+ *
+ * All the tests later (even if to be run in subprocesses.. which will
+ * inherit the global states of the parent process) will try to create qdev
+ * and realize the device.
+ *
+ * Realization of such anonymous qdev (with no parent object) requires both
+ * the machine object and its "unattached" container to be at least present.
+ */
+static void test_init_machine(void)
+{
+    /* This is a fake machine - it doesn't need to be a machine object */
+    Object *machine = container_create(object_get_root(), "machine");
+
+    /* This container must exist for anonymous qdevs to realize() */
+    container_create(machine, "unattached");
+}
+
 /* Test simple static property setting to default value */
 static void test_static_prop_subprocess(void)
 {
@@ -295,6 +314,8 @@ int main(int argc, char **argv)
     type_register_static(&nohotplug_type);
     type_register_static(&nondevice_type);
 
+    test_init_machine();
+
     g_test_add_func("/qdev/properties/static/default/subprocess",
                     test_static_prop_subprocess);
     g_test_add_func("/qdev/properties/static/default",
-- 
2.45.0
Re: [PATCH 03/12] tests: Fix test-qdev-global-props on anonymous qdev realize()
Posted by Daniel P. Berrangé 2 days, 5 hours ago
On Wed, Nov 20, 2024 at 04:56:54PM -0500, Peter Xu wrote:
> test-qdev-global-props creates a few subprocesses and test things based on
> qdev realize().  One thing was overlooked since the start, that anonymous
> creations of qdev (then realize() the device) requires the machine object's
> presence, as all these devices need to be attached to QOM tree, by default
> to path "/machine/unattached".
> 
> The test didn't crash simply because container_get() has an implicit
> semantic to silently create any missing container, hence what happened here
> is container_get() (when running these tests) will try to create containers
> at QOM path "/machine" on the fly.  That's probably unexpected by the test,
> but worked like charm before.
> 
> We're going to fix device_set_realized() soon, but before that make the
> test case prepared, by creating the machine object on its own.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  tests/unit/test-qdev-global-props.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Re: [PATCH 03/12] tests: Fix test-qdev-global-props on anonymous qdev realize()
Posted by Philippe Mathieu-Daudé 2 days, 5 hours ago
On 20/11/24 22:56, Peter Xu wrote:
> test-qdev-global-props creates a few subprocesses and test things based on
> qdev realize().  One thing was overlooked since the start, that anonymous
> creations of qdev (then realize() the device) requires the machine object's
> presence, as all these devices need to be attached to QOM tree, by default
> to path "/machine/unattached".
> 
> The test didn't crash simply because container_get() has an implicit
> semantic to silently create any missing container, hence what happened here
> is container_get() (when running these tests) will try to create containers
> at QOM path "/machine" on the fly.  That's probably unexpected by the test,
> but worked like charm before.
> 
> We're going to fix device_set_realized() soon, but before that make the
> test case prepared, by creating the machine object on its own.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>   tests/unit/test-qdev-global-props.c | 21 +++++++++++++++++++++
>   1 file changed, 21 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>