aboutsummaryrefslogtreecommitdiff
path: root/tests/test_artifactory.c
blob: 1a21f0ef2112d768d2e27bd9b4df74b73f112112 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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();
}