diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-02-18 21:58:34 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-02-18 21:58:34 +0100 |
| commit | 0645c9fc2c7f801ba3c7d68a17c137a63ada299f (patch) | |
| tree | ce222906f546f03301defba9ce81ba57591a8eb3 /lib/tests | |
| parent | 39680498ee0987a5e0eb203a2c0539aa1fa94d39 (diff) | |
| download | monitoring-plugins-0645c9fc2c7f801ba3c7d68a17c137a63ada299f.tar.gz | |
Implement new output functionality
Diffstat (limited to 'lib/tests')
| -rw-r--r-- | lib/tests/Makefile.am | 6 | ||||
| -rw-r--r-- | lib/tests/test_generic_output.c | 315 | ||||
| -rw-r--r-- | lib/tests/test_generic_output.t | 6 |
3 files changed, 324 insertions, 3 deletions
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am index 31d79df6..9be94f6d 100644 --- a/lib/tests/Makefile.am +++ b/lib/tests/Makefile.am | |||
| @@ -8,9 +8,9 @@ check_PROGRAMS = @EXTRA_TEST@ | |||
| 8 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ | 8 | AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\" \ |
| 9 | -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins | 9 | -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins |
| 10 | 10 | ||
| 11 | EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 | 11 | EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 test_generic_output |
| 12 | 12 | ||
| 13 | np_test_scripts = test_base64.t test_cmd.t test_disk.t test_ini1.t test_ini3.t test_opts1.t test_opts2.t test_opts3.t test_tcp.t test_utils.t | 13 | np_test_scripts = test_base64.t test_cmd.t test_disk.t test_ini1.t test_ini3.t test_opts1.t test_opts2.t test_opts3.t test_tcp.t test_utils.t test_generic_output.t |
| 14 | np_test_files = config-dos.ini config-opts.ini config-tiny.ini plugin.ini plugins.ini | 14 | np_test_files = config-dos.ini config-opts.ini config-tiny.ini plugin.ini plugins.ini |
| 15 | EXTRA_DIST = $(np_test_scripts) $(np_test_files) var | 15 | EXTRA_DIST = $(np_test_scripts) $(np_test_files) var |
| 16 | 16 | ||
| @@ -29,7 +29,7 @@ AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags) | |||
| 29 | AM_LDFLAGS = $(tap_ldflags) -ltap | 29 | AM_LDFLAGS = $(tap_ldflags) -ltap |
| 30 | LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a $(LIB_CRYPTO) | 30 | LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a $(LIB_CRYPTO) |
| 31 | 31 | ||
| 32 | SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c | 32 | SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c test_generic_output.c |
| 33 | 33 | ||
| 34 | test: ${noinst_PROGRAMS} | 34 | test: ${noinst_PROGRAMS} |
| 35 | perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) | 35 | perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) |
diff --git a/lib/tests/test_generic_output.c b/lib/tests/test_generic_output.c new file mode 100644 index 00000000..e67aefc9 --- /dev/null +++ b/lib/tests/test_generic_output.c | |||
| @@ -0,0 +1,315 @@ | |||
| 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, "Test subcheck state directly after setting it"); | ||
| 114 | |||
| 115 | mp_perfdata pd1 = perfdata_init(); | ||
| 116 | |||
| 117 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 118 | |||
| 119 | pd1.uom = "s"; | ||
| 120 | pd1.label = "foo"; | ||
| 121 | |||
| 122 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
| 123 | |||
| 124 | mp_subcheck sc2 = mp_subcheck_init(); | ||
| 125 | sc2.output = "baz"; | ||
| 126 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
| 127 | |||
| 128 | ok(mp_compute_subcheck_state(sc2) == STATE_OK, "Test subcheck 2 state after setting it"); | ||
| 129 | |||
| 130 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
| 131 | |||
| 132 | ok(mp_compute_subcheck_state(sc1) == STATE_WARNING, "Test subcheck state after adding a subcheck"); | ||
| 133 | |||
| 134 | mp_check check = mp_check_init(); | ||
| 135 | mp_add_subcheck_to_check(&check, sc1); | ||
| 136 | |||
| 137 | ok(mp_compute_check_state(check) == STATE_WARNING, "Test main check result"); | ||
| 138 | |||
| 139 | char *output = mp_fmt_output(check); | ||
| 140 | |||
| 141 | // diag("Formatted output. Length: %u", strlen(output)); | ||
| 142 | // diag(output); | ||
| 143 | |||
| 144 | ok(output != NULL, "Output should not be NULL"); | ||
| 145 | |||
| 146 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
| 147 | "\t\\_[WARNING] - foobar\n" | ||
| 148 | "\t\t\\_[OK] - baz\n" | ||
| 149 | "|foo=23s;;; \n"; | ||
| 150 | |||
| 151 | // diag("Expected output. Length: %u", strlen(expected)); | ||
| 152 | // diag(expected); | ||
| 153 | |||
| 154 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
| 155 | } | ||
| 156 | |||
| 157 | void test_deep_check_hierarchy(void) { | ||
| 158 | // level 4 | ||
| 159 | mp_subcheck sc4 = mp_subcheck_init(); | ||
| 160 | sc4.output = "level4"; | ||
| 161 | sc4 = mp_set_subcheck_state(sc4, STATE_OK); | ||
| 162 | |||
| 163 | // level 3 | ||
| 164 | mp_subcheck sc3 = mp_subcheck_init(); | ||
| 165 | sc3.output = "level3"; | ||
| 166 | sc3 = mp_set_subcheck_state(sc3, STATE_OK); | ||
| 167 | |||
| 168 | // level 2 | ||
| 169 | mp_subcheck sc2 = mp_subcheck_init(); | ||
| 170 | sc2.output = "baz"; | ||
| 171 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
| 172 | |||
| 173 | // level 1 | ||
| 174 | mp_subcheck sc1 = mp_subcheck_init(); | ||
| 175 | |||
| 176 | sc1.output = "foobar"; | ||
| 177 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
| 178 | |||
| 179 | mp_perfdata pd1 = perfdata_init(); | ||
| 180 | |||
| 181 | pd1.uom = "s"; | ||
| 182 | pd1.label = "foo"; | ||
| 183 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 184 | |||
| 185 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
| 186 | |||
| 187 | // main check | ||
| 188 | mp_check check = mp_check_init(); | ||
| 189 | |||
| 190 | mp_add_subcheck_to_subcheck(&sc3, sc4); | ||
| 191 | mp_add_subcheck_to_subcheck(&sc2, sc3); | ||
| 192 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
| 193 | mp_add_subcheck_to_check(&check, sc1); | ||
| 194 | |||
| 195 | char *output = mp_fmt_output(check); | ||
| 196 | |||
| 197 | size_t output_length = strlen(output); | ||
| 198 | |||
| 199 | // diag("Formatted output of length %i", output_length); | ||
| 200 | // diag(output); | ||
| 201 | |||
| 202 | ok(output != NULL, "Output should not be NULL"); | ||
| 203 | |||
| 204 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
| 205 | "\t\\_[WARNING] - foobar\n" | ||
| 206 | "\t\t\\_[OK] - baz\n" | ||
| 207 | "\t\t\t\\_[OK] - level3\n" | ||
| 208 | "\t\t\t\t\\_[OK] - level4\n" | ||
| 209 | "|foo=23s;;; \n"; | ||
| 210 | |||
| 211 | size_t expected_length = strlen(expected); | ||
| 212 | |||
| 213 | // diag("Expected output of length: %i", expected_length); | ||
| 214 | // diag(expected); | ||
| 215 | |||
| 216 | ok(output_length == expected_length, "Outputs are of equal length"); | ||
| 217 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
| 218 | } | ||
| 219 | |||
| 220 | void test_deep_check_hierarchy2(void) { | ||
| 221 | // level 1 | ||
| 222 | mp_subcheck sc1 = mp_subcheck_init(); | ||
| 223 | |||
| 224 | sc1.output = "foobar"; | ||
| 225 | sc1 = mp_set_subcheck_state(sc1, STATE_WARNING); | ||
| 226 | |||
| 227 | mp_perfdata pd1 = perfdata_init(); | ||
| 228 | pd1.uom = "s"; | ||
| 229 | pd1.label = "foo"; | ||
| 230 | pd1 = mp_set_pd_value(pd1, 23); | ||
| 231 | |||
| 232 | mp_add_perfdata_to_subcheck(&sc1, pd1); | ||
| 233 | |||
| 234 | // level 2 | ||
| 235 | mp_subcheck sc2 = mp_subcheck_init(); | ||
| 236 | sc2.output = "baz"; | ||
| 237 | sc2 = mp_set_subcheck_state(sc2, STATE_OK); | ||
| 238 | |||
| 239 | mp_perfdata pd2 = perfdata_init(); | ||
| 240 | pd2.uom = "B"; | ||
| 241 | pd2.label = "baz"; | ||
| 242 | pd2 = mp_set_pd_value(pd2, 1024); | ||
| 243 | mp_add_perfdata_to_subcheck(&sc2, pd2); | ||
| 244 | |||
| 245 | // level 3 | ||
| 246 | mp_subcheck sc3 = mp_subcheck_init(); | ||
| 247 | sc3.output = "level3"; | ||
| 248 | sc3 = mp_set_subcheck_state(sc3, STATE_OK); | ||
| 249 | |||
| 250 | mp_perfdata pd3 = perfdata_init(); | ||
| 251 | pd3.label = "floatMe"; | ||
| 252 | pd3 = mp_set_pd_value(pd3, 1024.1024); | ||
| 253 | mp_add_perfdata_to_subcheck(&sc3, pd3); | ||
| 254 | |||
| 255 | // level 4 | ||
| 256 | mp_subcheck sc4 = mp_subcheck_init(); | ||
| 257 | sc4.output = "level4"; | ||
| 258 | sc4 = mp_set_subcheck_state(sc4, STATE_OK); | ||
| 259 | |||
| 260 | mp_check check = mp_check_init(); | ||
| 261 | |||
| 262 | mp_add_subcheck_to_subcheck(&sc3, sc4); | ||
| 263 | mp_add_subcheck_to_subcheck(&sc2, sc3); | ||
| 264 | mp_add_subcheck_to_subcheck(&sc1, sc2); | ||
| 265 | mp_add_subcheck_to_check(&check, sc1); | ||
| 266 | |||
| 267 | char *output = mp_fmt_output(check); | ||
| 268 | |||
| 269 | // diag("Formatted output of length: %i", strlen(output)); | ||
| 270 | // diag(output); | ||
| 271 | |||
| 272 | ok(output != NULL, "Output should not be NULL"); | ||
| 273 | |||
| 274 | char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n" | ||
| 275 | "\t\\_[WARNING] - foobar\n" | ||
| 276 | "\t\t\\_[OK] - baz\n" | ||
| 277 | "\t\t\t\\_[OK] - level3\n" | ||
| 278 | "\t\t\t\t\\_[OK] - level4\n" | ||
| 279 | "|foo=23s;;; baz=1024B;;; floatMe=1024.102400;;; \n"; | ||
| 280 | |||
| 281 | // diag("Expected output of length: %i", strlen(expected)); | ||
| 282 | // diag(expected); | ||
| 283 | |||
| 284 | ok(strcmp(output, expected) == 0, "Output is as expected"); | ||
| 285 | } | ||
| 286 | |||
| 287 | void test_default_states1(void) { | ||
| 288 | mp_subcheck sc = mp_subcheck_init(); | ||
| 289 | |||
| 290 | mp_state_enum state1 = mp_compute_subcheck_state(sc); | ||
| 291 | ok(state1 == STATE_UNKNOWN, "Default default state is Unknown"); | ||
| 292 | |||
| 293 | sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL); | ||
| 294 | |||
| 295 | mp_state_enum state2 = mp_compute_subcheck_state(sc); | ||
| 296 | ok(state2 == STATE_CRITICAL, "Default state is Critical"); | ||
| 297 | |||
| 298 | sc = mp_set_subcheck_state(sc, STATE_OK); | ||
| 299 | |||
| 300 | mp_state_enum state3 = mp_compute_subcheck_state(sc); | ||
| 301 | ok(state3 == STATE_OK, "Default state is Critical"); | ||
| 302 | } | ||
| 303 | |||
| 304 | void test_default_states2(void) { | ||
| 305 | mp_check check = mp_check_init(); | ||
| 306 | |||
| 307 | mp_subcheck sc = mp_subcheck_init(); | ||
| 308 | sc.output = "placeholder"; | ||
| 309 | sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL); | ||
| 310 | |||
| 311 | mp_add_subcheck_to_check(&check, sc); | ||
| 312 | |||
| 313 | mp_state_enum result_state = mp_compute_check_state(check); | ||
| 314 | ok(result_state == STATE_CRITICAL, "Derived state is the proper default state"); | ||
| 315 | } | ||
diff --git a/lib/tests/test_generic_output.t b/lib/tests/test_generic_output.t new file mode 100644 index 00000000..48c2ddf4 --- /dev/null +++ b/lib/tests/test_generic_output.t | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #!/usr/bin/perl | ||
| 2 | use Test::More; | ||
| 3 | if (! -e "./test_generic_output") { | ||
| 4 | plan skip_all => "./test_generic_output not compiled - please enable libtap library to test"; | ||
| 5 | } | ||
| 6 | exec "./test_generic_output"; | ||
