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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 .gitignore             |   1 +
 tests/Makefile.am      |   5 ++
 tests/test-imageuris.c | 154 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
 create mode 100644 tests/test-imageuris.c

diff --git a/.gitignore b/.gitignore
index b8e6a4a..7add1ad 100644
--- a/.gitignore
+++ b/.gitignore
@@ -69,6 +69,7 @@ tests/test-platformlist
 tests/test-isodetect
 tests/test-mediauris
 tests/test-treeuris
+tests/test-imageuris
 tests/*.log
 tests/*.trs
 build/
diff --git a/tests/Makefile.am b/tests/Makefile.am
index fa4ba75..5c9497d 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
 
@@ -112,6 +113,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..775e80f
--- /dev/null
+++ b/tests/test-imageuris.c
@@ -0,0 +1,154 @@
+/*
+ * 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@xxxxxxxxxx>
+ */
+
+#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);
+
+        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;
+    }
+
+    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_db_get_type();
+    osinfo_os_get_type();
+    osinfo_list_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

_______________________________________________
Libosinfo mailing list
Libosinfo@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libosinfo




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux