core: clean up test run flags

Let's make them typesafe, and let's add a nice macro helper for checking
if we are in a test run, which should make testing for this much easier
to read for most cases.
This commit is contained in:
Lennart Poettering 2018-10-09 16:15:54 +02:00
parent c52b19d65f
commit 638cece45d
5 changed files with 29 additions and 25 deletions

View File

@ -225,9 +225,10 @@ static int verify_unit(Unit *u, bool check_man) {
}
int verify_units(char **filenames, UnitFileScope scope, bool check_man, bool run_generators) {
const uint8_t flags = MANAGER_TEST_RUN_BASIC |
MANAGER_TEST_RUN_ENV_GENERATORS |
run_generators * MANAGER_TEST_RUN_GENERATORS;
const ManagerTestRunFlags flags =
MANAGER_TEST_RUN_BASIC |
MANAGER_TEST_RUN_ENV_GENERATORS |
run_generators * MANAGER_TEST_RUN_GENERATORS;
_cleanup_(manager_freep) Manager *m = NULL;
Unit *units[strv_length(filenames)];

View File

@ -2305,7 +2305,7 @@ int manager_setup_cgroup(Manager *m) {
(void) sd_event_source_set_description(m->cgroup_inotify_event_source, "cgroup-inotify");
} else if (MANAGER_IS_SYSTEM(m) && m->test_run_flags == 0) {
} else if (MANAGER_IS_SYSTEM(m) && !MANAGER_IS_TEST_RUN(m)) {
/* On the legacy hierarchy we only get notifications via cgroup agents. (Which isn't really reliable,
* since it does not generate events when control groups with children run empty. */
@ -2334,11 +2334,11 @@ int manager_setup_cgroup(Manager *m) {
if (m->pin_cgroupfs_fd < 0)
return log_error_errno(errno, "Failed to open pin file: %m");
} else if (!m->test_run_flags)
} else if (!MANAGER_IS_TEST_RUN(m))
return log_error_errno(r, "Failed to create %s control group: %m", scope_path);
/* 7. Always enable hierarchical support if it exists... */
if (!all_unified && m->test_run_flags == 0)
if (!all_unified && !MANAGER_IS_TEST_RUN(m))
(void) cg_set_attribute("memory", "/", "memory.use_hierarchy", "1");
/* 8. Figure out which controllers are supported, and log about it */

View File

@ -351,7 +351,7 @@ static int manager_setup_time_change(Manager *m) {
assert(m);
if (m->test_run_flags)
if (MANAGER_IS_TEST_RUN(m))
return 0;
m->time_change_event_source = sd_event_source_unref(m->time_change_event_source);
@ -407,7 +407,7 @@ static int manager_setup_timezone_change(Manager *m) {
assert(m);
if (m->test_run_flags != 0)
if (MANAGER_IS_TEST_RUN(m))
return 0;
/* We watch /etc/localtime for three events: change of the link count (which might mean removal from /etc even
@ -446,7 +446,7 @@ static int enable_special_signals(Manager *m) {
assert(m);
if (m->test_run_flags)
if (MANAGER_IS_TEST_RUN(m))
return 0;
/* Enable that we get SIGINT on control-alt-del. In containers
@ -711,7 +711,7 @@ static int manager_setup_sigchld_event_source(Manager *m) {
return 0;
}
int manager_new(UnitFileScope scope, unsigned test_run_flags, Manager **_m) {
int manager_new(UnitFileScope scope, ManagerTestRunFlags test_run_flags, Manager **_m) {
_cleanup_(manager_freep) Manager *m = NULL;
int r;
@ -865,7 +865,7 @@ int manager_new(UnitFileScope scope, unsigned test_run_flags, Manager **_m) {
static int manager_setup_notify(Manager *m) {
int r;
if (m->test_run_flags)
if (MANAGER_IS_TEST_RUN(m))
return 0;
if (m->notify_fd < 0) {
@ -944,7 +944,7 @@ static int manager_setup_cgroups_agent(Manager *m) {
* to it. The system instance hence listens on this special socket, but the user instances listen on the system
* bus for these messages. */
if (m->test_run_flags)
if (MANAGER_IS_TEST_RUN(m))
return 0;
if (!MANAGER_IS_SYSTEM(m))
@ -1521,7 +1521,7 @@ static bool manager_dbus_is_running(Manager *m, bool deserialized) {
* and the service unit. If the 'deserialized' parameter is true we'll check the deserialized state of the unit
* rather than the current one. */
if (m->test_run_flags != 0)
if (MANAGER_IS_TEST_RUN(m))
return false;
u = manager_get_unit(m, SPECIAL_DBUS_SOCKET);
@ -1569,7 +1569,7 @@ static void manager_preset_all(Manager *m) {
if (!MANAGER_IS_SYSTEM(m))
return;
if (m->test_run_flags != 0)
if (MANAGER_IS_TEST_RUN(m))
return;
/* If this is the first boot, and we are in the host system, then preset everything */
@ -1589,7 +1589,7 @@ int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
/* If we are running in test mode, we still want to run the generators,
* but we should not touch the real generator directories. */
r = lookup_paths_init(&m->lookup_paths, m->unit_file_scope,
m->test_run_flags ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0,
MANAGER_IS_TEST_RUN(m) ? LOOKUP_PATHS_TEMPORARY_GENERATED : 0,
NULL);
if (r < 0)
return r;
@ -3593,7 +3593,7 @@ static void manager_notify_finished(Manager *m) {
char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
usec_t firmware_usec, loader_usec, kernel_usec, initrd_usec, userspace_usec, total_usec;
if (m->test_run_flags)
if (MANAGER_IS_TEST_RUN(m))
return;
if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0) {
@ -3788,7 +3788,7 @@ static int manager_run_environment_generators(Manager *m) {
const char **paths;
void* args[] = {&tmp, &tmp, &m->environment};
if (m->test_run_flags && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS))
if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_ENV_GENERATORS))
return 0;
paths = MANAGER_IS_SYSTEM(m) ? system_env_generator_binary_paths : user_env_generator_binary_paths;
@ -3806,7 +3806,7 @@ static int manager_run_generators(Manager *m) {
assert(m);
if (m->test_run_flags && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS))
if (MANAGER_IS_TEST_RUN(m) && !(m->test_run_flags & MANAGER_TEST_RUN_GENERATORS))
return 0;
paths = generator_binary_paths(m->unit_file_scope);
@ -3920,7 +3920,7 @@ static bool manager_journal_is_running(Manager *m) {
assert(m);
if (m->test_run_flags != 0)
if (MANAGER_IS_TEST_RUN(m))
return false;
/* If we are the user manager we can safely assume that the journal is up */

View File

@ -108,14 +108,15 @@ typedef enum ManagerTimestamp {
#include "show-status.h"
#include "unit-name.h"
enum {
/* 0 = run normally */
typedef enum ManagerTestRunFlags {
MANAGER_TEST_NORMAL = 0, /* run normally */
MANAGER_TEST_RUN_MINIMAL = 1 << 0, /* create basic data structures */
MANAGER_TEST_RUN_BASIC = 1 << 1, /* interact with the environment */
MANAGER_TEST_RUN_ENV_GENERATORS = 1 << 2, /* also run env generators */
MANAGER_TEST_RUN_GENERATORS = 1 << 3, /* also run unit generators */
MANAGER_TEST_FULL = MANAGER_TEST_RUN_BASIC | MANAGER_TEST_RUN_ENV_GENERATORS | MANAGER_TEST_RUN_GENERATORS,
};
} ManagerTestRunFlags;
assert_cc((MANAGER_TEST_FULL & UINT8_MAX) == MANAGER_TEST_FULL);
struct Manager {
@ -299,7 +300,7 @@ struct Manager {
/* Have we ever changed the "kernel.pid_max" sysctl? */
bool sysctl_pid_max_changed:1;
unsigned test_run_flags:8;
ManagerTestRunFlags test_run_flags:8;
/* If non-zero, exit with the following value when the systemd
* process terminate. Useful for containers: systemd-nspawn could get
@ -410,7 +411,9 @@ struct Manager {
/* The objective is set to OK as soon as we enter the main loop, and set otherwise as soon as we are done with it */
#define MANAGER_IS_RUNNING(m) ((m)->objective == MANAGER_OK)
int manager_new(UnitFileScope scope, unsigned test_run_flags, Manager **m);
#define MANAGER_IS_TEST_RUN(m) ((m)->test_run_flags != 0)
int manager_new(UnitFileScope scope, ManagerTestRunFlags test_run_flags, Manager **m);
Manager* manager_free(Manager *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);

View File

@ -5283,7 +5283,7 @@ void unit_export_state_files(Unit *u) {
if (!MANAGER_IS_SYSTEM(u->manager))
return;
if (u->manager->test_run_flags != 0)
if (MANAGER_IS_TEST_RUN(u->manager))
return;
/* Exports a couple of unit properties to /run/systemd/units/, so that journald can quickly query this data