pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__xe_get_score_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2024 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <limits.h>
13 
14 #include <crm/common/scores.h>
16 
17 extern int pcmk__score_red;
18 extern int pcmk__score_green;
19 extern int pcmk__score_yellow;
20 
21 static int default_score = 99;
22 
23 static void
24 assert_score(const char *score_s, int expected_rc, int expected_score)
25 {
26  int score = 0;
27  int rc = pcmk_rc_ok;
28  xmlNode *xml = pcmk__xe_create(NULL, __func__);
29 
30  crm_xml_add(xml, "test_attr", score_s);
31  rc = pcmk__xe_get_score(xml, "test_attr", &score, default_score);
32  assert_int_equal(rc, expected_rc);
33  assert_int_equal(score, expected_score);
34  free_xml(xml);
35 }
36 
37 static void
38 invalid_args(void **state)
39 {
40  int score = 0;
41  xmlNode *xml = pcmk__xe_create(NULL, __func__);
42 
43  assert_int_equal(pcmk__xe_get_score(NULL, NULL, &score, default_score),
44  EINVAL);
45  assert_int_equal(pcmk__xe_get_score(xml, NULL, &score, default_score),
46  EINVAL);
47  assert_int_equal(pcmk__xe_get_score(NULL, "test", &score, default_score),
48  EINVAL);
49  free_xml(xml);
50 }
51 
52 static void
53 null_score_string(void **state)
54 {
55  assert_score(NULL, pcmk_rc_ok, default_score);
56 
57  // Test out-of-bounds default score
58 
59  default_score = -2000000;
60  assert_score(NULL, pcmk_rc_ok, -PCMK_SCORE_INFINITY);
61 
62  default_score = 2000000;
63  assert_score(NULL, pcmk_rc_ok, PCMK_SCORE_INFINITY);
64 
65  default_score = 99;
66 }
67 
68 static void
69 null_score(void **state)
70 {
71  xmlNode *xml = pcmk__xe_create(NULL, __func__);
72 
73  assert_int_equal(pcmk__xe_get_score(xml, "test_attr", NULL, default_score),
74  pcmk_rc_ok);
75 
76  crm_xml_add(xml, "test_attr", "0");
77  assert_int_equal(pcmk__xe_get_score(xml, "test_attr", NULL, default_score),
78  pcmk_rc_ok);
79 
80  crm_xml_add(xml, "test_attr", "foo");
81  assert_int_equal(pcmk__xe_get_score(xml, "test_attr", NULL, default_score),
82  EINVAL);
83 
84  free_xml(xml);
85 }
86 
87 static void
88 bad_input(void **state)
89 {
90  assert_score("redder", EINVAL, default_score);
91  assert_score("3.141592", pcmk_rc_ok, 3);
92  assert_score("0xf00d", pcmk_rc_ok, 0);
93 }
94 
95 static void
96 special_values(void **state)
97 {
98  assert_score("-INFINITY", pcmk_rc_ok, -PCMK_SCORE_INFINITY);
99  assert_score("INFINITY", pcmk_rc_ok, PCMK_SCORE_INFINITY);
100  assert_score("+INFINITY", pcmk_rc_ok, PCMK_SCORE_INFINITY);
101 
102  pcmk__score_red = 10;
103  pcmk__score_green = 20;
104  pcmk__score_yellow = 30;
105 
106  assert_score("red", pcmk_rc_ok, pcmk__score_red);
107  assert_score("green", pcmk_rc_ok, pcmk__score_green);
108  assert_score("yellow", pcmk_rc_ok, pcmk__score_yellow);
109 
110  assert_score("ReD", pcmk_rc_ok, pcmk__score_red);
111  assert_score("GrEeN", pcmk_rc_ok, pcmk__score_green);
112  assert_score("yElLoW", pcmk_rc_ok, pcmk__score_yellow);
113 }
114 
115 /* These ridiculous macros turn an integer constant into a string constant. */
116 #define A(x) #x
117 #define B(x) A(x)
118 
119 static void
120 outside_limits(void **state)
121 {
122  char *very_long = crm_strdup_printf(" %lld0", LLONG_MAX);
123 
124  // Still within int range
125  assert_score(B(PCMK_SCORE_INFINITY) "00", pcmk_rc_ok, PCMK_SCORE_INFINITY);
126  assert_score("-" B(PCMK_SCORE_INFINITY) "00", pcmk_rc_ok,
128 
129  // Outside long long range
130  assert_score(very_long, pcmk_rc_ok, PCMK_SCORE_INFINITY);
131  very_long[0] = '-';
132  assert_score(very_long, pcmk_rc_ok, -PCMK_SCORE_INFINITY);
133 }
134 
135 static void
136 inside_limits(void **state)
137 {
138  assert_score("1234", pcmk_rc_ok, 1234);
139  assert_score("-1234", pcmk_rc_ok, -1234);
140 }
141 
142 PCMK__UNIT_TEST(NULL, NULL,
143  cmocka_unit_test(invalid_args),
144  cmocka_unit_test(null_score_string),
145  cmocka_unit_test(null_score),
146  cmocka_unit_test(bad_input),
147  cmocka_unit_test(special_values),
148  cmocka_unit_test(outside_limits),
149  cmocka_unit_test(inside_limits))
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
Definition: nvpair.c:313
int pcmk__score_green
Definition: scores.c:22
int pcmk__score_yellow
Definition: scores.c:23
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
void free_xml(xmlNode *child)
Definition: xml.c:958
int pcmk__score_red
Definition: scores.c:21
int pcmk__xe_get_score(const xmlNode *xml, const char *name, int *score, int default_score)
Definition: xml.c:549
Pacemaker APIs related to scores.
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
Definition: xml.c:770
#define B(x)
#define PCMK_SCORE_INFINITY
Integer score to use to represent "infinity".
Definition: scores.h:24