aboutsummaryrefslogtreecommitdiff
path: root/src/delivery_test.c
blob: 177a9e73fa9023ca124113a05c0d4cd1c5bd94e8 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "delivery.h"

void delivery_tests_run(struct Delivery *ctx) {
    static const int SETUP = 0;
    static const int PARALLEL = 1;
    static const int SERIAL = 2;
    struct MultiProcessingPool *pool[3];
    struct Process proc;
    memset(&proc, 0, sizeof(proc));

    if (!globals.workaround.conda_reactivate) {
        globals.workaround.conda_reactivate = calloc(PATH_MAX, sizeof(*globals.workaround.conda_reactivate));
    } else {
        memset(globals.workaround.conda_reactivate, 0, PATH_MAX);
    }
    snprintf(globals.workaround.conda_reactivate, PATH_MAX - 1, "\nmamba activate ${CONDA_DEFAULT_ENV}\n");

    if (!ctx->tests[0].name) {
        msg(STASIS_MSG_WARN | STASIS_MSG_L2, "no tests are defined!\n");
    } else {
        pool[PARALLEL] = mp_pool_init("parallel", ctx->storage.tmpdir);
        if (!pool[PARALLEL]) {
            perror("mp_pool_init/parallel");
            exit(1);
        }

        pool[SERIAL] = mp_pool_init("serial", ctx->storage.tmpdir);
        if (!pool[SERIAL]) {
            perror("mp_pool_init/serial");
            exit(1);
        }

        pool[SETUP] = mp_pool_init("setup", ctx->storage.tmpdir);
        if (!pool[SETUP]) {
            perror("mp_pool_init/setup");
            exit(1);
        }

        const char *runner_cmd_fmt = "set -e -x\n%s\n";
        for (size_t i = 0; i < sizeof(ctx->tests) / sizeof(ctx->tests[0]); i++) {
            struct Test *test = &ctx->tests[i];
            if (!test->name && !test->repository && !test->script) {
                // skip unused test records
                continue;
            }
            msg(STASIS_MSG_L2, "Executing tests for %s %s\n", test->name, test->version);
            if (!test->script || !strlen(test->script)) {
                msg(STASIS_MSG_WARN | STASIS_MSG_L3, "Nothing to do. To fix, declare a 'script' in section: [test:%s]\n",
                    test->name);
                continue;
            }

            char destdir[PATH_MAX];
            sprintf(destdir, "%s/%s", ctx->storage.build_sources_dir, path_basename(test->repository));

            if (!access(destdir, F_OK)) {
                msg(STASIS_MSG_L3, "Purging repository %s\n", destdir);
                if (rmtree(destdir)) {
                    COE_CHECK_ABORT(1, "Unable to remove repository\n");
                }
            }
            msg(STASIS_MSG_L3, "Cloning repository %s\n", test->repository);
            if (!git_clone(&proc, test->repository, destdir, test->version)) {
                test->repository_info_tag = strdup(git_describe(destdir));
                test->repository_info_ref = strdup(git_rev_parse(destdir, "HEAD"));
            } else {
                COE_CHECK_ABORT(1, "Unable to clone repository\n");
            }

            if (test->repository_remove_tags && strlist_count(test->repository_remove_tags)) {
                filter_repo_tags(destdir, test->repository_remove_tags);
            }

            if (pushd(destdir)) {
                COE_CHECK_ABORT(1, "Unable to enter repository directory\n");
            } else {
                char *cmd = calloc(strlen(test->script) + STASIS_BUFSIZ, sizeof(*cmd));
                if (!cmd) {
                    SYSERROR("Unable to allocate test script buffer: %s", strerror(errno));
                    exit(1);
                }

                msg(STASIS_MSG_L3, "Testing %s\n", test->name);
                memset(&proc, 0, sizeof(proc));

                strcpy(cmd, test->script);
                char *cmd_rendered = tpl_render(cmd);
                if (cmd_rendered) {
                    if (strcmp(cmd_rendered, cmd) != 0) {
                        strcpy(cmd, cmd_rendered);
                        cmd[strlen(cmd_rendered) ? strlen(cmd_rendered) - 1 : 0] = 0;
                    }
                    guard_free(cmd_rendered);
                } else {
                    SYSERROR("An error occurred while rendering the following:\n%s", cmd);
                    exit(1);
                }

                if (test->disable) {
                    msg(STASIS_MSG_L2, "Script execution disabled by configuration\n", test->name);
                    guard_free(cmd);
                    continue;
                }

                char runner_cmd[0xFFFF] = {0};
                char pool_name[100] = "parallel";
                struct MultiProcessingTask *task = NULL;
                int selected = PARALLEL;
                if (!test->parallel) {
                    selected = SERIAL;
                    memset(pool_name, 0, sizeof(pool_name));
                    strcpy(pool_name, "serial");
                }

                sprintf(runner_cmd, runner_cmd_fmt, cmd);
                task = mp_pool_task(pool[selected], test->name, destdir, runner_cmd);
                if (!task) {
                    SYSERROR("Failed to add task to %s pool: %s", pool_name, runner_cmd);
                    popd();
                    if (!globals.continue_on_error) {
                        tpl_free();
                        delivery_free(ctx);
                        globals_free();
                    }
                    exit(1);
                }
                guard_free(cmd);
                popd();

            }
        }

        // Configure "script_setup" tasks
        // Directories should exist now, so no need to go through initializing everything all over again.
        for (size_t i = 0; i < sizeof(ctx->tests) / sizeof(ctx->tests[0]); i++) {
            struct Test *test = &ctx->tests[i];
            if (test->script_setup) {
                char destdir[PATH_MAX];
                sprintf(destdir, "%s/%s", ctx->storage.build_sources_dir, path_basename(test->repository));
                if (access(destdir, F_OK)) {
                    SYSERROR("%s: %s", destdir, strerror(errno));
                    exit(1);
                }
                if (!pushd(destdir)) {
                    char *cmd = calloc(strlen(test->script_setup) + STASIS_BUFSIZ, sizeof(*cmd));
                    if (!cmd) {
                        SYSERROR("Unable to allocate test script_setup buffer: %s", strerror(errno));
                        exit(1);
                    }

                    strcpy(cmd, test->script_setup);
                    char *cmd_rendered = tpl_render(cmd);
                    if (cmd_rendered) {
                        if (strcmp(cmd_rendered, cmd) != 0) {
                            strcpy(cmd, cmd_rendered);
                            cmd[strlen(cmd_rendered) ? strlen(cmd_rendered) - 1 : 0] = 0;
                        }
                        guard_free(cmd_rendered);
                    } else {
                        SYSERROR("An error occurred while rendering the following:\n%s", cmd);
                        exit(1);
                    }

                    struct MultiProcessingTask *task = NULL;
                    char runner_cmd[0xFFFF] = {0};
                    sprintf(runner_cmd, runner_cmd_fmt, cmd);

                    task = mp_pool_task(pool[SETUP], test->name, destdir, runner_cmd);
                    if (!task) {
                        SYSERROR("Failed to add task %s to setup pool: %s", test->name, runner_cmd);
                        popd();
                        if (!globals.continue_on_error) {
                            tpl_free();
                            delivery_free(ctx);
                            globals_free();
                        }
                        exit(1);
                    }
                    guard_free(cmd);
                    popd();
                } else {
                    SYSERROR("Failed to change directory: %s\n", destdir);
                    exit(1);
                }
            }
        }

        size_t opt_flags = 0;
        if (globals.parallel_fail_fast) {
            opt_flags |= MP_POOL_FAIL_FAST;
        }

        for (size_t p = 0; p < sizeof(pool) / sizeof(*pool); p++) {
            int pool_status;
            long jobs = globals.cpu_limit;

            if (!pool[p]->num_used) {
                // Skip empty pool
                continue;
            }

            // Setup tasks run sequentially
            if (p == (size_t) SETUP) {
                jobs = 1;
            }

            // Run tasks in the pool
            // 1. Setup (builds)
            // 2. Parallel (fast jobs)
            // 3. Serial (long jobs)
            pool_status = mp_pool_join(pool[p], jobs, opt_flags);

            // On error show a summary of the current pool, and die
            if (pool_status != 0) {
                mp_pool_show_summary(pool[p]);
                COE_CHECK_ABORT(true, "Task failure");
            }
        }

        // All tasks were successful
        for (size_t p = 0; p < sizeof(pool) / sizeof(*pool); p++) {
            mp_pool_show_summary(pool[p]);
            mp_pool_free(&pool[p]);
        }
    }
}

int delivery_fixup_test_results(struct Delivery *ctx) {
    struct dirent *rec;
    DIR *dp;

    dp = opendir(ctx->storage.results_dir);
    if (!dp) {
        perror(ctx->storage.results_dir);
        return -1;
    }

    while ((rec = readdir(dp)) != NULL) {
        char path[PATH_MAX];
        memset(path, 0, sizeof(path));

        if (!strcmp(rec->d_name, ".") || !strcmp(rec->d_name, "..")) {
            continue;
        } else if (!endswith(rec->d_name, ".xml")) {
            continue;
        }

        sprintf(path, "%s/%s", ctx->storage.results_dir, rec->d_name);
        msg(STASIS_MSG_L3, "%s\n", rec->d_name);
        if (xml_pretty_print_in_place(path, STASIS_XML_PRETTY_PRINT_PROG, STASIS_XML_PRETTY_PRINT_ARGS)) {
            msg(STASIS_MSG_L3 | STASIS_MSG_WARN, "Failed to rewrite file '%s'\n", rec->d_name);
        }
    }

    closedir(dp);
    return 0;
}