Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2016,2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for check_intr.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
25 /* silence the compiler's warning 'function defined but not used' */
26 static _Noreturn void print_version (void) __attribute__((unused));
27 static _Noreturn void usage (FILE * out) __attribute__((unused));
28
29 #define NPL_TESTING
30 # include "../plugins/check_intr.c"
31 #undef NPL_TESTING
32
33 static bool
34 test_intr_proc_stat_exists ()
35 {
36 FILE *fp;
37
38 if ((fp = fopen (get_path_proc_stat (), "r")) == NULL)
39 return false;
40
41 fclose (fp);
42 return true;
43 }
44
45 static int
46 test_intr_monotonic (const void *tdata)
47 {
48 unsigned int ncpus0 = 0, ncpus1 = 0;
49 unsigned long *vintr[2] = { NULL, NULL };
50
51 long delta = get_intrdelta (&ncpus0, &ncpus1, &vintr, 1, 1, false);
52
53 if (delta <= 0)
54 return -1;
55
56 return 0;
57 }
58
59 static int
60 test_intr_proc_parsing ()
61 {
62 int ret = 0;
63 const char *env_variable = "NPL_TEST_PATH_PROCSTAT";
64
65 ret = setenv (env_variable, NPL_TEST_PATH_PROCSTAT, 1);
66 if (ret < 0)
67 return EXIT_AM_HARDFAIL;
68
69 /* next function will parse the line "4315363 33 45306 0 0 ..." */
70 if (cpu_stats_get_intr () != 4315363)
71 ret = -1;
72
73 unsetenv (env_variable);
74 return ret;
75 }
76
77 static int
78 mymain (void)
79 {
80 int ret = 0;
81
82 if (!test_intr_proc_stat_exists ())
83 return EXIT_AM_SKIP;
84
85 #define DO_TEST(MSG, FUNC, DATA) \
86 do { if (test_run (MSG, FUNC, DATA) < 0) ret = -1; } while (0)
87
88 DO_TEST ("check if get_intrdelta() is monotonic",
89 test_intr_monotonic, NULL);
90 DO_TEST ("check for parsing errors in cpu_stats_get_intr()",
91 test_intr_proc_parsing, NULL);
92
93 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
94 }
95
96 TEST_MAIN (mymain)