aboutsummaryrefslogtreecommitdiff
path: root/tests/test_download.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2024-07-15 10:07:25 -0400
committerGitHub <noreply@github.com>2024-07-15 10:07:25 -0400
commit07dc44efdc5c2fbc2b34c969e623d3b0bc0df15a (patch)
tree1f41c27e50baeee149b59b8c3d37a9c72cbd0ded /tests/test_download.c
parent70cd78cdef69237ba3c511b9e091715ec6d093e5 (diff)
downloadstasis-07dc44efdc5c2fbc2b34c969e623d3b0bc0df15a.tar.gz
Unit tests (#12)
* Change return value of conda_setup_headless() from void to int * Replace exit() with return; * Return early if unpacking the micromamba binary fails * Exit program when pointer to INIFILE is NULL. * Validation function cannot otherwise proceed * The way the logic is set up I've decided to duplicate the installation code for now until I find time to revise it * The only meaningful difference between a "fresh start" and reusing the conda installation is a rmtree(). * Exposes STASIS_DOWNLOAD_TIMEOUT environment variable * Sets the connection timeout for libcurl to 30, instead of 300. * Export ini_section_create() function * Add download() tests * Add conda_*() tests * Add boilerplate source file for test framework * Fixes segfault reported by @GeorgeJCleary (#10) * The key is now an array index. When key is -1, the env variable is not defined. * Free resources only when continue on error is disabled (#11) * Fix segfault due to premature shutdown/cleanup * If conda_setup_headless cannot succeed, die * Set STASIS_SYSCONFDIR for tests
Diffstat (limited to 'tests/test_download.c')
-rw-r--r--tests/test_download.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/test_download.c b/tests/test_download.c
new file mode 100644
index 0000000..cee7683
--- /dev/null
+++ b/tests/test_download.c
@@ -0,0 +1,50 @@
+#include "testing.h"
+
+void test_download() {
+ struct testcase {
+ const char *url;
+ long http_code;
+ const char *data;
+ const char *errmsg;
+ };
+ struct testcase tc[] = {
+ {.url = "https://ssb.stsci.edu/jhunk/stasis_test/test_download.txt", .http_code = 200L, .data = "It works!\n", .errmsg = NULL},
+ {.url = "https://ssb.stsci.edu/jhunk/stasis_test/test_download.broken", .http_code = 404L, .data = "<html", .errmsg = NULL},
+ {.url = "https://example.tld", .http_code = -1L, .data = NULL, .errmsg = "Couldn't resolve host name"},
+ };
+
+ for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
+ const char *filename = "output.txt";
+ char errmsg[BUFSIZ] = {0};
+ char *errmsg_p = errmsg;
+ long http_code = download((char *) tc[i].url, filename, &errmsg_p);
+ if (tc[i].errmsg) {
+ STASIS_ASSERT(strlen(errmsg_p), "an error should have been thrown by curl, but wasn't");
+ fflush(stderr);
+ SYSERROR("curl error message: %s", errmsg_p);
+ } else {
+ STASIS_ASSERT(!strlen(errmsg_p), "unexpected error thrown by curl");
+ }
+ STASIS_ASSERT(http_code == tc[i].http_code, "expecting non-error HTTP code");
+
+ char **data = file_readlines(filename, 0, 100, NULL);
+ if (http_code >= 0) {
+ STASIS_ASSERT(data != NULL, "data should not be null");
+ STASIS_ASSERT(strncmp(data[0], tc[i].data, strlen(tc[i].data)) == 0, "data file does not match the expected contents");
+ } else {
+ STASIS_ASSERT(http_code == -1, "http_code should be -1 on fatal curl error");
+ STASIS_ASSERT(data == NULL, "data should be NULL on fatal curl error");
+ }
+ guard_free(data);
+ remove(filename);
+ }
+}
+
+int main(int argc, char *argv[]) {
+ STASIS_TEST_BEGIN_MAIN();
+ STASIS_TEST_FUNC *tests[] = {
+ test_download,
+ };
+ STASIS_TEST_RUN(tests);
+ STASIS_TEST_END_MAIN();
+} \ No newline at end of file