Skip to content

Commit f4c0ecc

Browse files
committed
Refactor: libcrmcommon: Helper in unit test for crm_parse_agent_spec()
Signed-off-by: Reid Wahl <[email protected]>
1 parent c5782fb commit f4c0ecc

File tree

1 file changed

+103
-53
lines changed

1 file changed

+103
-53
lines changed

lib/common/tests/agents/crm_parse_agent_spec_test.c

Lines changed: 103 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 the Pacemaker project contributors
2+
* Copyright 2022-2025 the Pacemaker project contributors
33
*
44
* The version control history for this file may have further details.
55
*
@@ -13,77 +13,127 @@
1313
#include <crm/common/agents.h>
1414

1515
static void
16-
all_params_null(void **state) {
17-
assert_int_equal(crm_parse_agent_spec(NULL, NULL, NULL, NULL), -EINVAL);
18-
assert_int_equal(crm_parse_agent_spec("", NULL, NULL, NULL), -EINVAL);
19-
assert_int_equal(crm_parse_agent_spec(":", NULL, NULL, NULL), -EINVAL);
20-
assert_int_equal(crm_parse_agent_spec("::", NULL, NULL, NULL), -EINVAL);
21-
}
22-
23-
static void
24-
no_prov_or_type(void **state) {
16+
assert_parse_agent_spec_as(int line, const char *spec, const char *expected_std,
17+
const char *expected_prov, const char *expected_type,
18+
int expected_rc, bool check_spt)
19+
{
2520
char *std = NULL;
2621
char *prov = NULL;
27-
char *ty = NULL;
28-
29-
assert_int_equal(crm_parse_agent_spec("ocf", &std, &prov, &ty), -EINVAL);
30-
assert_int_equal(crm_parse_agent_spec("ocf:", &std, &prov, &ty), -EINVAL);
31-
assert_int_equal(crm_parse_agent_spec("ocf::", &std, &prov, &ty), -EINVAL);
22+
char *type = NULL;
23+
int rc = crm_parse_agent_spec(spec, &std, &prov, &type);
24+
25+
/* @TODO Define pcmk__assert_int_equal() or similar to do the casting, for
26+
* reuse in other unit tests. See T222. It would be nice to convert all of
27+
* these "assert_X()" functions in unit tests into macro wrappers for
28+
* "assert_X_as()".
29+
*/
30+
_assert_int_equal(cast_to_largest_integral_type(rc),
31+
cast_to_largest_integral_type(expected_rc),
32+
__FILE__, line);
33+
34+
/* @TODO Move this definition to a common file, for reuse in other unit
35+
* tests. See other TODO above.
36+
*
37+
* Note: Commit 09621179 adds an _assert_ptr_equal() function and changes
38+
* the assert_null() and assert_ptr_equal() definitions to use it. However,
39+
* this commit is not in any cmocka release at the time of writing.
40+
*/
41+
#define pcmk__assert_null(c) \
42+
_assert_int_equal(cast_ptr_to_largest_integral_type(c), \
43+
cast_ptr_to_largest_integral_type(NULL), \
44+
__FILE__, (line))
45+
46+
if (!check_spt) {
47+
/* This is a temporary hack to work around an issue that will be fixed
48+
* in an upcoming commit
49+
*/
50+
return;
51+
}
52+
53+
if (expected_std == NULL) {
54+
pcmk__assert_null(std);
55+
} else {
56+
_assert_string_equal(std, expected_std, __FILE__, line);
57+
free(std);
58+
}
59+
60+
if (expected_prov == NULL) {
61+
pcmk__assert_null(prov);
62+
} else {
63+
_assert_string_equal(prov, expected_prov, __FILE__, line);
64+
free(prov);
65+
}
66+
67+
if (expected_type == NULL) {
68+
pcmk__assert_null(type);
69+
} else {
70+
_assert_string_equal(type, expected_type, __FILE__, line);
71+
free(type);
72+
}
3273
}
3374

75+
#define assert_parse_agent_spec(spec, expected_std, expected_prov, \
76+
expected_type, expected_rc, check_spt) \
77+
assert_parse_agent_spec_as(__LINE__, (spec), (expected_std), \
78+
(expected_prov), (expected_type), \
79+
(expected_rc), (check_spt))
80+
3481
static void
35-
no_type(void **state) {
36-
char *std = NULL;
37-
char *prov = NULL;
38-
char *ty = NULL;
82+
all_params_null(void **state)
83+
{
84+
int rc = pcmk_ok;
3985

40-
assert_int_equal(crm_parse_agent_spec("ocf:pacemaker:", &std, &prov, &ty), -EINVAL);
41-
}
86+
rc = crm_parse_agent_spec(NULL, NULL, NULL, NULL);
87+
assert_int_equal(rc, -EINVAL);
4288

43-
static void
44-
get_std_and_ty(void **state) {
45-
char *std = NULL;
46-
char *prov = NULL;
47-
char *ty = NULL;
89+
rc = crm_parse_agent_spec("", NULL, NULL, NULL);
90+
assert_int_equal(rc, -EINVAL);
4891

49-
assert_int_equal(crm_parse_agent_spec("stonith:fence_xvm", &std, &prov, &ty), pcmk_ok);
50-
assert_string_equal(std, "stonith");
51-
assert_null(prov);
52-
assert_string_equal(ty, "fence_xvm");
92+
rc = crm_parse_agent_spec(":", NULL, NULL, NULL);
93+
assert_int_equal(rc, -EINVAL);
5394

54-
free(std);
55-
free(ty);
95+
rc = crm_parse_agent_spec("::", NULL, NULL, NULL);
96+
assert_int_equal(rc, -EINVAL);
5697
}
5798

5899
static void
59-
get_all_values(void **state) {
60-
char *std = NULL;
61-
char *prov = NULL;
62-
char *ty = NULL;
100+
no_prov_or_type(void **state)
101+
{
102+
assert_parse_agent_spec("ocf", NULL, NULL, NULL, -EINVAL, true);
63103

64-
assert_int_equal(crm_parse_agent_spec("ocf:pacemaker:ping", &std, &prov, &ty), pcmk_ok);
65-
assert_string_equal(std, "ocf");
66-
assert_string_equal(prov, "pacemaker");
67-
assert_string_equal(ty, "ping");
104+
// @FIXME std is freed on error, so set it to NULL or don't check its value
105+
assert_parse_agent_spec("ocf:", NULL, NULL, NULL, -EINVAL, false);
106+
assert_parse_agent_spec("ocf::", NULL, NULL, NULL, -EINVAL, false);
107+
}
68108

69-
free(std);
70-
free(prov);
71-
free(ty);
109+
static void
110+
no_type(void **state)
111+
{
112+
/* @FIXME std and prov are freed on error, so set them to NULL or don't
113+
* check their values
114+
*/
115+
assert_parse_agent_spec("ocf:pacemaker:", NULL, NULL, NULL, -EINVAL, false);
72116
}
73117

74118
static void
75-
get_systemd_values(void **state) {
76-
char *std = NULL;
77-
char *prov = NULL;
78-
char *ty = NULL;
119+
get_std_and_ty(void **state)
120+
{
121+
assert_parse_agent_spec("stonith:fence_xvm", "stonith", NULL, "fence_xvm",
122+
pcmk_ok, true);
123+
}
79124

80-
assert_int_equal(crm_parse_agent_spec("systemd:UNIT@A:B", &std, &prov, &ty), pcmk_ok);
81-
assert_string_equal(std, "systemd");
82-
assert_null(prov);
83-
assert_string_equal(ty, "UNIT@A:B");
125+
static void
126+
get_all_values(void **state)
127+
{
128+
assert_parse_agent_spec("ocf:pacemaker:ping", "ocf", "pacemaker", "ping",
129+
pcmk_ok, true);
130+
}
84131

85-
free(std);
86-
free(ty);
132+
static void
133+
get_systemd_values(void **state)
134+
{
135+
assert_parse_agent_spec("systemd:UNIT@A:B", "systemd", NULL, "UNIT@A:B",
136+
pcmk_ok, true);
87137
}
88138

89139
PCMK__UNIT_TEST(NULL, NULL,

0 commit comments

Comments
 (0)