diff --git a/lib/common/tests/io/Makefile.am b/lib/common/tests/io/Makefile.am index 8f934f1bc6..5c3b163211 100644 --- a/lib/common/tests/io/Makefile.am +++ b/lib/common/tests/io/Makefile.am @@ -1,30 +1,37 @@ # # Copyright 2020-2021 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 + +include $(top_srcdir)/lib/common/mock.mk + +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/lib/common LDADD = $(top_builddir)/lib/common/libcrmcommon.la -lcmocka +pcmk__get_tmpdir_test_LDADD = $(top_builddir)/lib/common/libcrmcommon_test.la -lcmocka +pcmk__get_tmpdir_test_LDFLAGS = -Wl,--wrap=getenv + include $(top_srcdir)/mk/glib-tap.mk # Add each test program here. Each test should be written as a little standalone # program using the glib unit testing functions. See the documentation for more # information. # # https://developer.gnome.org/glib/unstable/glib-Testing.html # # Add "_test" to the end of all test program names to simplify .gitignore. test_programs = \ - pcmk__full_path_test + pcmk__full_path_test \ + pcmk__get_tmpdir_test # If any extra data needs to be added to the source distribution, add it to the # following list. dist_test_data = # If any extra data needs to be used by tests but should not be added to the # source distribution, add it to the following list. test_data = diff --git a/lib/common/tests/io/pcmk__get_tmpdir_test.c b/lib/common/tests/io/pcmk__get_tmpdir_test.c new file mode 100644 index 0000000000..3843a780c4 --- /dev/null +++ b/lib/common/tests/io/pcmk__get_tmpdir_test.c @@ -0,0 +1,93 @@ +/* + * Copyright 2021 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 + +static bool use_mocked = false; + +char * +__wrap_getenv(const char *name) +{ + /* If coverage is enabled, something in "make check" will want to call + * the real getenv(), which will of course fail if the mocked version gets + * used. This bool needs to be set and unset around every unit test so + * the mocked version is only called in that time. + */ + if (use_mocked) { + return mock_type(char *); + } else { + return __real_getenv(name); + } +} + +static void +getenv_returns_invalid(void **state) +{ + const char *result; + + use_mocked = true; + + will_return(__wrap_getenv, NULL); // getenv("TMPDIR") return value + result = pcmk__get_tmpdir(); + assert_string_equal(result, "/tmp"); + + will_return(__wrap_getenv, ""); // getenv("TMPDIR") return value + result = pcmk__get_tmpdir(); + assert_string_equal(result, "/tmp"); + + will_return(__wrap_getenv, "subpath"); // getenv("TMPDIR") return value + result = pcmk__get_tmpdir(); + assert_string_equal(result, "/tmp"); + + use_mocked = false; +} + +static void +getenv_returns_valid(void **state) +{ + const char *result; + + use_mocked = true; + + will_return(__wrap_getenv, "/var/tmp"); // getenv("TMPDIR") return value + result = pcmk__get_tmpdir(); + assert_string_equal(result, "/var/tmp"); + + will_return(__wrap_getenv, "/"); // getenv("TMPDIR") return value + result = pcmk__get_tmpdir(); + assert_string_equal(result, "/"); + + will_return(__wrap_getenv, "/tmp/abcd.1234"); // getenv("TMPDIR") return value + result = pcmk__get_tmpdir(); + assert_string_equal(result, "/tmp/abcd.1234"); + + use_mocked = false; +} + +int +main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(getenv_returns_invalid), + cmocka_unit_test(getenv_returns_valid), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/utils/Makefile.am b/lib/common/tests/utils/Makefile.am index 0b7f20247e..34e5052e4f 100644 --- a/lib/common/tests/utils/Makefile.am +++ b/lib/common/tests/utils/Makefile.am @@ -1,30 +1,37 @@ # # Copyright 2020-2021 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 + +include $(top_srcdir)/lib/common/mock.mk + +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/lib/common LDADD = $(top_builddir)/lib/common/libcrmcommon.la -lcmocka +pcmk_hostname_test_LDADD = $(top_builddir)/lib/common/libcrmcommon_test.la -lcmocka +pcmk_hostname_test_LDFLAGS = -Wl,--wrap=uname + include $(top_srcdir)/mk/glib-tap.mk # Add each test program here. Each test should be written as a little standalone # program using the glib unit testing functions. See the documentation for more # information. # # https://developer.gnome.org/glib/unstable/glib-Testing.html # # Add "_test" to the end of all test program names to simplify .gitignore. -test_programs = pcmk_str_is_infinity_test \ +test_programs = pcmk_hostname_test \ + pcmk_str_is_infinity_test \ pcmk_str_is_minus_infinity_test # If any extra data needs to be added to the source distribution, add it to the # following list. dist_test_data = # If any extra data needs to be used by tests but should not be added to the # source distribution, add it to the following list. test_data = diff --git a/lib/common/tests/utils/pcmk_hostname_test.c b/lib/common/tests/utils/pcmk_hostname_test.c new file mode 100644 index 0000000000..7eb747b1b1 --- /dev/null +++ b/lib/common/tests/utils/pcmk_hostname_test.c @@ -0,0 +1,71 @@ +/* + * Copyright 2021 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 + +int +__wrap_uname(struct utsname *buf) +{ + int retval = mock_type(int); + + if (retval == 0) { + strcpy(buf->nodename, mock_type(char *)); + } + + return retval; +} + +static void +uname_succeeded_test(void **state) +{ + char *retval; + + will_return(__wrap_uname, 0); // uname() return value + will_return(__wrap_uname, "somename"); // uname() buf->nodename + + retval = pcmk_hostname(); + assert_non_null(retval); + assert_string_equal("somename", retval); + + free(retval); +} + +static void +uname_failed_test(void **state) +{ + char *retval; + + will_return(__wrap_uname, -1); // uname() return value + + retval = pcmk_hostname(); + assert_null(retval); +} + +int +main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(uname_succeeded_test), + cmocka_unit_test(uname_failed_test), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +}