test-mempress: Migrate to new assertion macros

This commit is contained in:
Daan De Meyer
2026-04-09 22:47:10 +02:00
committed by Daan De Meyer
parent f08796065d
commit 1121c77526
+82 -83
View File
@@ -39,16 +39,16 @@ static void *fake_pressure_thread(void *p) {
usleep_safe(150);
assert_se(write(c->fifo_fd, &(const char) { 'x' }, 1) == 1);
ASSERT_EQ(write(c->fifo_fd, &(const char) { 'x' }, 1), 1);
usleep_safe(150);
cfd = accept4(c->socket_fd, NULL, NULL, SOCK_CLOEXEC);
assert_se(cfd >= 0);
ASSERT_OK_ERRNO(cfd);
char buf[STRLEN("hello")+1] = {};
assert_se(read(cfd, buf, sizeof(buf)-1) == sizeof(buf)-1);
ASSERT_EQ(read(cfd, buf, sizeof(buf)-1), (ssize_t) (sizeof(buf)-1));
ASSERT_STREQ(buf, "hello");
assert_se(write(cfd, &(const char) { 'z' }, 1) == 1);
ASSERT_EQ(write(cfd, &(const char) { 'z' }, 1), 1);
return NULL;
}
@@ -57,15 +57,15 @@ static int fake_pressure_callback(sd_event_source *s, void *userdata) {
int *value = userdata;
const char *d;
assert_se(s);
assert_se(sd_event_source_get_description(s, &d) >= 0);
ASSERT_NOT_NULL(s);
ASSERT_OK(sd_event_source_get_description(s, &d));
*value *= d[0];
log_notice("memory pressure event: %s", d);
if (*value == 7 * 'f' * 's')
assert_se(sd_event_exit(sd_event_source_get_event(s), 0) >= 0);
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 0));
return 0;
}
@@ -73,57 +73,56 @@ static int fake_pressure_callback(sd_event_source *s, void *userdata) {
TEST(fake_pressure) {
_cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL, *ef = NULL;
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
_cleanup_free_ char *j = NULL, *k = NULL;
_cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
_cleanup_close_ int fifo_fd = -EBADF, socket_fd = -EBADF;
union sockaddr_union sa;
pthread_t th;
int value = 7;
assert_se(sd_event_default(&e) >= 0);
ASSERT_OK(sd_event_default(&e));
assert_se(mkdtemp_malloc(NULL, &tmp) >= 0);
ASSERT_OK(mkdtemp_malloc(NULL, &tmp));
assert_se(j = path_join(tmp, "fifo"));
assert_se(mkfifo(j, 0600) >= 0);
_cleanup_free_ char *j = ASSERT_NOT_NULL(path_join(tmp, "fifo"));
ASSERT_OK_ERRNO(mkfifo(j, 0600));
fifo_fd = open(j, O_CLOEXEC|O_RDWR|O_NONBLOCK);
assert_se(fifo_fd >= 0);
ASSERT_OK_ERRNO(fifo_fd);
assert_se(k = path_join(tmp, "sock"));
_cleanup_free_ char *k = ASSERT_NOT_NULL(path_join(tmp, "sock"));
socket_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
assert_se(socket_fd >= 0);
assert_se(sockaddr_un_set_path(&sa.un, k) >= 0);
assert_se(bind(socket_fd, &sa.sa, sockaddr_un_len(&sa.un)) >= 0);
assert_se(listen(socket_fd, 1) >= 0);
ASSERT_OK_ERRNO(socket_fd);
ASSERT_OK(sockaddr_un_set_path(&sa.un, k));
ASSERT_OK_ERRNO(bind(socket_fd, &sa.sa, sockaddr_un_len(&sa.un)));
ASSERT_OK_ERRNO(listen(socket_fd, 1));
/* Ideally we'd just allocate this on the stack, but AddressSanitizer doesn't like it if threads
* access each other's stack */
struct fake_pressure_context *fp = new(struct fake_pressure_context, 1);
assert_se(fp);
ASSERT_NOT_NULL(fp);
*fp = (struct fake_pressure_context) {
.fifo_fd = fifo_fd,
.socket_fd = socket_fd,
};
assert_se(pthread_create(&th, NULL, fake_pressure_thread, TAKE_PTR(fp)) == 0);
ASSERT_EQ(pthread_create(&th, NULL, fake_pressure_thread, TAKE_PTR(fp)), 0);
assert_se(setenv("MEMORY_PRESSURE_WATCH", j, /* override= */ true) >= 0);
assert_se(unsetenv("MEMORY_PRESSURE_WRITE") >= 0);
ASSERT_OK_ERRNO(setenv("MEMORY_PRESSURE_WATCH", j, /* override= */ true));
ASSERT_OK_ERRNO(unsetenv("MEMORY_PRESSURE_WRITE"));
assert_se(sd_event_add_memory_pressure(e, &es, fake_pressure_callback, &value) >= 0);
assert_se(sd_event_source_set_description(es, "fifo event source") >= 0);
ASSERT_OK(sd_event_add_memory_pressure(e, &es, fake_pressure_callback, &value));
ASSERT_OK(sd_event_source_set_description(es, "fifo event source"));
assert_se(setenv("MEMORY_PRESSURE_WATCH", k, /* override= */ true) >= 0);
assert_se(setenv("MEMORY_PRESSURE_WRITE", "aGVsbG8K", /* override= */ true) >= 0);
ASSERT_OK_ERRNO(setenv("MEMORY_PRESSURE_WATCH", k, /* override= */ true));
ASSERT_OK_ERRNO(setenv("MEMORY_PRESSURE_WRITE", "aGVsbG8K", /* override= */ true));
assert_se(sd_event_add_memory_pressure(e, &ef, fake_pressure_callback, &value) >= 0);
assert_se(sd_event_source_set_description(ef, "socket event source") >= 0);
ASSERT_OK(sd_event_add_memory_pressure(e, &ef, fake_pressure_callback, &value));
ASSERT_OK(sd_event_source_set_description(ef, "socket event source"));
assert_se(sd_event_loop(e) >= 0);
ASSERT_OK(sd_event_loop(e));
assert_se(value == 7 * 'f' * 's');
ASSERT_EQ(value, 7 * 'f' * 's');
assert_se(pthread_join(th, NULL) == 0);
ASSERT_EQ(pthread_join(th, NULL), 0);
}
struct real_pressure_context {
@@ -134,15 +133,15 @@ static int real_pressure_callback(sd_event_source *s, void *userdata) {
struct real_pressure_context *c = ASSERT_PTR(userdata);
const char *d;
assert_se(s);
assert_se(sd_event_source_get_description(s, &d) >= 0);
ASSERT_NOT_NULL(s);
ASSERT_OK(sd_event_source_get_description(s, &d));
log_notice("real_memory pressure event: %s", d);
sd_event_trim_memory();
assert_se(c->pid);
assert_se(sd_event_source_send_child_signal(c->pid, SIGKILL, NULL, 0) >= 0);
ASSERT_NOT_NULL(c->pid);
ASSERT_OK(sd_event_source_send_child_signal(c->pid, SIGKILL, NULL, 0));
c->pid = NULL;
return 0;
@@ -156,13 +155,13 @@ _noreturn_ static void real_pressure_eat_memory(int pipe_fd) {
/* Allocates and touches 10M at a time, until runs out of memory */
char x;
assert_se(read(pipe_fd, &x, 1) == 1); /* Wait for the GO! */
ASSERT_EQ(read(pipe_fd, &x, 1), 1); /* Wait for the GO! */
for (;;) {
void *p;
p = mmap(NULL, MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
assert_se(p != MAP_FAILED);
ASSERT_TRUE(p != MAP_FAILED);
log_info("Eating another %s.", FORMAT_BYTES(MMAP_SIZE));
@@ -176,16 +175,16 @@ _noreturn_ static void real_pressure_eat_memory(int pipe_fd) {
}
static int real_pressure_child_callback(sd_event_source *s, const siginfo_t *si, void *userdata) {
assert_se(s);
assert_se(si);
ASSERT_NOT_NULL(s);
ASSERT_NOT_NULL(si);
log_notice("child dead");
assert_se(si->si_signo == SIGCHLD);
assert_se(si->si_status == SIGKILL);
assert_se(si->si_code == CLD_KILLED);
ASSERT_EQ(si->si_signo, SIGCHLD);
ASSERT_EQ(si->si_status, SIGKILL);
ASSERT_EQ(si->si_code, CLD_KILLED);
assert_se(sd_event_exit(sd_event_source_get_event(s), 31) >= 0);
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 31));
return 0;
}
@@ -205,42 +204,42 @@ TEST(real_pressure) {
if (r < 0)
return (void) log_tests_skipped_errno(r, "can't connect to system bus");
assert_se(bus_wait_for_jobs_new(bus, &w) >= 0);
ASSERT_OK(bus_wait_for_jobs_new(bus, &w));
assert_se(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit") >= 0);
assert_se(asprintf(&scope, "test-%" PRIu64 ".scope", random_u64()) >= 0);
assert_se(sd_bus_message_append(m, "ss", scope, "fail") >= 0);
assert_se(sd_bus_message_open_container(m, 'a', "(sv)") >= 0);
assert_se(sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, 0) >= 0);
assert_se(sd_bus_message_append(m, "(sv)", "MemoryAccounting", "b", true) >= 0);
assert_se(sd_bus_message_close_container(m) >= 0);
assert_se(sd_bus_message_append(m, "a(sa(sv))", 0) >= 0);
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit"));
ASSERT_OK(asprintf(&scope, "test-%" PRIu64 ".scope", random_u64()));
ASSERT_OK(sd_bus_message_append(m, "ss", scope, "fail"));
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)"));
ASSERT_OK(sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, 0));
ASSERT_OK(sd_bus_message_append(m, "(sv)", "MemoryAccounting", "b", true));
ASSERT_OK(sd_bus_message_close_container(m));
ASSERT_OK(sd_bus_message_append(m, "a(sa(sv))", 0));
r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0)
return (void) log_tests_skipped_errno(r, "can't issue transient unit call");
assert_se(sd_bus_message_read(reply, "o", &object) >= 0);
ASSERT_OK(sd_bus_message_read(reply, "o", &object));
assert_se(bus_wait_for_jobs_one(w, object, /* flags= */ BUS_WAIT_JOBS_LOG_ERROR, /* extra_args= */ NULL) >= 0);
ASSERT_OK(bus_wait_for_jobs_one(w, object, /* flags= */ BUS_WAIT_JOBS_LOG_ERROR, /* extra_args= */ NULL));
assert_se(sd_event_default(&e) >= 0);
ASSERT_OK(sd_event_default(&e));
assert_se(pipe2(pipe_fd, O_CLOEXEC) >= 0);
ASSERT_OK_ERRNO(pipe2(pipe_fd, O_CLOEXEC));
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
r = pidref_safe_fork("(eat-memory)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM, &pidref);
assert_se(r >= 0);
ASSERT_OK(r);
if (r == 0) {
real_pressure_eat_memory(pipe_fd[0]);
_exit(EXIT_SUCCESS);
}
assert_se(event_add_child_pidref(e, &cs, &pidref, WEXITED, real_pressure_child_callback, NULL) >= 0);
assert_se(sd_event_source_set_child_process_own(cs, true) >= 0);
ASSERT_OK(event_add_child_pidref(e, &cs, &pidref, WEXITED, real_pressure_child_callback, NULL));
ASSERT_OK(sd_event_source_set_child_process_own(cs, true));
assert_se(unsetenv("MEMORY_PRESSURE_WATCH") >= 0);
assert_se(unsetenv("MEMORY_PRESSURE_WRITE") >= 0);
ASSERT_OK_ERRNO(unsetenv("MEMORY_PRESSURE_WATCH"));
ASSERT_OK_ERRNO(unsetenv("MEMORY_PRESSURE_WRITE"));
struct real_pressure_context context = {
.pid = cs,
@@ -250,21 +249,21 @@ TEST(real_pressure) {
if (r < 0)
return (void) log_tests_skipped_errno(r, "can't allocate memory pressure fd");
assert_se(sd_event_source_set_description(es, "real pressure event source") >= 0);
assert_se(sd_event_source_set_memory_pressure_type(es, "some") == 0);
assert_se(sd_event_source_set_memory_pressure_type(es, "full") > 0);
assert_se(sd_event_source_set_memory_pressure_type(es, "full") == 0);
assert_se(sd_event_source_set_memory_pressure_type(es, "some") > 0);
assert_se(sd_event_source_set_memory_pressure_type(es, "some") == 0);
assert_se(sd_event_source_set_memory_pressure_period(es, 70 * USEC_PER_MSEC, USEC_PER_SEC) > 0);
assert_se(sd_event_source_set_memory_pressure_period(es, 70 * USEC_PER_MSEC, USEC_PER_SEC) == 0);
assert_se(sd_event_source_set_enabled(es, SD_EVENT_ONESHOT) >= 0);
ASSERT_OK(sd_event_source_set_description(es, "real pressure event source"));
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_type(es, "some"));
ASSERT_OK_POSITIVE(sd_event_source_set_memory_pressure_type(es, "full"));
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_type(es, "full"));
ASSERT_OK_POSITIVE(sd_event_source_set_memory_pressure_type(es, "some"));
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_type(es, "some"));
ASSERT_OK_POSITIVE(sd_event_source_set_memory_pressure_period(es, 70 * USEC_PER_MSEC, USEC_PER_SEC));
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_period(es, 70 * USEC_PER_MSEC, USEC_PER_SEC));
ASSERT_OK(sd_event_source_set_enabled(es, SD_EVENT_ONESHOT));
_cleanup_free_ char *uo = NULL;
assert_se(uo = unit_dbus_path_from_name(scope));
ASSERT_NOT_NULL(uo = unit_dbus_path_from_name(scope));
uint64_t mcurrent = UINT64_MAX;
assert_se(sd_bus_get_property_trivial(bus, "org.freedesktop.systemd1", uo, "org.freedesktop.systemd1.Scope", "MemoryCurrent", &error, 't', &mcurrent) >= 0);
ASSERT_OK(sd_bus_get_property_trivial(bus, "org.freedesktop.systemd1", uo, "org.freedesktop.systemd1.Scope", "MemoryCurrent", &error, 't', &mcurrent));
printf("current: %" PRIu64 "\n", mcurrent);
if (mcurrent == UINT64_MAX)
@@ -272,14 +271,14 @@ TEST(real_pressure) {
m = sd_bus_message_unref(m);
assert_se(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties") >= 0);
assert_se(sd_bus_message_append(m, "sb", scope, true) >= 0);
assert_se(sd_bus_message_open_container(m, 'a', "(sv)") >= 0);
assert_se(sd_bus_message_append(m, "(sv)", "MemoryHigh", "t", mcurrent + (15 * 1024 * 1024)) >= 0);
assert_se(sd_bus_message_append(m, "(sv)", "MemoryMax", "t", mcurrent + (50 * 1024 * 1024)) >= 0);
assert_se(sd_bus_message_close_container(m) >= 0);
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties"));
ASSERT_OK(sd_bus_message_append(m, "sb", scope, true));
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)"));
ASSERT_OK(sd_bus_message_append(m, "(sv)", "MemoryHigh", "t", mcurrent + (15 * 1024 * 1024)));
ASSERT_OK(sd_bus_message_append(m, "(sv)", "MemoryMax", "t", mcurrent + (50 * 1024 * 1024)));
ASSERT_OK(sd_bus_message_close_container(m));
assert_se(sd_bus_call(bus, m, 0, NULL, NULL) >= 0);
ASSERT_OK(sd_bus_call(bus, m, 0, NULL, NULL));
/* Generate some memory allocations via mempool */
#define NN (1024)
@@ -291,12 +290,12 @@ TEST(real_pressure) {
free(h);
/* Now start eating memory */
assert_se(write(pipe_fd[1], &(const char) { 'x' }, 1) == 1);
ASSERT_EQ(write(pipe_fd[1], &(const char) { 'x' }, 1), 1);
assert_se(sd_event_loop(e) >= 0);
ASSERT_OK(sd_event_loop(e));
int ex = 0;
assert_se(sd_event_get_exit_code(e, &ex) >= 0);
assert_se(ex == 31);
ASSERT_OK(sd_event_get_exit_code(e, &ex));
ASSERT_EQ(ex, 31);
}
static int outro(void) {