[Libosinfo] [libosinfo PATCH] tests: Reduce code duplication in URI tests

Fabiano Fidêncio fabiano at fidencio.org
Mon Feb 4 15:42:56 UTC 2019


From: Fabiano Fidêncio <fidencio at redhat.com>

Let's introduce a common file that does the check of the URI for all the
URI related tests.

By doing this we can avoid code duplication while also avoiding to have
all the tests merged together into a single one, which is not so good as
limits the developer to run and wait for the whole set of tests instead
of having the ability to just run the a specific one.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 tests/Makefile.am        |   6 +--
 tests/test-imageuris.c   |  84 ++----------------------------------
 tests/test-mediauris.c   |  84 ++----------------------------------
 tests/test-treeuris.c    |  84 ++----------------------------------
 tests/test-uris-common.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-uris-common.h |  26 +++++++++++
 6 files changed, 151 insertions(+), 243 deletions(-)
 create mode 100644 tests/test-uris-common.c
 create mode 100644 tests/test-uris-common.h

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 45703df..f073b41 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -113,15 +113,15 @@ test_isodetect_SOURCES = test-isodetect.c
 if HAVE_CURL
 test_mediauris_LDADD = $(COMMON_LDADD) $(CURL_LIBS)
 test_mediauris_CFLAGS = $(COMMON_CFLAGS) $(CURL_CFLAGS)
-test_mediauris_SOURCES = test-mediauris.c
+test_mediauris_SOURCES = test-mediauris.c test-uris-common.c
 
 test_treeuris_LDADD = $(COMMON_LDADD) $(CURL_LIBS)
 test_treeuris_CFLAGS = $(COMMON_CFLAGS) $(CURL_CFLAGS)
-test_treeuris_SOURCES = test-treeuris.c
+test_treeuris_SOURCES = test-treeuris.c test-uris-common.c
 
 test_imageuris_LDADD = $(COMMON_LDADD) $(CURL_LIBS)
 test_imageuris_CFLAGS = $(COMMON_CFLAGS) $(CURL_CFLAGS)
-test_imageuris_SOURCES = test-imageuris.c
+test_imageuris_SOURCES = test-imageuris.c test-uris-common.c
 endif
 
 test_install_script_LDADD = $(COMMON_LDADD)
diff --git a/tests/test-imageuris.c b/tests/test-imageuris.c
index 33fdc19..ca6e648 100644
--- a/tests/test-imageuris.c
+++ b/tests/test-imageuris.c
@@ -24,87 +24,23 @@
 #include <osinfo/osinfo.h>
 #include <curl/curl.h>
 
-static size_t write_callback(char *ptr,
-                             size_t size,
-                             size_t nmemb,
-                             void *userdata)
-{
-    abort();
-}
-
-static void test_image(OsinfoImageList *imagelist, GError **error)
-{
-    GList *imageel = NULL, *tmp;
-
-    tmp = imageel = osinfo_list_get_elements(OSINFO_LIST(imagelist));
-    while (tmp) {
-        OsinfoImage *image = tmp->data;
-        const gchar *url = osinfo_image_get_url(image);
-        const gchar *debugstr;
-        CURL *curl;
-        CURLcode res;
-        long response_code;
-
-        if (url == NULL || g_str_equal(url, "")) {
-            tmp = tmp->next;
-            continue;
-        }
-
-        curl = curl_easy_init();
-        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
-        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
-        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
-        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
-        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
-
-        if ((debugstr = g_getenv("LIBOSINFO_TEST_DEBUG"))) {
-            int debug_level = atoi(debugstr);
-
-            curl_easy_setopt(curl, CURLOPT_VERBOSE, debug_level > 0 ? 1L : 0L);
-        }
-
-        g_test_message("%s", url);
-        curl_easy_setopt(curl, CURLOPT_URL, url);
-        res = curl_easy_perform(curl);
-        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
-
-        g_test_message("res=%d, %s; code=%ld", res, curl_easy_strerror(res), response_code);
-
-        if (res == CURLE_OPERATION_TIMEDOUT) {
-            g_printerr("Ignoring network timeout failure for %s\n", url);
-        } else {
-            if (res != CURLE_OK) {
-                g_printerr("Failed URI %s res=%d (%s) code=%ld\n",
-                           url, res, curl_easy_strerror(res), response_code);
-            }
-            g_assert_cmpint(res, ==, CURLE_OK);
-        }
-
-        tmp = tmp->next;
-        curl_easy_cleanup(curl);
-    }
-
-    g_list_free(imageel);
-}
+#include "test-uris-common.h"
 
 static void
