[Libosinfo] [libosinfo PATCH 10/10] test-os: Add test_devices_duplication()

Fabiano Fidêncio fidencio at redhat.com
Thu Nov 8 19:18:48 UTC 2018


As devices are inherited from entries that "derives-from" or "clones"
any other entries, there's not reason to keep declaring them in several
places.

By adding this test, hopefully there'll be no more devices duplication
from now on.

https://bugzilla.redhat.com/show_bug.cgi?id=1634807

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 tests/Makefile.am |   2 +-
 tests/test-os.c   | 144 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 145 insertions(+), 1 deletion(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 06f81bf..4612f69 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -60,7 +60,7 @@ test_product_SOURCES = test-product.c
 
 test_os_LDADD = $(COMMON_LDADD)
 test_os_CFLAGS = $(COMMON_CFLAGS)
-test_os_SOURCES = test-os.c
+test_os_SOURCES = test-os.c ../osinfo/osinfo_product.c
 
 test_productfilter_LDADD = $(COMMON_LDADD)
 test_productfilter_CFLAGS = $(COMMON_CFLAGS)
diff --git a/tests/test-os.c b/tests/test-os.c
index 2ec7960..7085074 100644
--- a/tests/test-os.c
+++ b/tests/test-os.c
@@ -21,6 +21,7 @@
 #include <config.h>
 
 #include <osinfo/osinfo.h>
+#include "osinfo/osinfo_product_private.h"
 
 
 
@@ -188,15 +189,158 @@ test_device_driver(void)
 }
 
 
+static GList *get_all_distros(OsinfoOsList *oslist)
+{
+    GList *oses;
+    GList *distros = NULL;
+
+    oses = osinfo_list_get_elements(OSINFO_LIST(oslist));
+    for (GList *it = oses; it != NULL; it = it->next) {
+        OsinfoOs *os;
+        const gchar *distro;
+
+        os = OSINFO_OS(it->data);
+        distro = osinfo_os_get_distro(os);
+
+        if (g_list_find_custom(distros, distro, (GCompareFunc) g_strcmp0) == NULL)
+            distros = g_list_prepend(distros, (gchar *)distro);
+    }
+
+    g_list_free(oses);
+
+    return distros;
+}
+
+
+static void check_duplicated_devices_cb(OsinfoProduct *product,
+                                        gpointer user_data)
+{
+    OsinfoOs *os = OSINFO_OS(user_data);
+    OsinfoOs *foreach_os = OSINFO_OS(product);
+    OsinfoDeviceList *devices_os, *devices_foreach_os;
+    GList *list_devices = NULL, *list_foreach_devices = NULL, *list_duplicated = NULL;
+
+    if (os == foreach_os)
+        return;
+
+    devices_os = osinfo_os_get_devices(os, NULL);
+    devices_foreach_os = osinfo_os_get_devices(foreach_os, NULL);
+
+    if (osinfo_list_get_length(OSINFO_LIST(devices_os)) == 0 ||
+        osinfo_list_get_length(OSINFO_LIST(devices_foreach_os)) == 0)
+        goto done;
+
+    list_devices = osinfo_list_get_elements(OSINFO_LIST(devices_os));
+    list_foreach_devices = osinfo_list_get_elements(OSINFO_LIST(devices_foreach_os));
+
+    for (GList *l = list_devices; l != NULL; l = l->next) {
+        OsinfoDevice *d = OSINFO_DEVICE(l->data);
+
+        for (GList *ll = list_foreach_devices; ll != NULL; ll = ll->next) {
+            OsinfoDevice *dd = OSINFO_DEVICE(ll->data);
+            if (d == dd)
+                list_duplicated = g_list_prepend(list_duplicated, d);
+        }
+    }
+
+    if (list_duplicated != NULL) {
+        gchar *string = NULL;
+        for (GList *l = list_duplicated; l != NULL; l = l->next) {
+            gchar *tmp = NULL;
+            if (string != NULL)
+                tmp = g_strdup_printf("%s\n  - %s\n",
+                                      string,
+                                      osinfo_device_get_name(OSINFO_DEVICE(l->data)));
+            else
+                tmp = g_strdup_printf("\n  - %s",
+                                      osinfo_device_get_name(OSINFO_DEVICE(l->data)));
+
+            g_free(string);
+            string = tmp;
+        }
+
+        g_debug("\nTesting \"%s\" against \"%s\" and found the following duplicated devices: %s",
+                osinfo_product_get_short_id(OSINFO_PRODUCT(os)),
+                osinfo_product_get_short_id(product),
+                string);
+        g_free(string);
+
+        g_test_fail();
+    }
+
+ done:
+    g_list_free(list_duplicated);
+    g_list_free(list_devices);
+    g_list_free(list_foreach_devices);
+    g_object_unref(devices_os);
+    g_object_unref(devices_foreach_os);
+}
+
+
+static void check_duplicated_devices(OsinfoOs *os)
+{
+    osinfo_product_foreach_related(OSINFO_PRODUCT(os),
+                                   OSINFO_PRODUCT_FOREACH_FLAG_DERIVES_FROM |
+                                   OSINFO_PRODUCT_FOREACH_FLAG_CLONES,
+                                   check_duplicated_devices_cb,
+                                   os);
+}
+
+
+static void test_devices_duplication(void)
+{
+    OsinfoLoader *loader = osinfo_loader_new();
+    OsinfoDb *db = osinfo_loader_get_db(loader);
+    OsinfoOsList *all_oses_list;
+    GList *distros;
+    GError *error = NULL;
+
+    g_assert_true(OSINFO_IS_LOADER(loader));
+    g_assert_true(OSINFO_IS_DB(db));
+
+    osinfo_loader_process_default_path(loader, &error);
+    g_assert_no_error(error);
+
+    all_oses_list = osinfo_db_get_os_list(db);
+    distros = get_all_distros(all_oses_list);
+
+    for (GList *l = distros; l != NULL; l = l->next) {
+        const gchar *distro;
+        OsinfoOsList *oslist;
+        OsinfoFilter *filter;
+
+        distro = l->data;
+
+        filter = osinfo_filter_new();
+        osinfo_filter_add_constraint(filter, OSINFO_OS_PROP_DISTRO, distro);
+        oslist = OSINFO_OSLIST(osinfo_list_new_filtered(OSINFO_LIST(all_oses_list), filter));
+
+        for (int i = 0; i < osinfo_list_get_length(OSINFO_LIST(oslist)); i++) {
+            OsinfoOs *os = OSINFO_OS(osinfo_list_get_nth(OSINFO_LIST(oslist), i));
+            check_duplicated_devices(os);
+        }
+
+        g_object_unref(filter);
+        g_object_unref(oslist);
+    }
+
+    g_list_free(distros);
+    g_object_unref(all_oses_list);
+    g_object_unref(loader);
+}
+
+
 int
 main(int argc, char *argv[])
 {
     g_test_init(&argc, &argv, NULL);
+    g_test_set_nonfatal_assertions();
 
     g_test_add_func("/os/basic", test_basic);
     g_test_add_func("/os/loader", test_loader);
     g_test_add_func("/os/devices", test_devices);
     g_test_add_func("/os/devices_filter", test_devices_filter);
+    g_test_add_func("/os/devices_duplication", test_devices_duplication);
     g_test_add_func("/os/device_driver", test_device_driver);
 
     /* Upfront so we don't confuse valgrind */
-- 
2.19.1




More information about the Libosinfo mailing list