diff --git a/lib/common/tests/procfs/Makefile.am b/lib/common/tests/procfs/Makefile.am index 414e8997d2..2bf936c9d2 100644 --- a/lib/common/tests/procfs/Makefile.am +++ b/lib/common/tests/procfs/Makefile.am @@ -1,21 +1,23 @@ # # Copyright 2022 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # AM_CPPFLAGS = -I$(top_srcdir)/include \ -I$(top_builddir)/include \ -I$(top_srcdir)/lib/common LDADD = $(top_builddir)/lib/common/libcrmcommon_test.la AM_LDFLAGS = $(LDFLAGS_WRAP) include $(top_srcdir)/mk/tap.mk # Add "_test" to the end of all test program names to simplify .gitignore. -check_PROGRAMS = pcmk__procfs_pid2path_test +check_PROGRAMS = pcmk__procfs_has_pids_false_test \ + pcmk__procfs_has_pids_true_test \ + pcmk__procfs_pid2path_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/procfs/pcmk__procfs_has_pids_false_test.c b/lib/common/tests/procfs/pcmk__procfs_has_pids_false_test.c new file mode 100644 index 0000000000..888ecb1f70 --- /dev/null +++ b/lib/common/tests/procfs/pcmk__procfs_has_pids_false_test.c @@ -0,0 +1,51 @@ +/* + * Copyright 2022 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU Lesser General Public License + * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. + */ + +#include +#include "mock_private.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#if SUPPORT_PROCFS + +static void +no_pids(void **state) +{ + // Set readlink() errno and link contents (for /proc/PID/exe) + pcmk__mock_readlink = true; + will_return(__wrap_readlink, ENOENT); + will_return(__wrap_readlink, NULL); + + assert_false(pcmk__procfs_has_pids()); + + pcmk__mock_readlink = false; +} + +#endif // SUPPORT_PROCFS + +int main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { +#if SUPPORT_PROCFS + cmocka_unit_test(no_pids), +#endif + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/procfs/pcmk__procfs_has_pids_true_test.c b/lib/common/tests/procfs/pcmk__procfs_has_pids_true_test.c new file mode 100644 index 0000000000..7c61b50a10 --- /dev/null +++ b/lib/common/tests/procfs/pcmk__procfs_has_pids_true_test.c @@ -0,0 +1,51 @@ +/* + * Copyright 2022 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU Lesser General Public License + * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. + */ + +#include +#include "mock_private.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#if SUPPORT_PROCFS + +static void +has_pids(void **state) +{ + // Set readlink() errno and link contents (for /proc/PID/exe) + pcmk__mock_readlink = true; + will_return(__wrap_readlink, 0); + will_return(__wrap_readlink, "/ok"); + + assert_true(pcmk__procfs_has_pids()); + + pcmk__mock_readlink = false; +} + +#endif // SUPPORT_PROCFS + +int main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { +#if SUPPORT_PROCFS + cmocka_unit_test(has_pids), +#endif + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +}