static void check(Manager *m, Unit *unit, int status_expected, int code_expected) { Service *service = NULL; usec_t ts; usec_t timeout = 2 * USEC_PER_SEC; assert_se(m); assert_se(unit); service = SERVICE(unit); printf("%s\n", unit->id); exec_context_dump(&service->exec_context, stdout, "\t"); ts = now(CLOCK_MONOTONIC); while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) { int r; usec_t n; r = sd_event_run(m->event, 100 * USEC_PER_MSEC); assert_se(r >= 0); n = now(CLOCK_MONOTONIC); if (ts + timeout < n) { log_error("Test timeout when testing %s", unit->id); exit(EXIT_FAILURE); } } exec_status_dump(&service->main_exec_status, stdout, "\t"); assert_se(service->main_exec_status.status == status_expected); assert_se(service->main_exec_status.code == code_expected); }
static void test_config_parse_log_extra_fields(void) { /* int config_parse_log_extra_fields( const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) */ int r; _cleanup_(manager_freep) Manager *m = NULL; _cleanup_(unit_freep) Unit *u = NULL; ExecContext c = {}; r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m); if (MANAGER_SKIP_TEST(r)) { log_notice_errno(r, "Skipping test: manager_new: %m"); return; } assert_se(r >= 0); assert_se(manager_startup(m, NULL, NULL) >= 0); assert_se(u = unit_new(m, sizeof(Service))); log_info("/* %s – basic test */", __func__); r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"", &c, u); assert_se(r >= 0); assert_se(c.n_log_extra_fields == 2); assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len)); assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len)); log_info("/* %s – add some */", __func__); r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '", &c, u); assert_se(r >= 0); assert_se(c.n_log_extra_fields == 4); assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len)); assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len)); assert_se(strneq(c.log_extra_fields[2].iov_base, "FOO2=BAR2", c.log_extra_fields[2].iov_len)); assert_se(strneq(c.log_extra_fields[3].iov_base, "QOOF2=quux", c.log_extra_fields[3].iov_len)); exec_context_dump(&c, stdout, " --> "); log_info("/* %s – reset */", __func__); r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, "LValue", 0, "", &c, u); assert_se(r >= 0); assert_se(c.n_log_extra_fields == 0); exec_context_free_log_extra_fields(&c); log_info("/* %s – bye */", __func__); }