Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tsload_thresholds.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for check_load.c
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "testutils.h"
24 #include "thresholds.h"
25
26 /* silence the compiler's warning 'function defined but not used' */
27 static _Noreturn void print_version (void) __attribute__ ((unused));
28 static _Noreturn void usage (FILE * out) __attribute__ ((unused));
29 static void validate_input (int i, double w, double c) __attribute__ ((unused));
30 static void normalize_loadavg (double *loadavg, int numcpus)
31 __attribute__ ((unused));
32
33 #define NPL_TESTING
34 #include "../plugins/check_load.c"
35 #undef NPL_TESTING
36
37 typedef struct test_data
38 {
39 double loadavg[3];
40 double wload[3];
41 double cload[3];
42 bool required[3];
43 nagstatus expect_status;
44 } test_data;
45
46 static int
47 test_loadavg_exit_status (const void *tdata)
48 {
49 const struct test_data *data = tdata;
50 nagstatus status;
51 int ret = 0;
52
53 status = loadavg_status (data->loadavg, data->wload, data->cload, data->required);
54
55 TEST_ASSERT_EQUAL_NUMERIC (status, data->expect_status);
56 return ret;
57 }
58
59 static int
60 mymain (void)
61 {
62 int ret = 0;
63
64 #define DO_TEST(L1,L2,L3, W1,W2,W3, C1,C2,C3, R1,R2,R3, EXPECT) \
65 do \
66 { \
67 test_data data = { \
68 .loadavg = { L1, L2, L3 }, \
69 .wload = { W1, W2, W3 }, \
70 .cload = { C1, C2, C3 }, \
71 .required = { R1, R2, R3 }, \
72 .expect_status = EXPECT \
73 }; \
74 if (test_run("check load exit status, expected: " #EXPECT, \
75 test_loadavg_exit_status, &data) < 0) \
76 ret = -1; \
77 } \
78 while (0)
79
80 DO_TEST (/* loadavg */ 2.8, 1.9, 1.3,
81 /* wload */ 3.0, 3.0, 3.0, /* cload */ 4.0, 4.0, 4.0,
82 /* required */ true, true, true,
83 /* expect_status */ STATE_OK);
84 DO_TEST (/* loadavg */ 2.8, 1.9, 1.3,
85 /* wload */ 3.0, 1.5, 1.5, /* cload */ 4.0, 4.0, 4.0,
86 /* required */ true, true, true,
87 /* expect_status */ STATE_WARNING);
88 DO_TEST (/* loadavg */ 2.8, 1.9, 1.3,
89 /* wload */ 3.0, 1.5, 1.5, /* cload */ 4.0, 4.0, 4.0,
90 /* required */ true, false, false,
91 /* expect_status */ STATE_OK);
92 DO_TEST (/* loadavg */ 2.8, 1.9, 1.3,
93 /* wload */ 3.0, 1.5, 1.5, /* cload */ 4.0, 4.0, 4.0,
94 /* required */ false, true, false,
95 /* expect_status */ STATE_WARNING);
96 DO_TEST (/* loadavg */ 2.8, 1.9, 1.3,
97 /* wload */ 3.0, 1.5, 1.5, /* cload */ 4.0, 4.0, 4.0,
98 /* required */ false, false, true,
99 /* expect_status */ STATE_OK);
100 DO_TEST (/* loadavg */ 2.8, 1.9, 1.3,
101 /* wload */ 3.0, 1.5, 1.5, /* cload */ 4.0, 4.0, 4.0,
102 /* required */ false, true, true,
103 /* expect_status */ STATE_WARNING);
104 DO_TEST (/* loadavg */ 2.8, 1.9, 5.5,
105 /* wload */ 1.0, 2.0, 2.0, /* cload */ 3.0, 3.0, 4.0,
106 /* required */ true, true, true,
107 /* expect_status */ STATE_CRITICAL);
108 DO_TEST (/* loadavg */ 2.8, 1.9, 5.5,
109 /* wload */ 1.0, 2.0, 2.0, /* cload */ 3.0, 3.0, 4.0,
110 /* required */ true, false, false,
111 /* expect_status */ STATE_WARNING);
112 DO_TEST (/* loadavg */ 2.8, 1.9, 5.5,
113 /* wload */ 1.0, 2.0, 3.0, /* cload */ 3.0, 3.0, 6.0,
114 /* required */ false, true, false,
115 /* expect_status */ STATE_OK);
116 DO_TEST (/* loadavg */ 2.8, 1.9, 5.5,
117 /* wload */ 1.0, 2.0, 3.0, /* cload */ 3.0, 3.0, 6.0,
118 /* required */ false, false, true,
119 /* expect_status */ STATE_WARNING);
120
121 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
122 }
123
124 TEST_MAIN (mymain)