[libvirt] [glib v3] Allow unknown devices in domain XML config

Changes in this version of series: In the newly added test case, ensure: * all device objects are freed * devices XML tree remains the same.

Validation (if attempted) should just fail in this case instead of crashing. --- libvirt-gconfig/libvirt-gconfig-object.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libvirt-gconfig/libvirt-gconfig-object.c b/libvirt-gconfig/libvirt-gconfig-object.c index 6225de2..851e35c 100644 --- a/libvirt-gconfig/libvirt-gconfig-object.c +++ b/libvirt-gconfig/libvirt-gconfig-object.c @@ -209,6 +209,14 @@ void gvir_config_object_validate(GVirConfigObject *config, return; } + if (!priv->schema) { + gvir_config_set_error_literal(err, + GVIR_CONFIG_OBJECT_ERROR, + 0, + _("No XML schema associated with this config object")); + return; + } + rngParser = xmlRelaxNGNewParserCtxt(priv->schema); if (!rngParser) { gvir_config_set_error(err, -- 2.9.3

We'll need to instatiate DomainDevice baseclass itself for unknown (think new devices added to libvirt XML) devices in a following patch. This change makes that possible. This doesn't break any API or ABI to the best of my knowledge and this assumption was confirmed by Emmanuele Bassi and Tim-Philipp Muller. --- libvirt-gconfig/libvirt-gconfig-domain-device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libvirt-gconfig/libvirt-gconfig-domain-device.c b/libvirt-gconfig/libvirt-gconfig-domain-device.c index 8a75cea..f78173a 100644 --- a/libvirt-gconfig/libvirt-gconfig-domain-device.c +++ b/libvirt-gconfig/libvirt-gconfig-domain-device.c @@ -33,7 +33,7 @@ struct _GVirConfigDomainDevicePrivate gboolean unused; }; -G_DEFINE_ABSTRACT_TYPE(GVirConfigDomainDevice, gvir_config_domain_device, GVIR_CONFIG_TYPE_OBJECT); +G_DEFINE_TYPE(GVirConfigDomainDevice, gvir_config_domain_device, GVIR_CONFIG_TYPE_OBJECT); static void gvir_config_domain_device_class_init(GVirConfigDomainDeviceClass *klass) -- 2.9.3

Currently we can and do get into serious trouble with this kind of code: devices = gvir_config_domain_get_devices(domain); gvir_config_domain_set_devices(domain, domain); since the first call above won't return a complete list of objects present in the domain but only the ones we have specific classes for and the second call above overwrites all device nodes under the domain. This lately made Boxes break against the latest libvirt, where a new device node was made compulsory[1]. Although we should add support for all know domain devices ASAP, new devices will be added in future and this can happen again. So let's first ensure that gvir_config_domain_get_devices() always returns all devices under the domain. All unknown/unimplemented devices will now be returned as the very generic DomainDevice objects. Once we add support for a particular device, there will be no API/ABI breakage since the new class will inherit from DomainDevice class. [1] https://bugzilla.redhat.com/show_bug.cgi?id=1388091 --- libvirt-gconfig/libvirt-gconfig-domain-device.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libvirt-gconfig/libvirt-gconfig-domain-device.c b/libvirt-gconfig/libvirt-gconfig-domain-device.c index f78173a..2475519 100644 --- a/libvirt-gconfig/libvirt-gconfig-domain-device.c +++ b/libvirt-gconfig/libvirt-gconfig-domain-device.c @@ -64,7 +64,7 @@ gvir_config_domain_device_new_from_tree(GVirConfigXmlDoc *doc, } else if (xmlStrEqual(tree->name, (xmlChar*)"controller")) { return gvir_config_domain_controller_new_from_tree(doc, tree); } else if (xmlStrEqual(tree->name, (xmlChar*)"lease")) { - goto unimplemented; + type = GVIR_CONFIG_TYPE_DOMAIN_DEVICE; } else if (xmlStrEqual(tree->name, (xmlChar*)"hostdev")) { return gvir_config_domain_hostdev_new_from_tree(doc, tree); } else if (xmlStrEqual(tree->name, (xmlChar*)"redirdev")) { @@ -76,7 +76,7 @@ gvir_config_domain_device_new_from_tree(GVirConfigXmlDoc *doc, } else if (xmlStrEqual(tree->name, (xmlChar*)"input")) { type = GVIR_CONFIG_TYPE_DOMAIN_INPUT; } else if (xmlStrEqual(tree->name, (xmlChar*)"hub")) { - goto unimplemented; + type = GVIR_CONFIG_TYPE_DOMAIN_DEVICE; } else if (xmlStrEqual(tree->name, (xmlChar*)"graphics")) { return gvir_config_domain_graphics_new_from_tree(doc, tree); } else if (xmlStrEqual(tree->name, (xmlChar*)"video")) { @@ -90,22 +90,22 @@ gvir_config_domain_device_new_from_tree(GVirConfigXmlDoc *doc, } else if (xmlStrEqual(tree->name, (xmlChar*)"channel")) { type = GVIR_CONFIG_TYPE_DOMAIN_CHANNEL; } else if (xmlStrEqual(tree->name, (xmlChar*)"watchdog")) { - goto unimplemented; + type = GVIR_CONFIG_TYPE_DOMAIN_DEVICE; } else if (xmlStrEqual(tree->name, (xmlChar*)"sound")) { type = GVIR_CONFIG_TYPE_DOMAIN_SOUND; } else if (xmlStrEqual(tree->name, (xmlChar*)"memballoon")) { type = GVIR_CONFIG_TYPE_DOMAIN_MEMBALLOON; } else { g_debug("Unknown device node: %s", tree->name); - return NULL; + type = GVIR_CONFIG_TYPE_DOMAIN_DEVICE; } g_return_val_if_fail(g_type_is_a(type, GVIR_CONFIG_TYPE_DOMAIN_DEVICE), NULL); + if (type == GVIR_CONFIG_TYPE_DOMAIN_DEVICE) + g_debug("Proper support for '%s' device nodes is not yet implemented", tree->name); + return GVIR_CONFIG_DOMAIN_DEVICE(gvir_config_object_new_from_tree(type, doc, NULL, tree)); -unimplemented: - g_debug("Parsing of '%s' device nodes is unimplemented", tree->name); - return NULL; } -- 2.9.3

We'll need to load XML from file in another function, that will be added in a following patch. --- tests/test-gconfig.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test-gconfig.c b/tests/test-gconfig.c index a26bb5f..5389a26 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -30,12 +30,11 @@ g_free(alloced_str); \ } G_STMT_END -static void check_xml(GVirConfigDomain *domain, const char *reference_file) +static char * load_xml(const char *reference_file) { const char *reference_path; GError *error = NULL; char *reference_xml; - char *xml; reference_path = g_test_get_filename(G_TEST_DIST, "xml", reference_file, NULL); @@ -45,6 +44,18 @@ static void check_xml(GVirConfigDomain *domain, const char *reference_file) * gedit, workaround this issue by removing trailing whitespace from * the reference file */ g_strchomp(reference_xml); + + return reference_xml; +} + + +static void check_xml(GVirConfigDomain *domain, const char *reference_file) +{ + char *reference_xml; + char *xml; + + reference_xml = load_xml(reference_file); + xml = gvir_config_object_to_xml(GVIR_CONFIG_OBJECT(domain)); g_assert_cmpstr(xml, ==, reference_xml); g_free(xml); -- 2.9.3

--- tests/test-gconfig.c | 24 ++++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 tests/xml/gconfig-domain-device-unknown.xml diff --git a/tests/test-gconfig.c b/tests/test-gconfig.c index 5389a26..685cf97 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,28 @@ static void test_domain_device_pci_hostdev(void) g_object_unref(G_OBJECT(domain)); } +static void test_domain_device_unknown(void) +{ + GVirConfigDomain *domain; + GList *devices; + GError *error = NULL; + char *xml; + + xml = load_xml("gconfig-domain-device-unknown.xml"); + + domain = gvir_config_domain_new_from_xml(xml, &error); + g_assert_no_error(error); + + devices = gvir_config_domain_get_devices(domain); + g_assert_nonnull(devices); + + check_xml(domain, "gconfig-domain-device-unknown.xml"); + + g_list_free_full(devices, g_object_unref); + g_object_unref(G_OBJECT(domain)); +} + + int main(int argc, char **argv) { gvir_config_init(&argc, &argv); @@ -793,6 +815,8 @@ int main(int argc, char **argv) test_domain_device_usb_redir); g_test_add_func("/libvirt-gconfig/domain-device-pci-hostdev", test_domain_device_pci_hostdev); + g_test_add_func("/libvirt-gconfig/domain-device-unknown", + test_domain_device_unknown); return g_test_run(); } diff --git a/tests/xml/gconfig-domain-device-unknown.xml b/tests/xml/gconfig-domain-device-unknown.xml new file mode 100644 index 0000000..f326505 --- /dev/null +++ b/tests/xml/gconfig-domain-device-unknown.xml @@ -0,0 +1,5 @@ +<domain> + <devices> + <unknown/> + </devices> +</domain> -- 2.9.3

