diff options
Diffstat (limited to 'lib/tests/test_generic_output.c')
| -rw-r--r-- | lib/tests/test_generic_output.c | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/lib/tests/test_generic_output.c b/lib/tests/test_generic_output.c new file mode 100644 index 00000000..e4a78bcd --- /dev/null +++ b/lib/tests/test_generic_output.c | |||
| @@ -0,0 +1,317 @@ | |||
| 1 | #include "../lib/output.h" | ||
| 2 | #include "../../tap/tap.h" | ||
| 3 | #include "./states.h" | ||
| 4 | |||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | void test_one_subcheck(void); | ||
| 8 | void test_two_subchecks(void); | ||
| 9 | |||
| 10 | void test_perfdata_formatting(void); | ||
| 11 | void test_perfdata_formatting2(void); | ||
| 12 | |||
| 13 | void test_deep_check_hierarchy(void); | ||
| 14 | void test_deep_check_hierarchy2(void); | ||
| 15 | |||
| 16 | void test_default_states1(void); | ||
| 17 | void test_default_states2(void); | ||
| 18 | |||
| 19 | int main(void) { | ||
| 20 | plan_tests(19); | ||
| 21 | |||
| 22 | diag("Simple test with one subcheck"); | ||
| 23 | test_one_subcheck(); | ||
| 24 | |||
| 25 | diag("Test with two subchecks"); | ||
| 26 | test_two_subchecks(); | ||
| 27 | |||
| 28 | diag("Test for performance data formatting"); | ||
| 29 | test_perfdata_formatting(); | ||
| 30 | |||
| 31 | diag("Another test for performance data formatting"); | ||
| 32 | test_perfdata_formatting2(); | ||
| 33 | |||
| 34 | diag("Test for deeper hierarchies"); | ||
| 35 | test_deep_check_hierarchy(); | ||
| 36 | |||
| 37 | diag("Another test for deeper hierarchies"); | ||
| 38 | test_deep_check_hierarchy2(); | ||
| 39 | |||
| 40 | diag("Testing the default state logic"); | ||
| 41 | test_default_states1(); | ||
| 42 | |||
| 43 | diag("Testing the default state logic #2"); | ||
| 44 | test_default_states2(); | ||
| 45 | |||
| 46 | return exit_status(); | ||
| 47 | } | ||
| 48 | |||
| 49 | void test_one_subcheck(void) { | ||
| 50 | mp_subcheck sc1 = mp_subcheck_init(); | ||
| 51 | |||
| 52 | sc1.output = "foobar"; | ||
| 53 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
| 54 | |||
| 55 | mp_check check = mp_check_init(); | ||
| 56 | mp_add_subcheck_to_check(&check, sc1); | ||
| 57 | |||
| 58 | ok(mp_compute_check_state(check) == STATE_WARNING, "Main state should be warning"); | ||
| 59 | |||
| 60 | char *output = mp_fmt_output(check); | ||
| 61 | |||
| 62 | // diag("Formatted output"); | ||
| 63 | // diag(output); | ||
| 64 | |||
| 65 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
| 66 | "\t\\_[WARNING] - foobar\n"; | ||
| 67 | |||
| 68 | // diag("Expected output"); | ||
| 69 | // diag(expected); | ||
| 70 | |||
| 71 | ok(strcmp(output, expected) == 0, "Simple output test"); | ||
| 72 | } | ||
| 73 | |||
| 74 | void test_perfdata_formatting2(void) { | ||
| 75 | mp_perfdata pd1 = perfdata_init(); | ||
| 76 | mp_perfdata pd2 = perfdata_init(); | ||
| 77 | |||
| 78 | pd1.label = "foo"; | ||
| 79 | pd2.label = "bar"; | ||
| 80 | |||
| 81 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 82 | pd2 = mp_set_pd_value(pd2, 1LL); | ||
| 83 | |||
| 84 | pd_list *tmp = pd_list_init(); | ||
| 85 | |||
| 86 | pd_list_append(tmp, pd1); | ||
| 87 | pd_list_append(tmp, pd2); | ||
| 88 | |||
| 89 | char *result = pd_list_to_string(*tmp); | ||
| 90 | |||
| 91 | ok(strcmp(result, "foo=23;;; bar=1;;;") == 0, "Perfdata string formatting"); | ||
| 92 | } | ||
| 93 | |||
| 94 | void test_perfdata_formatting(void) { | ||
| 95 | mp_perfdata pd1 = perfdata_init(); | ||
| 96 | |||
| 97 | pd1.uom = "s"; | ||
| 98 | pd1.label = "foo"; | ||
| 99 | |||
| 100 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 101 | |||
| 102 | char *pd_string = pd_to_string(pd1); | ||
| 103 | |||
| 104 | ok(strcmp(pd_string, "foo=23s;;;") == 0, "Perfdata string formatting"); | ||
| 105 | } | ||
| 106 | |||
| 107 | void test_two_subchecks(void) { | ||
| 108 | mp_subcheck sc1 = mp_subcheck_init(); | ||
| 109 | |||
| 110 | sc1.output = "foobar"; | ||
| 111 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
| 112 | |||
| 113 | ok(mp_compute_subcheck_state(sc1) == STATE_WARNING, | ||
| 114 | "Test subcheck state directly after setting it"); | ||
| 115 | |||
| 116 | mp_perfdata pd1 = perfdata_init(); | ||
| 117 | |||
| 118 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 119 | |||
| 120 | pd1.uom = "s"; | ||
| 121 | pd1.label = "foo"; | ||
| 122 | |||
| 123 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
| 124 | |||
| 125 | mp_subcheck sc2 = mp_subcheck_init(); | ||
| 126 | sc2.output = "baz"; | ||
| 127 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
| 128 | |||
| 129 | ok(mp_compute_subcheck_state(sc2) == STATE_OK, "Test subcheck 2 state after setting it"); | ||
| 130 | |||
| 131 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
| 132 | |||
| 133 | ok(mp_compute_subcheck_state(sc1) == STATE_WARNING, | ||
| 134 | "Test subcheck state after adding a subcheck"); | ||
| 135 | |||
| 136 | mp_check check = mp_check_init(); | ||
| 137 | mp_add_subcheck_to_check(&check, sc1); | ||
| 138 | |||
| 139 | ok(mp_compute_check_state(check) == STATE_WARNING, "Test main check result"); | ||
| 140 | |||
| 141 | char *output = mp_fmt_output(check); | ||
| 142 | |||
| 143 | // diag("Formatted output. Length: %u", strlen(output)); | ||
| 144 | // diag(output); | ||
| 145 | |||
| 146 | ok(output != NULL, "Output should not be NULL"); | ||
| 147 | |||
| 148 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
| 149 | "\t\\_[WARNING] - foobar\n" | ||
| 150 | "\t\t\\_[OK] - baz\n" | ||
| 151 | "|foo=23s;;; \n"; | ||
| 152 | |||
| 153 | // diag("Expected output. Length: %u", strlen(expected)); | ||
| 154 | // diag(expected); | ||
| 155 | |||
| 156 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
| 157 | } | ||
| 158 | |||
| 159 | void test_deep_check_hierarchy(void) { | ||
| 160 | // level 4 | ||
| 161 | mp_subcheck sc4 = mp_subcheck_init(); | ||
| 162 | sc4.output = "level4"; | ||
| 163 | sc4 = mp_set_subcheck_state(sc4, STATE_OK); | ||
| 164 | |||
| 165 | // level 3 | ||
| 166 | mp_subcheck sc3 = mp_subcheck_init(); | ||
| 167 | sc3.output = "level3"; | ||
| 168 | sc3 = mp_set_subcheck_state(sc3, STATE_OK); | ||
| 169 | |||
| 170 | // level 2 | ||
| 171 | mp_subcheck sc2 = mp_subcheck_init(); | ||
| 172 | sc2.output = "baz"; | ||
| 173 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
| 174 | |||
| 175 | // level 1 | ||
| 176 | mp_subcheck sc1 = mp_subcheck_init(); | ||
| 177 | |||
| 178 | sc1.output = "foobar"; | ||
| 179 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
| 180 | |||
| 181 | mp_perfdata pd1 = perfdata_init(); | ||
| 182 | |||
| 183 | pd1.uom = "s"; | ||
| 184 | pd1.label = "foo"; | ||
| 185 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 186 | |||
| 187 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
| 188 | |||
| 189 | // main check | ||
| 190 | mp_check check = mp_check_init(); | ||
| 191 | |||
| 192 | mp_add_subcheck_to_subcheck(&sc3, sc4); | ||
| 193 | mp_add_subcheck_to_subcheck(&sc2, sc3); | ||
| 194 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
| 195 | mp_add_subcheck_to_check(&check, sc1); | ||
| 196 | |||
| 197 | char *output = mp_fmt_output(check); | ||
| 198 | |||
| 199 | size_t output_length = strlen(output); | ||
| 200 | |||
| 201 | // diag("Formatted output of length %i", output_length); | ||
| 202 | // diag(output); | ||
| 203 | |||
| 204 | ok(output != NULL, "Output should not be NULL"); | ||
| 205 | |||
| 206 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
| 207 | "\t\\_[WARNING] - foobar\n" | ||
| 208 | "\t\t\\_[OK] - baz\n" | ||
| 209 | "\t\t\t\\_[OK] - level3\n" | ||
| 210 | "\t\t\t\t\\_[OK] - level4\n" | ||
| 211 | "|foo=23s;;; \n"; | ||
| 212 | |||
| 213 | size_t expected_length = strlen(expected); | ||
| 214 | |||
| 215 | // diag("Expected output of length: %i", expected_length); | ||
| 216 | // diag(expected); | ||
| 217 | |||
| 218 | ok(output_length == expected_length, "Outputs are of equal length"); | ||
| 219 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
| 220 | } | ||
| 221 | |||
| 222 | void test_deep_check_hierarchy2(void) { | ||
| 223 | // level 1 | ||
| 224 | mp_subcheck sc1 = mp_subcheck_init(); | ||
| 225 | |||
| 226 | sc1.output = "foobar"; | ||
| 227 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
| 228 | |||
| 229 | mp_perfdata pd1 = perfdata_init(); | ||
| 230 | pd1.uom = "s"; | ||
| 231 | pd1.label = "foo"; | ||
| 232 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 233 | |||
| 234 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
| 235 | |||
| 236 | // level 2 | ||
| 237 | mp_subcheck sc2 = mp_subcheck_init(); | ||
| 238 | sc2.output = "baz"; | ||
| 239 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
| 240 | |||
| 241 | mp_perfdata pd2 = perfdata_init(); | ||
| 242 | pd2.uom = "B"; | ||
| 243 | pd2.label = "baz"; | ||
| 244 | pd2 = mp_set_pd_value(pd2, 1024); | ||
| 245 | mp_add_perfdata_to_subcheck(&sc2, pd2); | ||
| 246 | |||
| 247 | // level 3 | ||
| 248 | mp_subcheck sc3 = mp_subcheck_init(); | ||
| 249 | sc3.output = "level3"; | ||
| 250 | sc3 = mp_set_subcheck_state(sc3, STATE_OK); | ||
| 251 | |||
| 252 | mp_perfdata pd3 = perfdata_init(); | ||
| 253 | pd3.label = "floatMe"; | ||
| 254 | pd3 = mp_set_pd_value(pd3, 1024.1024); | ||
| 255 | mp_add_perfdata_to_subcheck(&sc3, pd3); | ||
| 256 | |||
| 257 | // level 4 | ||
| 258 | mp_subcheck sc4 = mp_subcheck_init(); | ||
| 259 | sc4.output = "level4"; | ||
| 260 | sc4 = mp_set_subcheck_state(sc4, STATE_OK); | ||
| 261 | |||
| 262 | mp_check check = mp_check_init(); | ||
| 263 | |||
| 264 | mp_add_subcheck_to_subcheck(&sc3, sc4); | ||
| 265 | mp_add_subcheck_to_subcheck(&sc2, sc3); | ||
| 266 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
| 267 | mp_add_subcheck_to_check(&check, sc1); | ||
| 268 | |||
| 269 | char *output = mp_fmt_output(check); | ||
| 270 | |||
| 271 | // diag("Formatted output of length: %i", strlen(output)); | ||
| 272 | // diag(output); | ||
| 273 | |||
| 274 | ok(output != NULL, "Output should not be NULL"); | ||
| 275 | |||
| 276 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
| 277 | "\t\\_[WARNING] - foobar\n" | ||
| 278 | "\t\t\\_[OK] - baz\n" | ||
| 279 | "\t\t\t\\_[OK] - level3\n" | ||
| 280 | "\t\t\t\t\\_[OK] - level4\n" | ||
| 281 | "|foo=23s;;; baz=1024B;;; floatMe=1024.102400;;; \n"; | ||
| 282 | |||
| 283 | // diag("Expected output of length: %i", strlen(expected)); | ||
| 284 | // diag(expected); | ||
| 285 | |||
| 286 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
| 287 | } | ||
| 288 | |||
| 289 | void test_default_states1(void) { | ||
| 290 | mp_subcheck sc = mp_subcheck_init(); | ||
| 291 | |||
| 292 | mp_state_enum state1 = mp_compute_subcheck_state(sc); | ||
| 293 | ok(state1 == STATE_UNKNOWN, "Default default state is Unknown"); | ||
| 294 | |||
| 295 | sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL); | ||
| 296 | |||
| 297 | mp_state_enum state2 = mp_compute_subcheck_state(sc); | ||
| 298 | ok(state2 == STATE_CRITICAL, "Default state is Critical"); | ||
| 299 | |||
| 300 | sc = mp_set_subcheck_state(sc, STATE_OK); | ||
| 301 | |||
| 302 | mp_state_enum state3 = mp_compute_subcheck_state(sc); | ||
| 303 | ok(state3 == STATE_OK, "Default state is Critical"); | ||
| 304 | } | ||
| 305 | |||
| 306 | void test_default_states2(void) { | ||
| 307 | mp_check check = mp_check_init(); | ||
| 308 | |||
| 309 | mp_subcheck sc = mp_subcheck_init(); | ||
| 310 | sc.output = "placeholder"; | ||
| 311 | sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL); | ||
| 312 | |||
| 313 | mp_add_subcheck_to_check(&check, sc); | ||
| 314 | |||
| 315 | mp_state_enum result_state = mp_compute_check_state(check); | ||
| 316 | ok(result_state == STATE_CRITICAL, "Derived state is the proper default state"); | ||
| 317 | } | ||
