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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*
* The MIT License (MIT)
*
* Copyright © 2015-2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#define CRITERION_LOGGING_COLORS
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include NN_H
#include "criterion/internal/test.h"
#include "criterion/options.h"
#include "criterion/internal/ordered-set.h"
#include "criterion/internal/preprocess.h"
#include "criterion/redirect.h"
#include "protocol/protocol.h"
#include "protocol/connect.h"
#include "protocol/messages.h"
#include "compat/alloc.h"
#include "compat/time.h"
#include "compat/posix.h"
#include "compat/processor.h"
#include "compat/kill.h"
#include "csptr/smalloc.h"
#include "string/diff.h"
#include "string/extglobmatch.h"
#include "string/i18n.h"
#include "io/event.h"
#include "io/output.h"
#include "log/logging.h"
#include "abort.h"
#include "client.h"
#include "common.h"
#include "err.h"
#include "report.h"
#include "runner.h"
#include "runner_coroutine.h"
#include "stats.h"
#ifdef __GNUC__
# include <valgrind/valgrind.h>
#else
# define ENABLE_VALGRIND_ERRORS
# define RUNNING_ON_VALGRIND 0
#endif
int cri_is_runner;
typedef const char *const msg_t;
#ifdef ENABLE_NLS
static msg_t msg_valgrind_jobs = N_("%1$sWarning! Criterion has detected "
"that it is running under valgrind, but the number of jobs have been "
"explicitely set. Reports might appear confusing!%2$s\n");
#else
static msg_t msg_valgrind_jobs = "%sWarning! Criterion has detected "
"that it is running under valgrind, but the number of jobs have been "
"explicitely set. Reports might appear confusing!%s\n";
#endif
static int cmp_suite(void *a, void *b)
{
struct criterion_suite *s1 = a, *s2 = b;
return strcmp(s1->name, s2->name);
}
static int cmp_test(void *a, void *b)
{
struct criterion_test *s1 = a, *s2 = b;
return strcmp(s1->name, s2->name);
}
static void dtor_suite_set(void *ptr, CR_UNUSED void *meta)
{
struct criterion_suite_set *s = ptr;
sfree(s->tests);
}
static void dtor_test_set(void *ptr, CR_UNUSED void *meta)
{
struct criterion_test_set *t = ptr;
sfree(t->suites);
}
CR_API void criterion_register_test(struct criterion_test_set *set,
struct criterion_test *test)
{
struct criterion_suite_set css = {
.suite = { .name = test->category },
};
struct criterion_suite_set *s = insert_ordered_set(set->suites, &css, sizeof (css));
if (!s->tests)
s->tests = new_ordered_set(cmp_test, NULL);
insert_ordered_set(s->tests, test, sizeof (*test));
++set->tests;
}
struct criterion_test_set *criterion_init(void)
{
struct criterion_ordered_set *suites = new_ordered_set(cmp_suite, dtor_suite_set);
struct cri_section *sections = NULL;
if (!cri_sections_getaddr("cr_sts", §ions)) {
for (struct cri_section *s = sections; s->addr; ++s) {
void *start = s->addr;
void *end = (char *) start + s->length;
FOREACH_SUITE_SEC(s, start, end) {
if (!*s || !*(*s)->name)
continue;
struct criterion_suite_set css = {
.suite = **s,
};
insert_ordered_set(suites, &css, sizeof (css));
}
}
}
free(sections);
struct criterion_test_set *set = smalloc(
.size = sizeof (struct criterion_test_set),
.dtor = dtor_test_set);
*set = (struct criterion_test_set) {
suites,
0,
};
sections = NULL;
if (!cri_sections_getaddr("cr_tst", §ions)) {
for (struct cri_section *s = sections; s->addr; ++s) {
void *start = s->addr;
void *end = (char *) start + s->length;
FOREACH_TEST_SEC(test, start, end) {
if (!*test)
continue;
if (!*(*test)->category || !*(*test)->name)
continue;
criterion_register_test(set, *test);
}
}
}
free(sections);
return set;
}
#define push_event(...) \
do { \
stat_push_event(ctx->stats, \
ctx->suite_stats, \
ctx->test_stats, \
&(struct event) { \
.kind = CR_VA_HEAD(__VA_ARGS__), \
CR_VA_TAIL(__VA_ARGS__) \
}); \
report(CR_VA_HEAD(__VA_ARGS__), ctx->test_stats); \
} while (0)
void disable_unmatching(struct criterion_test_set *set)
{
if (!compile_pattern(criterion_options.pattern))
exit(3);
FOREACH_SET(struct criterion_suite_set *s, set->suites) {
if ((s->suite.data && s->suite.data->disabled) || !s->tests)
continue;
FOREACH_SET(struct criterion_test *test, s->tests) {
int ret = match(test->data->identifier_);
if (ret == 0)
test->data->disabled = true;
}
}
free_pattern();
}
CR_API struct criterion_test_set *criterion_initialize(void)
{
/* make sure we don't re-enter from a test worker. See #247. */
if (getenv("BXFI_MAP")) {
cr_panic("Re-entering criterion from a test worker. This is a "
"catastrophic bug, please report it on the issue tracker.\n"
"Bailing out to avoid fork-bombing the system.");
}
init_i18n();
cri_diff_init();
#ifndef ENABLE_VALGRIND_ERRORS
VALGRIND_DISABLE_ERROR_REPORTING;
#endif
if (RUNNING_ON_VALGRIND) {
criterion_options.no_early_exit = 1;
criterion_options.jobs = 1;
}
criterion_register_output_provider("tap", tap_report);
criterion_register_output_provider("xml", xml_report);
criterion_register_output_provider("json", json_report);
setup_parent_job();
return criterion_init();
}
CR_API void criterion_finalize(struct criterion_test_set *set)
{
sfree(set);
#ifndef ENABLE_VALGRIND_ERRORS
VALGRIND_ENABLE_ERROR_REPORTING;
#endif
criterion_free_output();
}
static struct client_ctx *spawn_next_client(struct server_ctx *sctx, ccrContext *ctx)
{
struct client_ctx new_ctx;
bxf_instance *instance = cri_run_next_test(NULL, NULL, NULL, &new_ctx, ctx);
if (!instance)
return NULL;
return add_client_from_worker(sctx, &new_ctx, instance);
}
static void run_tests_async(struct criterion_test_set *set,
struct criterion_global_stats *stats,
const char *url,
int socket)
{
ccrContext ctx = 0;
size_t nb_workers = DEF(criterion_options.jobs, get_processor_count());
size_t active_workers = 0;
int has_msg = 0;
struct server_ctx sctx;
init_server_context(&sctx, stats);
sctx.socket = socket;
/* initialization of coroutine */
cri_run_next_test(set, stats, url, NULL, &ctx);
for (size_t i = 0; i < nb_workers; ++i) {
struct client_ctx *cctx = spawn_next_client(&sctx, &ctx);
if (!cctx)
break;
++active_workers;
}
if (!active_workers && !criterion_options.wait_for_clients)
goto cleanup;
criterion_protocol_msg msg = criterion_protocol_msg_init_zero;
while ((has_msg = read_message(socket, &msg)) == 1) {
struct client_ctx *cctx = process_client_message(&sctx, &msg);
/* drop invalid messages */
if (!cctx)
continue;
if (!cctx->alive) {
if ((cctx->tstats->test_status == CR_STATUS_FAILED) && criterion_options.fail_fast)
cr_terminate(cctx->gstats);
if (cctx->kind == WORKER) {
remove_client_by_pid(&sctx, cctx->instance->pid);
cctx = spawn_next_client(&sctx, &ctx);
if (cctx == NULL)
--active_workers;
}
}
if (!active_workers && !criterion_options.wait_for_clients)
break;
free_message(&msg);
}
cleanup:
if (has_msg)
free_message(&msg);
destroy_server_context(&sctx);
ccrAbort(ctx);
}
static int criterion_run_all_tests_impl(struct criterion_test_set *set)
{
cri_report_init();
if (RUNNING_ON_VALGRIND) {
if (criterion_options.jobs != 1)
criterion_pimportant(CRITERION_PREFIX_DASHES,
_(msg_valgrind_jobs), CR_FG_BOLD, CR_RESET);
}
char url[sizeof ("ipc://" NN_SOCKET_PATH "criterion_.sock") + 21];
snprintf(url, sizeof (url), "ipc://" NN_SOCKET_PATH "criterion_%llu.sock", get_process_id());
int sock = cri_proto_bind(url);
if (sock < 0)
cr_panic("Could not initialize the message server: %s.", strerror(errno));
g_client_socket = cri_proto_connect(url);
if (g_client_socket < 0)
cr_panic("Could not initialize the message client: %s.", strerror(errno));
cri_alloc_init();
report(PRE_ALL, set);
log(pre_all, set);
struct criterion_global_stats *stats = stats_init();
run_tests_async(set, stats, url, sock);
report(POST_ALL, stats);
if (criterion_options.logging_threshold == CRITERION_LOG_LEVEL_QUIET) {
cri_restore_outputs();
}
process_all_output(stats);
log(post_all, stats);
cri_alloc_term();
cri_report_term();
cri_proto_close(g_client_socket);
cri_proto_close(sock);
int ok = stats->tests_failed == 0 && stats->errors == 0;
if (!criterion_options.ignore_warnings)
ok = ok && stats->warnings == 0;
sfree(stats);
return ok;
}
CR_API int criterion_run_all_tests(struct criterion_test_set *set)
{
/* make sure we don't re-enter from a test worker. See #247. */
if (getenv("BXFI_MAP")) {
cr_panic("Re-entering criterion from a test worker. This is a "
"catastrophic bug, please report it on the issue tracker.\n"
"Bailing out to avoid fork-bombing the system.");
}
#ifndef ENABLE_VALGRIND_ERRORS
VALGRIND_DISABLE_ERROR_REPORTING;
#endif
cri_is_runner = 1;
if (criterion_options.pattern)
disable_unmatching(set);
if (criterion_options.debug) {
criterion_options.jobs = 1;
criterion_options.crash = true;
criterion_options.logging_threshold = 1;
}
if (criterion_options.logging_threshold == CRITERION_LOG_LEVEL_QUIET) {
cri_silence_outputs();
}
int res = criterion_run_all_tests_impl(set);
#ifndef ENABLE_VALGRIND_ERRORS
VALGRIND_ENABLE_ERROR_REPORTING;
#endif
return criterion_options.always_succeed || res;
}
|