aboutsummaryrefslogtreecommitdiff
path: root/tests/test_artifactory.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2024-08-12 15:11:17 -0400
committerGitHub <noreply@github.com>2024-08-12 15:11:17 -0400
commit95672b2e7a6cc0c07306893d5bb0b80ee3570f7a (patch)
tree7167084154e521b1c82945d7e0405dbe524074ad /tests/test_artifactory.c
parentdd2768ddcd61172cc58311fa51138281144397ae (diff)
downloadstasis-95672b2e7a6cc0c07306893d5bb0b80ee3570f7a.tar.gz
More unit tests (and fixing bugs) (#23)
* Add test_artifactory.c to suite * Add test_ini_getval_wrapper to ini.c * basic ini_getval_TYPE() conversion checks * bugfix: use strtof in INIVAL_TYPE_FLOAT case * Include stdio.h to pull in FILE structure * bugfix: free data at index during strlist_set operation * Previous behavior of setting the pointer to NULL introduced a subtle memory leak * Set strlist error when index it out of range * Import private delivery functions required for mock context creation * Remove static declaration * populate_delivery_cfg() * Add test_junitxml.c * Fix duplicate define value for JUNIT_RESULT_STATE_ERROR * Add static junit test data * Copy test data to current test directory
Diffstat (limited to 'tests/test_artifactory.c')
-rw-r--r--tests/test_artifactory.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/tests/test_artifactory.c b/tests/test_artifactory.c
new file mode 100644
index 0000000..1a21f0e
--- /dev/null
+++ b/tests/test_artifactory.c
@@ -0,0 +1,129 @@
+#include "testing.h"
+
+// Import private functions from core
+extern int delivery_init_platform(struct Delivery *ctx);
+extern int populate_delivery_cfg(struct Delivery *ctx, int render_mode);
+
+struct JFRT_Auth gauth;
+struct JFRT_Auth gnoauth;
+struct Delivery ctx;
+const char *gbuild_name = "test_stasis_jf_build_collect_env";
+const char *gbuild_num = "1";
+
+// Delete a build
+// For now, I'm keeping these out of the core library.
+static int jfrog_cli_rt_build_delete(struct JFRT_Auth *auth, char *build_name, char *build_num) {
+ char cmd[STASIS_BUFSIZ];
+ memset(cmd, 0, sizeof(cmd));
+ snprintf(cmd, sizeof(cmd) - 1, "--build \"%s/%s\"", build_name, build_num);
+ return jfrog_cli(auth, "rt", "delete", cmd);
+}
+
+static int jfrog_cli_rt_delete(struct JFRT_Auth *auth, char *pattern) {
+ char cmd[STASIS_BUFSIZ];
+ memset(cmd, 0, sizeof(cmd));
+ snprintf(cmd, sizeof(cmd) - 1, "\"%s\"", pattern);
+ return jfrog_cli(auth, "rt", "delete", cmd);
+}
+
+void test_jfrog_cli_rt_ping() {
+ STASIS_ASSERT_FATAL(jfrog_cli_rt_ping(&gauth) == 0, "server ping failed.");
+ STASIS_ASSERT(jfrog_cli_rt_ping(&gnoauth) != 0, "server ping should have failed; auth context is empty.");
+}
+
+void test_jfrog_cli_rt_build_collect_publish() {
+ struct JFRT_Upload upload;
+ jfrt_upload_init(&upload);
+
+ char *filename = "empty_file.txt";
+ touch(filename);
+ upload.build_name = gbuild_name;
+ upload.build_number = gbuild_num;
+ STASIS_ASSERT(jfrog_cli_rt_upload(&gauth, &upload, filename, getenv("STASIS_JF_REPO")) == 0, "jf upload failed");
+ STASIS_ASSERT(jfrog_cli_rt_build_collect_env(&gauth, gbuild_name, gbuild_num) == 0, "jf environment collection failed");
+ STASIS_ASSERT(jfrog_cli_rt_build_publish(&gauth, gbuild_name, gbuild_num) == 0, "jf publish build failed");
+ STASIS_ASSERT(jfrog_cli_rt_build_delete(&gauth, gbuild_name, gbuild_num) == 0, "jf delete build failed");
+}
+
+void test_jfrog_cli_rt_upload() {
+ struct JFRT_Upload upload;
+ jfrt_upload_init(&upload);
+
+ char *filename = "empty_file_upload.txt";
+ touch(filename);
+ STASIS_ASSERT(jfrog_cli_rt_upload(&gauth, &upload, filename, getenv("STASIS_JF_REPO")) == 0, "jf upload failed");
+}
+
+void test_jfrog_cli_rt_download() {
+ struct JFRT_Download dl;
+ memset(&dl, 0, sizeof(dl));
+
+ char *filename = "empty_file_upload.txt";
+ char path[PATH_MAX] = {0};
+ sprintf(path, "%s/%s", getenv("STASIS_JF_REPO"), filename);
+ STASIS_ASSERT(jfrog_cli_rt_download(&gauth, &dl, filename, ".") == 0, "jf download failed");
+ STASIS_ASSERT(jfrog_cli_rt_delete(&gauth, path) == 0, "jf delete test artifact failed");
+}
+
+int main(int argc, char *argv[]) {
+ STASIS_TEST_BEGIN_MAIN();
+ memset(&gauth, 0, sizeof(gauth));
+ memset(&gnoauth, 0, sizeof(gnoauth));
+ memset(&ctx, 0, sizeof(ctx));
+
+ const char *basedir = realpath(".", NULL);
+ const char *ws = "workspace";
+ mkdir(ws, 0755);
+ if (pushd(ws)) {
+ SYSERROR("failed to change directory to %s", ws);
+ STASIS_ASSERT_FATAL(true, "workspace creation failed");
+ }
+
+ // enable messages from the jf tool
+ globals.verbose = true;
+
+ // create a limited delivery context
+ path_store(&ctx.storage.tools_dir, PATH_MAX, ".", "tools");
+ path_store(&ctx.storage.build_dir, PATH_MAX, ".", "build");
+ path_store(&ctx.storage.tmpdir, PATH_MAX, ".", "tmp");
+ const char *sysconfdir = getenv("STASIS_SYSCONFDIR");
+ if (!sysconfdir) {
+ sysconfdir = STASIS_SYSCONFDIR;
+ }
+
+ char path[PATH_MAX] = {0};
+ sprintf(path, "%s/bin:%s", ctx.storage.tools_dir, getenv("PATH"));
+ setenv("PATH", path, 1);
+
+ // The default config contains the URL information to download jfrog-cli
+ char cfg_path[PATH_MAX] = {0};
+ if (strstr(sysconfdir, "..")) {
+ sprintf(cfg_path, "%s/%s/stasis.ini", basedir, sysconfdir);
+ } else {
+ sprintf(cfg_path, "%s/stasis.ini", sysconfdir);
+ }
+ ctx._stasis_ini_fp.cfg = ini_open(cfg_path);
+ if (!ctx._stasis_ini_fp.cfg) {
+ SYSERROR("%s: configuration is invalid", cfg_path);
+ return STASIS_TEST_SUITE_SKIP;
+ }
+ delivery_init_platform(&ctx);
+ populate_delivery_cfg(&ctx, INI_READ_RENDER);
+ delivery_init_artifactory(&ctx);
+
+ // Skip this suite if we're not configured to use it
+ if (jfrt_auth_init(&gauth)) {
+ SYSERROR("%s", "Not configured to test Artifactory. Skipping.");
+ return STASIS_TEST_SUITE_SKIP;
+ }
+
+ STASIS_TEST_FUNC *tests[] = {
+ test_jfrog_cli_rt_ping,
+ test_jfrog_cli_rt_build_collect_publish,
+ test_jfrog_cli_rt_upload,
+ test_jfrog_cli_rt_download,
+ };
+ STASIS_TEST_RUN(tests);
+ popd();
+ STASIS_TEST_END_MAIN();
+} \ No newline at end of file