diff --git a/lib/common/tests/output/Makefile.am b/lib/common/tests/output/Makefile.am index d3a90207bc..5c78e8cfd4 100644 --- a/lib/common/tests/output/Makefile.am +++ b/lib/common/tests/output/Makefile.am @@ -1,23 +1,24 @@ # # Copyright 2021-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 \ -lcmocka AM_CFLAGS = -DPCMK__UNIT_TESTING 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__register_format_test +check_PROGRAMS = pcmk__register_format_test \ + pcmk__register_formats_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/output/pcmk__register_formats_test.c b/lib/common/tests/output/pcmk__register_formats_test.c new file mode 100644 index 0000000000..6d34b5e187 --- /dev/null +++ b/lib/common/tests/output/pcmk__register_formats_test.c @@ -0,0 +1,116 @@ +/* + * 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 +#include + +static pcmk__output_t * +null_create_fn(char **argv) { + return NULL; +} + +static pcmk__output_t * +null_create_fn_2(char **argv) { + return NULL; +} + +static void +no_formats(void **state) { + pcmk__register_formats(NULL, NULL); + assert_null(pcmk__output_formatters()); +} + +static void +invalid_entries(void **state) { + /* Here, we can only test that an empty name won't be added. A NULL name is + * the marker for the end of the format table. + */ + pcmk__supported_format_t formats[] = { + { "", null_create_fn, NULL }, + { NULL }, + }; + + pcmk__assert_asserts(pcmk__register_formats(NULL, formats)); +} + +static void +valid_entries(void **state) { + GHashTable *formatters = NULL; + + pcmk__supported_format_t formats[] = { + { "fmt1", null_create_fn, NULL }, + { "fmt2", null_create_fn_2, NULL }, + { NULL }, + }; + + pcmk__register_formats(NULL, formats); + + formatters = pcmk__output_formatters(); + assert_int_equal(g_hash_table_size(formatters), 2); + assert_ptr_equal(g_hash_table_lookup(formatters, "fmt1"), null_create_fn); + assert_ptr_equal(g_hash_table_lookup(formatters, "fmt2"), null_create_fn_2); + + pcmk__unregister_formats(); +} + +static void +duplicate_keys(void **state) { + GHashTable *formatters = NULL; + + pcmk__supported_format_t formats[] = { + { "fmt1", null_create_fn, NULL }, + { "fmt1", null_create_fn_2, NULL }, + { NULL }, + }; + + pcmk__register_formats(NULL, formats); + + formatters = pcmk__output_formatters(); + assert_int_equal(g_hash_table_size(formatters), 1); + assert_ptr_equal(g_hash_table_lookup(formatters, "fmt1"), null_create_fn_2); + + pcmk__unregister_formats(); +} + +static void +duplicate_values(void **state) { + GHashTable *formatters = NULL; + + pcmk__supported_format_t formats[] = { + { "fmt1", null_create_fn, NULL }, + { "fmt2", null_create_fn, NULL }, + { NULL }, + }; + + pcmk__register_formats(NULL, formats); + + formatters = pcmk__output_formatters(); + assert_int_equal(g_hash_table_size(formatters), 2); + assert_ptr_equal(g_hash_table_lookup(formatters, "fmt1"), null_create_fn); + assert_ptr_equal(g_hash_table_lookup(formatters, "fmt2"), null_create_fn); + + pcmk__unregister_formats(); +} + +int +main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(no_formats), + cmocka_unit_test(invalid_entries), + cmocka_unit_test(valid_entries), + cmocka_unit_test(duplicate_keys), + cmocka_unit_test(duplicate_values), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +}