summaryrefslogtreecommitdiffstats
path: root/lib/common/tests/nvpair
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common/tests/nvpair')
-rw-r--r--lib/common/tests/nvpair/Makefile.am7
-rw-r--r--lib/common/tests/nvpair/crm_meta_name_test.c41
-rw-r--r--lib/common/tests/nvpair/crm_meta_value_test.c58
-rw-r--r--lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c10
-rw-r--r--lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c11
-rw-r--r--lib/common/tests/nvpair/pcmk__xe_get_datetime_test.c108
-rw-r--r--lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c12
7 files changed, 229 insertions, 18 deletions
diff --git a/lib/common/tests/nvpair/Makefile.am b/lib/common/tests/nvpair/Makefile.am
index 7f406bd..9f762d4 100644
--- a/lib/common/tests/nvpair/Makefile.am
+++ b/lib/common/tests/nvpair/Makefile.am
@@ -1,5 +1,5 @@
#
-# Copyright 2021-2023 the Pacemaker project contributors
+# Copyright 2021-2024 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
@@ -11,7 +11,10 @@ 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_attr_is_true_test \
+check_PROGRAMS = crm_meta_name_test \
+ crm_meta_value_test \
+ pcmk__xe_attr_is_true_test \
+ pcmk__xe_get_datetime_test \
pcmk__xe_get_bool_attr_test \
pcmk__xe_set_bool_attr_test
diff --git a/lib/common/tests/nvpair/crm_meta_name_test.c b/lib/common/tests/nvpair/crm_meta_name_test.c
new file mode 100644
index 0000000..7c6c32b
--- /dev/null
+++ b/lib/common/tests/nvpair/crm_meta_name_test.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2022-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 <crm_internal.h>
+
+#include <crm/common/unittest_internal.h>
+#include <crm/common/xml.h>
+
+static void
+empty_params(void **state)
+{
+ pcmk__assert_asserts(crm_meta_name(NULL));
+}
+
+static void
+standard_usage(void **state)
+{
+ char *s = NULL;
+
+ s = crm_meta_name(PCMK_META_NOTIFY);
+ assert_string_equal(s, "CRM_meta_notify");
+ free(s);
+
+ s = crm_meta_name(PCMK_META_RESOURCE_STICKINESS);
+ assert_string_equal(s, "CRM_meta_resource_stickiness");
+ free(s);
+
+ s = crm_meta_name("blah");
+ assert_string_equal(s, "CRM_meta_blah");
+ free(s);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(empty_params),
+ cmocka_unit_test(standard_usage))
diff --git a/lib/common/tests/nvpair/crm_meta_value_test.c b/lib/common/tests/nvpair/crm_meta_value_test.c
new file mode 100644
index 0000000..ffe9619
--- /dev/null
+++ b/lib/common/tests/nvpair/crm_meta_value_test.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2022-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 <crm_internal.h>
+
+#include <crm/common/unittest_internal.h>
+#include <crm/common/xml.h>
+
+#include <glib.h>
+
+static void
+empty_params(void **state)
+{
+ GHashTable *tbl = pcmk__strkey_table(free, free);
+
+ assert_null(crm_meta_value(NULL, NULL));
+ assert_null(crm_meta_value(tbl, NULL));
+
+ g_hash_table_destroy(tbl);
+}
+
+static void
+key_not_in_table(void **state)
+{
+ GHashTable *tbl = pcmk__strkey_table(free, free);
+
+ assert_null(crm_meta_value(tbl, PCMK_META_NOTIFY));
+ assert_null(crm_meta_value(tbl, PCMK_META_RESOURCE_STICKINESS));
+
+ g_hash_table_destroy(tbl);
+}
+
+static void
+key_in_table(void **state)
+{
+ GHashTable *tbl = pcmk__strkey_table(free, free);
+
+ g_hash_table_insert(tbl, crm_meta_name(PCMK_META_NOTIFY), strdup("1"));
+ g_hash_table_insert(tbl, crm_meta_name(PCMK_META_RESOURCE_STICKINESS),
+ strdup("2"));
+
+ assert_string_equal(crm_meta_value(tbl, PCMK_META_NOTIFY), "1");
+ assert_string_equal(crm_meta_value(tbl, PCMK_META_RESOURCE_STICKINESS),
+ "2");
+
+ g_hash_table_destroy(tbl);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(empty_params),
+ cmocka_unit_test(key_not_in_table),
+ cmocka_unit_test(key_in_table))
diff --git a/lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c b/lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c
index 3723707..84187da 100644
--- a/lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c
+++ b/lib/common/tests/nvpair/pcmk__xe_attr_is_true_test.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2021 the Pacemaker project contributors
+ * Copyright 2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
@@ -15,7 +15,7 @@
static void
empty_input(void **state)
{
- xmlNode *node = string2xml("<node/>");
+ xmlNode *node = pcmk__xml_parse("<node/>");
assert_false(pcmk__xe_attr_is_true(NULL, NULL));
assert_false(pcmk__xe_attr_is_true(NULL, "whatever"));
@@ -27,7 +27,7 @@ empty_input(void **state)
static void
attr_missing(void **state)
{
- xmlNode *node = string2xml("<node a=\"true\" b=\"false\"/>");
+ xmlNode *node = pcmk__xml_parse("<node a=\"true\" b=\"false\"/>");
assert_false(pcmk__xe_attr_is_true(node, "c"));
free_xml(node);
@@ -36,7 +36,7 @@ attr_missing(void **state)
static void
attr_present(void **state)
{
- xmlNode *node = string2xml("<node a=\"true\" b=\"false\"/>");
+ xmlNode *node = pcmk__xml_parse("<node a=\"true\" b=\"false\"/>");
assert_true(pcmk__xe_attr_is_true(node, "a"));
assert_false(pcmk__xe_attr_is_true(node, "b"));
@@ -44,7 +44,7 @@ attr_present(void **state)
free_xml(node);
}
-PCMK__UNIT_TEST(NULL, NULL,
+PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
cmocka_unit_test(empty_input),
cmocka_unit_test(attr_missing),
cmocka_unit_test(attr_present))
diff --git a/lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c b/lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c
index 500d8a6..4823f6a 100644
--- a/lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c
+++ b/lib/common/tests/nvpair/pcmk__xe_get_bool_attr_test.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2021-2023 the Pacemaker project contributors
+ * Copyright 2021-2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
@@ -15,7 +15,7 @@
static void
empty_input(void **state)
{
- xmlNode *node = string2xml("<node/>");
+ xmlNode *node = pcmk__xml_parse("<node/>");
bool value;
assert_int_equal(pcmk__xe_get_bool_attr(NULL, NULL, &value), ENODATA);
@@ -29,7 +29,7 @@ empty_input(void **state)
static void
attr_missing(void **state)
{
- xmlNode *node = string2xml("<node a=\"true\" b=\"false\"/>");
+ xmlNode *node = pcmk__xml_parse("<node a=\"true\" b=\"false\"/>");
bool value;
assert_int_equal(pcmk__xe_get_bool_attr(node, "c", &value), ENODATA);
@@ -39,7 +39,8 @@ attr_missing(void **state)
static void
attr_present(void **state)
{
- xmlNode *node = string2xml("<node a=\"true\" b=\"false\" c=\"blah\"/>");
+ xmlNode *node = pcmk__xml_parse("<node a=\"true\" b=\"false\" "
+ "c=\"blah\"/>");
bool value;
value = false;
@@ -53,7 +54,7 @@ attr_present(void **state)
free_xml(node);
}
-PCMK__UNIT_TEST(NULL, NULL,
+PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
cmocka_unit_test(empty_input),
cmocka_unit_test(attr_missing),
cmocka_unit_test(attr_present))
diff --git a/lib/common/tests/nvpair/pcmk__xe_get_datetime_test.c b/lib/common/tests/nvpair/pcmk__xe_get_datetime_test.c
new file mode 100644
index 0000000..6da1e23
--- /dev/null
+++ b/lib/common/tests/nvpair/pcmk__xe_get_datetime_test.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright 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 <crm_internal.h>
+
+#include <errno.h>
+#include <libxml/tree.h>
+
+#include <crm/common/unittest_internal.h>
+
+#include <crm/common/iso8601.h>
+#include <crm/common/xml.h>
+#include <crm/common/nvpair_internal.h>
+
+#define REFERENCE_ISO8601 "2024-001"
+#define ATTR_PRESENT "start"
+#define ATTR_MISSING "end"
+#define REFERENCE_XML "<date_expression id=\"id1\" " \
+ ATTR_PRESENT "=\"" REFERENCE_ISO8601 "\"" \
+ " operation=\"gt\">"
+#define BAD_XML "<date_expression id=\"id1\" " \
+ ATTR_PRESENT "=\"not_a_time\"" \
+ " operation=\"gt\">"
+
+static void
+null_invalid(void **state)
+{
+ xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
+ crm_time_t *t = NULL;
+
+ assert_int_equal(pcmk__xe_get_datetime(NULL, NULL, NULL), EINVAL);
+ assert_int_equal(pcmk__xe_get_datetime(xml, NULL, NULL), EINVAL);
+ assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, NULL), EINVAL);
+ assert_int_equal(pcmk__xe_get_datetime(xml, NULL, &t), EINVAL);
+ assert_null(t);
+ assert_int_equal(pcmk__xe_get_datetime(NULL, ATTR_PRESENT, NULL), EINVAL);
+ assert_int_equal(pcmk__xe_get_datetime(NULL, ATTR_PRESENT, &t), EINVAL);
+ assert_null(t);
+ assert_int_equal(pcmk__xe_get_datetime(NULL, NULL, &t), EINVAL);
+ assert_null(t);
+
+ free_xml(xml);
+}
+
+static void
+nonnull_time_invalid(void **state)
+{
+ xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
+ crm_time_t *t = crm_time_new_undefined();
+
+ assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, &t), EINVAL);
+
+ crm_time_free(t);
+ free_xml(xml);
+}
+
+static void
+attr_missing(void **state)
+{
+ xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
+ crm_time_t *t = NULL;
+
+ assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_MISSING, &t), pcmk_rc_ok);
+ assert_null(t);
+
+ free_xml(xml);
+}
+
+static void
+attr_valid(void **state)
+{
+ xmlNode *xml = pcmk__xml_parse(REFERENCE_XML);
+ crm_time_t *t = NULL;
+ crm_time_t *reference = crm_time_new(REFERENCE_ISO8601);
+
+ assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, &t), pcmk_rc_ok);
+ assert_int_equal(crm_time_compare(t, reference), 0);
+
+ crm_time_free(t);
+ crm_time_free(reference);
+ free_xml(xml);
+}
+
+static void
+attr_invalid(void **state)
+{
+ xmlNode *xml = pcmk__xml_parse(BAD_XML);
+ crm_time_t *t = NULL;
+
+ assert_int_equal(pcmk__xe_get_datetime(xml, ATTR_PRESENT, &t),
+ pcmk_rc_unpack_error);
+ assert_null(t);
+
+ free_xml(xml);
+}
+
+PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
+ cmocka_unit_test(null_invalid),
+ cmocka_unit_test(nonnull_time_invalid),
+ cmocka_unit_test(attr_missing),
+ cmocka_unit_test(attr_valid),
+ cmocka_unit_test(attr_invalid))
diff --git a/lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c b/lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c
index e1fb9c4..dda2878 100644
--- a/lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c
+++ b/lib/common/tests/nvpair/pcmk__xe_set_bool_attr_test.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2021 the Pacemaker project contributors
+ * Copyright 2021-2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
@@ -11,21 +11,21 @@
#include <crm/common/unittest_internal.h>
#include <crm/common/xml_internal.h>
-#include <crm/msg_xml.h>
+#include <crm/common/xml.h>
static void
set_attr(void **state)
{
- xmlNode *node = string2xml("<node/>");
+ xmlNode *node = pcmk__xml_parse("<node/>");
pcmk__xe_set_bool_attr(node, "a", true);
pcmk__xe_set_bool_attr(node, "b", false);
- assert_string_equal(crm_element_value(node, "a"), XML_BOOLEAN_TRUE);
- assert_string_equal(crm_element_value(node, "b"), XML_BOOLEAN_FALSE);
+ assert_string_equal(crm_element_value(node, "a"), PCMK_VALUE_TRUE);
+ assert_string_equal(crm_element_value(node, "b"), PCMK_VALUE_FALSE);
free_xml(node);
}
-PCMK__UNIT_TEST(NULL, NULL,
+PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
cmocka_unit_test(set_attr))