From: Zbigniew Jędrzejewski-Szmek Date: Sat, 28 Jun 2025 18:12:32 +0000 (+0200) Subject: tests: use new ASSERT_OK_OR macro X-Git-Tag: v258-rc1~206^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ea64d55080353c263998e8bd28a74ffa01b22393;p=thirdparty%2Fsystemd.git tests: use new ASSERT_OK_OR macro --- diff --git a/src/test/test-audit-util.c b/src/test/test-audit-util.c index af2813d6170..61521f33978 100644 --- a/src/test/test-audit-util.c +++ b/src/test/test-audit-util.c @@ -13,18 +13,14 @@ TEST(audit_loginuid_from_pid) { ASSERT_OK(pidref_set_pid(&pid1, 1)); uid_t uid; - r = audit_loginuid_from_pid(&self, &uid); - if (r != -ENODATA) - ASSERT_OK(r); + ASSERT_OK_OR(r = audit_loginuid_from_pid(&self, &uid), -ENODATA); if (r >= 0) log_info("self audit login uid: " UID_FMT, uid); ASSERT_ERROR(audit_loginuid_from_pid(&pid1, &uid), ENODATA); uint32_t sessionid; - r = audit_session_from_pid(&self, &sessionid); - if (r != -ENODATA) - ASSERT_OK(r); + ASSERT_OK_OR(r = audit_session_from_pid(&self, &sessionid), -ENODATA); if (r >= 0) log_info("self audit session id: %" PRIu32, sessionid); diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 0512532d0bf..8debc215271 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -232,28 +232,21 @@ TEST(proc, .sd_booted = true) { continue; r = cg_pid_get_path_shifted(pid.pid, NULL, &path_shifted); - if (r != -ESRCH) - ASSERT_OK(r); + ASSERT_OK_OR(r, -ESRCH); r = cg_pidref_get_unit(&pid, &unit); - if (r != -ESRCH) - ASSERT_OK(r); + ASSERT_OK_OR(r, -ESRCH); r = cg_pid_get_slice(pid.pid, &slice); - if (r != -ESRCH) - ASSERT_OK(r); + ASSERT_OK_OR(r, -ESRCH); /* Not all processes belong to a specific user or a machine */ r = cg_pidref_get_owner_uid(&pid, &uid); - if (!IN_SET(r, -ESRCH, -ENXIO)) - ASSERT_OK(r); + ASSERT_OK_OR(r, -ESRCH, -ENXIO); r = cg_pidref_get_session(&pid, &session); - if (!IN_SET(r, -ESRCH, -ENXIO)) - ASSERT_OK(r); + ASSERT_OK_OR(r, -ESRCH, -ENXIO); r = cg_pid_get_user_unit(pid.pid, &user_unit); - if (!IN_SET(r, -ESRCH, -ENXIO)) - ASSERT_OK(r); + ASSERT_OK_OR(r, -ESRCH, -ENXIO); r = cg_pid_get_machine_name(pid.pid, &machine); - if (!IN_SET(r, -ESRCH, -ENOENT)) - ASSERT_OK(r); + ASSERT_OK_OR(r, -ESRCH, -ENOENT); log_debug(PID_FMT": %s, %s, "UID_FMT", %s, %s, %s, %s, %s", pid.pid, diff --git a/src/test/test-execute.c b/src/test/test-execute.c index 621bbd49900..23c1fe2eedb 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -223,9 +223,7 @@ static void start_parent_slices(Unit *unit) { slice = UNIT_GET_SLICE(unit); if (slice) { start_parent_slices(slice); - int r = unit_start(slice, NULL); - if (r != -EALREADY) - ASSERT_OK(r); + ASSERT_OK_OR(unit_start(slice, NULL), -EALREADY); } } @@ -1472,7 +1470,6 @@ static int prepare_ns(const char *process_name) { ASSERT_OK(r); if (r == 0) { _cleanup_free_ char *unit_dir = NULL, *build_dir = NULL, *build_dir_mount = NULL; - int ret; const char *coverage = getenv("COVERAGE_BUILD_DIR"); if (!coverage) @@ -1494,9 +1491,7 @@ static int prepare_ns(const char *process_name) { ASSERT_OK(copy_directory_at(AT_FDCWD, unit_dir, AT_FDCWD, PRIVATE_UNIT_DIR, COPY_MERGE_EMPTY)); /* Mount tmpfs on the following directories to make not StateDirectory= or friends disturb the host. */ - ret = get_build_exec_dir(&build_dir); - if (ret != -ENOEXEC) - ASSERT_OK(ret); + ASSERT_OK_OR(get_build_exec_dir(&build_dir), -ENOEXEC); if (build_dir) { /* Account for a build directory being in one of the soon-to-be-tmpfs directories. If we @@ -1512,11 +1507,11 @@ static int prepare_ns(const char *process_name) { ASSERT_OK(mount_nofollow_verbose(LOG_DEBUG, "tmpfs", p, "tmpfs", MS_NOSUID|MS_NODEV, NULL)); if (build_dir_mount) { - ret = RET_NERRNO(access(build_dir, F_OK)); - if (ret != -ENOENT) - ASSERT_OK(ret); + int k; - if (ret == -ENOENT) { + ASSERT_OK_OR(k = RET_NERRNO(access(build_dir, F_OK)), -ENOENT); + + if (k == -ENOENT) { /* The build directory got overmounted by tmpfs, so let's use the "backup" bind mount to * bring it back. */ ASSERT_OK(mkdir_p(build_dir, 0755)); diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 376b20b0484..c0095a749c9 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -86,9 +86,7 @@ static void test_pid_get_comm_one(pid_t pid) { ASSERT_TRUE(pid_is_kernel_thread(pid) == 0 || pid != 1); - r = get_process_exe(pid, &f); - if (r != -EACCES) - ASSERT_OK(r); + ASSERT_OK_OR(get_process_exe(pid, &f), -EACCES); log_info("PID"PID_FMT" exe: '%s'", pid, strna(f)); ASSERT_OK_ZERO(pid_get_uid(pid, &u)); @@ -97,9 +95,7 @@ static void test_pid_get_comm_one(pid_t pid) { ASSERT_OK_ZERO(get_process_gid(pid, &g)); log_info("PID"PID_FMT" GID: "GID_FMT, pid, g); - r = get_process_environ(pid, &env); - if (r != -EACCES) - ASSERT_OK(r); + ASSERT_OK_OR(get_process_environ(pid, &env), -EACCES); log_info("PID"PID_FMT" strlen(environ): %zi", pid, env ? (ssize_t)strlen(env) : (ssize_t)-errno); if (!detect_container() && pid == 1) @@ -166,9 +162,7 @@ TEST(pid_get_cmdline) { for (;;) { pid_t pid; - - r = proc_dir_read(d, &pid); - ASSERT_OK(r); + ASSERT_OK(r = proc_dir_read(d, &pid)); if (r == 0) /* EOF */ break;