Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2020 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/pressure.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 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE /* activate extra prototypes for glibc */
25 #endif
26
27 #include "testutils.h"
28
29 # define NPL_TESTING
30 # include "../lib/pressure.c"
31 # undef NPL_TESTING
32
33 static struct proc_psi_oneline *psi_oneline = NULL;
34 static struct proc_psi_twolines *psi_twolines = NULL;
35 static unsigned long long starvation[2];
36
37 static int
38 test_init ()
39 {
40 const char *env_variable_cpu = "NPL_TEST_PATH_PROCPRESSURE_CPU",
41 *env_variable_io = "NPL_TEST_PATH_PROCPRESSURE_IO";
42
43 if (setenv (env_variable_cpu, NPL_TEST_PATH_PROCPRESSURE_CPU, 1) < 0)
44 return EXIT_AM_HARDFAIL;
45 if (setenv (env_variable_io, NPL_TEST_PATH_PROCPRESSURE_IO, 1) < 0)
46 return EXIT_AM_HARDFAIL;
47
48 proc_psi_read_cpu (&psi_oneline, &starvation[0], 1);
49 proc_psi_read_io (&psi_twolines, &starvation[0], 1);
50
51 unsetenv (env_variable_cpu);
52 unsetenv (env_variable_io);
53
54 return 0;
55 }
56
57 static void
58 test_memory_release ()
59 {
60 free (psi_oneline);
61 free (psi_twolines);
62 }
63
64 typedef struct test_data
65 {
66 unsigned long long value;
67 unsigned long long expect_value;
68 } test_data;
69
70 static int
71 test_procpressure (const void *tdata)
72 {
73 const struct test_data *data = tdata;
74 int ret = 0;
75
76 TEST_ASSERT_EQUAL_NUMERIC (data->value, data->expect_value);
77 return ret;
78 }
79
80 static int
81 mymain (void)
82 {
83 int err, ret = 0;
84
85 if ((err = test_init ()) != 0)
86 return err;
87
88 # define DO_TEST(DATATYPE, VALUE, EXPECT_VALUE) \
89 do \
90 { \
91 test_data data = { \
92 .value = VALUE, \
93 .expect_value = EXPECT_VALUE, \
94 }; \
95 if (test_run("check " PATH_PSI_PROC_CPU " parser", \
96 test_procpressure, (&data)) < 0) \
97 ret = -1; \
98 } \
99 while (0)
100
101 /* test the proc parser */
102
103 /* we multiply by 100 the averages to somewhat transform
104 * the double values into integer ones */
105 DO_TEST ("cpu some avg10", psi_oneline->avg10 * 100, 748ULL);
106 DO_TEST ("cpu some avg60", psi_oneline->avg60 * 100, 625ULL);
107 DO_TEST ("cpu some avg300", psi_oneline->avg300 * 100, 666ULL);
108 DO_TEST ("cpu single total", psi_oneline->total, 200932088ULL);
109
110 DO_TEST ("io some avg10", psi_twolines->some_avg10 * 100, 8ULL);
111 DO_TEST ("io some avg60", psi_twolines->some_avg60 * 100, 2ULL);
112 DO_TEST ("io some avg300", psi_twolines->some_avg300 * 100, 1ULL);
113 DO_TEST ("io some total", psi_twolines->some_total, 40636071ULL);
114
115 DO_TEST ("io full avg10", psi_twolines->full_avg10 * 100, 4ULL);
116 DO_TEST ("io full avg60", psi_twolines->full_avg60 * 100, 1ULL);
117 DO_TEST ("io full avg300", psi_twolines->full_avg300 * 100, 0ULL);
118 DO_TEST ("io full total", psi_twolines->full_total, 32897091ULL);
119
120 test_memory_release ();
121
122 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
123 }
124
125 TEST_MAIN (mymain)