test-login: skip consistency checks when logind is not active

There are two ways in swich sd_login_* functions acquire data:
some are derived from the cgroup path, but others use the data serialized
by logind.

When the tests are executed under Fedora's mock, without systemd-spawn
but instead in a traditional chroot, test-login gets confused:
the "outside" cgroup path is visible, so sd_pid_get_unit() and
sd_pid_get_session() work, but sd_session_is_active() and other functions
that need logind data fail.

Such a buildroot setup is fairly bad, but it can be encountered in the wild, so
let's just skip the tests in that case.

/* Information printed is from the live system */
sd_pid_get_unit(0, …) → "session-237.scope"
sd_pid_get_user_unit(0, …) → "n/a"
sd_pid_get_slice(0, …) → "user-1000.slice"
sd_pid_get_session(0, …) → "237"
sd_pid_get_owner_uid(0, …) → 1000
sd_pid_get_cgroup(0, …) → "/user.slice/user-1000.slice/session-237.scope"
sd_uid_get_display(1000, …) → "(null)"
sd_uid_get_sessions(1000, …) → [0] ""
sd_uid_get_seats(1000, …) → [0] ""
Assertion 'r >= 0' failed at src/libsystemd/sd-login/test-login.c:104, function test_login(). Aborting.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-12-16 15:56:44 +01:00
parent 221019166f
commit ac5644635d
1 changed files with 52 additions and 46 deletions

View File

@ -112,6 +112,11 @@ static void test_login(void) {
if (session) {
r = sd_session_is_active(session);
if (r == -ENXIO)
log_notice("sd_session_is_active() failed with ENXIO, it seems logind is not running.");
else {
/* All those tests will fail with ENXIO, so let's skip them. */
assert_se(r >= 0);
log_info("sd_session_is_active(\"%s\") → %s", session, yes_no(r));
@ -175,6 +180,7 @@ static void test_login(void) {
assert_se(sd_uid_get_state(u, &state2) == 0);
log_info("sd_uid_get_state("UID_FMT", …) → %s", u, state2);
}
}
if (seat) {
_cleanup_free_ char *session2 = NULL, *buf = NULL;
@ -214,7 +220,7 @@ static void test_login(void) {
assert_se(sd_get_seats(NULL) == r);
r = sd_seat_get_active(NULL, &t, NULL);
assert_se(IN_SET(r, 0, -ENODATA));
assert_se(IN_SET(r, 0, -ENODATA, -ENXIO));
log_info("sd_seat_get_active(NULL, …) (active session on current seat) → %s / \"%s\"", e(r), strnull(t));
free(t);