diff --git a/lib/common/tests/xml/Makefile.am b/lib/common/tests/xml/Makefile.am index 342ca076cc..0ccdcc37b6 100644 --- a/lib/common/tests/xml/Makefile.am +++ b/lib/common/tests/xml/Makefile.am @@ -1,16 +1,17 @@ # # 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. # include $(top_srcdir)/mk/tap.mk include $(top_srcdir)/mk/unittest.mk # Add "_test" to the end of all test program names to simplify .gitignore. -check_PROGRAMS = pcmk__xe_foreach_child_test +check_PROGRAMS = pcmk__xe_foreach_child_test \ + pcmk__xe_match_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/xml/pcmk__xe_match_test.c b/lib/common/tests/xml/pcmk__xe_match_test.c new file mode 100644 index 0000000000..fd529ba803 --- /dev/null +++ b/lib/common/tests/xml/pcmk__xe_match_test.c @@ -0,0 +1,105 @@ +/* + * 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 + +const char *str1 = + "\n" + " \n" + " \n" + " content\n" + " \n" + " \n" + " \n" + " content\n" + " \n" + " \n" + " \n" + " content\n" + " \n" + " \n" + " \n" + " content\n" + " \n" + " \n" + " \n" + " content\n" + " \n" + ""; + +static void +bad_input(void **state) { + xmlNode *xml = string2xml(str1); + + assert_null(pcmk__xe_match(NULL, NULL, NULL, NULL)); + assert_null(pcmk__xe_match(NULL, NULL, NULL, "attrX")); + + free_xml(xml); +} + +static void +not_found(void **state) { + xmlNode *xml = string2xml(str1); + + /* No node with an attrX attribute */ + assert_null(pcmk__xe_match(xml, NULL, "attrX", NULL)); + /* No nodeX node */ + assert_null(pcmk__xe_match(xml, "nodeX", NULL, NULL)); + /* No nodeA node with attrX */ + assert_null(pcmk__xe_match(xml, "nodeA", "attrX", NULL)); + /* No nodeA node with attrA=XYZ */ + assert_null(pcmk__xe_match(xml, "nodeA", "attrA", "XYZ")); + + free_xml(xml); +} + +static void +find_attrB(void **state) { + xmlNode *xml = string2xml(str1); + xmlNode *result = NULL; + + /* Find the first node with attrB */ + result = pcmk__xe_match(xml, NULL, "attrB", NULL); + assert_non_null(result); + assert_string_equal(crm_element_value(result, "id"), "3"); + + /* Find the first nodeB with attrB */ + result = pcmk__xe_match(xml, "nodeB", "attrB", NULL); + assert_non_null(result); + assert_string_equal(crm_element_value(result, "id"), "5"); + + free_xml(xml); +} + +static void +find_attrA_matching(void **state) { + xmlNode *xml = string2xml(str1); + xmlNode *result = NULL; + + /* Find attrA=456 */ + result = pcmk__xe_match(xml, NULL, "attrA", "456"); + assert_non_null(result); + assert_string_equal(crm_element_value(result, "id"), "2"); + + /* Find a nodeB with attrA=123 */ + result = pcmk__xe_match(xml, "nodeB", "attrA", "123"); + assert_non_null(result); + assert_string_equal(crm_element_value(result, "id"), "4"); + + free_xml(xml); +} + +PCMK__UNIT_TEST(NULL, NULL, + cmocka_unit_test(bad_input), + cmocka_unit_test(not_found), + cmocka_unit_test(find_attrB), + cmocka_unit_test(find_attrA_matching));