diff --git a/lib/common/tests/schemas/crm_schema_init_test.c b/lib/common/tests/schemas/crm_schema_init_test.c index a64cc723d6..18c9a0b234 100644 --- a/lib/common/tests/schemas/crm_schema_init_test.c +++ b/lib/common/tests/schemas/crm_schema_init_test.c @@ -1,138 +1,137 @@ /* * Copyright 2023-2024 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 -/* crm_schema_init is best tested by writing tests for all the other public - * functions it calls. However, it's useful to have a test to make sure - * incorporating a second directory of schema files (like may be seen on - * Pacemaker Remote nodes) works. - */ - static char *remote_schema_dir = NULL; static int -symlink_schema(const char *tmpdir, const char *target_file, const char *link_file) { +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) { +rmtree(const char *dir) +{ return nftw(dir, rm_files, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS); } static int -setup(void **state) { +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 crm_schema_init here because that is the function we're * testing. It needs to be called in each unit test. However, we can * call crm_schema_cleanup in teardown(). */ return 0; } static int -teardown(void **state) { +teardown(void **state) +{ int rc = 0; char *f = NULL; crm_schema_cleanup(); unsetenv("PCMK_remote_schema_directory"); unsetenv("PCMK_schema_directory"); rc = rmtree(remote_schema_dir); free(remote_schema_dir); free(f); return rc; } static void -extra_schema_files(void **state) { +extra_schema_files(void **state) +{ crm_schema_init(); pcmk__log_known_schemas(); /* 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_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-3.0", get_schema_name(14)); assert_string_equal("pacemaker-3.1", get_schema_name(15)); assert_string_equal("pacemaker-3.2", get_schema_name(16)); - /* This will one day be removed */ + // @COMPAT pacemaker-next is deprecated since 2.1.5 assert_string_equal("pacemaker-next", get_schema_name(17)); assert_string_equal(PCMK_VALUE_NONE, get_schema_name(18)); } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(extra_schema_files)); diff --git a/lib/common/tests/schemas/get_schema_name_test.c b/lib/common/tests/schemas/get_schema_name_test.c index 6efdbc792a..f1eb9fe0ef 100644 --- a/lib/common/tests/schemas/get_schema_name_test.c +++ b/lib/common/tests/schemas/get_schema_name_test.c @@ -1,47 +1,56 @@ /* * Copyright 2023-2024 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) { +setup(void **state) +{ setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); crm_schema_init(); return 0; } static int -teardown(void **state) { +teardown(void **state) +{ crm_schema_cleanup(); unsetenv("PCMK_schema_directory"); return 0; } static void -bad_input(void **state) { +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) { +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)); + + // @COMPAT pacemaker-next is deprecated since 2.1.5 + assert_string_equal("pacemaker-next", get_schema_name(15)); + + assert_string_equal(PCMK_VALUE_NONE, get_schema_name(16)); } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(bad_input), cmocka_unit_test(typical_usage)); diff --git a/lib/common/tests/schemas/get_schema_version_test.c b/lib/common/tests/schemas/get_schema_version_test.c index d2055503c3..863c6687ae 100644 --- a/lib/common/tests/schemas/get_schema_version_test.c +++ b/lib/common/tests/schemas/get_schema_version_test.c @@ -1,53 +1,63 @@ /* * Copyright 2023-2024 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) { +setup(void **state) +{ setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); crm_schema_init(); return 0; } static int -teardown(void **state) { +teardown(void **state) +{ crm_schema_cleanup(); unsetenv("PCMK_schema_directory"); return 0; } static void -bad_input(void **state) { - assert_int_equal(16, get_schema_version(NULL)); +bad_input(void **state) +{ assert_int_equal(-1, get_schema_version("")); assert_int_equal(-1, get_schema_version("blahblah")); assert_int_equal(-1, get_schema_version("pacemaker-2.47")); assert_int_equal(-1, get_schema_version("pacemaker-47.0")); } static void -typical_usage(void **state) { +typical_usage(void **state) +{ assert_int_equal(0, get_schema_version("pacemaker-1.0")); assert_int_equal(0, get_schema_version("PACEMAKER-1.0")); assert_int_equal(1, get_schema_version("pacemaker-1.2")); assert_int_equal(3, get_schema_version("pacemaker-2.0")); assert_int_equal(3, get_schema_version("pAcEmAkEr-2.0")); assert_int_equal(8, get_schema_version("pacemaker-2.5")); assert_int_equal(14, get_schema_version("pacemaker-3.0")); assert_int_equal(14, get_schema_version("paceMAKER-3.0")); + + // @COMPAT pacemaker-next is deprecated since 2.1.5 + assert_int_equal(15, get_schema_version("pacemaker-next")); + + assert_int_equal(16, get_schema_version("none")); + assert_int_equal(16, get_schema_version("NONE")); + assert_int_equal(16, get_schema_version(NULL)); // defaults to "none" } PCMK__UNIT_TEST(setup, teardown, cmocka_unit_test(bad_input), cmocka_unit_test(typical_usage)); 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 0a9ac0c75e..b89e2d4725 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,155 +1,157 @@ /* * Copyright 2023-2024 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) { +setup(void **state) +{ setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); crm_schema_init(); return 0; } static int -teardown(void **state) { +teardown(void **state) +{ crm_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); free_xml(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); i++; } g_list_free_full(already_included, free); free_xml(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); i++; } schema_node = pcmk__xe_next(schema_node); 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); i++; } g_list_free_full(already_included, free); free_xml(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__schema_files_later_than_test.c b/lib/common/tests/schemas/pcmk__schema_files_later_than_test.c index 50b5f43343..68744ef27a 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,104 +1,106 @@ /* * 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) { +setup(void **state) +{ setenv("PCMK_schema_directory", PCMK__TEST_SCHEMA_DIR, 1); crm_schema_init(); return 0; } static int -teardown(void **state) { +teardown(void **state) +{ crm_schema_cleanup(); 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("upgrade-1.3.xsl", g_list_nth_data(schemas, 1)); assert_string_equal("pacemaker-1.3.rng", 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("upgrade-2.10-leave.xsl", g_list_nth_data(schemas, 13)); assert_string_equal("upgrade-2.10-enter.xsl", g_list_nth_data(schemas, 14)); assert_string_equal("upgrade-2.10.xsl", g_list_nth_data(schemas, 15)); assert_string_equal("pacemaker-2.10.rng", 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("upgrade-2.10-leave.xsl", g_list_nth_data(schemas, 9)); assert_string_equal("upgrade-2.10-enter.xsl", g_list_nth_data(schemas, 10)); assert_string_equal("upgrade-2.10.xsl", g_list_nth_data(schemas, 11)); assert_string_equal("pacemaker-2.10.rng", 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("upgrade-2.10-leave.xsl", g_list_nth_data(schemas, 0)); assert_string_equal("upgrade-2.10-enter.xsl", g_list_nth_data(schemas, 1)); assert_string_equal("upgrade-2.10.xsl", g_list_nth_data(schemas, 2)); assert_string_equal("pacemaker-2.10.rng", 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))