diff --git a/lib/common/tests/schemas/Makefile.am b/lib/common/tests/schemas/Makefile.am index 394217f14c..bf53248d86 100644 --- a/lib/common/tests/schemas/Makefile.am +++ b/lib/common/tests/schemas/Makefile.am @@ -1,58 +1,59 @@ # # Copyright 2023 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. # include $(top_srcdir)/mk/tap.mk include $(top_srcdir)/mk/unittest.mk CFLAGS += -DPCMK__TEST_SCHEMA_DIR='"$(abs_builddir)/schemas"' # Add "_test" to the end of all test program names to simplify .gitignore. -check_PROGRAMS = get_schema_version_test \ +check_PROGRAMS = get_schema_name_test \ + get_schema_version_test \ pcmk__build_schema_xml_node_test \ pcmk__schema_files_later_than_test \ pcmk__xml_find_x_0_schema_index_test TESTS = $(check_PROGRAMS) $(TESTS): setup-schema-dir # Set up a temporary schemas/ directory containing only some of the full set of # pacemaker schema files. This lets us know exactly how many schemas are present, # allowing us to write tests without having to make changes when new schemas are # added. # # This directory contains the following: # # * pacemaker-next.rng - Used to verify that this sorts before all versions # * upgrade-*.xsl - Required by various schema versions # * pacemaker-[0-9]*.rng - We're only pulling in 15 schemas, which is enough # to get everything through pacemaker-3.0.rng. This # includes 2.10, needed so we can check that versions # are compared as numbers instead of strings. # * other RNG files - This catches everything except the pacemaker-*rng # files. These files are included by the top-level # pacemaker-*rng files, so we need them for tests. # This will glob more than we need, but the extra ones # won't get in the way. .PHONY: setup-schema-dir setup-schema-dir: $(MKDIR_P) schemas ( cd schemas ; \ ln -sf $(abs_top_builddir)/xml/pacemaker-next.rng . ; \ ln -sf $(abs_top_builddir)/xml/upgrade-*.xsl . ; \ for f in $(shell ls -1v $(abs_top_builddir)/xml/pacemaker-[0-9]*.rng | head -15); do \ ln -sf $$f $$(basename $$f); \ done ; \ for f in $(shell ls -1 $(top_srcdir)/xml/*.rng | grep -v pacemaker); do \ ln -sf ../$$f $$(basename $$f); \ done ) .PHONY: clean-local clean-local: -rm -rf schemas diff --git a/lib/common/tests/schemas/get_schema_name_test.c b/lib/common/tests/schemas/get_schema_name_test.c new file mode 100644 index 0000000000..d5d4447dd7 --- /dev/null +++ b/lib/common/tests/schemas/get_schema_name_test.c @@ -0,0 +1,47 @@ +/* + * Copyright 2023 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. + */ + +#include + +#include +#include +#include + +static int +setup(void **state) { + setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); + crm_schema_init(); + return 0; +} + +static int +teardown(void **state) { + crm_schema_cleanup(); + unsetenv("PCMK_schema_directory"); + return 0; +} + +static void +bad_input(void **state) { + assert_string_equal("unknown", get_schema_name(-1)); + assert_string_equal("unknown", get_schema_name(47000)); +} + +static void +typical_usage(void **state) { + assert_string_equal("pacemaker-1.0", get_schema_name(0)); + assert_string_equal("pacemaker-1.2", get_schema_name(1)); + assert_string_equal("pacemaker-2.0", get_schema_name(3)); + assert_string_equal("pacemaker-2.5", get_schema_name(8)); + assert_string_equal("pacemaker-3.0", get_schema_name(14)); +} + +PCMK__UNIT_TEST(setup, teardown, + cmocka_unit_test(bad_input), + cmocka_unit_test(typical_usage));