On Thu, Nov 03, 2016 at 02:40:58PM +0100, Zeeshan Ali wrote:
--- tests/test-gconfig.c | 24 ++++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 tests/xml/gconfig-domain-device-unknown.xml
diff --git a/tests/test-gconfig.c b/tests/test-gconfig.c index 5389a26..685cf97 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,28 @@ static void test_domain_device_pci_hostdev(void) g_object_unref(G_OBJECT(domain)); }
+static void test_domain_device_unknown(void) +{ + GVirConfigDomain *domain; + GList *devices; + GError *error = NULL; + char *xml; + + xml = load_xml("gconfig-domain-device-unknown.xml"); + + domain = gvir_config_domain_new_from_xml(xml, &error); + g_assert_no_error(error); + + devices = gvir_config_domain_get_devices(domain); + g_assert_nonnull(devices);
+ gvir_config_domain_set_devices(domain, devices);
+ + check_xml(domain, "gconfig-domain-device-unknown.xml"); + + g_list_free_full(devices, g_object_unref); + g_object_unref(G_OBJECT(domain)); +}
Christophe

On Thu, Nov 3, 2016 at 4:31 PM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Thu, Nov 03, 2016 at 02:40:58PM +0100, Zeeshan Ali wrote:
--- tests/test-gconfig.c | 24 ++++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 tests/xml/gconfig-domain-device-unknown.xml
diff --git a/tests/test-gconfig.c b/tests/test-gconfig.c index 5389a26..685cf97 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,28 @@ static void test_domain_device_pci_hostdev(void) g_object_unref(G_OBJECT(domain)); }
+static void test_domain_device_unknown(void) +{ + GVirConfigDomain *domain; + GList *devices; + GError *error = NULL; + char *xml; + + xml = load_xml("gconfig-domain-device-unknown.xml"); + + domain = gvir_config_domain_new_from_xml(xml, &error); + g_assert_no_error(error); + + devices = gvir_config_domain_get_devices(domain); + g_assert_nonnull(devices);
+ gvir_config_domain_set_devices(domain, devices);
Ah yes. Fixed that (and adjusted the test xml), pushed the series. Thanks. -- Regards, Zeeshan Ali

On Thu, Nov 03, 2016 at 02:40:53PM +0100, Zeeshan Ali wrote:
Changes in this version of series:
In the newly added test case, ensure: * all device objects are freed * devices XML tree remains the same.
ACK all Regards, Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
participants (3)
-
Christophe Fergeau
-
Daniel P. Berrange
-
Zeeshan Ali