|
1 | 1 | /* |
2 | | - * Copyright 2022-2023 the Pacemaker project contributors |
| 2 | + * Copyright 2022-2025 the Pacemaker project contributors |
3 | 3 | * |
4 | 4 | * The version control history for this file may have further details. |
5 | 5 | * |
|
13 | 13 | #include <crm/common/agents.h> |
14 | 14 |
|
15 | 15 | 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 | +{ |
25 | 20 | char *std = NULL; |
26 | 21 | 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 | + } |
32 | 73 | } |
33 | 74 |
|
| 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 | + |
34 | 81 | 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; |
39 | 85 |
|
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); |
42 | 88 |
|
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); |
48 | 91 |
|
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); |
53 | 94 |
|
54 | | - free(std); |
55 | | - free(ty); |
| 95 | + rc = crm_parse_agent_spec("::", NULL, NULL, NULL); |
| 96 | + assert_int_equal(rc, -EINVAL); |
56 | 97 | } |
57 | 98 |
|
58 | 99 | 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); |
63 | 103 |
|
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 | +} |
68 | 108 |
|
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); |
72 | 116 | } |
73 | 117 |
|
74 | 118 | 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 | +} |
79 | 124 |
|
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 | +} |
84 | 131 |
|
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); |
87 | 137 | } |
88 | 138 |
|
89 | 139 | PCMK__UNIT_TEST(NULL, NULL, |
|
0 commit comments