diff --git a/lib/common/tests/schemas/pcmk__build_schema_xml_node_test.c b/lib/common/tests/schemas/pcmk__build_schema_xml_node_test.c index 9cb29ad7b2..fca6182d09 100644 --- a/lib/common/tests/schemas/pcmk__build_schema_xml_node_test.c +++ b/lib/common/tests/schemas/pcmk__build_schema_xml_node_test.c @@ -1,159 +1,157 @@ /* - * Copyright 2023-2024 the Pacemaker project contributors + * Copyright 2023-2025 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 #include const char *rngs1[] = { "pacemaker-3.0.rng", "status-1.0.rng", "alerts-2.10.rng", "nvset-2.9.rng", "score.rng", "rule-2.9.rng", "tags-1.3.rng", "acls-2.0.rng", "fencing-2.4.rng", "constraints-3.0.rng", "resources-3.0.rng", "nvset-3.0.rng", "nodes-3.0.rng", "options-3.0.rng", NULL }; const char *rngs2[] = { "pacemaker-2.0.rng", "status-1.0.rng", "tags-1.3.rng", "acls-2.0.rng", "fencing-1.2.rng", "constraints-1.2.rng", "rule.rng", "score.rng", "resources-1.3.rng", "nvset-1.3.rng", "nodes-1.3.rng", "options-1.0.rng", "nvset.rng", "cib-1.2.rng", NULL }; const char *rngs3[] = { "pacemaker-2.1.rng", "constraints-2.1.rng", NULL }; static int setup(void **state) { setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); - pcmk__schema_init(); pcmk__xml_test_setup_group(state); return 0; } static int teardown(void **state) { pcmk__xml_test_teardown_group(state); - pcmk__schema_cleanup(); unsetenv("PCMK_schema_directory"); return 0; } static void invalid_name(void **state) { GList *already_included = NULL; xmlNode *parent = pcmk__xe_create(NULL, PCMK__XA_SCHEMAS); pcmk__build_schema_xml_node(parent, "pacemaker-9.0", &already_included); assert_null(parent->children); assert_null(already_included); pcmk__xml_free(parent); } static void single_schema(void **state) { GList *already_included = NULL; xmlNode *parent = pcmk__xe_create(NULL, PCMK__XA_SCHEMAS); xmlNode *schema_node = NULL; xmlNode *file_node = NULL; int i = 0; pcmk__build_schema_xml_node(parent, "pacemaker-3.0", &already_included); assert_non_null(already_included); assert_non_null(parent->children); /* Test that the result looks like this: * * * * CDATA * CDATA * ... * * */ schema_node = pcmk__xe_first_child(parent, NULL, NULL, NULL); assert_string_equal("pacemaker-3.0", crm_element_value(schema_node, PCMK_XA_VERSION)); file_node = pcmk__xe_first_child(schema_node, NULL, NULL, NULL); while (file_node != NULL && rngs1[i] != NULL) { assert_string_equal(rngs1[i], crm_element_value(file_node, PCMK_XA_PATH)); assert_int_equal(pcmk__xml_first_child(file_node)->type, XML_CDATA_SECTION_NODE); file_node = pcmk__xe_next(file_node, NULL); i++; } g_list_free_full(already_included, free); pcmk__xml_free(parent); } static void multiple_schemas(void **state) { GList *already_included = NULL; xmlNode *parent = pcmk__xe_create(NULL, PCMK__XA_SCHEMAS); xmlNode *schema_node = NULL; xmlNode *file_node = NULL; int i = 0; pcmk__build_schema_xml_node(parent, "pacemaker-2.0", &already_included); pcmk__build_schema_xml_node(parent, "pacemaker-2.1", &already_included); assert_non_null(already_included); assert_non_null(parent->children); /* Like single_schema, but make sure files aren't included multiple times * when the function is called repeatedly. */ schema_node = pcmk__xe_first_child(parent, NULL, NULL, NULL); assert_string_equal("pacemaker-2.0", crm_element_value(schema_node, PCMK_XA_VERSION)); file_node = pcmk__xe_first_child(schema_node, NULL, NULL, NULL); while (file_node != NULL && rngs2[i] != NULL) { assert_string_equal(rngs2[i], crm_element_value(file_node, PCMK_XA_PATH)); assert_int_equal(pcmk__xml_first_child(file_node)->type, XML_CDATA_SECTION_NODE); file_node = pcmk__xe_next(file_node, NULL); i++; } schema_node = pcmk__xe_next(schema_node, NULL); assert_string_equal("pacemaker-2.1", crm_element_value(schema_node, PCMK_XA_VERSION)); file_node = pcmk__xe_first_child(schema_node, NULL, NULL, NULL); i = 0; while (file_node != NULL && rngs3[i] != NULL) { assert_string_equal(rngs3[i], crm_element_value(file_node, PCMK_XA_PATH)); assert_int_equal(pcmk__xml_first_child(file_node)->type, XML_CDATA_SECTION_NODE); file_node = pcmk__xe_next(file_node, NULL); i++; } g_list_free_full(already_included, free); pcmk__xml_free(parent); } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(invalid_name), cmocka_unit_test(single_schema), cmocka_unit_test(multiple_schemas)) diff --git a/lib/common/tests/schemas/pcmk__cmp_schemas_by_name_test.c b/lib/common/tests/schemas/pcmk__cmp_schemas_by_name_test.c index fa5d76f6f6..74e0ba42b6 100644 --- a/lib/common/tests/schemas/pcmk__cmp_schemas_by_name_test.c +++ b/lib/common/tests/schemas/pcmk__cmp_schemas_by_name_test.c @@ -1,93 +1,93 @@ /* - * Copyright 2024 the Pacemaker project contributors + * Copyright 2024-2025 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 #include "crmcommon_private.h" static int setup(void **state) { setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); - pcmk__schema_init(); + pcmk__xml_test_setup_group(state); return 0; } static int teardown(void **state) { - pcmk__schema_cleanup(); + pcmk__xml_test_teardown_group(state); unsetenv("PCMK_schema_directory"); return 0; } // Unknown schemas (including NULL) are unsupported, but sort first as failsafe static void unknown_is_lesser(void **state) { assert_true(pcmk__cmp_schemas_by_name("pacemaker-0.1", "pacemaker-0.2") == 0); assert_true(pcmk__cmp_schemas_by_name("pacemaker-0.1", "pacemaker-1.0") < 0); assert_true(pcmk__cmp_schemas_by_name("pacemaker-1.0", "pacemaker-0.1") > 0); assert_true(pcmk__cmp_schemas_by_name("pacemaker-0.1", PCMK_VALUE_NONE) < 0); assert_true(pcmk__cmp_schemas_by_name(PCMK_VALUE_NONE, "pacemaker-0.1") > 0); assert_true(pcmk__cmp_schemas_by_name(NULL, NULL) == 0); assert_true(pcmk__cmp_schemas_by_name(NULL, "pacemaker-0.0") == 0); assert_true(pcmk__cmp_schemas_by_name(NULL, "pacemaker-2.0") < 0); assert_true(pcmk__cmp_schemas_by_name("pacemaker-1.3", NULL) > 0); assert_true(pcmk__cmp_schemas_by_name(NULL, PCMK_VALUE_NONE) < 0); assert_true(pcmk__cmp_schemas_by_name(PCMK_VALUE_NONE, NULL) > 0); } // @COMPAT none is deprecated since 2.1.8 static void none_is_greater(void **state) { assert_true(pcmk__cmp_schemas_by_name(PCMK_VALUE_NONE, PCMK_VALUE_NONE) == 0); assert_true(pcmk__cmp_schemas_by_name("pacemaker-3.10", PCMK_VALUE_NONE) < 0); assert_true(pcmk__cmp_schemas_by_name(PCMK_VALUE_NONE, "pacemaker-1.0") > 0); } static void known_numeric(void **state) { assert_true(pcmk__cmp_schemas_by_name("pacemaker-1.0", "pacemaker-1.0") == 0); assert_true(pcmk__cmp_schemas_by_name("pacemaker-1.2", "pacemaker-1.0") > 0); assert_true(pcmk__cmp_schemas_by_name("pacemaker-1.2", "pacemaker-2.0") < 0); } static void case_sensitive(void **state) { assert_true(pcmk__cmp_schemas_by_name("Pacemaker-1.0", "pacemaker-1.0") != 0); assert_true(pcmk__cmp_schemas_by_name("PACEMAKER-1.2", "pacemaker-1.2") != 0); assert_true(pcmk__cmp_schemas_by_name("PaceMaker-2.0", "pacemaker-2.0") != 0); } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(unknown_is_lesser), cmocka_unit_test(none_is_greater), cmocka_unit_test(known_numeric), cmocka_unit_test(case_sensitive)); diff --git a/lib/common/tests/schemas/pcmk__get_schema_test.c b/lib/common/tests/schemas/pcmk__get_schema_test.c index 3990171ff5..09b74fc41d 100644 --- a/lib/common/tests/schemas/pcmk__get_schema_test.c +++ b/lib/common/tests/schemas/pcmk__get_schema_test.c @@ -1,79 +1,79 @@ /* - * Copyright 2023-2024 the Pacemaker project contributors + * Copyright 2023-2025 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 #include "crmcommon_private.h" static int setup(void **state) { setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); - pcmk__schema_init(); + pcmk__xml_test_setup_group(state); return 0; } static int teardown(void **state) { - pcmk__schema_cleanup(); + pcmk__xml_test_teardown_group(state); unsetenv("PCMK_schema_directory"); return 0; } static void assert_schema(const char *name, int expected_index) { GList *schema_entry = NULL; pcmk__schema_t *schema = NULL; schema_entry = pcmk__get_schema(name); assert_non_null(schema_entry); schema = schema_entry->data; assert_non_null(schema); assert_int_equal(schema->schema_index, expected_index); } static void unknown_schema(void **state) { assert_null(pcmk__get_schema(NULL)); assert_null(pcmk__get_schema("")); assert_null(pcmk__get_schema("blahblah")); assert_null(pcmk__get_schema("pacemaker-2.47")); assert_null(pcmk__get_schema("pacemaker-47.0")); } static void known_schema(void **state) { assert_schema("pacemaker-1.0", 0); assert_schema("pacemaker-1.2", 1); assert_schema("pacemaker-2.0", 3); assert_schema("pacemaker-2.5", 8); assert_schema("pacemaker-3.0", 14); } static void case_sensitive(void **state) { assert_null(pcmk__get_schema("PACEMAKER-1.0")); assert_null(pcmk__get_schema("pAcEmAkEr-2.0")); assert_null(pcmk__get_schema("paceMAKER-3.0")); } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(unknown_schema), cmocka_unit_test(known_schema), cmocka_unit_test(case_sensitive)); diff --git a/lib/common/tests/schemas/pcmk__schema_files_later_than_test.c b/lib/common/tests/schemas/pcmk__schema_files_later_than_test.c index 76ae519a56..c48944da3e 100644 --- a/lib/common/tests/schemas/pcmk__schema_files_later_than_test.c +++ b/lib/common/tests/schemas/pcmk__schema_files_later_than_test.c @@ -1,106 +1,106 @@ /* - * Copyright 2023-2024 the Pacemaker project contributors + * Copyright 2023-2025 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); - pcmk__schema_init(); + pcmk__xml_test_setup_group(state); return 0; } static int teardown(void **state) { - pcmk__schema_cleanup(); + pcmk__xml_test_teardown_group(state); unsetenv("PCMK_schema_directory"); return 0; } static void invalid_name(void **state) { assert_null(pcmk__schema_files_later_than("xyz")); assert_null(pcmk__schema_files_later_than("pacemaker-")); } static void valid_name(void **state) { GList *schemas = NULL; schemas = pcmk__schema_files_later_than("pacemaker-1.0"); assert_int_equal(g_list_length(schemas), 18); /* There is no "pacemaker-1.1". */ assert_string_equal("pacemaker-1.2.rng", g_list_nth_data(schemas, 0)); assert_string_equal("pacemaker-1.3.rng", g_list_nth_data(schemas, 1)); assert_string_equal("upgrade-1.3-0.xsl", g_list_nth_data(schemas, 2)); assert_string_equal("pacemaker-2.0.rng", g_list_nth_data(schemas, 3)); assert_string_equal("pacemaker-2.1.rng", g_list_nth_data(schemas, 4)); assert_string_equal("pacemaker-2.2.rng", g_list_nth_data(schemas, 5)); assert_string_equal("pacemaker-2.3.rng", g_list_nth_data(schemas, 6)); assert_string_equal("pacemaker-2.4.rng", g_list_nth_data(schemas, 7)); assert_string_equal("pacemaker-2.5.rng", g_list_nth_data(schemas, 8)); assert_string_equal("pacemaker-2.6.rng", g_list_nth_data(schemas, 9)); assert_string_equal("pacemaker-2.7.rng", g_list_nth_data(schemas, 10)); assert_string_equal("pacemaker-2.8.rng", g_list_nth_data(schemas, 11)); assert_string_equal("pacemaker-2.9.rng", g_list_nth_data(schemas, 12)); assert_string_equal("pacemaker-2.10.rng", g_list_nth_data(schemas, 13)); assert_string_equal("upgrade-2.10-0.xsl", g_list_nth_data(schemas, 14)); assert_string_equal("upgrade-2.10-1.xsl", g_list_nth_data(schemas, 15)); assert_string_equal("upgrade-2.10-2.xsl", g_list_nth_data(schemas, 16)); assert_string_equal("pacemaker-3.0.rng", g_list_nth_data(schemas, 17)); g_list_free_full(schemas, free); /* Adding .rng to the end of the schema we're requesting is also valid. */ schemas = pcmk__schema_files_later_than("pacemaker-2.0.rng"); assert_int_equal(g_list_length(schemas), 14); assert_string_equal("pacemaker-2.1.rng", g_list_nth_data(schemas, 0)); assert_string_equal("pacemaker-2.2.rng", g_list_nth_data(schemas, 1)); assert_string_equal("pacemaker-2.3.rng", g_list_nth_data(schemas, 2)); assert_string_equal("pacemaker-2.4.rng", g_list_nth_data(schemas, 3)); assert_string_equal("pacemaker-2.5.rng", g_list_nth_data(schemas, 4)); assert_string_equal("pacemaker-2.6.rng", g_list_nth_data(schemas, 5)); assert_string_equal("pacemaker-2.7.rng", g_list_nth_data(schemas, 6)); assert_string_equal("pacemaker-2.8.rng", g_list_nth_data(schemas, 7)); assert_string_equal("pacemaker-2.9.rng", g_list_nth_data(schemas, 8)); assert_string_equal("pacemaker-2.10.rng", g_list_nth_data(schemas, 9)); assert_string_equal("upgrade-2.10-0.xsl", g_list_nth_data(schemas, 10)); assert_string_equal("upgrade-2.10-1.xsl", g_list_nth_data(schemas, 11)); assert_string_equal("upgrade-2.10-2.xsl", g_list_nth_data(schemas, 12)); assert_string_equal("pacemaker-3.0.rng", g_list_nth_data(schemas, 13)); g_list_free_full(schemas, free); /* Check that "pacemaker-2.10" counts as later than "pacemaker-2.9". */ schemas = pcmk__schema_files_later_than("pacemaker-2.9"); assert_int_equal(g_list_length(schemas), 5); assert_string_equal("pacemaker-2.10.rng", g_list_nth_data(schemas, 0)); assert_string_equal("upgrade-2.10-0.xsl", g_list_nth_data(schemas, 1)); assert_string_equal("upgrade-2.10-1.xsl", g_list_nth_data(schemas, 2)); assert_string_equal("upgrade-2.10-2.xsl", g_list_nth_data(schemas, 3)); assert_string_equal("pacemaker-3.0.rng", g_list_nth_data(schemas, 4)); g_list_free_full(schemas, free); /* And then something way in the future that will never apply due to our * special schema directory. */ schemas = pcmk__schema_files_later_than("pacemaker-9.0"); assert_null(schemas); } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(invalid_name), cmocka_unit_test(valid_name)) diff --git a/lib/common/tests/schemas/pcmk__schema_init_test.c b/lib/common/tests/schemas/pcmk__schema_init_test.c index 19c20cc7da..8df9e6940a 100644 --- a/lib/common/tests/schemas/pcmk__schema_init_test.c +++ b/lib/common/tests/schemas/pcmk__schema_init_test.c @@ -1,149 +1,149 @@ /* - * Copyright 2023-2024 the Pacemaker project contributors + * Copyright 2023-2025 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 #include #include #include "crmcommon_private.h" static char *remote_schema_dir = NULL; static int symlink_schema(const char *tmpdir, const char *target_file, const char *link_file) { int rc = 0; char *oldpath = NULL; char *newpath = NULL; oldpath = crm_strdup_printf("%s/%s", PCMK__TEST_SCHEMA_DIR, target_file); newpath = crm_strdup_printf("%s/%s", tmpdir, link_file); rc = symlink(oldpath, newpath); free(oldpath); free(newpath); return rc; } static int rm_files(const char *pathname, const struct stat *sbuf, int type, struct FTW *ftwb) { return remove(pathname); } static int rmtree(const char *dir) { return nftw(dir, rm_files, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS); } static int setup(void **state) { char *dir = NULL; /* Create a directory to hold additional schema files. These don't need * to be anything special - we can just copy existing schemas but give * them new names. */ dir = crm_strdup_printf("%s/test-schemas.XXXXXX", pcmk__get_tmpdir()); remote_schema_dir = mkdtemp(dir); if (remote_schema_dir == NULL) { free(dir); return -1; } /* Add new files to simulate a remote node not being up-to-date. We can't * add a new major version here without also creating an XSL transform, and * we can't add an older version (like 1.1 or 2.11 or something) because * remotes will only ever ask for stuff newer than their newest. */ if (symlink_schema(dir, "pacemaker-3.0.rng", "pacemaker-3.1.rng") != 0) { rmdir(dir); free(dir); return -1; } if (symlink_schema(dir, "pacemaker-3.0.rng", "pacemaker-3.2.rng") != 0) { rmdir(dir); free(dir); return -1; } setenv("PCMK_remote_schema_directory", remote_schema_dir, 1); setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); /* Do not call pcmk__schema_init() here because that is the function we're * testing. It needs to be called in each unit test. However, we can call - * pcmk__schema_cleanup() in teardown(). + * pcmk__schema_cleanup() via the XML teardown function in teardown(). */ return 0; } static int teardown(void **state) { int rc = 0; char *f = NULL; - pcmk__schema_cleanup(); + pcmk__xml_test_teardown_group(state); unsetenv("PCMK_remote_schema_directory"); unsetenv("PCMK_schema_directory"); rc = rmtree(remote_schema_dir); free(remote_schema_dir); free(f); return rc; } static void assert_schema(const char *schema_name, int schema_index) { GList *entry = NULL; pcmk__schema_t *schema = NULL; entry = pcmk__get_schema(schema_name); assert_non_null(entry); schema = entry->data; assert_non_null(schema); assert_int_equal(schema_index, schema->schema_index); } static void extra_schema_files(void **state) { pcmk__schema_init(); /* Just iterate through the list of schemas and make sure everything * (including the new schemas we loaded from a second directory) is in * the right order. */ assert_schema("pacemaker-1.0", 0); assert_schema("pacemaker-1.2", 1); assert_schema("pacemaker-2.0", 3); assert_schema("pacemaker-3.0", 14); assert_schema("pacemaker-3.1", 15); assert_schema("pacemaker-3.2", 16); // @COMPAT none is deprecated since 2.1.8 assert_schema(PCMK_VALUE_NONE, 17); } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(extra_schema_files));