[libvirt] [PATCH v2] [glib 1/5] gconfig: Allow schema to be NULL

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

Hi, As usual I forgot to attach a summary. The changes in v2 are: * Keeping a debug() log for unknown devices. * Adding the test case XML file I forgot to add last time. On Thu, Nov 3, 2016 at 10:06 AM, Zeeshan Ali <zeenix@gmail.com> wrote:
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
-- Regards, Zeeshan Ali

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 | 22 ++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 27 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..b91f5af 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,26 @@ 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); + + g_list_free(devices); + g_object_unref(G_OBJECT(domain)); +} + + int main(int argc, char **argv) { gvir_config_init(&argc, &argv); @@ -793,6 +813,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..0372feb --- /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 10:06:22AM +0100, Zeeshan Ali wrote:
--- tests/test-gconfig.c | 22 ++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 27 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..b91f5af 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,26 @@ 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); + + g_list_free(devices); + g_object_unref(G_OBJECT(domain)); +}
The whole point of this work is to make sure that the unknown nodes are kept when regenerating the XML document, so I would expect a call to check_xml() too after calling set_devices(domain, devices); You should have g_list_free_full(devices, g_object_unref); when freeing the device list. Christophe

On Thu, Nov 3, 2016 at 1:03 PM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Thu, Nov 03, 2016 at 10:06:22AM +0100, Zeeshan Ali wrote:
--- tests/test-gconfig.c | 22 ++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 27 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..b91f5af 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,26 @@ 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); + + g_list_free(devices); + g_object_unref(G_OBJECT(domain)); +}
The whole point of this work is to make sure that the unknown nodes are kept when regenerating the XML document, so I would expect a call to check_xml() too after calling set_devices(domain, devices);
* g_assert_nonnull(devices); actually checks that the only dev in the original xml is still there or not. * check_xml() does a simple string comparison and that fails since set_devices() changes the format.
You should have g_list_free_full(devices, g_object_unref); when freeing the device list.
will change that. -- Regards, Zeeshan Ali

On Thu, Nov 03, 2016 at 02:05:46PM +0100, Zeeshan Ali wrote:
On Thu, Nov 3, 2016 at 1:03 PM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Thu, Nov 03, 2016 at 10:06:22AM +0100, Zeeshan Ali wrote:
--- tests/test-gconfig.c | 22 ++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 27 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..b91f5af 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,26 @@ 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); + + g_list_free(devices); + g_object_unref(G_OBJECT(domain)); +}
The whole point of this work is to make sure that the unknown nodes are kept when regenerating the XML document, so I would expect a call to check_xml() too after calling set_devices(domain, devices);
* g_assert_nonnull(devices); actually checks that the only dev in the original xml is still there or not.
It checks that a device is there, it does say nothing about how it's going to be serialized, so the test case could be working while what you actually need is broken.
* check_xml() does a simple string comparison and that fails since set_devices() changes the format.
Isn't it possible to use the exact same format as the output for the source XML? Christophe

On Thu, Nov 3, 2016 at 2:17 PM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Thu, Nov 03, 2016 at 02:05:46PM +0100, Zeeshan Ali wrote:
On Thu, Nov 3, 2016 at 1:03 PM, Christophe Fergeau <cfergeau@redhat.com> wrote:
On Thu, Nov 03, 2016 at 10:06:22AM +0100, Zeeshan Ali wrote:
--- tests/test-gconfig.c | 22 ++++++++++++++++++++++ tests/xml/gconfig-domain-device-unknown.xml | 5 +++++ 2 files changed, 27 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..b91f5af 100644 --- a/tests/test-gconfig.c +++ b/tests/test-gconfig.c @@ -762,6 +762,26 @@ 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); + + g_list_free(devices); + g_object_unref(G_OBJECT(domain)); +}
The whole point of this work is to make sure that the unknown nodes are kept when regenerating the XML document, so I would expect a call to check_xml() too after calling set_devices(domain, devices);
* g_assert_nonnull(devices); actually checks that the only dev in the original xml is still there or not.
It checks that a device is there, it does say nothing about how it's going to be serialized, so the test case could be working while what you actually need is broken.
* check_xml() does a simple string comparison and that fails since set_devices() changes the format.
Isn't it possible to use the exact same format as the output for the source XML?
Hmm.. yeah i see that there was only a space in the reference that's the difference. Removing that space helps. -- Regards, Zeeshan Ali
participants (2)
-
Christophe Fergeau
-
Zeeshan Ali