[Libosinfo] [libosinfo PATCH v3 09/10] tests: Add test-imageuris

Fabiano Fidêncio fidencio at redhat.com
Wed Nov 14 14:49:50 UTC 2018


In a quite similar way than test-mediauris and test-treeuris, let's have
a test for the imageuris.

https://gitlab.com/libosinfo/osinfo-db/issues/10

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 tests/Makefile.am      |   5 ++
 tests/test-imageuris.c | 150 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 155 insertions(+)
 create mode 100644 tests/test-imageuris.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8b06e22..770356e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -26,6 +26,7 @@ if HAVE_CURL
 check_PROGRAMS += \
 	test-mediauris \
 	test-treeuris \
+	test-imageuris \
 	$(NULL)
 endif
 
@@ -108,6 +109,10 @@ test_mediauris_SOURCES = test-mediauris.c
 test_treeuris_LDADD = $(COMMON_LDADD) $(CURL_LIBS)
 test_treeuris_CFLAGS = $(COMMON_CFLAGS) $(CURL_CFLAGS)
 test_treeuris_SOURCES = test-treeuris.c
+
+test_imageuris_LDADD = $(COMMON_LDADD) $(CURL_LIBS)
+test_imageuris_CFLAGS = $(COMMON_CFLAGS) $(CURL_CFLAGS)
+test_imageuris_SOURCES = test-imageuris.c
 endif
 
 test_install_script_LDADD = $(COMMON_LDADD)
diff --git a/tests/test-imageuris.c b/tests/test-imageuris.c
new file mode 100644
index 0000000..a57307e
--- /dev/null
+++ b/tests/test-imageuris.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2108 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/>
+ *
+ * Authors:
+ *   Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <osinfo/osinfo.h>
+#include <curl/curl.h>
+
+static void test_image(OsinfoImageList *imagelist, GError **error, CURL *curl)
+{
+    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);
+        CURLcode res;
+        long response_code;
+
+        if (url == NULL || g_str_equal(url, "")) {
+            tmp = tmp->next;
+            continue;
+        }
+
+        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);
+        g_assert_cmpint(res, ==, CURLE_OK);
+
+        tmp = tmp->next;
+    }
+
+    g_list_free(imageel);
+}
+
+static void
+test_uris(void)
+{
+    CURL *curl;
+    OsinfoLoader *loader = osinfo_loader_new();
+    OsinfoDb *db = osinfo_loader_get_db(loader);
+    GError *error = NULL;
+    OsinfoOsList *oslist = NULL;
+    GList *osel = NULL, *tmp;
+    const gchar *debugstr;
+
+    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);
+
+    if ((debugstr = g_getenv("LIBOSINFO_TEST_DEBUG"))) {
+        int debug_level = atoi(debugstr);
+
+        curl_easy_setopt(curl, CURLOPT_VERBOSE, debug_level > 0 ? 1L : 0L);
+    }
+
+    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);
+
+    oslist = osinfo_db_get_os_list(db);
+    tmp = osel = osinfo_list_get_elements(OSINFO_LIST(oslist));
+    while (tmp) {
+        OsinfoOs *os = tmp->data;
+        OsinfoImageList *imagelist = osinfo_os_get_image_list(os);
+
+        test_image(imagelist, &error, curl);
+
+        g_assert_no_error(error);
+
+        g_object_unref(imagelist);
+        tmp = tmp->next;
+    }
+
+    curl_easy_cleanup(curl);
+
+    g_list_free(osel);
+    if (oslist)
+        g_object_unref(oslist);
+
+    g_object_unref(loader);
+}
+
+
+
+int
+main(int argc, char *argv[])
+{
+    int ret;
+
+    g_test_init(&argc, &argv, NULL);
+    g_test_set_nonfatal_assertions();
+
+    g_test_add_func("/imageuris/uris", test_uris);
+
+    if (!g_getenv("LIBOSINFO_NETWORK_TESTS"))
+        return 77; /* Skip */
+
+    /* Upfront so we don't confuse valgrind */
+    curl_global_init(CURL_GLOBAL_ALL);
+    osinfo_entity_get_type();
+    osinfo_db_get_type();
+    osinfo_device_get_type();
+    osinfo_platform_get_type();
+    osinfo_os_get_type();
+    osinfo_list_get_type();
+    osinfo_devicelist_get_type();
+    osinfo_platformlist_get_type();
+    osinfo_oslist_get_type();
+    osinfo_filter_get_type();
+
+    ret = g_test_run();
+
+    curl_global_cleanup();
+
+    return ret;
+}
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ * End:
+ */
-- 
2.19.1




More information about the Libosinfo mailing list