-test_uris(gconstpointer data)
+test_imageuris(gconstpointer data)
 {
     OsinfoOs *os = OSINFO_OS(data);
     GError *error = NULL;
 
     OsinfoImageList *imagelist = osinfo_os_get_image_list(os);
 
-    test_image(imagelist, &error);
+    test_uri(OSINFO_LIST(imagelist), (GetURLFunc) osinfo_image_get_url, &error);
 
     g_assert_no_error(error);
 
     g_object_unref(imagelist);
 }
 
-
-
 int
 main(int argc, char *argv[])
 {
@@ -112,7 +48,6 @@ main(int argc, char *argv[])
     OsinfoLoader *loader = osinfo_loader_new();
     OsinfoDb *db = osinfo_loader_get_db(loader);
     OsinfoOsList *oslist = NULL;
-    GList *osel = NULL, *tmp;
     GError *error = NULL;
 
     g_test_init(&argc, &argv, NULL);
@@ -133,21 +68,10 @@ main(int argc, char *argv[])
     g_assert_no_error(error);
 
     oslist = osinfo_db_get_os_list(db);
-    tmp = osel = osinfo_list_get_elements(OSINFO_LIST(oslist));
-    while (tmp) {
-        OsinfoOs *os = tmp->data;
-        gchar *name = g_strdup_printf("/imageuris/uris/%s",
-                                      osinfo_product_get_short_id(OSINFO_PRODUCT(os)));
-
-        g_test_add_data_func(name, os, test_uris);
-        g_free(name);
-
-        tmp = tmp->next;
-    }
+    test_uris(oslist, "/imageuris/uris", test_imageuris);
 
     ret = g_test_run();
 
-    g_list_free(osel);
     if (oslist)
         g_object_unref(oslist);
 
diff --git a/tests/test-mediauris.c b/tests/test-mediauris.c
index e35f152..c9e09c7 100644
--- a/tests/test-mediauris.c
+++ b/tests/test-mediauris.c
@@ -24,87 +24,23 @@
 #include <osinfo/osinfo.h>
 #include <curl/curl.h>
 
-static size_t write_callback(char *ptr,
-                             size_t size,
-                             size_t nmemb,
-                             void *userdata)
-{
-    abort();
-}
-
-static void test_media(OsinfoMediaList *medialist, GError **error)
-{
-    GList *mediael = NULL, *tmp;
-
-    tmp = mediael = osinfo_list_get_elements(OSINFO_LIST(medialist));
-    while (tmp) {
-        OsinfoMedia *media = tmp->data;
-        const gchar *url = osinfo_media_get_url(media);
-        const gchar *debugstr;
-        CURL *curl;
-        CURLcode res;
-        long response_code;
-
-        if (url == NULL || g_str_equal(url, "")) {
-            tmp = tmp->next;
-            continue;
-        }
-
-        curl = curl_easy_init();
-        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
-        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
-        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
-        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
-        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
-
-        if ((debugstr = g_getenv("LIBOSINFO_TEST_DEBUG"))) {
-            int debug_level = atoi(debugstr);
-
-            curl_easy_setopt(curl, CURLOPT_VERBOSE, debug_level > 0 ? 1L : 0L);
-        }
-
-        g_test_message("%s", url);
-        curl_easy_setopt(curl, CURLOPT_URL, url);
-        res = curl_easy_perform(curl);
-        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
-
-        g_test_message("res=%d, %s; code=%ld", res, curl_easy_strerror(res), response_code);
-
-        if (res == CURLE_OPERATION_TIMEDOUT) {
-            g_printerr("Ignoring network timeout failure for %s\n", url);
-        } else {
-            if (res != CURLE_OK) {
-                g_printerr("Failed URI %s res=%d (%s) code=%ld\n",
-                           url, res, curl_easy_strerror(res), response_code);
-            }
-            g_assert_cmpint(res, ==, CURLE_OK);
-        }
-
-        tmp = tmp->next;
-        curl_easy_cleanup(curl);
-    }
-
-    g_list_free(mediael);
-}
+#include "test-uris-common.h"
 
 static void
-test_uris(gconstpointer data)
+test_mediauris(gconstpointer data)
 {
     OsinfoOs *os = OSINFO_OS(data);
     GError *error = NULL;
 
     OsinfoMediaList *medialist = osinfo_os_get_media_list(os);
 
-    test_media(medialist, &error);
+    test_uri(OSINFO_LIST(medialist), (GetURLFunc) osinfo_media_get_url, &error);
 
     g_assert_no_error(error);
 
     g_object_unref(medialist);
 }
 
-
-
 int
 main(int argc, char *argv[])
 {
@@ -112,7 +48,6 @@ main(int argc, char *argv[])
     OsinfoLoader *loader = osinfo_loader_new();
     OsinfoDb *db = osinfo_loader_get_db(loader);
     OsinfoOsList *oslist = NULL;
-    GList *osel = NULL, *tmp;
     GError *error = NULL;
 
     g_test_init(&argc, &argv, NULL);
@@ -139,21 +74,10 @@ main(int argc, char *argv[])
     g_assert_no_error(error);
 
     oslist = osinfo_db_get_os_list(db);
-    tmp = osel = osinfo_list_get_elements(OSINFO_LIST(oslist));
-    while (tmp) {
-        OsinfoOs *os = tmp->data;
-        gchar *name = g_strdup_printf("/mediauris/uris/%s",
-                                      osinfo_product_get_short_id(OSINFO_PRODUCT(os)));
-
-        g_test_add_data_func(name, os, test_uris);
-        g_free(name);
-
-        tmp = tmp->next;
-    }
+    test_uris(oslist, "/mediauris/uris", test_mediauris);
 
     ret = g_test_run();
 
-    g_list_free(osel);
     if (oslist)
         g_object_unref(oslist);
 
diff --git a/tests/test-treeuris.c b/tests/test-treeuris.c
index 76ab862..48ce82a 100644
--- a/tests/test-treeuris.c
+++ b/tests/test-treeuris.c
@@ -24,86 +24,22 @@
 #include <osinfo/osinfo.h>
 #include <curl/curl.h>
 
-static size_t write_callback(char *ptr,
-                             size_t size,
-                             size_t nmemb,
-                             void *userdata)
-{
-    abort();
-}
-
-static void test_tree(OsinfoTreeList *treelist, GError **error)
-{
-    GList *treeel = NULL, *tmp;
-
-    tmp = treeel = osinfo_list_get_elements(OSINFO_LIST(treelist));
-    while (tmp) {
-        OsinfoTree *tree = tmp->data;
-        const gchar *url = osinfo_tree_get_url(tree);
-        const gchar *debugstr;
-        CURL *curl;
-        CURLcode res;
-        long response_code;
-
-        if (url == NULL || g_str_equal(url, "")) {
-            tmp = tmp->next;
-            continue;
-        }
-
-        curl = curl_easy_init();
-        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
-        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
-        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
-        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
-        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
-
-        if ((debugstr = g_getenv("LIBOSINFO_TEST_DEBUG"))) {
-            int debug_level = atoi(debugstr);
-
-            curl_easy_setopt(curl, CURLOPT_VERBOSE, debug_level > 0 ? 1L : 0L);
-        }
-
-        g_test_message("%s", url);
-        curl_easy_setopt(curl, CURLOPT_URL, url);
-        res = curl_easy_perform(curl);
-        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
-
-        g_test_message("res=%d, %s; code=%ld", res, curl_easy_strerror(res), response_code);
-
-        if (res == CURLE_OPERATION_TIMEDOUT) {
-            g_printerr("Ignoring network timeout failure for %s\n", url);
-        } else {
-            if (res != CURLE_OK) {
-                g_printerr("Failed URI %s res=%d (%s) code=%ld\n",
-                           url, res, curl_easy_strerror(res), response_code);
-            }
-            g_assert_cmpint(res, ==, CURLE_OK);
-        }
-
-        tmp = tmp->next;
-        curl_easy_cleanup(curl);
-    }
-
-    g_list_free(treeel);
-}
+#include "test-uris-common.h"
 
 static void
-test_uris(gconstpointer data)
+test_treeuris(gconstpointer data)
 {
     OsinfoOs *os = OSINFO_OS(data);
     GError *error = NULL;
     OsinfoTreeList *treelist = osinfo_os_get_tree_list(os);
 
-    test_tree(treelist, &error);
+    test_uri(OSINFO_LIST(treelist), (GetURLFunc) osinfo_tree_get_url, &error);
 
     g_assert_no_error(error);
 
     g_object_unref(treelist);
 }
 
-
-
 int
 main(int argc, char *argv[])
 {
@@ -111,7 +47,6 @@ main(int argc, char *argv[])
     OsinfoLoader *loader = osinfo_loader_new();
     OsinfoDb *db = osinfo_loader_get_db(loader);
     OsinfoOsList *oslist = NULL;
-    GList *osel = NULL, *tmp;
     GError *error = NULL;
 
     g_test_init(&argc, &argv, NULL);
@@ -137,21 +72,10 @@ main(int argc, char *argv[])
     g_assert_no_error(error);
 
     oslist = osinfo_db_get_os_list(db);
-    tmp = osel = osinfo_list_get_elements(OSINFO_LIST(oslist));
-    while (tmp) {
-        OsinfoOs *os = tmp->data;
-        gchar *name = g_strdup_printf("/treeuris/uris/%s",
-                                      osinfo_product_get_short_id(OSINFO_PRODUCT(os)));
-
-        g_test_add_data_func(name, os, test_uris);
-        g_free(name);
-
-        tmp = tmp->next;
-    }
+    test_uris(oslist, "/treeuris/uris", test_treeuris);
 
     ret = g_test_run();
 
-    g_list_free(osel);
     if (oslist)
         g_object_unref(oslist);
 
diff --git a/tests/test-uris-common.c b/tests/test-uris-common.c
new file mode 100644
index 0000000..0352ce1
--- /dev/null
+++ b/tests/test-uris-common.c
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <osinfo/osinfo.h>
+#include <curl/curl.h>
+
+#include "test-uris-common.h"
+
+static size_t write_callback(char *ptr,
+                             size_t size,
+                             size_t nmemb,
+                             void *userdata)
+{
+    abort();
+}
+
+void test_uri(OsinfoList *list, GetURLFunc get_url_func, GError **error)
+{
+    GList *el = NULL, *tmp;
+
+    tmp = el = osinfo_list_get_elements(list);
+    while (tmp) {
+        const gchar *url = get_url_func(tmp->data);
+        const gchar *debugstr;
+        CURL *curl;
+        CURLcode res;
+        long response_code;
+
+        if (url == NULL || g_str_equal(url, "")) {
+            tmp = tmp->next;
+            continue;
+        }
+
+        curl = curl_easy_init();
+        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
+        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
+        curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
+        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
+        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
+        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
+
+        if ((debugstr = g_getenv("LIBOSINFO_TEST_DEBUG"))) {
+            int debug_level = atoi(debugstr);
+
+            curl_easy_setopt(curl, CURLOPT_VERBOSE, debug_level > 0 ? 1L : 0L);
+        }
+
+        g_test_message("%s", url);
+        curl_easy_setopt(curl, CURLOPT_URL, url);
+        res = curl_easy_perform(curl);
+        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
+
+        g_test_message("res=%d, %s; code=%ld", res, curl_easy_strerror(res), response_code);
+
+        if (res == CURLE_OPERATION_TIMEDOUT) {
+            g_printerr("Ignoring network timeout failure for %s\n", url);
+        } else {
+            if (res != CURLE_OK) {
+                g_printerr("Failed URI %s res=%d (%s) code=%ld\n",
+                           url, res, curl_easy_strerror(res), response_code);
+            }
+            g_assert_cmpint(res, ==, CURLE_OK);
+        }
+
+        tmp = tmp->next;
+        curl_easy_cleanup(curl);
+    }
+
+    g_list_free(el);
+}
+
+
+void test_uris(OsinfoOsList *oslist, const gchar *prefix, TestURLFunc test_func)
+{
+    GList *osel = NULL, *tmp;
+
+    tmp = osel = osinfo_list_get_elements(OSINFO_LIST(oslist));
+
+    while (tmp) {
+        OsinfoOs *os = tmp->data;
+        gchar *name = g_strdup_printf("%s/%s",
+                                      prefix,
+                                      osinfo_product_get_short_id(OSINFO_PRODUCT(os)));
+
+        g_test_add_data_func(name, os, test_func);
+        g_free(name);
+
+        tmp = tmp->next;
+    }
+
+    g_list_free(osel);
+}
+
diff --git a/tests/test-uris-common.h b/tests/test-uris-common.h
new file mode 100644
index 0000000..34cc242
--- /dev/null
+++ b/tests/test-uris-common.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2019 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>
+ */
+
+#include <config.h>
+
+#include <osinfo/osinfo.h>
+
+typedef const gchar *(*GetURLFunc)(gpointer p);
+typedef void (*TestURLFunc)(gconstpointer p);
+
+void test_uri(OsinfoList *list, GetURLFunc get_url_func, GError **error);
+void test_uris(OsinfoOsList *oslist, const gchar *prefix, TestURLFunc test_func);
-- 
1.8.3.1




More information about the Libosinfo mailing list